Move parser crate items around

This commit is contained in:
Greg Shuflin 2021-11-14 03:55:35 -08:00
parent 29207876ae
commit 96393604c3
5 changed files with 35 additions and 33 deletions

View File

@ -1,10 +1,38 @@
#![allow(clippy::upper_case_acronyms)]
pub mod new;
mod new;
mod test;
use std::fmt;
use crate::{
ast::{ASTItem, AST},
identifier::{Id, IdStore},
};
pub struct Parser {
id_store: IdStore<ASTItem>,
}
impl Parser {
pub(crate) fn new() -> Self {
Self { id_store: IdStore::new() }
}
pub(crate) fn parse(&mut self, input: &str) -> Result<AST, ParseError> {
use peg::str::LineCol;
new::schala_parser::program(input, self).map_err(|err: peg::error::ParseError<LineCol>| {
let msg = err.to_string();
ParseError { msg, location: err.location.offset.into() }
})
}
fn fresh(&mut self) -> Id<ASTItem> {
self.id_store.fresh()
}
}
/// Represents a parsing error
#[derive(Debug)]
pub struct ParseError {

View File

@ -1,40 +1,14 @@
use std::rc::Rc;
use super::Parser;
//TODO make use of the format_parse_error function
//use crate::error::{SchalaError, format_parse_error};
use crate::{
ast::*,
identifier::{Id, IdStore},
parsing::ParseError,
};
use crate::ast::*;
fn rc_string(s: &str) -> Rc<String> {
Rc::new(s.to_string())
}
pub struct Parser {
id_store: IdStore<ASTItem>,
}
impl Parser {
pub(crate) fn new() -> Self {
Self { id_store: IdStore::new() }
}
pub(crate) fn parse(&mut self, input: &str) -> Result<AST, ParseError> {
use peg::str::LineCol;
schala_parser::program(input, self).map_err(|err: peg::error::ParseError<LineCol>| {
let msg = err.to_string();
ParseError { msg, location: err.location.offset.into() }
})
}
fn fresh(&mut self) -> Id<ASTItem> {
self.id_store.fresh()
}
}
enum ExtendedPart<'a> {
Index(Vec<Expression>),
Accessor(&'a str),

View File

@ -6,7 +6,7 @@ use std::{fmt::Write, rc::Rc};
use pretty_assertions::assert_eq;
use super::new::{schala_parser, Parser};
use super::{new::schala_parser, Parser};
use crate::{ast::*, parsing::Location};
fn rc(s: &str) -> Rc<String> {

View File

@ -17,7 +17,7 @@ pub struct Schala<'a> {
/// Contains information for type-checking
type_context: type_inference::TypeContext,
/// Schala Parser
active_parser: parsing::new::Parser,
active_parser: parsing::Parser,
/// Execution state for AST-walking interpreter
eval_state: tree_walk_eval::State<'a>,
@ -45,7 +45,7 @@ impl<'a> Schala<'a> {
source_reference: SourceReference::new(),
symbol_table: symbol_table::SymbolTable::new(),
type_context: type_inference::TypeContext::new(),
active_parser: parsing::new::Parser::new(),
active_parser: parsing::Parser::new(),
eval_state: tree_walk_eval::State::new(),
timings: Vec::new(),
}

View File

@ -52,7 +52,7 @@ where T: Hash + Eq
/// Quickly create an AST from a string, with no error checking. For test use only
#[cfg(test)]
pub fn quick_ast(input: &str) -> crate::ast::AST {
let mut parser = crate::parsing::new::Parser::new();
let mut parser = crate::parsing::Parser::new();
let output = parser.parse(input);
output.unwrap()
}