Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Bug fix + handling of variables with a weight equal to 0 (a suspended
[simgrid.git] / src / surf / surf_parse.l
1 /*      $Id$     */
2
3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
4
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. */
7
8 %option noyywrap
9 %{
10 #include "xbt/sysdep.h"
11 #include"surf/surf_parse.h"
12 #include "xbt/log.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(parse, surf ,"Logging specific to the SURF  module");
14
15   YY_BUFFER_STATE input_buffer;
16   FILE *file_to_parse;
17
18   int line_pos = 1;
19   int char_pos = 0;
20   int tok_num = 0;
21 %}
22
23 %x comment foo str
24 space           [ \t]
25 letter          [A-Za-z._-]
26 digit           [0-9]
27
28 %%
29         int comment_caller=0;
30
31         char string_buf[MAX_STR_CONST];
32         char *string_buf_ptr = NULL;
33
34 "//"[^\n]*
35 "/*"         {
36              comment_caller = INITIAL;
37              BEGIN(comment);
38              }
39
40 <foo>"/*"    {
41              comment_caller = foo;
42              BEGIN(comment);
43              }
44
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);
49
50 \"      string_buf_ptr = string_buf; char_pos++; BEGIN(str);
51
52 <str>\"        { /* saw closing quote - all done */
53         BEGIN(INITIAL);
54         *string_buf_ptr = '\0';
55         surf_parse_text=string_buf;
56         char_pos++;
57         return TOKEN_WORD;
58         /* return string constant token type and
59          * value to parser
60          */
61         }
62
63 <str>\n        {
64         /* error - unterminated string constant */
65         /* generate error message */
66         }
67
68 <str>\\[0-7]{1,3} {
69         /* octal escape sequence */
70         int result;
71
72         (void) sscanf( surf_parse_text + 1, "%o", &result );
73
74         if ( result > 0xff )
75                 /* error, constant is out-of-bounds */
76
77         *string_buf_ptr++ = result;
78         char_pos++;
79         }
80
81 <str>\\[0-9]+ {
82         /* generate error - bad escape sequence; something
83          * like '\48' or '\0777777'
84          */
85         }
86
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++;}
92
93 <str>\\(.|\n)  {*string_buf_ptr++ = surf_parse_text[1];         
94                 if(surf_parse_text[1]=='\n') {
95                   ++line_pos;char_pos=0;
96                 } else { char_pos++;}
97                }
98
99 <str>[^\\\n\"]+        {
100         char *yptr = surf_parse_text;
101
102         while ( *yptr )
103           *string_buf_ptr++ = *yptr++;
104           char_pos++;
105         }
106
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);}
114 . { char_pos++;}
115 %%
116 /* {space}+                { return(TOKEN_SPACE);} */
117
118 static void __print_val(void) {
119   switch(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", tok_num);return;}
129   }
130 /*   if(strcmp(surf_parse_text,"")!=0) */
131     printf("-->%s<-- [line %d, pos %d]\n",surf_parse_text,line_pos,char_pos);
132 /*   else */
133 /*     printf("--><-- [line %d, pos %d]\n",line_pos,char_pos); */
134
135   return;
136 }
137
138 e_surf_token_t surf_parse(void) {
139   tok_num = surf_parse_lex();
140   __print_val();
141   char_pos += strlen(surf_parse_text);
142   return(tok_num);
143 }
144
145 void find_section(const char* file, const char* section_name)
146 {
147   e_surf_token_t token;
148   int found = 0;
149
150   surf_parse_open(file);
151
152   while((token=surf_parse())) {
153     if(token!=TOKEN_BEGIN_SECTION) continue;
154
155     token=surf_parse();
156     xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
157     if(strcmp(surf_parse_text,section_name)==0) found=1;
158
159     token=surf_parse();
160     xbt_assert1((token==TOKEN_CLOSURE),"Parse error line %d",line_pos);
161
162     if(found) return;
163   }
164
165   CRITICAL2("Could not find %s section in %s\n",section_name,file);
166   xbt_abort();
167 }
168
169 void close_section(const char* section_name)
170 {
171   e_surf_token_t token;
172
173   token=surf_parse();
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).", 
177               section_name);
178   
179   token=surf_parse();
180   xbt_assert1((token==TOKEN_CLOSURE),"Parse error line %d",line_pos);
181
182   surf_parse_close();
183 }
184
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);
190
191   line_pos = 1;
192   char_pos = 0;
193   tok_num = 0;
194 }
195
196 void  surf_parse_close(void) {
197   surf_parse__delete_buffer(input_buffer);
198   fclose(file_to_parse);
199
200   line_pos = 1;
201   char_pos = 0;
202   tok_num = 0;
203 }
204
205 void surf_parse_float(xbt_maxmin_float_t *value)
206
207   e_surf_token_t token;
208   int ret = 0;
209
210   token = surf_parse();         /* power_scale */
211   xbt_assert1((token == TOKEN_WORD), "Parse error line %d", line_pos);
212   ret = sscanf(surf_parse_text, XBT_MAXMIN_FLOAT_T, value);
213   xbt_assert2((ret==1), "Parse error line %d : %s not a number", line_pos,
214               surf_parse_text);
215 }
216
217 void surf_parse_trace(tmgr_trace_t *trace)
218 {
219   e_surf_token_t token;
220   
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)
224     *trace = NULL;
225   else
226     *trace = tmgr_trace_new(surf_parse_text);
227 }
228 /*  Local variables: */
229 /*  mode: c */
230 /*  End: */