Have Parser accept SourceMap reference

This commit is contained in:
greg 2019-10-23 09:53:18 -07:00
parent 1ffe61cf5f
commit 5bb1a245c4
4 changed files with 20 additions and 12 deletions

View File

@ -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<ParseRecord>,
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<Token>) -> Parser {
pub fn new(initial_input: Vec<Token>, 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]

View File

@ -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<AST> {
let mut source_map = crate::source_map::SourceMap::new();
let tokens: Vec<crate::tokenizing::Token> = 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<crate::tokenizing::Token> = 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<crate::tokenizing::Token> = tokenize($statement_text);
let mut parser = super::Parser::new(tokens);
let mut parser = super::Parser::new(tokens, &mut source_map);
parser.statement().unwrap()
}
}

View File

@ -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<RefCell<symbol_table::SymbolTable>>;
pub type SourceMapHandle = Rc<RefCell<source_map::SourceMap>>;
@ -21,11 +22,12 @@ pub type SourceMapHandle = Rc<RefCell<source_map::SourceMap>>;
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<parsing::Parser>,
active_parser: Option<parsing::Parser<'static>>,
}
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<tokenizing::Token>, 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| {

View File

@ -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()
}