Make tests not directly dependent on peg
This commit is contained in:
parent
d38bb2278c
commit
f4029fe31a
@ -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<AST, ParseError> {
|
||||
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<LineCol>| {
|
||||
let msg = err.to_string();
|
||||
ParseError { msg, location: err.location.offset.into() }
|
||||
})
|
||||
#[cfg(test)]
|
||||
fn expression(&mut self, input: &str) -> Result<Expression, ParseError> {
|
||||
peg_parser::schala_parser::expression(input, self).map_err(ParseError::from_peg)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn block(&mut self, input: &str) -> Result<Block, ParseError> {
|
||||
peg_parser::schala_parser::block(input, self).map_err(ParseError::from_peg)
|
||||
}
|
||||
|
||||
fn fresh(&mut self) -> Id<ASTItem> {
|
||||
@ -40,6 +47,13 @@ pub struct ParseError {
|
||||
pub location: Location,
|
||||
}
|
||||
|
||||
impl ParseError {
|
||||
fn from_peg(err: peg::error::ParseError<peg::str::LineCol>) -> 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,
|
||||
|
@ -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<String> {
|
||||
@ -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(
|
||||
|
Loading…
Reference in New Issue
Block a user