From fc088923c054a764e2d89c3d9ad0bb3eda281de6 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Thu, 18 Nov 2021 01:37:05 -0800 Subject: [PATCH] More test work --- schala-lang/src/parsing/combinator.rs | 59 +-------------------------- schala-lang/src/parsing/mod.rs | 49 ++++++++++++++-------- schala-lang/src/parsing/test.rs | 43 +++++++++++++++++++ 3 files changed, 75 insertions(+), 76 deletions(-) diff --git a/schala-lang/src/parsing/combinator.rs b/schala-lang/src/parsing/combinator.rs index 1cce387..253d0c9 100644 --- a/schala-lang/src/parsing/combinator.rs +++ b/schala-lang/src/parsing/combinator.rs @@ -78,7 +78,7 @@ fn statement_delimiter(input: Span) -> ParseResult<()> { tok(alt((value((), line_ending), value((), char(';')))))(input) } -fn block(input: Span) -> ParseResult { +pub fn block(input: Span) -> ParseResult { context( "block", map( @@ -376,61 +376,4 @@ mod test { ) ); } - - #[test] - fn combinator_test3() { - let source = "{}"; - assert_eq!(span!(block, source).unwrap().1, vec![].into()); - let source = r#"{ - - //hella - 4_5 //bog - 11; /*chutney*/0xf - }"#; - let parsed = span!(block, source).map_err(|err| match err { - Err::Error(err) | Err::Failure(err) => { - let err = VerboseError { - errors: err.errors.into_iter().map(|(sp, kind)| (*sp.fragment(), kind)).collect(), - }; - nom::error::convert_error(source, err) - } - _ => panic!(), - }); - - if let Err(err) = parsed { - println!("{}", err); - panic!("parse error desu!"); - } - - assert_eq!( - parsed.unwrap().1, - vec![ - Statement { - id: Default::default(), - location: Default::default(), - kind: StatementKind::Expression(Expression::new( - Default::default(), - ExpressionKind::NatLiteral(45) - )) - }, - Statement { - id: Default::default(), - location: Default::default(), - kind: StatementKind::Expression(Expression::new( - Default::default(), - ExpressionKind::NatLiteral(11) - )) - }, - Statement { - id: Default::default(), - location: Default::default(), - kind: StatementKind::Expression(Expression::new( - Default::default(), - ExpressionKind::NatLiteral(15) - )) - }, - ] - .into() - ); - } } diff --git a/schala-lang/src/parsing/mod.rs b/schala-lang/src/parsing/mod.rs index 3e952a9..8cc2365 100644 --- a/schala-lang/src/parsing/mod.rs +++ b/schala-lang/src/parsing/mod.rs @@ -4,7 +4,9 @@ pub mod combinator; mod peg_parser; mod test; -use std::fmt; +use std::{cell::RefCell, fmt, rc::Rc}; + +use combinator::Span; #[cfg(test)] use crate::ast::{Block, Expression}; @@ -33,25 +35,10 @@ impl Parser { #[cfg(test)] fn expression_comb(&mut self, input: &str) -> Result { - use std::{cell::RefCell, rc::Rc}; - - use combinator::Span; - use nom::{error::VerboseError, Err}; - let id_store: IdStore = IdStore::new(); let span = Span::new_extra(input, Rc::new(RefCell::new(id_store))); - combinator::expression(span) - .map_err(|err| match err { - Err::Error(err) | Err::Failure(err) => { - let err = VerboseError { - errors: err.errors.into_iter().map(|(sp, kind)| (*sp.fragment(), kind)).collect(), - }; - let msg = nom::error::convert_error(input, err); - ParseError { msg, location: (0).into() } - } - _ => panic!(), - }) - .map(|(_, output)| output) + + combinator::expression(span).map_err(|err| convert_err(input, err)).map(|(_, output)| output) } #[cfg(test)] @@ -59,11 +46,37 @@ impl Parser { peg_parser::schala_parser::block(input, self).map_err(ParseError::from_peg) } + #[cfg(test)] + fn block_comb(&mut self, input: &str) -> Result { + let id_store: IdStore = IdStore::new(); + let span = Span::new_extra(input, Rc::new(RefCell::new(id_store))); + + combinator::block(span).map_err(|err| convert_err(input, err)).map(|(_, output)| output) + } + fn fresh(&mut self) -> Id { self.id_store.fresh() } } +fn convert_err<'a>( + input: &'a str, + err: nom::Err>>, +) -> ParseError { + use nom::{error::VerboseError, Err}; + + match err { + Err::Error(err) | Err::Failure(err) => { + let err = VerboseError { + errors: err.errors.into_iter().map(|(sp, kind)| (*sp.fragment(), kind)).collect(), + }; + let msg = nom::error::convert_error(input, err); + ParseError { msg, location: (0).into() } + } + _ => panic!(), + } +} + /// Represents a parsing error #[derive(Debug)] pub struct ParseError { diff --git a/schala-lang/src/parsing/test.rs b/schala-lang/src/parsing/test.rs index 146ee41..5d541ee 100644 --- a/schala-lang/src/parsing/test.rs +++ b/schala-lang/src/parsing/test.rs @@ -1336,6 +1336,49 @@ fn blocks() { ))] .into() ); + + let block = parser.block_comb("{}"); + assert_eq!(block.unwrap(), vec![].into()); + + let source = r#"{ + + //hella + 4_5 //bog + 11; /*chutney*/0xf + }"#; + + let block = parser.block_comb(source); + + assert_eq!( + block.unwrap(), + vec![ + Statement { + id: Default::default(), + location: Default::default(), + kind: StatementKind::Expression(Expression::new( + Default::default(), + ExpressionKind::NatLiteral(45) + )) + }, + Statement { + id: Default::default(), + location: Default::default(), + kind: StatementKind::Expression(Expression::new( + Default::default(), + ExpressionKind::NatLiteral(11) + )) + }, + Statement { + id: Default::default(), + location: Default::default(), + kind: StatementKind::Expression(Expression::new( + Default::default(), + ExpressionKind::NatLiteral(15) + )) + }, + ] + .into() + ); } #[test]