diff --git a/schala-lang/language/src/parsing.rs b/schala-lang/language/src/parsing.rs index 8ab3f09..b8e9bba 100644 --- a/schala-lang/language/src/parsing.rs +++ b/schala-lang/language/src/parsing.rs @@ -195,13 +195,13 @@ pub struct ParseRecord { } /// Main data structure for doing parsing. -pub struct Parser { +pub struct Parser<'a> { token_handler: TokenHandler, parse_record: Vec, parse_level: u32, restrictions: ParserRestrictions, id_store: ItemIdStore, - source_map: SourceMap, + source_map: &'a mut SourceMap, } @@ -244,16 +244,16 @@ impl TokenHandler { } } -impl Parser { +impl<'a> Parser<'a> { /// Create a new parser initialized with some tokens. - pub fn new(initial_input: Vec) -> Parser { + pub fn new(initial_input: Vec, source_map_pointer: &mut SourceMap) -> Parser { Parser { token_handler: TokenHandler::new(initial_input), parse_record: vec![], parse_level: 0, restrictions: ParserRestrictions { no_struct_literal: false }, id_store: ItemIdStore::new(), - source_map: SourceMap::new(), + source_map: source_map_pointer, } } @@ -346,7 +346,7 @@ macro_rules! delimited { } -impl Parser { +impl<'a> Parser<'a> { /// `program := (statement delimiter)* EOF` /// `delimiter := NEWLINE | ';'` #[recursive_descent_method] diff --git a/schala-lang/language/src/parsing/test.rs b/schala-lang/language/src/parsing/test.rs index 0bac8ab..1c74aa9 100644 --- a/schala-lang/language/src/parsing/test.rs +++ b/schala-lang/language/src/parsing/test.rs @@ -2,6 +2,7 @@ use ::std::rc::Rc; use std::str::FromStr; +use crate::util::quick_ast; use super::tokenize; use super::ParseResult; use crate::ast::*; @@ -14,8 +15,9 @@ use super::Variant::*; use super::ForBody::*; fn parse(input: &str) -> ParseResult { + let mut source_map = crate::source_map::SourceMap::new(); let tokens: Vec = tokenize(input); - let mut parser = super::Parser::new(tokens); + let mut parser = super::Parser::new(tokens, &mut source_map); parser.parse() } @@ -74,8 +76,9 @@ macro_rules! ex { ($expr_type:expr, $type_anno:expr) => { Expression::with_anno(ItemIdStore::new_id(), $expr_type, $type_anno) }; (s $expr_text:expr) => { { + let mut source_map = crate::source_map::SourceMap::new(); let tokens: Vec = tokenize($expr_text); - let mut parser = super::Parser::new(tokens); + let mut parser = super::Parser::new(tokens, &mut source_map); parser.expression().unwrap() } }; @@ -98,8 +101,9 @@ macro_rules! exst { }; (s $statement_text:expr) => { { + let mut source_map = crate::source_map::SourceMap::new(); let tokens: Vec = tokenize($statement_text); - let mut parser = super::Parser::new(tokens); + let mut parser = super::Parser::new(tokens, &mut source_map); parser.statement().unwrap() } } diff --git a/schala-lang/language/src/schala.rs b/schala-lang/language/src/schala.rs index df41c55..17ff59a 100644 --- a/schala-lang/language/src/schala.rs +++ b/schala-lang/language/src/schala.rs @@ -11,6 +11,7 @@ ComputationRequest, ComputationResponse, LangMetaRequest, LangMetaResponse, GlobalOutputStats, DebugResponse, DebugAsk}; use crate::{ast, reduced_ast, tokenizing, parsing, eval, typechecking, symbol_table, source_map}; +use source_map::SourceMap; pub type SymbolTableHandle = Rc>; pub type SourceMapHandle = Rc>; @@ -21,11 +22,12 @@ pub type SourceMapHandle = Rc>; pub struct Schala { source_reference: SourceReference, source_map: SourceMapHandle, + source_map_: SourceMap, state: eval::State<'static>, symbol_table: SymbolTableHandle, resolver: crate::scope_resolution::ScopeResolver<'static>, type_context: typechecking::TypeContext<'static>, - active_parser: Option, + active_parser: Option>, } impl Schala { @@ -46,6 +48,7 @@ impl Schala { source_reference: SourceReference::new(), symbol_table: symbols.clone(), source_map: source_map.clone(), + source_map_: SourceMap::new(), resolver: crate::scope_resolution::ScopeResolver::new(symbols.clone()), state: eval::State::new(symbols), type_context: typechecking::TypeContext::new(), @@ -109,7 +112,7 @@ fn parsing(input: Vec, handle: &mut Schala, comp: Option<&mut use crate::parsing::Parser; use ParsingDebugType::*; - let mut parser = handle.active_parser.take().unwrap_or_else(|| Parser::new(input)); + let mut parser = handle.active_parser.take().unwrap_or_else(|| Parser::new(input, &mut handle.source_map_)); let ast = parser.parse(); comp.map(|comp| { diff --git a/schala-lang/language/src/util.rs b/schala-lang/language/src/util.rs index 7e830ee..d482cde 100644 --- a/schala-lang/language/src/util.rs +++ b/schala-lang/language/src/util.rs @@ -49,8 +49,9 @@ impl<'a, T, V> ScopeStack<'a, T, V> where T: Hash + Eq { /// this is intended for use in tests, and does no error-handling whatsoever #[allow(dead_code)] pub fn quick_ast(input: &str) -> crate::ast::AST { + let mut source_map = crate::source_map::SourceMap::new(); let tokens = crate::tokenizing::tokenize(input); - let mut parser = crate::parsing::Parser::new(tokens); + let mut parser = crate::parsing::Parser::new(tokens, &mut source_map); parser.parse().unwrap() }