Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Version 0.6 (protocol not changed; ABI expended)
[simgrid.git] / src / gras / DataDesc / ddt_parse.yy.l
1 /* $Id$ */
2
3 /* DataDesc/ddt_parse -- automatic parsing of data structures */
4
5 /* Authors: Arnaud Legrand, Martin Quinson            */
6 /* Copyright (C) 2003, 2004 Martin Quinson.                                 */
7
8 /* This program is free software; you can redistribute it and/or modify it
9    under the terms of the license (GNU LGPL) which comes with this package. */
10
11 %option noyywrap
12 %{
13 #include"DataDesc/datadesc_private.h"
14 #include"DataDesc/ddt_parse.yy.h"
15 #include <string.h>
16   YY_BUFFER_STATE input_buffer;
17   FILE *file_to_parse;
18
19   int gras_ddt_parse_line_pos = 1;
20   int gras_ddt_parse_col_pos = 0;
21   int gras_ddt_parse_char_pos = 0;
22   int gras_ddt_parse_tok_num = 0;
23   const char *definition;
24   GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(lexer,ddt_parse);
25 #define SHOW_WHERE DEBUG4("%d:%d (char #%d): seen '%s'", gras_ddt_parse_line_pos,gras_ddt_parse_col_pos,gras_ddt_parse_char_pos,yytext)
26 %}
27
28 %x annotate comment foo
29 space           [ \t]
30 letter          [A-Za-z._-]
31 digit           [0-9]
32
33 %%
34    int comment_caller=0;
35    int annotate_caller=0;
36
37    char string_buf[GRAS_DDT_PARSE_MAX_STR_CONST];
38    char *string_buf_ptr = NULL;
39
40 "//"[^\n]*
41
42 "/*g"{space}* { /****************** ANNOTATION ************************/
43   DEBUG0("Begin annotation");
44   annotate_caller = INITIAL;
45   gras_ddt_parse_char_pos+= strlen(yytext);
46   gras_ddt_parse_col_pos+= strlen(yytext);
47   BEGIN(annotate);
48 }
49 <foo>"/*g"{space}* { /* trim annotation */
50   DEBUG0("Begin annotation");
51   annotate_caller = foo;
52   gras_ddt_parse_char_pos+= strlen(yytext);
53   gras_ddt_parse_col_pos+= strlen(yytext);
54   BEGIN(annotate);
55 }
56
57 <annotate>{space}*"g*/" {
58   DEBUG0("End annotation");
59   gras_ddt_parse_char_pos+= strlen(yytext);
60   gras_ddt_parse_col_pos+= strlen(yytext);
61   BEGIN(annotate_caller);
62 }
63
64 <annotate>"*/" {
65   PARSE_ERROR0("``/*g'' construct closed by a regular ``*/''");
66 }
67 <annotate>\n  {
68   PARSE_ERROR0("Type annotation cannot spread over several lines");
69 }
70
71 <annotate>.* { /* eat the rest */
72   gras_ddt_parse_char_pos+= strlen(yytext);
73   gras_ddt_parse_col_pos+= strlen(yytext);
74   return GRAS_DDT_PARSE_TOKEN_ANNOTATE;
75 }
76
77 "/*[^g]"   { /****************** COMMENTS ************************/
78          // constructs like /*g [string] g*/ are not comments but size annotations
79   comment_caller = INITIAL;
80   BEGIN(comment);
81 }
82
83 <foo>"/*[^g]"    {
84   comment_caller = foo;
85   BEGIN(comment);
86 }
87
88 <comment>[^*\n]*      { /* eat anything that's not a '*' */
89 }
90 <comment>"*"+[^*/\n]* { /* eat up '*'s not followed by '/'s */
91 }
92 <comment>\n             {
93   ++gras_ddt_parse_line_pos;
94   gras_ddt_parse_col_pos=0;
95   gras_ddt_parse_char_pos++;
96 }
97 <comment>"*"+"/" {
98   gras_ddt_parse_char_pos+= strlen(yytext);
99   gras_ddt_parse_col_pos+= strlen(yytext);
100   BEGIN(comment_caller);
101 }
102
103 ({letter}|{digit})* {  /****************** STATEMENTS ************************/
104   gras_ddt_parse_char_pos += strlen(yytext);
105   gras_ddt_parse_col_pos += strlen(yytext);
106   SHOW_WHERE;
107   return(GRAS_DDT_PARSE_TOKEN_WORD);
108 }
109 "{"  { 
110   gras_ddt_parse_char_pos++; 
111   gras_ddt_parse_col_pos++; 
112   SHOW_WHERE;
113   return(GRAS_DDT_PARSE_TOKEN_LA);
114 }
115 "}" {
116   gras_ddt_parse_char_pos++;
117   gras_ddt_parse_col_pos++;
118   SHOW_WHERE;
119   return(GRAS_DDT_PARSE_TOKEN_RA);
120 }
121 "["  { 
122   gras_ddt_parse_char_pos++; 
123   gras_ddt_parse_col_pos++; 
124   SHOW_WHERE;
125   return(GRAS_DDT_PARSE_TOKEN_LB);
126 }
127 "]" {
128   gras_ddt_parse_char_pos++;
129   gras_ddt_parse_col_pos++;
130   SHOW_WHERE;
131   return(GRAS_DDT_PARSE_TOKEN_RB);
132 }
133 "("  { 
134   gras_ddt_parse_char_pos++; 
135   gras_ddt_parse_col_pos++; 
136   SHOW_WHERE;
137   return(GRAS_DDT_PARSE_TOKEN_LP);
138 }
139 ")" {
140   gras_ddt_parse_char_pos++;
141   gras_ddt_parse_col_pos++;
142   SHOW_WHERE;
143   return(GRAS_DDT_PARSE_TOKEN_RP);
144 }
145 "*" {
146   gras_ddt_parse_char_pos++;
147   gras_ddt_parse_col_pos++;
148   SHOW_WHERE;
149   return(GRAS_DDT_PARSE_TOKEN_STAR);
150 }
151 ";" {
152   gras_ddt_parse_char_pos++;
153   gras_ddt_parse_col_pos++;
154   SHOW_WHERE;
155   return(GRAS_DDT_PARSE_TOKEN_SEMI_COLON);
156 }
157 "," { 
158   gras_ddt_parse_char_pos++;
159   gras_ddt_parse_col_pos++;
160   SHOW_WHERE;
161   return(GRAS_DDT_PARSE_TOKEN_COLON);
162 }
163 "\n" {
164  gras_ddt_parse_line_pos++; 
165  gras_ddt_parse_char_pos++;
166  gras_ddt_parse_col_pos=0;
167   SHOW_WHERE;
168 }
169 . { 
170   gras_ddt_parse_char_pos++;
171   gras_ddt_parse_col_pos++;
172   SHOW_WHERE;
173 }
174 %%
175 /* {space}+                { return(TOKEN_SPACE);} */
176
177 void gras_ddt_parse_dump(void) {
178   switch(gras_ddt_parse_tok_num) {
179   case GRAS_DDT_PARSE_TOKEN_LA      : {printf("TOKEN_LA ");break;}
180   case GRAS_DDT_PARSE_TOKEN_RA      : {printf("TOKEN_RA ");break;}
181   case GRAS_DDT_PARSE_TOKEN_WORD    : {printf("TOKEN_WORD ");break;}
182     //  case GRAS_DDT_PARSE_TOKEN_SPACE   : {printf("TOKEN_SPACE ");break;}
183     //  case GRAS_DDT_PARSE_TOKEN_COMMENT : {printf("TOKEN_COMMENT ");break;}
184   case GRAS_DDT_PARSE_TOKEN_NEWLINE : {printf("TOKEN_NEWLINE\n");return;}
185   case GRAS_DDT_PARSE_TOKEN_EMPTY : {printf("TOKEN_EMPTY\n");return;}
186   default             : {printf("Unknown token %d\n", gras_ddt_parse_tok_num);return;}
187   }
188   printf("-->%s<-- [line %d, pos %d]\n",yytext,gras_ddt_parse_line_pos,gras_ddt_parse_char_pos);
189   return;
190 }
191
192 int gras_ddt_parse_lex_n_dump(void) {
193   gras_ddt_parse_tok_num = gras_ddt_parse_lex();
194   //  gras_ddt_parse_char_pos += strlen(yytext);
195   return(gras_ddt_parse_tok_num);
196 }
197
198 void  gras_ddt_parse_pointer_init(const char *file) {
199   file_to_parse = fopen(file,"r");
200   input_buffer = yy_create_buffer( file_to_parse, 10 );
201   yy_switch_to_buffer(input_buffer);
202
203   gras_ddt_parse_line_pos = 1;
204   gras_ddt_parse_char_pos = 0;
205   gras_ddt_parse_col_pos = 0;
206   gras_ddt_parse_tok_num = 0;
207 }
208
209 void  gras_ddt_parse_pointer_close(void) {
210   yy_delete_buffer(input_buffer);
211   fclose(file_to_parse);
212
213   gras_ddt_parse_line_pos = 1;
214   gras_ddt_parse_char_pos = 0;
215   gras_ddt_parse_tok_num = 0;
216 }
217
218
219 void  gras_ddt_parse_pointer_string_init(const char *string_to_parse) {
220   input_buffer = yy_scan_string (string_to_parse);
221   definition = string_to_parse;
222   yy_switch_to_buffer(input_buffer);
223
224   gras_ddt_parse_line_pos = 1;
225   gras_ddt_parse_char_pos = 0;
226   gras_ddt_parse_tok_num = 0;
227 }
228
229 void  gras_ddt_parse_pointer_string_close(void) {
230   yy_delete_buffer(input_buffer);
231
232   gras_ddt_parse_line_pos = 1;
233   gras_ddt_parse_char_pos = 0;
234   gras_ddt_parse_tok_num = 0;
235 }
236
237 // Local variables:
238 // mode: c
239 // End: