* Parses a program */
| 928 | * Parses a program |
| 929 | */ |
| 930 | void parse(t_program* program, t_program* parent_program, std::set<std::string>& known_includes) { |
| 931 | // Get scope file path |
| 932 | string path = program->get_path(); |
| 933 | if( ! known_includes.insert(path).second) { |
| 934 | failure("Recursion detected, file: \"%s\"", path.c_str()); |
| 935 | } |
| 936 | |
| 937 | // Set current dir global, which is used in the include_file function |
| 938 | g_curdir = directory_name(path); |
| 939 | g_curpath = path; |
| 940 | |
| 941 | // Open the file |
| 942 | // skip UTF-8 BOM if there is one |
| 943 | yyin = fopen(path.c_str(), "r"); |
| 944 | if (yyin == 0) { |
| 945 | failure("Could not open input file: \"%s\"", path.c_str()); |
| 946 | } |
| 947 | if (skip_utf8_bom(yyin)) |
| 948 | pverbose("Skipped UTF-8 BOM at %s\n", path.c_str()); |
| 949 | |
| 950 | // Create new scope and scan for includes |
| 951 | pverbose("Scanning %s for includes\n", path.c_str()); |
| 952 | g_parse_mode = INCLUDES; |
| 953 | g_program = program; |
| 954 | g_scope = program->scope(); |
| 955 | try { |
| 956 | yylineno = 1; |
| 957 | if (yyparse() != 0) { |
| 958 | failure("Parser error during include pass."); |
| 959 | } |
| 960 | } catch (string &x) { |
| 961 | failure(x.c_str()); |
| 962 | } |
| 963 | fclose(yyin); |
| 964 | |
| 965 | // Recursively parse all the include programs |
| 966 | vector<t_program*>& includes = program->get_includes(); |
| 967 | vector<t_program*>::iterator iter; |
| 968 | for (iter = includes.begin(); iter != includes.end(); ++iter) { |
| 969 | parse(*iter, program, known_includes); |
| 970 | } |
| 971 | |
| 972 | // reset program doctext status before parsing a new file |
| 973 | reset_program_doctext_info(); |
| 974 | |
| 975 | // Parse the program file |
| 976 | g_parse_mode = PROGRAM; |
| 977 | g_program = program; |
| 978 | g_scope = program->scope(); |
| 979 | g_parent_scope = (parent_program != nullptr) ? parent_program->scope() : nullptr; |
| 980 | g_parent_prefix = program->get_name() + "."; |
| 981 | g_curpath = path; |
| 982 | |
| 983 | // Open the file |
| 984 | // skip UTF-8 BOM if there is one |
| 985 | yyin = fopen(path.c_str(), "r"); |
| 986 | if (yyin == 0) { |
| 987 | failure("Could not open input file: \"%s\"", path.c_str()); |