|
1 | 1 | use super::function::FunctionParser; |
2 | 2 | use super::literal::ConstantDeclarationParser; |
3 | 3 | use crate::{ |
| 4 | + CompilationFlags, ProgramSource, |
4 | 5 | lang::{Program, SourceLocation}, |
5 | 6 | parser::{ |
6 | 7 | error::{ParseError, ParseResult, SemanticError}, |
@@ -55,14 +56,9 @@ impl Parse<Program> for ProgramParser { |
55 | 56 | let file_id = ctx.get_next_file_id(); |
56 | 57 | ctx.current_file_id = file_id; |
57 | 58 | filepaths.insert(file_id, filepath.clone()); |
58 | | - let input = std::fs::read_to_string(filepath.clone()).map_err(|_| { |
59 | | - SemanticError::with_context( |
60 | | - format!("Imported file not found: {filepath}"), |
61 | | - "import declaration", |
62 | | - ) |
63 | | - })?; |
64 | | - source_code.insert(file_id, input.clone()); |
65 | | - let subprogram = parse_program_helper(filepath.as_str(), input.as_str(), ctx)?; |
| 59 | + ctx.current_source_code = ProgramSource::Filepath(filepath).get_content(&ctx.flags).unwrap(); |
| 60 | + source_code.insert(file_id, ctx.current_source_code.clone()); |
| 61 | + let subprogram = parse_program_helper(ctx)?; |
66 | 62 | functions.extend(subprogram.functions); |
67 | 63 | function_locations.extend(subprogram.function_locations); |
68 | 64 | source_code.extend(subprogram.source_code); |
@@ -138,22 +134,17 @@ impl Parse<String> for ImportStatementParser { |
138 | 134 | } |
139 | 135 | } |
140 | 136 |
|
141 | | -fn parse_program_helper(filepath: &str, input: &str, ctx: &mut ParseContext) -> Result<Program, ParseError> { |
| 137 | +fn parse_program_helper(ctx: &mut ParseContext) -> Result<Program, ParseError> { |
142 | 138 | // Preprocess source to remove comments |
143 | | - let processed_input = lexer::preprocess_source(input); |
| 139 | + let processed_input = lexer::preprocess_source(&ctx.current_source_code); |
144 | 140 |
|
145 | 141 | // Parse grammar into AST nodes |
146 | 142 | let program_pair = parse_source(&processed_input)?; |
147 | 143 |
|
148 | 144 | // Parse into semantic structures |
149 | | - ctx.current_filepath = filepath.to_string(); |
150 | | - ctx.current_source_code = input.to_string(); |
151 | | - ctx.imported_filepaths.insert(filepath.to_string()); |
152 | 145 | ProgramParser.parse(program_pair, ctx) |
153 | 146 | } |
154 | 147 |
|
155 | | -pub fn parse_program(filepath: &str, input: &str) -> Result<Program, ParseError> { |
156 | | - let mut ctx = ParseContext::new(filepath, input); |
157 | | - ctx.imported_filepaths.insert(filepath.to_string()); |
158 | | - parse_program_helper(filepath, input, &mut ctx) |
| 148 | +pub fn parse_program(input: &ProgramSource, flags: CompilationFlags) -> Result<Program, ParseError> { |
| 149 | + parse_program_helper(&mut ParseContext::new(input, flags)?) |
159 | 150 | } |
0 commit comments