From 96393604c3b263c246d4101f40e56dea65777db8 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Sun, 14 Nov 2021 03:55:35 -0800 Subject: [PATCH] Move parser crate items around --- schala-lang/src/parsing/mod.rs | 30 +++++++++++++++++++++++++++++- schala-lang/src/parsing/new.rs | 30 ++---------------------------- schala-lang/src/parsing/test.rs | 2 +- schala-lang/src/schala.rs | 4 ++-- schala-lang/src/util.rs | 2 +- 5 files changed, 35 insertions(+), 33 deletions(-) diff --git a/schala-lang/src/parsing/mod.rs b/schala-lang/src/parsing/mod.rs index cbcb438..b380d78 100644 --- a/schala-lang/src/parsing/mod.rs +++ b/schala-lang/src/parsing/mod.rs @@ -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, +} + +impl Parser { + pub(crate) fn new() -> Self { + Self { id_store: IdStore::new() } + } + + pub(crate) fn parse(&mut self, input: &str) -> Result { + use peg::str::LineCol; + + new::schala_parser::program(input, self).map_err(|err: peg::error::ParseError| { + let msg = err.to_string(); + ParseError { msg, location: err.location.offset.into() } + }) + } + + fn fresh(&mut self) -> Id { + self.id_store.fresh() + } +} + /// Represents a parsing error #[derive(Debug)] pub struct ParseError { diff --git a/schala-lang/src/parsing/new.rs b/schala-lang/src/parsing/new.rs index 40771cb..9b4adc9 100644 --- a/schala-lang/src/parsing/new.rs +++ b/schala-lang/src/parsing/new.rs @@ -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 { Rc::new(s.to_string()) } -pub struct Parser { - id_store: IdStore, -} - -impl Parser { - pub(crate) fn new() -> Self { - Self { id_store: IdStore::new() } - } - - pub(crate) fn parse(&mut self, input: &str) -> Result { - use peg::str::LineCol; - - schala_parser::program(input, self).map_err(|err: peg::error::ParseError| { - let msg = err.to_string(); - ParseError { msg, location: err.location.offset.into() } - }) - } - - fn fresh(&mut self) -> Id { - self.id_store.fresh() - } -} - enum ExtendedPart<'a> { Index(Vec), Accessor(&'a str), diff --git a/schala-lang/src/parsing/test.rs b/schala-lang/src/parsing/test.rs index 50add16..41de80a 100644 --- a/schala-lang/src/parsing/test.rs +++ b/schala-lang/src/parsing/test.rs @@ -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 { diff --git a/schala-lang/src/schala.rs b/schala-lang/src/schala.rs index 03cb9fb..9207721 100644 --- a/schala-lang/src/schala.rs +++ b/schala-lang/src/schala.rs @@ -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(), } diff --git a/schala-lang/src/util.rs b/schala-lang/src/util.rs index c8ca959..a699cf4 100644 --- a/schala-lang/src/util.rs +++ b/schala-lang/src/util.rs @@ -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() }