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 input_buffer;
31 char string_buf[MAX_STR_CONST];
32 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 {++line_pos;char_pos=0;}
48 <comment>"*"+"/" BEGIN(comment_caller);
50 \" string_buf_ptr = string_buf; 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'; char_pos++;}
88 <str>\\t {*string_buf_ptr++ = '\t'; char_pos++;}
89 <str>\\r {*string_buf_ptr++ = '\r'; char_pos++;}
90 <str>\\b {*string_buf_ptr++ = '\b'; char_pos++;}
91 <str>\\f {*string_buf_ptr++ = '\f'; char_pos++;}
93 <str>\\(.|\n) {*string_buf_ptr++ = surf_parse_text[1];
94 if(surf_parse_text[1]=='\n') {
95 ++line_pos;char_pos=0;
100 char *yptr = surf_parse_text;
103 *string_buf_ptr++ = *yptr++;
107 ({letter}|{digit})* { char_pos+= strlen(surf_parse_text); return(TOKEN_WORD);}
108 "(" { char_pos++; return(TOKEN_LP);}
109 ")" { char_pos++;return(TOKEN_RP);}
110 "</" { char_pos+=2; return(TOKEN_END_SECTION);}
111 "<" { char_pos++; return(TOKEN_BEGIN_SECTION);}
112 ">" { char_pos++;return(TOKEN_CLOSURE);}
113 "\n" { line_pos++; char_pos=0; return(TOKEN_NEWLINE);}
116 /* {space}+ { return(TOKEN_SPACE);} */
118 static void __print_val(void) {
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", tok_num);return;}
130 /* if(strcmp(surf_parse_text,"")!=0) */
131 printf("-->%s<-- [line %d, pos %d]\n",surf_parse_text,line_pos,char_pos);
133 /* printf("--><-- [line %d, pos %d]\n",line_pos,char_pos); */
138 e_surf_token_t surf_parse(void) {
139 tok_num = surf_parse_lex();
141 char_pos += strlen(surf_parse_text);
145 void find_section(const char* file, const char* section_name)
147 e_surf_token_t token;
150 surf_parse_open(file);
152 while((token=surf_parse())) {
153 if(token!=TOKEN_BEGIN_SECTION) continue;
156 xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
157 if(strcmp(surf_parse_text,section_name)==0) found=1;
160 xbt_assert1((token==TOKEN_CLOSURE),"Parse error line %d",line_pos);
165 CRITICAL2("Could not find %s section in %s\n",section_name,file);
169 void close_section(const char* section_name)
171 e_surf_token_t token;
174 xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
175 xbt_assert1((strcmp(surf_parse_text,section_name)==0),
176 "Closing section does not match the opening one (%s).",
180 xbt_assert1((token==TOKEN_CLOSURE),"Parse error line %d",line_pos);
185 void surf_parse_open(const char *file) {
186 file_to_parse = fopen(file,"r");
187 xbt_assert1((file_to_parse), "Unable to open \"%s\"\n",file);
188 input_buffer = surf_parse__create_buffer( file_to_parse, 10 );
189 surf_parse__switch_to_buffer(input_buffer);
196 void surf_parse_close(void) {
197 surf_parse__delete_buffer(input_buffer);
198 fclose(file_to_parse);
205 void surf_parse_double(double *value)
207 e_surf_token_t token;
210 token = surf_parse(); /* power_scale */
211 xbt_assert1((token == TOKEN_WORD), "Parse error line %d", line_pos);
212 ret = sscanf(surf_parse_text, "%lg", value);
213 xbt_assert2((ret==1), "Parse error line %d : %s not a number", line_pos,
217 void surf_parse_trace(tmgr_trace_t *trace)
219 e_surf_token_t token;
221 token = surf_parse(); /* power_trace */
222 xbt_assert1((token == TOKEN_WORD), "Parse error line %d", line_pos);
223 if (strcmp(surf_parse_text, "") == 0)
226 *trace = tmgr_trace_new(surf_parse_text);
228 /* Local variables: */