Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
make valgrind happy happy.
[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 surf_input_buffer;
16   FILE *surf_file_to_parse;
17
18   int surf_line_pos = 1;
19   int surf_char_pos = -1;
20   int surf_tok_num = 0;
21   char string_buf[MAX_STR_CONST];
22   char *string_buf_ptr = NULL;
23 %}
24
25 %x comment foo str
26 space           [ \t]
27 letter          [A-Za-z._-]
28 digit           [0-9]
29
30 %%
31         int comment_caller=0;
32
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             {++surf_line_pos;surf_char_pos=0;}
48 <comment>"*"+"/"        BEGIN(comment_caller);
49
50 \"      string_buf_ptr = string_buf; surf_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         surf_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         surf_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';    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++;}
92
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++;}
97                }
98
99 <str>[^\\\n\"]+        {
100         char *yptr = surf_parse_text;
101
102         while ( *yptr )
103           *string_buf_ptr++ = *yptr++;
104           surf_char_pos++;
105         }
106
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++;}
115 %%
116 /* {space}+                { return(TOKEN_SPACE);} */
117
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;}
129   }
130
131   printf("-->%s<-- [line %d, pos %d]\n",surf_parse_text,surf_line_pos,surf_char_pos);
132
133   return;
134 }
135
136 e_surf_token_t surf_parse(void) {
137   surf_tok_num = surf_parse_lex();
138   __print_val();
139   surf_char_pos += strlen(surf_parse_text);
140   return(surf_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",surf_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",surf_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",surf_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",surf_line_pos);
179
180   surf_parse_close();
181 }
182
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);
188
189   surf_line_pos = 1;
190   surf_char_pos = 0;
191   surf_tok_num = 0;
192 }
193
194 void  surf_parse_close(void) {
195   surf_parse__delete_buffer(surf_input_buffer);
196   fclose(surf_file_to_parse);
197
198   surf_line_pos = 1;
199   surf_char_pos = 0;
200   surf_tok_num = 0;
201 }
202
203 void surf_parse_double(double *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", 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,
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", surf_line_pos);
221   if (strcmp(surf_parse_text, "") == 0)
222     *trace = NULL;
223   else
224     *trace = tmgr_trace_new(surf_parse_text);
225 }
226
227 void surf_parse_deployment_line(char **host, int *argc, char ***argv)
228 {
229   e_surf_token_t token;
230
231
232   /* Parse Host name */
233   *host = xbt_strdup(surf_parse_text);  
234
235   *argc = 0;
236   *argv = NULL;
237
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);
242
243     (*argc)++;
244     *argv=xbt_realloc (*argv, (*argc) * sizeof(char*));
245     (*argv)[(*argc)-1]=xbt_strdup(surf_parse_text);
246   }
247 }
248
249
250 /*  Local variables: */
251 /*  mode: c */
252 /*  End: */