Compare commits

..

No commits in common. "f4029fe31a0d2415ea8caa7a2c9cffe6049387eb" and "96393604c3b263c246d4101f40e56dea65777db8" have entirely different histories.

3 changed files with 16 additions and 30 deletions

View File

@ -1,12 +1,10 @@
#![allow(clippy::upper_case_acronyms)] #![allow(clippy::upper_case_acronyms)]
mod peg_parser; mod new;
mod test; mod test;
use std::fmt; use std::fmt;
#[cfg(test)]
use crate::ast::{Block, Expression};
use crate::{ use crate::{
ast::{ASTItem, AST}, ast::{ASTItem, AST},
identifier::{Id, IdStore}, identifier::{Id, IdStore},
@ -22,17 +20,12 @@ impl Parser {
} }
pub(crate) fn parse(&mut self, input: &str) -> Result<AST, ParseError> { pub(crate) fn parse(&mut self, input: &str) -> Result<AST, ParseError> {
peg_parser::schala_parser::program(input, self).map_err(ParseError::from_peg) use peg::str::LineCol;
}
#[cfg(test)] new::schala_parser::program(input, self).map_err(|err: peg::error::ParseError<LineCol>| {
fn expression(&mut self, input: &str) -> Result<Expression, ParseError> { let msg = err.to_string();
peg_parser::schala_parser::expression(input, self).map_err(ParseError::from_peg) ParseError { msg, location: err.location.offset.into() }
} })
#[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> { fn fresh(&mut self) -> Id<ASTItem> {
@ -47,13 +40,6 @@ pub struct ParseError {
pub location: Location, 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)] #[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Location { pub struct Location {
pub(crate) offset: usize, pub(crate) offset: usize,

View File

@ -6,7 +6,7 @@ use std::{fmt::Write, rc::Rc};
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
use super::Parser; use super::{new::schala_parser, Parser};
use crate::{ast::*, parsing::Location}; use crate::{ast::*, parsing::Location};
fn rc(s: &str) -> Rc<String> { fn rc(s: &str) -> Rc<String> {
@ -88,10 +88,10 @@ fn ty_simple(name: &str) -> TypeIdentifier {
macro_rules! assert_ast { macro_rules! assert_ast {
($input:expr, $statements:expr) => { ($input:expr, $statements:expr) => {
let mut parser = Parser::new(); let mut parser = Parser::new();
let ast = parser.parse($input); let ast = schala_parser::program($input, &mut parser);
let expected = AST { id: Default::default(), statements: $statements.into() }; let expected = AST { id: Default::default(), statements: $statements.into() };
if ast.is_err() { if ast.is_err() {
println!("Parse error: {}", ast.unwrap_err().msg); println!("Parse error: {}", ast.unwrap_err());
panic!(); panic!();
} }
assert_eq!(ast.unwrap(), expected); assert_eq!(ast.unwrap(), expected);
@ -101,17 +101,17 @@ macro_rules! assert_ast {
macro_rules! assert_fail { macro_rules! assert_fail {
($input:expr, $failure:expr) => { ($input:expr, $failure:expr) => {
let mut parser = Parser::new(); let mut parser = Parser::new();
let err = parser.parse($input).unwrap_err(); let err = schala_parser::program($input, &mut parser).unwrap_err();
assert_eq!(err.msg, $failure); assert_eq!(err.to_string(), $failure);
}; };
} }
macro_rules! assert_expr { macro_rules! assert_expr {
($input:expr, $correct:expr) => { ($input:expr, $correct:expr) => {
let mut parser = Parser::new(); let mut parser = Parser::new();
let expr = parser.expression($input); let expr = schala_parser::expression($input, &mut parser);
if expr.is_err() { if expr.is_err() {
println!("Expression parse error: {}", expr.unwrap_err().msg); println!("Expression parse error: {}", expr.unwrap_err());
panic!(); panic!();
} }
assert_eq!(expr.unwrap(), $correct); assert_eq!(expr.unwrap(), $correct);
@ -121,7 +121,7 @@ macro_rules! assert_expr {
macro_rules! assert_fail_expr { macro_rules! assert_fail_expr {
($input:expr, $failure:expr) => { ($input:expr, $failure:expr) => {
let mut parser = Parser::new(); let mut parser = Parser::new();
let _err = parser.expression($input).unwrap_err(); let _err = schala_parser::expression($input, &mut parser).unwrap_err();
//TODO make real tests for failures //TODO make real tests for failures
//assert_eq!(err.to_string(), $failure); //assert_eq!(err.to_string(), $failure);
}; };
@ -1302,7 +1302,7 @@ fn blocks() {
let mut parser = Parser::new(); let mut parser = Parser::new();
for case in cases.iter() { for case in cases.iter() {
let block = parser.block(case); let block = schala_parser::block(case, &mut parser);
assert_eq!(block.unwrap(), vec![exst(Value(qn!(a)))].into()); assert_eq!(block.unwrap(), vec![exst(Value(qn!(a)))].into());
} }
@ -1311,7 +1311,7 @@ fn blocks() {
fn foo() { } fn foo() { }
} }
}"#; }"#;
let block = parser.block(source); let block = schala_parser::block(source, &mut parser);
assert_eq!( assert_eq!(
block.unwrap(), block.unwrap(),
vec![decl(Declaration::FuncDecl( vec![decl(Declaration::FuncDecl(