Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
27034bf426b34bbba632eecc0c0eca1d32710e58
[simgrid.git] / src / surf / surf_parse.l
1 /* Authors: Arnaud Legrand                                                  */
2
3 /* This program is free software; you can redistribute it and/or modify it
4    under the terms of the license (GNU LGPL) which comes with this package. */
5
6 %option noyywrap
7 %{
8 #include "xbt/sysdep.h"
9 #include"surf/surf_parse.h"
10 #include "xbt/log.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(parse, surf ,"Logging specific to the SURF  module");
12
13   YY_BUFFER_STATE input_buffer;
14   FILE *file_to_parse;
15
16   int line_pos = 1;
17   int char_pos = 0;
18   int tok_num = 0;
19 %}
20
21 %x comment foo str
22 space           [ \t]
23 letter          [A-Za-z._-]
24 digit           [0-9]
25
26 %%
27         int comment_caller=0;
28
29         char string_buf[MAX_STR_CONST];
30         char *string_buf_ptr = NULL;
31
32 "//"[^\n]*
33 "/*"         {
34              comment_caller = INITIAL;
35              BEGIN(comment);
36              }
37
38 <foo>"/*"    {
39              comment_caller = foo;
40              BEGIN(comment);
41              }
42
43 <comment>[^*\n]*        /* eat anything that's not a '*' */
44 <comment>"*"+[^*/\n]*   /* eat up '*'s not followed by '/'s */
45 <comment>\n             {++line_pos;char_pos=0;}
46 <comment>"*"+"/"        BEGIN(comment_caller);
47
48 \"      string_buf_ptr = string_buf; char_pos++; BEGIN(str);
49
50 <str>\"        { /* saw closing quote - all done */
51         BEGIN(INITIAL);
52         *string_buf_ptr = '\0';
53         surf_parse_text=string_buf;
54         char_pos++;
55         return TOKEN_WORD;
56         /* return string constant token type and
57          * value to parser
58          */
59         }
60
61 <str>\n        {
62         /* error - unterminated string constant */
63         /* generate error message */
64         }
65
66 <str>\\[0-7]{1,3} {
67         /* octal escape sequence */
68         int result;
69
70         (void) sscanf( surf_parse_text + 1, "%o", &result );
71
72         if ( result > 0xff )
73                 /* error, constant is out-of-bounds */
74
75         *string_buf_ptr++ = result;
76         char_pos++;
77         }
78
79 <str>\\[0-9]+ {
80         /* generate error - bad escape sequence; something
81          * like '\48' or '\0777777'
82          */
83         }
84
85 <str>\\n  {*string_buf_ptr++ = '\n';    char_pos++;}
86 <str>\\t  {*string_buf_ptr++ = '\t';    char_pos++;}
87 <str>\\r  {*string_buf_ptr++ = '\r';    char_pos++;}
88 <str>\\b  {*string_buf_ptr++ = '\b';    char_pos++;}
89 <str>\\f  {*string_buf_ptr++ = '\f';    char_pos++;}
90
91 <str>\\(.|\n)  {*string_buf_ptr++ = surf_parse_text[1];         
92                 if(surf_parse_text[1]=='\n') {
93                   ++line_pos;char_pos=0;
94                 } else { char_pos++;}
95                }
96
97 <str>[^\\\n\"]+        {
98         char *yptr = surf_parse_text;
99
100         while ( *yptr )
101           *string_buf_ptr++ = *yptr++;
102           char_pos++;
103         }
104
105 ({letter}|{digit})*     { char_pos+= strlen(surf_parse_text); return(TOKEN_WORD);}
106 "("                     { char_pos++; return(TOKEN_LP);}
107 ")"                     { char_pos++;return(TOKEN_RP);}
108 "</"                    { char_pos+=2; return(TOKEN_END_SECTION);}
109 "<"                     { char_pos++; return(TOKEN_BEGIN_SECTION);}
110 ">"                     { char_pos++;return(TOKEN_CLOSURE);}
111 "\n"                    { line_pos++; char_pos=0; return(TOKEN_NEWLINE);}
112 . { char_pos++;}
113 %%
114 /* {space}+                { return(TOKEN_SPACE);} */
115
116 static void __print_val(void) {
117   switch(tok_num) {
118   case TOKEN_LP      : {printf("TOKEN_LP ");break;}
119   case TOKEN_RP      : {printf("TOKEN_RP ");break;}
120   case TOKEN_BEGIN_SECTION : {printf("TOKEN_BEGIN_SECTION ");break;}
121   case TOKEN_END_SECTION : {printf("TOKEN_END_SECTION ");break;}
122   case TOKEN_CLOSURE : {printf("TOKEN_CLOSURE ");break;}
123   case TOKEN_WORD    : {printf("TOKEN_WORD ");break;}
124   case TOKEN_NEWLINE : {printf("TOKEN_NEWLINE\n");return;}
125   case TOKEN_EMPTY : {printf("TOKEN_EMPTY\n");return;}
126   default             : {printf("Unknown token %d\n", tok_num);return;}
127   }
128 /*   if(strcmp(surf_parse_text,"")!=0) */
129     printf("-->%s<-- [line %d, pos %d]\n",surf_parse_text,line_pos,char_pos);
130 /*   else */
131 /*     printf("--><-- [line %d, pos %d]\n",line_pos,char_pos); */
132
133   return;
134 }
135
136 e_surf_token_t surf_parse(void) {
137   tok_num = surf_parse_lex();
138   __print_val();
139   char_pos += strlen(surf_parse_text);
140   return(tok_num);
141 }
142
143 void find_section(const char* file, const char* section_name)
144 {
145   e_surf_token_t token;
146   int found = 0;
147
148   surf_parse_open(file);
149
150   while((token=surf_parse())) {
151     if(token!=TOKEN_BEGIN_SECTION) continue;
152
153     token=surf_parse();
154     xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
155     if(strcmp(surf_parse_text,section_name)==0) found=1;
156
157     token=surf_parse();
158     xbt_assert1((token==TOKEN_CLOSURE),"Parse error line %d",line_pos);
159
160     if(found) return;
161   }
162
163   CRITICAL2("Could not find %s section in %s\n",section_name,file);
164   xbt_abort();
165 }
166
167 void close_section(const char* section_name)
168 {
169   e_surf_token_t token;
170
171   token=surf_parse();
172   xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
173   xbt_assert1((strcmp(surf_parse_text,section_name)==0), 
174               "Closing section does not match the opening one (%s).", 
175               section_name);
176   
177   token=surf_parse();
178   xbt_assert1((token==TOKEN_CLOSURE),"Parse error line %d",line_pos);
179
180   surf_parse_close();
181 }
182
183 void  surf_parse_open(const char *file) {
184   file_to_parse = fopen(file,"r");
185   xbt_assert1((file_to_parse), "Unable to open \"%s\"\n",file)
186   input_buffer = surf_parse__create_buffer( file_to_parse, 10 );
187   surf_parse__switch_to_buffer(input_buffer);
188
189   line_pos = 1;
190   char_pos = 0;
191   tok_num = 0;
192 }
193
194 void  surf_parse_close(void) {
195   surf_parse__delete_buffer(input_buffer);
196   fclose(file_to_parse);
197
198   line_pos = 1;
199   char_pos = 0;
200   tok_num = 0;
201 }
202
203 void surf_parse_float(xbt_maxmin_float_t *value)
204
205   e_surf_token_t token;
206   int ret = 0;
207
208   token = surf_parse();         /* power_scale */
209   xbt_assert1((token == TOKEN_WORD), "Parse error line %d", line_pos);
210   ret = sscanf(surf_parse_text, XBT_MAXMIN_FLOAT_T, value);
211   xbt_assert2((ret==1), "Parse error line %d : %s not a number", line_pos,
212               surf_parse_text);
213 }
214
215 void surf_parse_trace(tmgr_trace_t *trace)
216 {
217   e_surf_token_t token;
218   
219   token = surf_parse();         /* power_trace */
220   xbt_assert1((token == TOKEN_WORD), "Parse error line %d", line_pos);
221   if (strcmp(surf_parse_text, "") == 0)
222     *trace = NULL;
223   else
224     *trace = tmgr_trace_new(surf_parse_text);
225 }
226 /*  Local variables: */
227 /*  mode: c */
228 /*  End: */