Finish porting over parsing rules

This commit is contained in:
Greg Shuflin 2021-11-12 00:32:11 -08:00
parent 5af42d0828
commit c5f7616303
2 changed files with 7 additions and 33 deletions

View File

@ -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 =

View File

@ -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<String> {
@ -17,18 +17,6 @@ fn bx<T>(item: T) -> Box<T> {
Box::new(item)
}
fn make_parser(input: &str) -> Parser {
let tokens: Vec<crate::tokenizing::Token> = tokenize(input);
let mut parser = super::Parser::new();
parser.add_new_tokens(tokens);
parser
}
fn parse(input: &str) -> ParseResult<AST> {
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",