3 /* DataDesc/ddt_parse -- automatic parsing of data structures */
5 /* Authors: Arnaud Legrand, Martin Quinson */
6 /* Copyright (C) 2003, 2004 Martin Quinson. */
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. */
13 #include "gras/DataDesc/datadesc_private.h"
14 #include "gras/DataDesc/ddt_parse.yy.h"
16 YY_BUFFER_STATE input_buffer;
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,"The crude internals of the lexer used for type parsing");
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)
28 %x annotate comment foo
35 int annotate_caller=0;
37 char string_buf[GRAS_DDT_PARSE_MAX_STR_CONST];
38 char *string_buf_ptr = NULL;
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);
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);
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);
65 PARSE_ERROR0("``/*g'' construct closed by a regular ``*/''");
68 PARSE_ERROR0("Type annotation cannot spread over several lines");
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;
77 "/*[^g]" { /****************** COMMENTS ************************/
78 /* constructs like : */
80 /* are not comments but size annotations */
81 comment_caller = INITIAL;
90 <comment>[^*\n]* { /* eat anything that's not a '*' */
92 <comment>"*"+[^*/\n]* { /* eat up '*'s not followed by '/'s */
95 ++gras_ddt_parse_line_pos;
96 gras_ddt_parse_col_pos=0;
97 gras_ddt_parse_char_pos++;
100 gras_ddt_parse_char_pos+= strlen(yytext);
101 gras_ddt_parse_col_pos+= strlen(yytext);
102 BEGIN(comment_caller);
105 ({letter}|{digit})* { /****************** STATEMENTS ************************/
106 gras_ddt_parse_char_pos += strlen(yytext);
107 gras_ddt_parse_col_pos += strlen(yytext);
109 return(GRAS_DDT_PARSE_TOKEN_WORD);
112 gras_ddt_parse_char_pos++;
113 gras_ddt_parse_col_pos++;
115 return(GRAS_DDT_PARSE_TOKEN_LA);
118 gras_ddt_parse_char_pos++;
119 gras_ddt_parse_col_pos++;
121 return(GRAS_DDT_PARSE_TOKEN_RA);
124 gras_ddt_parse_char_pos++;
125 gras_ddt_parse_col_pos++;
127 return(GRAS_DDT_PARSE_TOKEN_LB);
130 gras_ddt_parse_char_pos++;
131 gras_ddt_parse_col_pos++;
133 return(GRAS_DDT_PARSE_TOKEN_RB);
136 gras_ddt_parse_char_pos++;
137 gras_ddt_parse_col_pos++;
139 return(GRAS_DDT_PARSE_TOKEN_LP);
142 gras_ddt_parse_char_pos++;
143 gras_ddt_parse_col_pos++;
145 return(GRAS_DDT_PARSE_TOKEN_RP);
148 gras_ddt_parse_char_pos++;
149 gras_ddt_parse_col_pos++;
151 return(GRAS_DDT_PARSE_TOKEN_STAR);
154 gras_ddt_parse_char_pos++;
155 gras_ddt_parse_col_pos++;
157 return(GRAS_DDT_PARSE_TOKEN_SEMI_COLON);
160 gras_ddt_parse_char_pos++;
161 gras_ddt_parse_col_pos++;
163 return(GRAS_DDT_PARSE_TOKEN_COLON);
166 gras_ddt_parse_line_pos++;
167 gras_ddt_parse_char_pos++;
168 gras_ddt_parse_col_pos=0;
172 gras_ddt_parse_char_pos++;
173 gras_ddt_parse_col_pos++;
177 /* {space}+ { return(TOKEN_SPACE);} */
179 void gras_ddt_parse_dump(void) {
180 switch(gras_ddt_parse_tok_num) {
181 case GRAS_DDT_PARSE_TOKEN_LA : {printf("TOKEN_LA ");break;}
182 case GRAS_DDT_PARSE_TOKEN_RA : {printf("TOKEN_RA ");break;}
183 case GRAS_DDT_PARSE_TOKEN_WORD : {printf("TOKEN_WORD ");break;}
184 /* case GRAS_DDT_PARSE_TOKEN_SPACE : {printf("TOKEN_SPACE ");break;}*/
185 /* case GRAS_DDT_PARSE_TOKEN_COMMENT : {printf("TOKEN_COMMENT ");break;}*/
186 case GRAS_DDT_PARSE_TOKEN_NEWLINE : {printf("TOKEN_NEWLINE\n");return;}
187 case GRAS_DDT_PARSE_TOKEN_EMPTY : {printf("TOKEN_EMPTY\n");return;}
188 default : {printf("Unknown token %d\n", gras_ddt_parse_tok_num);return;}
190 printf("-->%s<-- [line %d, pos %d]\n",yytext,gras_ddt_parse_line_pos,gras_ddt_parse_char_pos);
194 int gras_ddt_parse_lex_n_dump(void) {
195 gras_ddt_parse_tok_num = gras_ddt_parse_lex();
196 /* gras_ddt_parse_char_pos += strlen(yytext);*/
197 return(gras_ddt_parse_tok_num);
200 void gras_ddt_parse_pointer_init(const char *file) {
201 file_to_parse = fopen(file,"r");
202 input_buffer = yy_create_buffer( file_to_parse, 10 );
203 yy_switch_to_buffer(input_buffer);
205 gras_ddt_parse_line_pos = 1;
206 gras_ddt_parse_char_pos = 0;
207 gras_ddt_parse_col_pos = 0;
208 gras_ddt_parse_tok_num = 0;
211 void gras_ddt_parse_pointer_close(void) {
212 yy_delete_buffer(input_buffer);
213 fclose(file_to_parse);
215 gras_ddt_parse_line_pos = 1;
216 gras_ddt_parse_char_pos = 0;
217 gras_ddt_parse_tok_num = 0;
221 void gras_ddt_parse_pointer_string_init(const char *string_to_parse) {
222 input_buffer = yy_scan_string (string_to_parse);
223 definition = string_to_parse;
224 yy_switch_to_buffer(input_buffer);
226 gras_ddt_parse_line_pos = 1;
227 gras_ddt_parse_char_pos = 0;
228 gras_ddt_parse_tok_num = 0;
231 void gras_ddt_parse_pointer_string_close(void) {
232 yy_delete_buffer(input_buffer);
234 gras_ddt_parse_line_pos = 1;
235 gras_ddt_parse_char_pos = 0;
236 gras_ddt_parse_tok_num = 0;
239 /* Local variables:*/