3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "xbt/sysdep.h"
11 #include"surf/surf_parse.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(parse, surf ,"Logging specific to the SURF module");
15 YY_BUFFER_STATE surf_input_buffer;
16 FILE *surf_file_to_parse;
18 int surf_line_pos = 1;
19 int surf_char_pos = -1;
21 char string_buf[MAX_STR_CONST];
22 char *string_buf_ptr = NULL;
36 comment_caller = INITIAL;
45 <comment>[^*\n]* /* eat anything that's not a '*' */
46 <comment>"*"+[^*/\n]* /* eat up '*'s not followed by '/'s */
47 <comment>\n {++surf_line_pos;surf_char_pos=0;}
48 <comment>"*"+"/" BEGIN(comment_caller);
50 \" string_buf_ptr = string_buf; surf_char_pos++; BEGIN(str);
52 <str>\" { /* saw closing quote - all done */
54 *string_buf_ptr = '\0';
55 surf_parse_text=string_buf;
58 /* return string constant token type and
64 /* error - unterminated string constant */
65 /* generate error message */
69 /* octal escape sequence */
72 (void) sscanf( surf_parse_text + 1, "%o", &result );
75 /* error, constant is out-of-bounds */
77 *string_buf_ptr++ = result;
82 /* generate error - bad escape sequence; something
83 * like '\48' or '\0777777'
87 <str>\\n {*string_buf_ptr++ = '\n'; surf_char_pos++;}
88 <str>\\t {*string_buf_ptr++ = '\t'; surf_char_pos++;}
89 <str>\\r {*string_buf_ptr++ = '\r'; surf_char_pos++;}
90 <str>\\b {*string_buf_ptr++ = '\b'; surf_char_pos++;}
91 <str>\\f {*string_buf_ptr++ = '\f'; surf_char_pos++;}
93 <str>\\(.|\n) {*string_buf_ptr++ = surf_parse_text[1];
94 if(surf_parse_text[1]=='\n') {
95 ++surf_line_pos;surf_char_pos=0;
96 } else { surf_char_pos++;}
100 char *yptr = surf_parse_text;
103 *string_buf_ptr++ = *yptr++;
107 ({letter}|{digit})* { return(TOKEN_WORD);} /* surf_char_pos+= strlen(surf_parse_text); */
108 "(" { surf_char_pos++; return(TOKEN_LP);}
109 ")" { surf_char_pos++;return(TOKEN_RP);}
110 "</" { surf_char_pos+=2; return(TOKEN_END_SECTION);}
111 "<" { surf_char_pos++; return(TOKEN_BEGIN_SECTION);}
112 ">" { surf_char_pos++;return(TOKEN_CLOSURE);}
113 "\n" { surf_line_pos++; surf_char_pos=-1; return(TOKEN_NEWLINE);}
114 . { surf_char_pos++;}
116 /* {space}+ { return(TOKEN_SPACE);} */
118 static void __print_val(void) {
119 switch(surf_tok_num) {
120 case TOKEN_LP : {printf("TOKEN_LP ");break;}
121 case TOKEN_RP : {printf("TOKEN_RP ");break;}
122 case TOKEN_BEGIN_SECTION : {printf("TOKEN_BEGIN_SECTION ");break;}
123 case TOKEN_END_SECTION : {printf("TOKEN_END_SECTION ");break;}
124 case TOKEN_CLOSURE : {printf("TOKEN_CLOSURE ");break;}
125 case TOKEN_WORD : {printf("TOKEN_WORD ");break;}
126 case TOKEN_NEWLINE : {printf("TOKEN_NEWLINE\n");return;}
127 case TOKEN_EMPTY : {printf("TOKEN_EMPTY\n");return;}
128 default : {printf("Unknown token %d\n", surf_tok_num);return;}
131 printf("-->%s<-- [line %d, pos %d]\n",surf_parse_text,surf_line_pos,surf_char_pos);
136 e_surf_token_t surf_parse(void) {
137 surf_tok_num = surf_parse_lex();
139 surf_char_pos += strlen(surf_parse_text);
140 return(surf_tok_num);
143 void find_section(const char* file, const char* section_name)
145 e_surf_token_t token;
148 surf_parse_open(file);
150 while((token=surf_parse())) {
151 if(token!=TOKEN_BEGIN_SECTION) continue;
154 xbt_assert1((token==TOKEN_WORD),"Parse error line %d",surf_line_pos);
155 if(strcmp(surf_parse_text,section_name)==0) found=1;
158 xbt_assert1((token==TOKEN_CLOSURE),"Parse error line %d",surf_line_pos);
163 CRITICAL2("Could not find %s section in %s\n",section_name,file);
167 void close_section(const char* section_name)
169 e_surf_token_t token;
172 xbt_assert1((token==TOKEN_WORD),"Parse error line %d",surf_line_pos);
173 xbt_assert1((strcmp(surf_parse_text,section_name)==0),
174 "Closing section does not match the opening one (%s).",
178 xbt_assert1((token==TOKEN_CLOSURE),"Parse error line %d",surf_line_pos);
183 void surf_parse_open(const char *file) {
184 surf_file_to_parse = fopen(file,"r");
185 xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n",file);
186 surf_input_buffer = surf_parse__create_buffer( surf_file_to_parse, 10 );
187 surf_parse__switch_to_buffer(surf_input_buffer);
194 void surf_parse_close(void) {
195 surf_parse__delete_buffer(surf_input_buffer);
196 fclose(surf_file_to_parse);
203 void surf_parse_double(double *value)
205 e_surf_token_t token;
208 token = surf_parse(); /* power_scale */
209 xbt_assert1((token == TOKEN_WORD), "Parse error line %d", surf_line_pos);
210 ret = sscanf(surf_parse_text, "%lg", value);
211 xbt_assert2((ret==1), "Parse error line %d : %s not a number", surf_line_pos,
215 void surf_parse_trace(tmgr_trace_t *trace)
217 e_surf_token_t token;
219 token = surf_parse(); /* power_trace */
220 xbt_assert1((token == TOKEN_WORD), "Parse error line %d", surf_line_pos);
221 if (strcmp(surf_parse_text, "") == 0)
224 *trace = tmgr_trace_new(surf_parse_text);
227 void surf_parse_deployment_line(char **host, int *argc, char ***argv)
229 e_surf_token_t token;
232 /* Parse Host name */
233 *host = xbt_strdup(surf_parse_text);
238 /* Parse command line */
239 while((token = surf_parse())) {
240 if(token == TOKEN_NEWLINE) return;
241 xbt_assert1((token == TOKEN_WORD), "Parse error line %d", surf_line_pos);
244 *argv=xbt_realloc (*argv, (*argc) * sizeof(char*));
245 (*argv)[(*argc)-1]=xbt_strdup(surf_parse_text);
250 /* Local variables: */