98 /* file with declaration of token constants. Generated by bison! */ |
98 /* file with declaration of token constants. Generated by bison! */ |
99 #include "iec_bison.h" |
99 #include "iec_bison.h" |
100 |
100 |
101 /* The interface through which bison and flex interact. */ |
101 /* The interface through which bison and flex interact. */ |
102 #include "stage1_2_priv.hh" |
102 #include "stage1_2_priv.hh" |
103 |
103 #include "create_enumtype_conversion_functions.hh" |
104 |
104 |
105 #include "../absyntax_utils/add_en_eno_param_decl.hh" /* required for add_en_eno_param_decl_c */ |
105 #include "../absyntax_utils/add_en_eno_param_decl.hh" /* required for add_en_eno_param_decl_c */ |
106 |
106 |
107 /* an ugly hack!! |
107 /* an ugly hack!! |
108 * We will probably not need it when we decide |
108 * We will probably not need it when we decide |
201 /* A global flag used to tell the parser whether to include the full variable location |
201 /* A global flag used to tell the parser whether to include the full variable location |
202 * when printing out error messages... |
202 * when printing out error messages... |
203 */ |
203 */ |
204 extern bool full_token_loc; |
204 extern bool full_token_loc; |
205 |
205 |
|
206 /* A global flag used to tell the parser whether to generate conversion function |
|
207 * for enumerated data types. |
|
208 */ |
|
209 extern bool conversion_functions_; |
|
210 |
206 /* A pointer to the root of the parsing tree that will be generated |
211 /* A pointer to the root of the parsing tree that will be generated |
207 * by bison. |
212 * by bison. |
208 */ |
213 */ |
209 extern symbol_c *tree_root; |
214 extern symbol_c *tree_root; |
210 |
215 |
2702 * If it were not for the above, we could use the rule |
2710 * If it were not for the above, we could use the rule |
2703 * identifier ':' enumerated_spec_init |
2711 * identifier ':' enumerated_spec_init |
2704 * and include the library_element_symtable.insert(...) code in the rule actions! |
2712 * and include the library_element_symtable.insert(...) code in the rule actions! |
2705 */ |
2713 */ |
2706 identifier ':' enumerated_specification {library_element_symtable.insert($1, prev_declared_enumerated_type_name_token);} |
2714 identifier ':' enumerated_specification {library_element_symtable.insert($1, prev_declared_enumerated_type_name_token);} |
2707 {$$ = new enumerated_type_declaration_c($1, new enumerated_spec_init_c($3, NULL, locloc(@3)), locloc(@$));} |
2715 { |
|
2716 $$ = new enumerated_type_declaration_c($1, new enumerated_spec_init_c($3, NULL, locloc(@3)), locloc(@$)); |
|
2717 if (conversion_functions_) { |
|
2718 const char *name = ((identifier_c *)$1)->value; |
|
2719 add_enumtype_conversion_functions(name); |
|
2720 } |
|
2721 } |
2708 | identifier ':' enumerated_specification {library_element_symtable.insert($1, prev_declared_enumerated_type_name_token);} ASSIGN enumerated_value |
2722 | identifier ':' enumerated_specification {library_element_symtable.insert($1, prev_declared_enumerated_type_name_token);} ASSIGN enumerated_value |
2709 {$$ = new enumerated_type_declaration_c($1, new enumerated_spec_init_c($3, $6, locf(@3), locl(@6)), locloc(@$));} |
2723 {$$ = new enumerated_type_declaration_c($1, new enumerated_spec_init_c($3, $6, locf(@3), locl(@6)), locloc(@$));} |
2710 /* ERROR_CHECK_BEGIN */ |
2724 /* ERROR_CHECK_BEGIN */ |
2711 | error ':' enumerated_spec_init |
2725 | error ':' enumerated_spec_init |
2712 {$$ = NULL; print_err_msg(locf(@1), locl(@1), "invalid name defined for enumerated type declaration."); yyerrok;} |
2726 {$$ = NULL; print_err_msg(locf(@1), locl(@1), "invalid name defined for enumerated type declaration."); yyerrok;} |
8338 |
8352 |
8339 fclose(in_file); |
8353 fclose(in_file); |
8340 return 0; |
8354 return 0; |
8341 } |
8355 } |
8342 |
8356 |
8343 |
8357 /* Create a tmp file from a char buffer. */ |
8344 |
8358 FILE *ftmpopen (void *buf, size_t size, const char *opentype) |
|
8359 { |
|
8360 FILE *f; |
|
8361 f = tmpfile(); |
|
8362 fwrite(buf, 1, size, f); |
|
8363 rewind(f); |
|
8364 return f; |
|
8365 } |
|
8366 |
|
8367 /* sstage2__ function allow to parse a ST code inside a string. |
|
8368 * We use this function to add into AST auto generated code like enum conversion functions. |
|
8369 * This appoach allow us to write future changes code indipendetly and to check generate code |
|
8370 * during developing. |
|
8371 */ |
|
8372 int sstage2__(const char *text, |
|
8373 symbol_c **tree_root_ref, |
|
8374 bool full_token_loc_ /* error messages specify full token location */ |
|
8375 ) { |
|
8376 |
|
8377 FILE *in_file = NULL; |
|
8378 |
|
8379 if((in_file = ftmpopen((void *)text, strlen(text), "r")) == NULL) { |
|
8380 perror("Error creating temp file."); |
|
8381 return -1; |
|
8382 } |
|
8383 |
|
8384 /* now parse the input file... */ |
|
8385 #if YYDEBUG |
|
8386 yydebug = 1; |
|
8387 #endif |
|
8388 yyin = in_file; |
|
8389 |
|
8390 /* We turn on "allow_function_overloading" flag to disable some checks. |
|
8391 * In detail when we add to symboltable a symbol already processed we |
|
8392 * don't want to get any error. |
|
8393 */ |
|
8394 |
|
8395 allow_function_overloading = true; |
|
8396 allow_extensible_function_parameters = false; |
|
8397 full_token_loc = full_token_loc_; |
|
8398 current_filename = "built-in"; |
|
8399 current_tracking = GetNewTracking(yyin); |
|
8400 {int res; |
|
8401 if ((res = yyparse()) != 0) { |
|
8402 fprintf (stderr, "\nParsing failed because of too many consecutive syntax errors. Bailing out!\n"); |
|
8403 exit(EXIT_FAILURE); |
|
8404 } |
|
8405 } |
|
8406 |
|
8407 if (yynerrs > 0) { |
|
8408 fprintf (stderr, "\n%d error(s) found. Bailing out!\n", yynerrs /* global variable */); |
|
8409 exit(EXIT_FAILURE); |
|
8410 } |
|
8411 |
|
8412 if (tree_root_ref != NULL) |
|
8413 *tree_root_ref = tree_root; |
|
8414 |
|
8415 fclose(in_file); |
|
8416 return 0; |
|
8417 } |
|
8418 |
|
8419 |
|
8420 /* Create entry in symbol table for function conversion data type*/ |
|
8421 void add_enumtype_conversion_functions(const char * dname) { |
|
8422 std::string strname; |
|
8423 std::string tmp; |
|
8424 |
|
8425 strname = dname; |
|
8426 for (int i = 0; create_enumtype_conversion_functions_c::functionDataType[i] != NULL; i++) { |
|
8427 tmp = strname + std::string("_TO_") + create_enumtype_conversion_functions_c::functionDataType[i]; |
|
8428 library_element_symtable.insert(tmp.c_str(), prev_declared_derived_function_name_token); |
|
8429 |
|
8430 tmp = create_enumtype_conversion_functions_c::functionDataType[i] + std::string("_TO_") + strname; |
|
8431 library_element_symtable.insert(tmp.c_str(), prev_declared_derived_function_name_token); |
|
8432 } |
|
8433 } |
|
8434 |
|
8435 |
|
8436 |
|
8437 |