Compare commits
2 Commits
96393604c3
...
f4029fe31a
Author | SHA1 | Date | |
---|---|---|---|
|
f4029fe31a | ||
|
d38bb2278c |
@ -1,10 +1,12 @@
|
|||||||
#![allow(clippy::upper_case_acronyms)]
|
#![allow(clippy::upper_case_acronyms)]
|
||||||
|
|
||||||
mod new;
|
mod peg_parser;
|
||||||
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},
|
||||||
@ -20,12 +22,17 @@ impl Parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn parse(&mut self, input: &str) -> Result<AST, ParseError> {
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
new::schala_parser::program(input, self).map_err(|err: peg::error::ParseError<LineCol>| {
|
#[cfg(test)]
|
||||||
let msg = err.to_string();
|
fn expression(&mut self, input: &str) -> Result<Expression, ParseError> {
|
||||||
ParseError { msg, location: err.location.offset.into() }
|
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> {
|
fn fresh(&mut self) -> Id<ASTItem> {
|
||||||
@ -40,6 +47,13 @@ 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,
|
||||||
|
@ -6,7 +6,7 @@ use std::{fmt::Write, rc::Rc};
|
|||||||
|
|
||||||
use pretty_assertions::assert_eq;
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
use super::{new::schala_parser, Parser};
|
use super::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 = schala_parser::program($input, &mut parser);
|
let ast = parser.parse($input);
|
||||||
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());
|
println!("Parse error: {}", ast.unwrap_err().msg);
|
||||||
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 = schala_parser::program($input, &mut parser).unwrap_err();
|
let err = parser.parse($input).unwrap_err();
|
||||||
assert_eq!(err.to_string(), $failure);
|
assert_eq!(err.msg, $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 = schala_parser::expression($input, &mut parser);
|
let expr = parser.expression($input);
|
||||||
if expr.is_err() {
|
if expr.is_err() {
|
||||||
println!("Expression parse error: {}", expr.unwrap_err());
|
println!("Expression parse error: {}", expr.unwrap_err().msg);
|
||||||
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 = schala_parser::expression($input, &mut parser).unwrap_err();
|
let _err = parser.expression($input).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 = schala_parser::block(case, &mut parser);
|
let block = parser.block(case);
|
||||||
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 = schala_parser::block(source, &mut parser);
|
let block = parser.block(source);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
block.unwrap(),
|
block.unwrap(),
|
||||||
vec![decl(Declaration::FuncDecl(
|
vec![decl(Declaration::FuncDecl(
|
||||||
|
Loading…
Reference in New Issue
Block a user