3 /* DataDesc/ddt_parse -- automatic parsing of data structures */
5 /* Copyright (c) 2004 Arnaud Legrand, Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
12 #include "gras/DataDesc/datadesc_private.h"
13 #include "gras/DataDesc/ddt_parse.yy.h"
15 YY_BUFFER_STATE gras_ddt_input_buffer;
16 FILE *gras_ddt_file_to_parse;
18 int gras_ddt_parse_line_pos = 1;
19 int gras_ddt_parse_col_pos = 0;
20 int gras_ddt_parse_char_pos = 0;
21 int gras_ddt_parse_tok_num = 0;
22 const char *definition;
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_ddt_lexer,gras_ddt_parse,"The crude internals of the lexer used for type parsing");
24 #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)
27 %x annotate comment foo
34 int annotate_caller=0;
38 "/*g"{space}* { /****************** ANNOTATION ************************/
39 DEBUG0("Begin annotation");
40 annotate_caller = INITIAL;
41 gras_ddt_parse_char_pos+= strlen(yytext);
42 gras_ddt_parse_col_pos+= strlen(yytext);
45 <foo>"/*g"{space}* { /* trim annotation */
46 DEBUG0("Begin annotation");
47 annotate_caller = foo;
48 gras_ddt_parse_char_pos+= strlen(yytext);
49 gras_ddt_parse_col_pos+= strlen(yytext);
53 <annotate>{space}*"g*/" {
54 DEBUG0("End annotation");
55 gras_ddt_parse_char_pos+= strlen(yytext);
56 gras_ddt_parse_col_pos+= strlen(yytext);
57 BEGIN(annotate_caller);
61 PARSE_ERROR0("``/*g'' construct closed by a regular ``*/''");
64 PARSE_ERROR0("Type annotation cannot spread over several lines");
67 <annotate>.* { /* eat the rest */
68 gras_ddt_parse_char_pos+= strlen(yytext);
69 gras_ddt_parse_col_pos+= strlen(yytext);
70 return GRAS_DDT_PARSE_TOKEN_ANNOTATE;
73 "/*[^g]" { /****************** COMMENTS ************************/
74 /* constructs like : */
76 /* are not comments but size annotations */
77 comment_caller = INITIAL;
86 <comment>[^*\n]* { /* eat anything that's not a '*' */
88 <comment>"*"+[^*/\n]* { /* eat up '*'s not followed by '/'s */
91 ++gras_ddt_parse_line_pos;
92 gras_ddt_parse_col_pos=0;
93 gras_ddt_parse_char_pos++;
96 gras_ddt_parse_char_pos+= strlen(yytext);
97 gras_ddt_parse_col_pos+= strlen(yytext);
98 BEGIN(comment_caller);
101 ({letter}|{digit})* { /****************** STATEMENTS ************************/
102 gras_ddt_parse_char_pos += strlen(yytext);
103 gras_ddt_parse_col_pos += strlen(yytext);
105 return(GRAS_DDT_PARSE_TOKEN_WORD);
108 gras_ddt_parse_char_pos++;
109 gras_ddt_parse_col_pos++;
111 return(GRAS_DDT_PARSE_TOKEN_LA);
114 gras_ddt_parse_char_pos++;
115 gras_ddt_parse_col_pos++;
117 return(GRAS_DDT_PARSE_TOKEN_RA);
120 gras_ddt_parse_char_pos++;
121 gras_ddt_parse_col_pos++;
123 return(GRAS_DDT_PARSE_TOKEN_LB);
126 gras_ddt_parse_char_pos++;
127 gras_ddt_parse_col_pos++;
129 return(GRAS_DDT_PARSE_TOKEN_RB);
132 gras_ddt_parse_char_pos++;
133 gras_ddt_parse_col_pos++;
135 return(GRAS_DDT_PARSE_TOKEN_LP);
138 gras_ddt_parse_char_pos++;
139 gras_ddt_parse_col_pos++;
141 return(GRAS_DDT_PARSE_TOKEN_RP);
144 gras_ddt_parse_char_pos++;
145 gras_ddt_parse_col_pos++;
147 return(GRAS_DDT_PARSE_TOKEN_STAR);
150 gras_ddt_parse_char_pos++;
151 gras_ddt_parse_col_pos++;
153 return(GRAS_DDT_PARSE_TOKEN_SEMI_COLON);
156 gras_ddt_parse_char_pos++;
157 gras_ddt_parse_col_pos++;
159 return(GRAS_DDT_PARSE_TOKEN_COLON);
162 gras_ddt_parse_line_pos++;
163 gras_ddt_parse_char_pos++;
164 gras_ddt_parse_col_pos=0;
168 gras_ddt_parse_char_pos++;
169 gras_ddt_parse_col_pos++;
173 /* {space}+ { return(TOKEN_SPACE);} */
175 void gras_ddt_parse_dump(void) {
176 switch(gras_ddt_parse_tok_num) {
177 case GRAS_DDT_PARSE_TOKEN_LA : {printf("TOKEN_LA ");break;}
178 case GRAS_DDT_PARSE_TOKEN_RA : {printf("TOKEN_RA ");break;}
179 case GRAS_DDT_PARSE_TOKEN_WORD : {printf("TOKEN_WORD ");break;}
180 /* case GRAS_DDT_PARSE_TOKEN_SPACE : {printf("TOKEN_SPACE ");break;}*/
181 /* case GRAS_DDT_PARSE_TOKEN_COMMENT : {printf("TOKEN_COMMENT ");break;}*/
182 case GRAS_DDT_PARSE_TOKEN_NEWLINE : {printf("TOKEN_NEWLINE\n");return;}
183 case GRAS_DDT_PARSE_TOKEN_EMPTY : {printf("TOKEN_EMPTY\n");return;}
184 default : {printf("Unknown token %d\n", gras_ddt_parse_tok_num);return;}
186 printf("-->%s<-- [line %d, pos %d]\n",yytext,gras_ddt_parse_line_pos,gras_ddt_parse_char_pos);
190 int gras_ddt_parse_lex_n_dump(void) {
191 gras_ddt_parse_tok_num = gras_ddt_parse_lex();
192 /* gras_ddt_parse_char_pos += strlen(yytext);*/
193 return(gras_ddt_parse_tok_num);
196 void gras_ddt_parse_pointer_init(const char *file) {
197 gras_ddt_file_to_parse = fopen(file,"r");
198 gras_ddt_input_buffer = yy_create_buffer( gras_ddt_file_to_parse, 10 );
199 yy_switch_to_buffer(gras_ddt_input_buffer);
201 gras_ddt_parse_line_pos = 1;
202 gras_ddt_parse_char_pos = 0;
203 gras_ddt_parse_col_pos = 0;
204 gras_ddt_parse_tok_num = 0;
207 void gras_ddt_parse_pointer_close(void) {
208 yy_delete_buffer(gras_ddt_input_buffer);
209 fclose(gras_ddt_file_to_parse);
211 gras_ddt_parse_line_pos = 1;
212 gras_ddt_parse_char_pos = 0;
213 gras_ddt_parse_tok_num = 0;
217 void gras_ddt_parse_pointer_string_init(const char *string_to_parse) {
218 gras_ddt_input_buffer = yy_scan_string (string_to_parse);
219 definition = string_to_parse;
220 yy_switch_to_buffer(gras_ddt_input_buffer);
222 gras_ddt_parse_line_pos = 1;
223 gras_ddt_parse_char_pos = 0;
224 gras_ddt_parse_tok_num = 0;
227 void gras_ddt_parse_pointer_string_close(void) {
228 yy_delete_buffer(gras_ddt_input_buffer);
230 gras_ddt_parse_line_pos = 1;
231 gras_ddt_parse_char_pos = 0;
232 gras_ddt_parse_tok_num = 0;
235 yyunput('\0',NULL); /* fake a use of this function to calm gcc down */
238 /* Local variables:*/