More work

This commit is contained in:
Greg Shuflin 2021-11-03 20:31:46 -07:00
parent 88b39b5561
commit 6cbe562241
2 changed files with 18 additions and 4 deletions

View File

@ -1,7 +1,6 @@
use crate::ast::*; use crate::ast::*;
use std::rc::Rc; use std::rc::Rc;
peg::parser! { peg::parser! {
pub grammar schala_parser() for str { pub grammar schala_parser() for str {
@ -22,7 +21,14 @@ peg::parser! {
primary() primary()
rule primary() -> ExpressionKind = rule primary() -> ExpressionKind =
float_literal() / nat_literal() / bool_literal() / string_literal() / paren_expr() float_literal() / nat_literal() / bool_literal() / string_literal() / paren_expr() /
list_expr()
rule list_expr() -> ExpressionKind =
"[" exprs:(expression() ** ",") "]" {
let mut exprs = exprs;
ExpressionKind::ListLiteral(exprs)
}
rule paren_expr() -> ExpressionKind = rule paren_expr() -> ExpressionKind =
"(" exprs:(expression() ** ",") ")" { "(" exprs:(expression() ** ",") ")" {

View File

@ -127,6 +127,13 @@ macro_rules! assert_expr2 {
assert_eq!(schala_parser::expression($input).unwrap(), $correct); assert_eq!(schala_parser::expression($input).unwrap(), $correct);
}; };
} }
macro_rules! assert_fail_expr2 {
($input:expr, $failure:expr) => {
let _err = schala_parser::expression($input).unwrap_err();
//TODO make real tests for failures
//assert_eq!(err.to_string(), $failure);
}
}
macro_rules! assert_fail_expr { macro_rules! assert_fail_expr {
($input:expr, $failure:expr) => { ($input:expr, $failure:expr) => {
@ -155,8 +162,9 @@ fn basic_literals() {
fn list_literals() { fn list_literals() {
use ExpressionKind::*; use ExpressionKind::*;
assert_expr!("[]", expr(ListLiteral(vec![]))); assert_expr2!("[]", expr(ListLiteral(vec![])));
assert_expr!("[1,2]", expr(ListLiteral(vec![expr(NatLiteral(1)), expr(NatLiteral(2)),]))); assert_expr2!("[1,2]", expr(ListLiteral(vec![expr(NatLiteral(1)), expr(NatLiteral(2)),])));
assert_fail_expr2!("[1,,2]", "some failure");
} }
#[test] #[test]