Add FileId to CompilationUnit.
This commit is contained in:
parent
64526a0b1c
commit
fe8ab3601c
@ -41,7 +41,7 @@ fn run(
|
|||||||
show_asm: bool,
|
show_asm: bool,
|
||||||
register_count: usize,
|
register_count: usize,
|
||||||
) -> Result<(), Vec<Diagnostic>> {
|
) -> Result<(), Vec<Diagnostic>> {
|
||||||
let mut compilation_unit = get_compilation_unit(&input)?;
|
let mut compilation_unit = get_compilation_unit(&input, None)?;
|
||||||
|
|
||||||
let mut symbol_table = SymbolTable::new();
|
let mut symbol_table = SymbolTable::new();
|
||||||
symbol_table.push_module_scope("global scope");
|
symbol_table.push_module_scope("global scope");
|
||||||
|
|||||||
@ -279,6 +279,7 @@ mod tests {
|
|||||||
x = \"Hello\"
|
x = \"Hello\"
|
||||||
end
|
end
|
||||||
",
|
",
|
||||||
|
None,
|
||||||
)?;
|
)?;
|
||||||
let mut symbol_table = SymbolTable::new();
|
let mut symbol_table = SymbolTable::new();
|
||||||
let mut types_table = TypesTable::new();
|
let mut types_table = TypesTable::new();
|
||||||
@ -302,6 +303,7 @@ mod tests {
|
|||||||
42 = 42
|
42 = 42
|
||||||
end
|
end
|
||||||
",
|
",
|
||||||
|
None,
|
||||||
)?;
|
)?;
|
||||||
let mut symbol_table = SymbolTable::new();
|
let mut symbol_table = SymbolTable::new();
|
||||||
let mut types_table = TypesTable::new();
|
let mut types_table = TypesTable::new();
|
||||||
@ -323,6 +325,7 @@ mod tests {
|
|||||||
x = 43
|
x = 43
|
||||||
end
|
end
|
||||||
",
|
",
|
||||||
|
None,
|
||||||
)?;
|
)?;
|
||||||
let mut symbol_table = SymbolTable::new();
|
let mut symbol_table = SymbolTable::new();
|
||||||
let mut types_table = TypesTable::new();
|
let mut types_table = TypesTable::new();
|
||||||
|
|||||||
@ -3,6 +3,7 @@ use crate::ast::extern_function::ExternFunction;
|
|||||||
use crate::ast::fqn_context::FqnContext;
|
use crate::ast::fqn_context::FqnContext;
|
||||||
use crate::ast::function::Function;
|
use crate::ast::function::Function;
|
||||||
use crate::ast::helpers::collect_diagnostics_into_mut;
|
use crate::ast::helpers::collect_diagnostics_into_mut;
|
||||||
|
use crate::compile_pipeline::FileId;
|
||||||
use crate::diagnostic::Diagnostic;
|
use crate::diagnostic::Diagnostic;
|
||||||
use crate::ir::ir_class::IrClass;
|
use crate::ir::ir_class::IrClass;
|
||||||
use crate::ir::ir_function::IrFunction;
|
use crate::ir::ir_function::IrFunction;
|
||||||
@ -13,6 +14,7 @@ use crate::types_table::TypesTable;
|
|||||||
use crate::{diagnostics_result, handle_diagnostics};
|
use crate::{diagnostics_result, handle_diagnostics};
|
||||||
|
|
||||||
pub struct CompilationUnit {
|
pub struct CompilationUnit {
|
||||||
|
file_id: Option<FileId>,
|
||||||
functions: Vec<Function>,
|
functions: Vec<Function>,
|
||||||
extern_functions: Vec<ExternFunction>,
|
extern_functions: Vec<ExternFunction>,
|
||||||
classes: Vec<Class>,
|
classes: Vec<Class>,
|
||||||
@ -20,11 +22,13 @@ pub struct CompilationUnit {
|
|||||||
|
|
||||||
impl CompilationUnit {
|
impl CompilationUnit {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
|
file_id: Option<FileId>,
|
||||||
functions: Vec<Function>,
|
functions: Vec<Function>,
|
||||||
extern_functions: Vec<ExternFunction>,
|
extern_functions: Vec<ExternFunction>,
|
||||||
classes: Vec<Class>,
|
classes: Vec<Class>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
file_id,
|
||||||
functions,
|
functions,
|
||||||
extern_functions,
|
extern_functions,
|
||||||
classes,
|
classes,
|
||||||
|
|||||||
@ -190,6 +190,7 @@ mod tests {
|
|||||||
|
|
||||||
fn useFoo(foo: Foo<String>) end
|
fn useFoo(foo: Foo<String>) end
|
||||||
",
|
",
|
||||||
|
None,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
let mut symbol_table = SymbolTable::new();
|
let mut symbol_table = SymbolTable::new();
|
||||||
|
|||||||
@ -7,16 +7,17 @@ use std::collections::HashMap;
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
pub type Filename = Rc<str>;
|
pub type Filename = Rc<str>;
|
||||||
|
pub type FileId = usize;
|
||||||
|
|
||||||
fn parse_compilation_units(
|
fn parse_compilation_units(
|
||||||
inputs: &HashMap<Filename, &str>,
|
inputs: &HashMap<FileId, &str>,
|
||||||
) -> Result<HashMap<Filename, CompilationUnit>, Diagnostics> {
|
) -> Result<HashMap<FileId, CompilationUnit>, Diagnostics> {
|
||||||
let mut parse_diagnostics = Vec::new();
|
let mut parse_diagnostics = Vec::new();
|
||||||
let mut compilation_units = HashMap::new();
|
let mut compilation_units = HashMap::new();
|
||||||
for (file_name, source) in inputs {
|
for (file_id, source) in inputs {
|
||||||
let (compilation_unit, mut ds) = parse_compilation_unit(source);
|
let (compilation_unit, mut ds) = parse_compilation_unit(source, Some(*file_id));
|
||||||
parse_diagnostics.append(&mut ds);
|
parse_diagnostics.append(&mut ds);
|
||||||
compilation_units.insert(file_name.clone(), compilation_unit);
|
compilation_units.insert(*file_id, compilation_unit);
|
||||||
}
|
}
|
||||||
if parse_diagnostics.is_empty() {
|
if parse_diagnostics.is_empty() {
|
||||||
Ok(compilation_units)
|
Ok(compilation_units)
|
||||||
@ -25,7 +26,7 @@ fn parse_compilation_units(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn compile_compilation_units(inputs: &HashMap<Filename, &str>) -> Result<(), Diagnostics> {
|
pub fn compile_compilation_units(inputs: &HashMap<FileId, &str>) -> Result<(), Diagnostics> {
|
||||||
let mut compilation_units = parse_compilation_units(inputs)?;
|
let mut compilation_units = parse_compilation_units(inputs)?;
|
||||||
|
|
||||||
let mut symbol_table = SymbolTable::new();
|
let mut symbol_table = SymbolTable::new();
|
||||||
|
|||||||
@ -138,7 +138,8 @@ mod tests {
|
|||||||
let c = 3
|
let c = 3
|
||||||
let x = a + b + c
|
let x = a + b + c
|
||||||
end
|
end
|
||||||
",
|
",
|
||||||
|
None,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
let mut symbol_table = SymbolTable::new();
|
let mut symbol_table = SymbolTable::new();
|
||||||
|
|||||||
@ -19,6 +19,7 @@ use crate::ast::parameter::Parameter;
|
|||||||
use crate::ast::statement::Statement;
|
use crate::ast::statement::Statement;
|
||||||
use crate::ast::string_literal::StringLiteral;
|
use crate::ast::string_literal::StringLiteral;
|
||||||
use crate::ast::type_use::TypeUse;
|
use crate::ast::type_use::TypeUse;
|
||||||
|
use crate::compile_pipeline::FileId;
|
||||||
use crate::diagnostic::{Diagnostic, Diagnostics};
|
use crate::diagnostic::{Diagnostic, Diagnostics};
|
||||||
use crate::error_codes::{LEXER_ERROR, PARSE_ERROR};
|
use crate::error_codes::{LEXER_ERROR, PARSE_ERROR};
|
||||||
use crate::lexer::{Lexer, LexerErrorKind};
|
use crate::lexer::{Lexer, LexerErrorKind};
|
||||||
@ -28,8 +29,11 @@ use std::str::FromStr;
|
|||||||
|
|
||||||
pub type ParseResult<T> = (T, Diagnostics);
|
pub type ParseResult<T> = (T, Diagnostics);
|
||||||
|
|
||||||
pub fn get_compilation_unit(input: &str) -> Result<CompilationUnit, Diagnostics> {
|
pub fn get_compilation_unit(
|
||||||
let (compilation_unit, diagnostics) = parse_compilation_unit(input);
|
input: &str,
|
||||||
|
file_id: Option<FileId>,
|
||||||
|
) -> Result<CompilationUnit, Diagnostics> {
|
||||||
|
let (compilation_unit, diagnostics) = parse_compilation_unit(input, file_id);
|
||||||
if diagnostics.is_empty() {
|
if diagnostics.is_empty() {
|
||||||
Ok(compilation_unit)
|
Ok(compilation_unit)
|
||||||
} else {
|
} else {
|
||||||
@ -37,11 +41,14 @@ pub fn get_compilation_unit(input: &str) -> Result<CompilationUnit, Diagnostics>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_compilation_unit(input: &str) -> ParseResult<CompilationUnit> {
|
pub fn parse_compilation_unit(
|
||||||
|
input: &str,
|
||||||
|
file_id: Option<FileId>,
|
||||||
|
) -> ParseResult<CompilationUnit> {
|
||||||
let mut parser = Parser::new(input);
|
let mut parser = Parser::new(input);
|
||||||
let mut diagnostics = Vec::new();
|
let mut diagnostics = Vec::new();
|
||||||
diagnostics.append(&mut parser.advance());
|
diagnostics.append(&mut parser.advance());
|
||||||
let (compilation_unit, mut ds) = parser.compilation_unit();
|
let (compilation_unit, mut ds) = parser.compilation_unit(file_id);
|
||||||
diagnostics.append(&mut ds);
|
diagnostics.append(&mut ds);
|
||||||
(compilation_unit, diagnostics)
|
(compilation_unit, diagnostics)
|
||||||
}
|
}
|
||||||
@ -297,7 +304,7 @@ impl<'a> Parser<'a> {
|
|||||||
node_id
|
node_id
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compilation_unit(&mut self) -> ParseResult<CompilationUnit> {
|
fn compilation_unit(&mut self, file_id: Option<FileId>) -> ParseResult<CompilationUnit> {
|
||||||
let mut functions: Vec<Function> = vec![];
|
let mut functions: Vec<Function> = vec![];
|
||||||
let mut extern_functions: Vec<ExternFunction> = vec![];
|
let mut extern_functions: Vec<ExternFunction> = vec![];
|
||||||
let mut classes: Vec<Class> = vec![];
|
let mut classes: Vec<Class> = vec![];
|
||||||
@ -330,7 +337,7 @@ impl<'a> Parser<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
(
|
(
|
||||||
CompilationUnit::new(functions, extern_functions, classes),
|
CompilationUnit::new(file_id, functions, extern_functions, classes),
|
||||||
diagnostics,
|
diagnostics,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -1364,7 +1371,7 @@ mod smoke_tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
fn smoke_test(input: &str) {
|
fn smoke_test(input: &str) {
|
||||||
let (_, diagnostics) = parse_compilation_unit(input);
|
let (_, diagnostics) = parse_compilation_unit(input, None);
|
||||||
if !diagnostics.is_empty() {
|
if !diagnostics.is_empty() {
|
||||||
eprintln!("{:#?}", diagnostics);
|
eprintln!("{:#?}", diagnostics);
|
||||||
panic!("There were diagnostics during parsing");
|
panic!("There were diagnostics during parsing");
|
||||||
@ -1583,7 +1590,7 @@ mod concrete_tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn assert_compilation_unit(input: &str) -> CompilationUnit {
|
fn assert_compilation_unit(input: &str) -> CompilationUnit {
|
||||||
let (compilation_unit, diagnostics) = parse_compilation_unit(input);
|
let (compilation_unit, diagnostics) = parse_compilation_unit(input, None);
|
||||||
if !diagnostics.is_empty() {
|
if !diagnostics.is_empty() {
|
||||||
report_diagnostics(&diagnostics);
|
report_diagnostics(&diagnostics);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,7 +29,7 @@ mod e2e_tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn prepare_context(input: &str) -> Result<DvmContext, Vec<Diagnostic>> {
|
fn prepare_context(input: &str) -> Result<DvmContext, Vec<Diagnostic>> {
|
||||||
let (mut compilation_unit, diagnostics) = parse_compilation_unit(input);
|
let (mut compilation_unit, diagnostics) = parse_compilation_unit(input, None);
|
||||||
if !diagnostics.is_empty() {
|
if !diagnostics.is_empty() {
|
||||||
return Err(diagnostics);
|
return Err(diagnostics);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user