From c5f76163032826347306b6a5ee5ca43fffd234cd Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Fri, 12 Nov 2021 00:32:11 -0800 Subject: [PATCH] Finish porting over parsing rules --- schala-lang/language/src/parsing/new.rs | 5 +++- schala-lang/language/src/parsing/test.rs | 35 ++---------------------- 2 files changed, 7 insertions(+), 33 deletions(-) diff --git a/schala-lang/language/src/parsing/new.rs b/schala-lang/language/src/parsing/new.rs index 483494f..ad87cb3 100644 --- a/schala-lang/language/src/parsing/new.rs +++ b/schala-lang/language/src/parsing/new.rs @@ -340,7 +340,10 @@ peg::parser! { //TODO improve the definition of identifiers rule identifier() -> &'input str = - $(['a'..='z' | 'A'..='Z' | '_'] ['a'..='z' | 'A'..='Z' | '0'..='9' | '_']*) + !reserved() text:$(['a'..='z' | 'A'..='Z' | '_'] ['a'..='z' | 'A'..='Z' | '0'..='9' | '_']*) { text } + + //TODO flesh out more + rule reserved() = "module" / "if" / "then" rule if_expr() -> ExpressionKind = diff --git a/schala-lang/language/src/parsing/test.rs b/schala-lang/language/src/parsing/test.rs index 836e45e..e22b458 100644 --- a/schala-lang/language/src/parsing/test.rs +++ b/schala-lang/language/src/parsing/test.rs @@ -6,7 +6,7 @@ use std::{fmt::Write, rc::Rc}; use pretty_assertions::assert_eq; -use super::{new::schala_parser, tokenize, ParseResult, Parser}; +use super::new::schala_parser; use crate::{ast::*, tokenizing::Location}; fn rc(s: &str) -> Rc { @@ -17,18 +17,6 @@ fn bx(item: T) -> Box { Box::new(item) } -fn make_parser(input: &str) -> Parser { - let tokens: Vec = tokenize(input); - let mut parser = super::Parser::new(); - parser.add_new_tokens(tokens); - parser -} - -fn parse(input: &str) -> ParseResult { - let mut parser = make_parser(input); - parser.parse() -} - fn stmt(kind: StatementKind) -> Statement { Statement { location: Location::default(), id: ItemId::default(), kind } } @@ -97,16 +85,6 @@ fn ty_simple(name: &str) -> TypeIdentifier { TypeIdentifier::Singleton(TypeSingletonName { name: rc(name), params: vec![] }) } -macro_rules! assert_ast { - ($input:expr, $statements:expr) => { - let ast = parse($input).unwrap(); - let expected = AST { id: Default::default(), statements: $statements.into() }; - println!("Expected: {}", expected); - println!("Actual: {}", ast); - assert_eq!(ast, expected); - }; -} - macro_rules! assert_ast2 { ($input:expr, $statements:expr) => { let ast = schala_parser::program($input); @@ -119,13 +97,6 @@ macro_rules! assert_ast2 { }; } -macro_rules! assert_fail { - ($input:expr, $failure:expr) => { - let err = parse($input).unwrap_err(); - assert_eq!(err.msg, $failure); - }; -} - macro_rules! assert_fail2 { ($input:expr, $failure:expr) => { let err = schala_parser::program($input).unwrap_err(); @@ -532,7 +503,7 @@ fn complex_lambdas() { #[test] fn reserved_words() { - assert_fail!("module::item::call()", "Expected an identifier, got Colon"); + assert_fail2!("module::item::call()", "error at 1:7: expected ['a' ..= 'z' | 'A' ..= 'Z' | '_']"); } #[test] @@ -796,7 +767,7 @@ fn functions() { ] }) ); - assert_fail!("a(b,,c)", "Expected a literal expression, got Comma"); + assert_fail2!("a(b,,c)","error at 1:5: expected one of \"(\", \".\", \"0b\", \"0x\", \"[\", \"\\\"\", \"_\", \"false\", \"for\", \"if\", \"true\", \"while\", ['+' | '-' | '!'], ['0' ..= '9'], ['a' ..= 'z' | 'A' ..= 'Z' | '_'], r#\"\\\"#"); assert_ast2!( "fn a(b, c: Int): Int",