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 "xbt/datadesc/datadesc_private.h"
11 #include "xbt/datadesc/ddt_parse.yy.h"
13 YY_BUFFER_STATE xbt_ddt_input_buffer;
14 FILE *xbt_ddt_file_to_parse;
16 int xbt_ddt_parse_line_pos = 1;
17 int xbt_ddt_parse_col_pos = 0;
18 int xbt_ddt_parse_char_pos = 0;
19 int xbt_ddt_parse_tok_num = 0;
20 const char *definition;
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_ddt_lexer,xbt_ddt_parse,"The crude internals of the lexer used for type parsing");
22 #define SHOW_WHERE XBT_DEBUG("%d:%d (char #%d): seen '%s'", xbt_ddt_parse_line_pos,xbt_ddt_parse_col_pos,xbt_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 xbt_ddt_parse_char_pos+= strlen(yytext);
40 xbt_ddt_parse_col_pos+= strlen(yytext);
43 <foo>"/*g"{space}* { /* trim annotation */
44 XBT_DEBUG("Begin annotation");
45 annotate_caller = foo;
46 xbt_ddt_parse_char_pos+= strlen(yytext);
47 xbt_ddt_parse_col_pos+= strlen(yytext);
51 <annotate>{space}*"g*/" {
52 XBT_DEBUG("End annotation");
53 xbt_ddt_parse_char_pos+= strlen(yytext);
54 xbt_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 xbt_ddt_parse_char_pos+= strlen(yytext);
67 xbt_ddt_parse_col_pos+= strlen(yytext);
68 return XBT_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 ++xbt_ddt_parse_line_pos;
90 xbt_ddt_parse_col_pos=0;
91 xbt_ddt_parse_char_pos++;
94 xbt_ddt_parse_char_pos+= strlen(yytext);
95 xbt_ddt_parse_col_pos+= strlen(yytext);
96 BEGIN(comment_caller);
99 ({letter}|{digit})* { /****************** STATEMENTS ************************/
100 xbt_ddt_parse_char_pos += strlen(yytext);
101 xbt_ddt_parse_col_pos += strlen(yytext);
103 return(XBT_DDT_PARSE_TOKEN_WORD);
106 xbt_ddt_parse_char_pos++;
107 xbt_ddt_parse_col_pos++;
109 return(XBT_DDT_PARSE_TOKEN_LA);
112 xbt_ddt_parse_char_pos++;
113 xbt_ddt_parse_col_pos++;
115 return(XBT_DDT_PARSE_TOKEN_RA);
118 xbt_ddt_parse_char_pos++;
119 xbt_ddt_parse_col_pos++;
121 return(XBT_DDT_PARSE_TOKEN_LB);
124 xbt_ddt_parse_char_pos++;
125 xbt_ddt_parse_col_pos++;
127 return(XBT_DDT_PARSE_TOKEN_RB);
130 xbt_ddt_parse_char_pos++;
131 xbt_ddt_parse_col_pos++;
133 return(XBT_DDT_PARSE_TOKEN_LP);
136 xbt_ddt_parse_char_pos++;
137 xbt_ddt_parse_col_pos++;
139 return(XBT_DDT_PARSE_TOKEN_RP);
142 xbt_ddt_parse_char_pos++;
143 xbt_ddt_parse_col_pos++;
145 return(XBT_DDT_PARSE_TOKEN_STAR);
148 xbt_ddt_parse_char_pos++;
149 xbt_ddt_parse_col_pos++;
151 return(XBT_DDT_PARSE_TOKEN_SEMI_COLON);
154 xbt_ddt_parse_char_pos++;
155 xbt_ddt_parse_col_pos++;
157 return(XBT_DDT_PARSE_TOKEN_COLON);
160 xbt_ddt_parse_line_pos++;
161 xbt_ddt_parse_char_pos++;
162 xbt_ddt_parse_col_pos=0;
166 xbt_ddt_parse_char_pos++;
167 xbt_ddt_parse_col_pos++;
171 /* {space}+ { return(TOKEN_SPACE);} */
173 void xbt_ddt_parse_dump(void) {
174 switch(xbt_ddt_parse_tok_num) {
175 case XBT_DDT_PARSE_TOKEN_LA : {printf("TOKEN_LA ");break;}
176 case XBT_DDT_PARSE_TOKEN_RA : {printf("TOKEN_RA ");break;}
177 case XBT_DDT_PARSE_TOKEN_WORD : {printf("TOKEN_WORD ");break;}
178 /* case XBT_DDT_PARSE_TOKEN_SPACE : {printf("TOKEN_SPACE ");break;}*/
179 /* case XBT_DDT_PARSE_TOKEN_COMMENT : {printf("TOKEN_COMMENT ");break;}*/
180 case XBT_DDT_PARSE_TOKEN_NEWLINE : {printf("TOKEN_NEWLINE\n");return;}
181 case XBT_DDT_PARSE_TOKEN_EMPTY : {printf("TOKEN_EMPTY\n");return;}
182 default : {printf("Unknown token %d\n", xbt_ddt_parse_tok_num);return;}
184 printf("-->%s<-- [line %d, pos %d]\n",yytext,xbt_ddt_parse_line_pos,xbt_ddt_parse_char_pos);
188 int xbt_ddt_parse_lex_n_dump(void) {
189 xbt_ddt_parse_tok_num = xbt_ddt_parse_lex();
190 /* xbt_ddt_parse_char_pos += strlen(yytext);*/
191 return(xbt_ddt_parse_tok_num);
194 void xbt_ddt_parse_pointer_init(const char *file) {
195 xbt_ddt_file_to_parse = fopen(file,"r");
196 xbt_ddt_input_buffer = yy_create_buffer( xbt_ddt_file_to_parse, 10 );
197 yy_switch_to_buffer(xbt_ddt_input_buffer);
199 xbt_ddt_parse_line_pos = 1;
200 xbt_ddt_parse_char_pos = 0;
201 xbt_ddt_parse_col_pos = 0;
202 xbt_ddt_parse_tok_num = 0;
205 void xbt_ddt_parse_pointer_close(void) {
206 yy_delete_buffer(xbt_ddt_input_buffer);
207 fclose(xbt_ddt_file_to_parse);
209 xbt_ddt_parse_line_pos = 1;
210 xbt_ddt_parse_char_pos = 0;
211 xbt_ddt_parse_tok_num = 0;
215 void xbt_ddt_parse_pointer_string_init(const char *string_to_parse) {
216 xbt_ddt_input_buffer = yy_scan_string (string_to_parse);
217 definition = string_to_parse;
218 yy_switch_to_buffer(xbt_ddt_input_buffer);
220 xbt_ddt_parse_line_pos = 1;
221 xbt_ddt_parse_char_pos = 0;
222 xbt_ddt_parse_tok_num = 0;
225 void xbt_ddt_parse_pointer_string_close(void) {
226 yy_delete_buffer(xbt_ddt_input_buffer);
228 xbt_ddt_parse_line_pos = 1;
229 xbt_ddt_parse_char_pos = 0;
230 xbt_ddt_parse_tok_num = 0;
233 yyunput('\0',NULL); /* fake a use of this function to calm gcc down */
236 /* Local variables:*/