1 /* DataDesc/ddt_parse -- automatic parsing of data structures */
3 /* Copyright (c) 2004 Arnaud Legrand, Martin Quinson. All rights reserved. */
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. */
10 #include "gras/DataDesc/datadesc_private.h"
11 #include "gras/DataDesc/ddt_parse.yy.h"
13 YY_BUFFER_STATE gras_ddt_input_buffer;
14 FILE *gras_ddt_file_to_parse;
16 int gras_ddt_parse_line_pos = 1;
17 int gras_ddt_parse_col_pos = 0;
18 int gras_ddt_parse_char_pos = 0;
19 int gras_ddt_parse_tok_num = 0;
20 const char *definition;
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_ddt_lexer,gras_ddt_parse,"The crude internals of the lexer used for type parsing");
22 #define SHOW_WHERE XBT_DEBUG("%d:%d (char #%d): seen '%s'", gras_ddt_parse_line_pos,gras_ddt_parse_col_pos,gras_ddt_parse_char_pos,yytext)
25 %x annotate comment foo
32 int annotate_caller=0;
36 "/*g"{space}* { /****************** ANNOTATION ************************/
37 XBT_DEBUG("Begin annotation");
38 annotate_caller = INITIAL;
39 gras_ddt_parse_char_pos+= strlen(yytext);
40 gras_ddt_parse_col_pos+= strlen(yytext);
43 <foo>"/*g"{space}* { /* trim annotation */
44 XBT_DEBUG("Begin annotation");
45 annotate_caller = foo;
46 gras_ddt_parse_char_pos+= strlen(yytext);
47 gras_ddt_parse_col_pos+= strlen(yytext);
51 <annotate>{space}*"g*/" {
52 XBT_DEBUG("End annotation");
53 gras_ddt_parse_char_pos+= strlen(yytext);
54 gras_ddt_parse_col_pos+= strlen(yytext);
55 BEGIN(annotate_caller);
59 PARSE_ERROR("``/*g'' construct closed by a regular ``*/''");
62 PARSE_ERROR("Type annotation cannot spread over several lines");
65 <annotate>.* { /* eat the rest */
66 gras_ddt_parse_char_pos+= strlen(yytext);
67 gras_ddt_parse_col_pos+= strlen(yytext);
68 return GRAS_DDT_PARSE_TOKEN_ANNOTATE;
71 "/*[^g]" { /****************** COMMENTS ************************/
72 /* constructs like : */
74 /* are not comments but size annotations */
75 comment_caller = INITIAL;
84 <comment>[^*\n]* { /* eat anything that's not a '*' */
86 <comment>"*"+[^*/\n]* { /* eat up '*'s not followed by '/'s */
89 ++gras_ddt_parse_line_pos;
90 gras_ddt_parse_col_pos=0;
91 gras_ddt_parse_char_pos++;
94 gras_ddt_parse_char_pos+= strlen(yytext);
95 gras_ddt_parse_col_pos+= strlen(yytext);
96 BEGIN(comment_caller);
99 ({letter}|{digit})* { /****************** STATEMENTS ************************/
100 gras_ddt_parse_char_pos += strlen(yytext);
101 gras_ddt_parse_col_pos += strlen(yytext);
103 return(GRAS_DDT_PARSE_TOKEN_WORD);
106 gras_ddt_parse_char_pos++;
107 gras_ddt_parse_col_pos++;
109 return(GRAS_DDT_PARSE_TOKEN_LA);
112 gras_ddt_parse_char_pos++;
113 gras_ddt_parse_col_pos++;
115 return(GRAS_DDT_PARSE_TOKEN_RA);
118 gras_ddt_parse_char_pos++;
119 gras_ddt_parse_col_pos++;
121 return(GRAS_DDT_PARSE_TOKEN_LB);
124 gras_ddt_parse_char_pos++;
125 gras_ddt_parse_col_pos++;
127 return(GRAS_DDT_PARSE_TOKEN_RB);
130 gras_ddt_parse_char_pos++;
131 gras_ddt_parse_col_pos++;
133 return(GRAS_DDT_PARSE_TOKEN_LP);
136 gras_ddt_parse_char_pos++;
137 gras_ddt_parse_col_pos++;
139 return(GRAS_DDT_PARSE_TOKEN_RP);
142 gras_ddt_parse_char_pos++;
143 gras_ddt_parse_col_pos++;
145 return(GRAS_DDT_PARSE_TOKEN_STAR);
148 gras_ddt_parse_char_pos++;
149 gras_ddt_parse_col_pos++;
151 return(GRAS_DDT_PARSE_TOKEN_SEMI_COLON);
154 gras_ddt_parse_char_pos++;
155 gras_ddt_parse_col_pos++;
157 return(GRAS_DDT_PARSE_TOKEN_COLON);
160 gras_ddt_parse_line_pos++;
161 gras_ddt_parse_char_pos++;
162 gras_ddt_parse_col_pos=0;
166 gras_ddt_parse_char_pos++;
167 gras_ddt_parse_col_pos++;
171 /* {space}+ { return(TOKEN_SPACE);} */
173 void gras_ddt_parse_dump(void) {
174 switch(gras_ddt_parse_tok_num) {
175 case GRAS_DDT_PARSE_TOKEN_LA : {printf("TOKEN_LA ");break;}
176 case GRAS_DDT_PARSE_TOKEN_RA : {printf("TOKEN_RA ");break;}
177 case GRAS_DDT_PARSE_TOKEN_WORD : {printf("TOKEN_WORD ");break;}
178 /* case GRAS_DDT_PARSE_TOKEN_SPACE : {printf("TOKEN_SPACE ");break;}*/
179 /* case GRAS_DDT_PARSE_TOKEN_COMMENT : {printf("TOKEN_COMMENT ");break;}*/
180 case GRAS_DDT_PARSE_TOKEN_NEWLINE : {printf("TOKEN_NEWLINE\n");return;}
181 case GRAS_DDT_PARSE_TOKEN_EMPTY : {printf("TOKEN_EMPTY\n");return;}
182 default : {printf("Unknown token %d\n", gras_ddt_parse_tok_num);return;}
184 printf("-->%s<-- [line %d, pos %d]\n",yytext,gras_ddt_parse_line_pos,gras_ddt_parse_char_pos);
188 int gras_ddt_parse_lex_n_dump(void) {
189 gras_ddt_parse_tok_num = gras_ddt_parse_lex();
190 /* gras_ddt_parse_char_pos += strlen(yytext);*/
191 return(gras_ddt_parse_tok_num);
194 void gras_ddt_parse_pointer_init(const char *file) {
195 gras_ddt_file_to_parse = fopen(file,"r");
196 gras_ddt_input_buffer = yy_create_buffer( gras_ddt_file_to_parse, 10 );
197 yy_switch_to_buffer(gras_ddt_input_buffer);
199 gras_ddt_parse_line_pos = 1;
200 gras_ddt_parse_char_pos = 0;
201 gras_ddt_parse_col_pos = 0;
202 gras_ddt_parse_tok_num = 0;
205 void gras_ddt_parse_pointer_close(void) {
206 yy_delete_buffer(gras_ddt_input_buffer);
207 fclose(gras_ddt_file_to_parse);
209 gras_ddt_parse_line_pos = 1;
210 gras_ddt_parse_char_pos = 0;
211 gras_ddt_parse_tok_num = 0;
215 void gras_ddt_parse_pointer_string_init(const char *string_to_parse) {
216 gras_ddt_input_buffer = yy_scan_string (string_to_parse);
217 definition = string_to_parse;
218 yy_switch_to_buffer(gras_ddt_input_buffer);
220 gras_ddt_parse_line_pos = 1;
221 gras_ddt_parse_char_pos = 0;
222 gras_ddt_parse_tok_num = 0;
225 void gras_ddt_parse_pointer_string_close(void) {
226 yy_delete_buffer(gras_ddt_input_buffer);
228 gras_ddt_parse_line_pos = 1;
229 gras_ddt_parse_char_pos = 0;
230 gras_ddt_parse_tok_num = 0;
233 yyunput('\0',NULL); /* fake a use of this function to calm gcc down */
236 /* Local variables:*/