From f4029fe31a0d2415ea8caa7a2c9cffe6049387eb Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Sun, 14 Nov 2021 04:35:04 -0800 Subject: [PATCH] Make tests not directly dependent on peg --- schala-lang/src/parsing/mod.rs | 24 +++++++++++++++++++----- schala-lang/src/parsing/test.rs | 20 ++++++++++---------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/schala-lang/src/parsing/mod.rs b/schala-lang/src/parsing/mod.rs index 8be965e..6be3d9e 100644 --- a/schala-lang/src/parsing/mod.rs +++ b/schala-lang/src/parsing/mod.rs @@ -5,6 +5,8 @@ mod test; use std::fmt; +#[cfg(test)] +use crate::ast::{Block, Expression}; use crate::{ ast::{ASTItem, AST}, identifier::{Id, IdStore}, @@ -20,12 +22,17 @@ impl Parser { } pub(crate) fn parse(&mut self, input: &str) -> Result { - use peg::str::LineCol; + peg_parser::schala_parser::program(input, self).map_err(ParseError::from_peg) + } - peg_parser::schala_parser::program(input, self).map_err(|err: peg::error::ParseError| { - let msg = err.to_string(); - ParseError { msg, location: err.location.offset.into() } - }) + #[cfg(test)] + fn expression(&mut self, input: &str) -> Result { + peg_parser::schala_parser::expression(input, self).map_err(ParseError::from_peg) + } + + #[cfg(test)] + fn block(&mut self, input: &str) -> Result { + peg_parser::schala_parser::block(input, self).map_err(ParseError::from_peg) } fn fresh(&mut self) -> Id { @@ -40,6 +47,13 @@ pub struct ParseError { pub location: Location, } +impl ParseError { + fn from_peg(err: peg::error::ParseError) -> Self { + let msg = err.to_string(); + Self { msg, location: err.location.offset.into() } + } +} + #[derive(Debug, Clone, Copy, PartialEq, Default)] pub struct Location { pub(crate) offset: usize, diff --git a/schala-lang/src/parsing/test.rs b/schala-lang/src/parsing/test.rs index 4fb6775..4169e1e 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::{peg_parser::schala_parser, Parser}; +use super::Parser; use crate::{ast::*, parsing::Location}; fn rc(s: &str) -> Rc { @@ -88,10 +88,10 @@ fn ty_simple(name: &str) -> TypeIdentifier { macro_rules! assert_ast { ($input:expr, $statements:expr) => { let mut parser = Parser::new(); - let ast = schala_parser::program($input, &mut parser); + let ast = parser.parse($input); let expected = AST { id: Default::default(), statements: $statements.into() }; if ast.is_err() { - println!("Parse error: {}", ast.unwrap_err()); + println!("Parse error: {}", ast.unwrap_err().msg); panic!(); } assert_eq!(ast.unwrap(), expected); @@ -101,17 +101,17 @@ macro_rules! assert_ast { macro_rules! assert_fail { ($input:expr, $failure:expr) => { let mut parser = Parser::new(); - let err = schala_parser::program($input, &mut parser).unwrap_err(); - assert_eq!(err.to_string(), $failure); + let err = parser.parse($input).unwrap_err(); + assert_eq!(err.msg, $failure); }; } macro_rules! assert_expr { ($input:expr, $correct:expr) => { let mut parser = Parser::new(); - let expr = schala_parser::expression($input, &mut parser); + let expr = parser.expression($input); if expr.is_err() { - println!("Expression parse error: {}", expr.unwrap_err()); + println!("Expression parse error: {}", expr.unwrap_err().msg); panic!(); } assert_eq!(expr.unwrap(), $correct); @@ -121,7 +121,7 @@ macro_rules! assert_expr { macro_rules! assert_fail_expr { ($input:expr, $failure:expr) => { let mut parser = Parser::new(); - let _err = schala_parser::expression($input, &mut parser).unwrap_err(); + let _err = parser.expression($input).unwrap_err(); //TODO make real tests for failures //assert_eq!(err.to_string(), $failure); }; @@ -1302,7 +1302,7 @@ fn blocks() { let mut parser = Parser::new(); for case in cases.iter() { - let block = schala_parser::block(case, &mut parser); + let block = parser.block(case); assert_eq!(block.unwrap(), vec![exst(Value(qn!(a)))].into()); } @@ -1311,7 +1311,7 @@ fn blocks() { fn foo() { } } }"#; - let block = schala_parser::block(source, &mut parser); + let block = parser.block(source); assert_eq!( block.unwrap(), vec![decl(Declaration::FuncDecl(