From c1ef0ee50686f58414cdd1ae8c1b92ad2fd5419a Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Sat, 20 Nov 2021 02:33:09 -0800 Subject: [PATCH] More parser refactoring --- schala-lang/src/parsing/combinator.rs | 8 +++--- schala-lang/src/parsing/mod.rs | 39 ++++++++++++++++++--------- schala-lang/src/parsing/test.rs | 2 +- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/schala-lang/src/parsing/combinator.rs b/schala-lang/src/parsing/combinator.rs index 740cdaf..e959d19 100644 --- a/schala-lang/src/parsing/combinator.rs +++ b/schala-lang/src/parsing/combinator.rs @@ -17,11 +17,13 @@ use nom::{ }; use nom_locate::{position, LocatedSpan}; -use crate::identifier::{Id, IdStore}; -use crate::parsing::StoreRef; +use crate::{ + identifier::{Id, IdStore}, + parsing::StoreRef, +}; pub type Span<'a> = LocatedSpan<&'a str, StoreRef>; -type ParseResult<'a, O> = IResult, O, VerboseError>>; +pub type ParseResult<'a, O> = IResult, O, VerboseError>>; use crate::ast::*; diff --git a/schala-lang/src/parsing/mod.rs b/schala-lang/src/parsing/mod.rs index 93baa3f..80c016f 100644 --- a/schala-lang/src/parsing/mod.rs +++ b/schala-lang/src/parsing/mod.rs @@ -32,16 +32,7 @@ impl Parser { pub(crate) fn parse_comb(&mut self, input: &str) -> Result { let span = Span::new_extra(input, self.id_store.clone()); - let (rest, output) = combinator::program(span).map_err(|err| convert_err(input, err))?; - - if rest.fragment() != &"" { - return Err(ParseError { - location: Default::default(), - msg: format!("Bad parse state, remaining text: `{}`", rest.fragment()), - }); - } - - Ok(output) + convert(input, combinator::program(span)) } #[cfg(test)] @@ -52,7 +43,7 @@ impl Parser { #[cfg(test)] fn expression_comb(&mut self, input: &str) -> Result { let span = Span::new_extra(input, self.id_store.clone()); - combinator::expression(span).map_err(|err| convert_err(input, err)).map(|(rest, output)| output) + convert(input, combinator::expression(span)) } #[cfg(test)] @@ -63,7 +54,7 @@ impl Parser { #[cfg(test)] fn block_comb(&mut self, input: &str) -> Result { let span = Span::new_extra(input, self.id_store.clone()); - combinator::block(span).map_err(|err| convert_err(input, err)).map(|(_, output)| output) + convert(input, combinator::block(span)) } fn fresh(&mut self) -> Id { @@ -71,6 +62,30 @@ impl Parser { } } +fn convert<'a, O>(input: &'a str, result: combinator::ParseResult<'a, O>) -> Result { + use nom::{error::VerboseError, Err, Finish}; + + match result.finish() { + Ok((rest, output)) => { + if rest.fragment() != &"" { + return Err(ParseError { + location: Default::default(), + msg: format!("Bad parse state, remaining text: `{}`", rest.fragment()), + }); + } + + Ok(output) + } + Err(err) => { + let err = VerboseError { + errors: err.errors.into_iter().map(|(sp, kind)| (*sp.fragment(), kind)).collect(), + }; + let msg = nom::error::convert_error(input, err); + Err(ParseError { msg, location: (0).into() }) + } + } +} + fn convert_err<'a>( input: &'a str, err: nom::Err>>, diff --git a/schala-lang/src/parsing/test.rs b/schala-lang/src/parsing/test.rs index db5e890..0a4ae8d 100644 --- a/schala-lang/src/parsing/test.rs +++ b/schala-lang/src/parsing/test.rs @@ -192,7 +192,7 @@ fn basic_literals() { assert_expr!("0b0_1_0", expr(NatLiteral(2))); assert_expr!("0xff", expr(NatLiteral(255))); assert_expr!("0x032f", expr(NatLiteral(815))); - assert_expr!("0xf_f_", expr(NatLiteral(255))); + assert_expr!("0xf_f", expr(NatLiteral(255))); assert_expr!("false", expr(BoolLiteral(false))); assert_expr!("true", expr(BoolLiteral(true))); }