Get rid of "2" in parse test macros
This commit is contained in:
parent
c67adc3a38
commit
671ce54dd3
@ -85,7 +85,7 @@ fn ty_simple(name: &str) -> TypeIdentifier {
|
||||
TypeIdentifier::Singleton(TypeSingletonName { name: rc(name), params: vec![] })
|
||||
}
|
||||
|
||||
macro_rules! assert_ast2 {
|
||||
macro_rules! assert_ast {
|
||||
($input:expr, $statements:expr) => {
|
||||
let ast = schala_parser::program($input);
|
||||
let expected = AST { id: Default::default(), statements: $statements.into() };
|
||||
@ -97,14 +97,14 @@ macro_rules! assert_ast2 {
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! assert_fail2 {
|
||||
macro_rules! assert_fail {
|
||||
($input:expr, $failure:expr) => {
|
||||
let err = schala_parser::program($input).unwrap_err();
|
||||
assert_eq!(err.to_string(), $failure);
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! assert_expr2 {
|
||||
macro_rules! assert_expr {
|
||||
($input:expr, $correct:expr) => {
|
||||
let expr = schala_parser::expression($input);
|
||||
if expr.is_err() {
|
||||
@ -115,7 +115,7 @@ macro_rules! assert_expr2 {
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! assert_fail_expr2 {
|
||||
macro_rules! assert_fail_expr {
|
||||
($input:expr, $failure:expr) => {
|
||||
let _err = schala_parser::expression($input).unwrap_err();
|
||||
//TODO make real tests for failures
|
||||
@ -126,25 +126,25 @@ macro_rules! assert_fail_expr2 {
|
||||
fn basic_literals() {
|
||||
use ExpressionKind::*;
|
||||
|
||||
assert_expr2!(".2", expr(FloatLiteral(0.2)));
|
||||
assert_expr2!("8.1", expr(FloatLiteral(8.1)));
|
||||
assert_expr2!("0b010", expr(NatLiteral(2)));
|
||||
assert_expr2!("0b0_1_0", expr(NatLiteral(2)));
|
||||
assert_expr2!("0xff", expr(NatLiteral(255)));
|
||||
assert_expr2!("0x032f", expr(NatLiteral(815)));
|
||||
assert_expr2!("0xf_f_", expr(NatLiteral(255)));
|
||||
assert_expr2!("false", expr(BoolLiteral(false)));
|
||||
assert_expr2!("true", expr(BoolLiteral(true)));
|
||||
assert_expr2!(r#""hello""#, expr(StringLiteral(rc("hello"))));
|
||||
assert_expr!(".2", expr(FloatLiteral(0.2)));
|
||||
assert_expr!("8.1", expr(FloatLiteral(8.1)));
|
||||
assert_expr!("0b010", expr(NatLiteral(2)));
|
||||
assert_expr!("0b0_1_0", expr(NatLiteral(2)));
|
||||
assert_expr!("0xff", expr(NatLiteral(255)));
|
||||
assert_expr!("0x032f", expr(NatLiteral(815)));
|
||||
assert_expr!("0xf_f_", expr(NatLiteral(255)));
|
||||
assert_expr!("false", expr(BoolLiteral(false)));
|
||||
assert_expr!("true", expr(BoolLiteral(true)));
|
||||
assert_expr!(r#""hello""#, expr(StringLiteral(rc("hello"))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_literals() {
|
||||
use ExpressionKind::*;
|
||||
|
||||
assert_expr2!("[]", expr(ListLiteral(vec![])));
|
||||
assert_expr2!("[1,2]", expr(ListLiteral(vec![expr(NatLiteral(1)), expr(NatLiteral(2)),])));
|
||||
assert_fail_expr2!("[1,,2]", "some failure");
|
||||
assert_expr!("[]", expr(ListLiteral(vec![])));
|
||||
assert_expr!("[1,2]", expr(ListLiteral(vec![expr(NatLiteral(1)), expr(NatLiteral(2)),])));
|
||||
assert_fail_expr!("[1,,2]", "some failure");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -152,8 +152,8 @@ fn binexps() {
|
||||
use ExpressionKind::*;
|
||||
use StatementKind::Expression;
|
||||
|
||||
assert_expr2!("0xf_f_+1", binop("+", expr(NatLiteral(255)), expr(NatLiteral(1))));
|
||||
assert_ast2!(
|
||||
assert_expr!("0xf_f_+1", binop("+", expr(NatLiteral(255)), expr(NatLiteral(1))));
|
||||
assert_ast!(
|
||||
"3; 4; 4.3",
|
||||
vec![
|
||||
stmt(Expression(expr(NatLiteral(3)))),
|
||||
@ -162,16 +162,16 @@ fn binexps() {
|
||||
]
|
||||
);
|
||||
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
"1 + 2 * 3",
|
||||
binop("+", expr(NatLiteral(1)), binop("*", expr(NatLiteral(2)), expr(NatLiteral(3))))
|
||||
);
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
"1 * 2 + 3",
|
||||
binop("+", binop("*", expr(NatLiteral(1)), expr(NatLiteral(2))), expr(NatLiteral(3)))
|
||||
);
|
||||
assert_expr2!("1 && 2", binop("&&", expr(NatLiteral(1)), expr(NatLiteral(2))));
|
||||
assert_expr2!(
|
||||
assert_expr!("1 && 2", binop("&&", expr(NatLiteral(1)), expr(NatLiteral(2))));
|
||||
assert_expr!(
|
||||
"1 + 2 * 3 + 4",
|
||||
binop(
|
||||
"+",
|
||||
@ -179,48 +179,48 @@ fn binexps() {
|
||||
expr(NatLiteral(4))
|
||||
)
|
||||
);
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
"(1 + 2) * 3",
|
||||
binop("*", binop("+", expr(NatLiteral(1)), expr(NatLiteral(2))), expr(NatLiteral(3)))
|
||||
);
|
||||
assert_expr2!(".1 + .2", binop("+", expr(FloatLiteral(0.1)), expr(FloatLiteral(0.2))));
|
||||
assert_expr2!("1 / 2.", binop("/", expr(NatLiteral(1)), expr(FloatLiteral(2.))));
|
||||
assert_expr!(".1 + .2", binop("+", expr(FloatLiteral(0.1)), expr(FloatLiteral(0.2))));
|
||||
assert_expr!("1 / 2.", binop("/", expr(NatLiteral(1)), expr(FloatLiteral(2.))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prefix_exps() {
|
||||
use ExpressionKind::*;
|
||||
|
||||
assert_expr2!("-3", prefixop("-", expr(NatLiteral(3))));
|
||||
assert_expr2!("-0.2", prefixop("-", expr(FloatLiteral(0.2))));
|
||||
assert_expr2!("!3", prefixop("!", expr(NatLiteral(3))));
|
||||
assert_expr2!("!t", prefixop("!", expr(Value(qn!(t)))));
|
||||
assert_expr2!("a <- -b", binop("<-", expr(Value(qn!(a))), prefixop("-", expr(Value(qn!(b))))));
|
||||
assert_expr2!("a <--b", binop("<--", expr(Value(qn!(a))), expr(Value(qn!(b)))));
|
||||
assert_expr!("-3", prefixop("-", expr(NatLiteral(3))));
|
||||
assert_expr!("-0.2", prefixop("-", expr(FloatLiteral(0.2))));
|
||||
assert_expr!("!3", prefixop("!", expr(NatLiteral(3))));
|
||||
assert_expr!("!t", prefixop("!", expr(Value(qn!(t)))));
|
||||
assert_expr!("a <- -b", binop("<-", expr(Value(qn!(a))), prefixop("-", expr(Value(qn!(b))))));
|
||||
assert_expr!("a <--b", binop("<--", expr(Value(qn!(a))), expr(Value(qn!(b)))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn operators() {
|
||||
use ExpressionKind::*;
|
||||
|
||||
assert_expr2!("a <- 1", binop("<-", expr(Value(qn!(a))), expr(NatLiteral(1))));
|
||||
assert_expr2!("a || 1", binop("||", expr(Value(qn!(a))), expr(NatLiteral(1))));
|
||||
assert_expr2!("a <> 1", binop("<>", expr(Value(qn!(a))), expr(NatLiteral(1))));
|
||||
assert_expr!("a <- 1", binop("<-", expr(Value(qn!(a))), expr(NatLiteral(1))));
|
||||
assert_expr!("a || 1", binop("||", expr(Value(qn!(a))), expr(NatLiteral(1))));
|
||||
assert_expr!("a <> 1", binop("<>", expr(Value(qn!(a))), expr(NatLiteral(1))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accessors() {
|
||||
use ExpressionKind::*;
|
||||
|
||||
assert_expr2!("a.b", expr(Access { name: rc("b"), expr: bx(expr(Value(qn!(a)))) }));
|
||||
assert_expr2!(
|
||||
assert_expr!("a.b", expr(Access { name: rc("b"), expr: bx(expr(Value(qn!(a)))) }));
|
||||
assert_expr!(
|
||||
"a.b.c",
|
||||
expr(Access {
|
||||
name: rc("c"),
|
||||
expr: bx(expr(Access { name: rc("b"), expr: bx(expr(Value(qn!(a)))) }))
|
||||
})
|
||||
);
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
"a.b.c(3)",
|
||||
expr(Call {
|
||||
f: bx(expr(Access {
|
||||
@ -230,7 +230,7 @@ fn accessors() {
|
||||
arguments: vec![InvocationArgument::Positional(expr(NatLiteral(3)))],
|
||||
})
|
||||
);
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
"a.b().c",
|
||||
expr(Access {
|
||||
name: rc("c"),
|
||||
@ -246,12 +246,12 @@ fn accessors() {
|
||||
fn tuples() {
|
||||
use ExpressionKind::*;
|
||||
|
||||
assert_expr2!("()", expr(TupleLiteral(vec![])));
|
||||
assert_expr2!(
|
||||
assert_expr!("()", expr(TupleLiteral(vec![])));
|
||||
assert_expr!(
|
||||
r#"("hella", 34)"#,
|
||||
expr(TupleLiteral(vec![expr(StringLiteral(rc("hella"))), expr(NatLiteral(34))]))
|
||||
);
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
r#"(1+2, "slough")"#,
|
||||
expr(TupleLiteral(vec![
|
||||
binop("+", expr(NatLiteral(1)), expr(NatLiteral(2))),
|
||||
@ -264,12 +264,12 @@ fn tuples() {
|
||||
fn identifiers() {
|
||||
use ExpressionKind::*;
|
||||
|
||||
assert_expr2!("a", expr(Value(qn!(a))));
|
||||
assert_expr2!("some_value", expr(Value(qn!(some_value))));
|
||||
assert_expr2!("alpha::beta::gamma", expr(Value(qn!(alpha, beta, gamma))));
|
||||
assert_expr2!("a + b", binop("+", expr(Value(qn!(a))), expr(Value(qn!(b)))));
|
||||
assert_expr2!("None", expr(Value(qn!(None))));
|
||||
assert_expr2!(
|
||||
assert_expr!("a", expr(Value(qn!(a))));
|
||||
assert_expr!("some_value", expr(Value(qn!(some_value))));
|
||||
assert_expr!("alpha::beta::gamma", expr(Value(qn!(alpha, beta, gamma))));
|
||||
assert_expr!("a + b", binop("+", expr(Value(qn!(a))), expr(Value(qn!(b)))));
|
||||
assert_expr!("None", expr(Value(qn!(None))));
|
||||
assert_expr!(
|
||||
"thing::item::call()",
|
||||
expr(Call { f: bx(expr(Value(qn!(thing, item, call)))), arguments: vec![] })
|
||||
);
|
||||
@ -278,14 +278,14 @@ fn identifiers() {
|
||||
#[test]
|
||||
fn named_struct() {
|
||||
use ExpressionKind::*;
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
"Pandas { a: x + y }",
|
||||
expr(NamedStruct {
|
||||
name: qn!(Pandas),
|
||||
fields: vec![(rc("a"), binop("+", expr(Value(qn!(x))), expr(Value(qn!(y)))))]
|
||||
})
|
||||
);
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
"Trousers { a:1, b:800 }",
|
||||
expr(NamedStruct {
|
||||
name: qn!(Trousers),
|
||||
@ -297,14 +297,14 @@ fn named_struct() {
|
||||
#[test]
|
||||
fn index() {
|
||||
use ExpressionKind::*;
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
"armok[b,c]",
|
||||
expr(Index {
|
||||
indexee: bx(expr(Value(qn!(armok)))),
|
||||
indexers: vec![expr(Value(qn!(b))), expr(Value(qn!(c)))]
|
||||
})
|
||||
);
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
"a[b,c][1]",
|
||||
expr(Index {
|
||||
indexee: bx(expr(Index {
|
||||
@ -314,7 +314,7 @@ fn index() {
|
||||
indexers: vec![expr(NatLiteral(1))]
|
||||
})
|
||||
);
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
"perspicacity()[a]",
|
||||
expr(Index {
|
||||
indexee: bx(expr(Call { f: bx(expr(Value(qn!(perspicacity)))), arguments: vec![] })),
|
||||
@ -326,17 +326,17 @@ fn index() {
|
||||
let b = expr(Index { indexee: bx(a), indexers: vec![expr(Value(qn!(b)))] });
|
||||
let c = expr(Call { f: bx(b), arguments: vec![] });
|
||||
let d = expr(Index { indexee: bx(c), indexers: vec![expr(Value(qn!(d)))] });
|
||||
assert_expr2!("a()[b]()[d]", d);
|
||||
assert_expr!("a()[b]()[d]", d);
|
||||
|
||||
assert_fail_expr2!("a[]", "Empty index expressions are not allowed");
|
||||
assert_fail_expr!("a[]", "Empty index expressions are not allowed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn while_expression() {
|
||||
use ExpressionKind::*;
|
||||
|
||||
assert_expr2!("while { }", expr(WhileExpression { condition: None, body: Block::default() }));
|
||||
assert_expr2!(
|
||||
assert_expr!("while { }", expr(WhileExpression { condition: None, body: Block::default() }));
|
||||
assert_expr!(
|
||||
"while a == b { }",
|
||||
expr(WhileExpression {
|
||||
condition: Some(bx(binop("==", expr(Value(qn!(a))), expr(Value(qn!(b)))))),
|
||||
@ -349,7 +349,7 @@ fn while_expression() {
|
||||
fn for_expression() {
|
||||
use ExpressionKind::*;
|
||||
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
"for { a <- garodzny::maybeValue } return 1",
|
||||
expr(ForExpression {
|
||||
enumerators: vec![Enumerator { id: rc("a"), generator: expr(Value(qn!(garodzny, maybeValue))) }],
|
||||
@ -357,7 +357,7 @@ fn for_expression() {
|
||||
})
|
||||
);
|
||||
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
"for n <- someRange { f(n) ; }",
|
||||
expr(ForExpression {
|
||||
enumerators: vec![Enumerator { id: rc("n"), generator: expr(Value(qn!(someRange))) }],
|
||||
@ -376,7 +376,7 @@ fn for_expression() {
|
||||
fn lambda_expressions() {
|
||||
use ExpressionKind::*;
|
||||
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
r#"\(x) { x + 1}"#,
|
||||
expr(Lambda {
|
||||
params: vec![FormalParam { name: rc!(x), anno: None, default: None }],
|
||||
@ -387,7 +387,7 @@ fn lambda_expressions() {
|
||||
})
|
||||
);
|
||||
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
r#"\ (x: Int, y) { a;b;c;}"#,
|
||||
expr(Lambda {
|
||||
params: vec![
|
||||
@ -404,7 +404,7 @@ fn lambda_expressions() {
|
||||
})
|
||||
);
|
||||
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
r#"\(x){y}(1)"#,
|
||||
expr(Call {
|
||||
f: bx(expr(Lambda {
|
||||
@ -416,7 +416,7 @@ fn lambda_expressions() {
|
||||
})
|
||||
);
|
||||
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
r#"\(x: Int): String { "q" }"#,
|
||||
expr(Lambda {
|
||||
params: vec![FormalParam { name: rc!(x), anno: Some(ty_simple("Int")), default: None },],
|
||||
@ -433,7 +433,7 @@ fn lambda_expressions() {
|
||||
fn single_param_lambda() {
|
||||
use ExpressionKind::*;
|
||||
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
r#"\x { x + 10 }"#,
|
||||
expr(Lambda {
|
||||
params: vec![FormalParam { name: rc!(x), anno: None, default: None },],
|
||||
@ -447,7 +447,7 @@ fn single_param_lambda() {
|
||||
})
|
||||
);
|
||||
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
r#"\x: Int { x + 10 }"#,
|
||||
expr(Lambda {
|
||||
params: vec![FormalParam { name: rc!(x), anno: Some(ty_simple("Int")), default: None },],
|
||||
@ -467,7 +467,7 @@ fn complex_lambdas() {
|
||||
use ExpressionKind::*;
|
||||
|
||||
//TODO support this without the semicolon after the lambda
|
||||
assert_ast2! {
|
||||
assert_ast! {
|
||||
r#"fn wahoo() { let a = 10; \(x) { x + a }; }
|
||||
wahoo()(3) "#,
|
||||
vec![
|
||||
@ -503,7 +503,7 @@ fn complex_lambdas() {
|
||||
|
||||
#[test]
|
||||
fn reserved_words() {
|
||||
assert_fail2!("module::item::call()", "error at 1:7: expected ['a' ..= 'z' | 'A' ..= 'Z' | '_']");
|
||||
assert_fail!("module::item::call()", "error at 1:7: expected ['a' ..= 'z' | 'A' ..= 'Z' | '_']");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -511,7 +511,7 @@ fn type_annotations() {
|
||||
use ExpressionKind::*;
|
||||
use TypeIdentifier::*;
|
||||
|
||||
assert_ast2!(
|
||||
assert_ast!(
|
||||
"let a = b : Int",
|
||||
vec![decl(Declaration::Binding {
|
||||
name: rc("a"),
|
||||
@ -521,11 +521,11 @@ fn type_annotations() {
|
||||
})]
|
||||
);
|
||||
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
"a: Int",
|
||||
expr_anno(Value(qn!(a)), Singleton(TypeSingletonName { name: rc("Int"), params: vec![] }))
|
||||
);
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
"a: Option<Int>",
|
||||
expr_anno(
|
||||
Value(qn!(a)),
|
||||
@ -535,7 +535,7 @@ fn type_annotations() {
|
||||
})
|
||||
)
|
||||
);
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
"a: KoreanBBQSpecifier<Kimchi, Option<Bulgogi> >",
|
||||
expr_anno(
|
||||
Value(qn!(a)),
|
||||
@ -551,7 +551,7 @@ fn type_annotations() {
|
||||
})
|
||||
)
|
||||
);
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
"a: (Int, Yolo<a>)",
|
||||
expr_anno(
|
||||
Value(qn!(a)),
|
||||
@ -569,7 +569,7 @@ fn type_annotations() {
|
||||
#[test]
|
||||
fn type_declarations() {
|
||||
use Declaration::TypeDecl;
|
||||
assert_ast2! {
|
||||
assert_ast! {
|
||||
"type Alpha = Alpha", vec![
|
||||
decl(TypeDecl {
|
||||
name: TypeSingletonName { name: rc("Alpha"), params: vec![] },
|
||||
@ -585,7 +585,7 @@ fn type_declarations() {
|
||||
]
|
||||
};
|
||||
|
||||
assert_ast2!(
|
||||
assert_ast!(
|
||||
"type mut Kuah = Kuah",
|
||||
decl(TypeDecl {
|
||||
name: TypeSingletonName { name: rc("Kuah"), params: vec![] },
|
||||
@ -598,7 +598,7 @@ fn type_declarations() {
|
||||
})
|
||||
);
|
||||
|
||||
assert_ast2! {
|
||||
assert_ast! {
|
||||
"type Alpha = Alpha { a: Int, b: Int }",
|
||||
vec![decl(TypeDecl {
|
||||
name: TypeSingletonName { name: rc("Alpha"), params: vec![] },
|
||||
@ -616,7 +616,7 @@ fn type_declarations() {
|
||||
})]
|
||||
};
|
||||
|
||||
assert_ast2! {
|
||||
assert_ast! {
|
||||
"type Alpha = { a: Int, b: Int }",
|
||||
vec![decl(TypeDecl {
|
||||
name: TypeSingletonName { name: rc("Alpha"), params: vec![] },
|
||||
@ -629,7 +629,7 @@ fn type_declarations() {
|
||||
})]
|
||||
};
|
||||
|
||||
assert_ast2!(
|
||||
assert_ast!(
|
||||
"type Option<T> = None | Some(T)",
|
||||
vec![decl(TypeDecl {
|
||||
name: TypeSingletonName {
|
||||
@ -651,12 +651,12 @@ fn type_declarations() {
|
||||
})]
|
||||
);
|
||||
|
||||
assert_ast2!(
|
||||
assert_ast!(
|
||||
"type alias Alpha = Beta",
|
||||
decl(Declaration::TypeAlias { alias: rc("Alpha"), original: rc("Beta") })
|
||||
);
|
||||
|
||||
assert_ast2!("type Complex<T, U> = Unit | Record { field: AnotherType<Bool>, field2: (Nat, Int), field3: T } | Tuple(Int, (String, T))",
|
||||
assert_ast!("type Complex<T, U> = Unit | Record { field: AnotherType<Bool>, field2: (Nat, Int), field3: T } | Tuple(Int, (String, T))",
|
||||
decl(TypeDecl {
|
||||
name: TypeSingletonName { name: rc("Complex"), params: vec![
|
||||
TypeIdentifier::Singleton(TypeSingletonName { name: rc("T"), params: vec![] }),
|
||||
@ -693,7 +693,7 @@ fn type_declarations() {
|
||||
fn declarations() {
|
||||
use ExpressionKind::*;
|
||||
|
||||
assert_ast2!(
|
||||
assert_ast!(
|
||||
"let q_q = Yolo::Swaggins",
|
||||
vec![decl(Declaration::Binding {
|
||||
name: rc("q_q"),
|
||||
@ -708,7 +708,7 @@ fn declarations() {
|
||||
fn bindings() {
|
||||
use ExpressionKind::*;
|
||||
|
||||
assert_ast2!(
|
||||
assert_ast!(
|
||||
"let mut a = 10",
|
||||
vec![decl(Declaration::Binding {
|
||||
name: rc("a"),
|
||||
@ -718,7 +718,7 @@ fn bindings() {
|
||||
})]
|
||||
);
|
||||
|
||||
assert_ast2!(
|
||||
assert_ast!(
|
||||
"let a = 2 + a",
|
||||
vec![stmt(StatementKind::Declaration(Declaration::Binding {
|
||||
name: rc("a"),
|
||||
@ -728,7 +728,7 @@ fn bindings() {
|
||||
}))]
|
||||
);
|
||||
|
||||
assert_ast2!(
|
||||
assert_ast!(
|
||||
"let a: Nat = 2",
|
||||
vec![stmt(StatementKind::Declaration(Declaration::Binding {
|
||||
name: rc("a"),
|
||||
@ -742,7 +742,7 @@ fn bindings() {
|
||||
#[test]
|
||||
fn functions() {
|
||||
use ExpressionKind::*;
|
||||
assert_ast2!(
|
||||
assert_ast!(
|
||||
"fn oi()",
|
||||
vec![stmt(StatementKind::Declaration(Declaration::FuncSig(Signature {
|
||||
name: rc("oi"),
|
||||
@ -752,12 +752,12 @@ fn functions() {
|
||||
})))]
|
||||
);
|
||||
|
||||
assert_ast2!(
|
||||
assert_ast!(
|
||||
"oi()",
|
||||
vec![stmt(StatementKind::Expression(expr(Call { f: bx(expr(Value(qn!(oi)))), arguments: vec![] })))]
|
||||
);
|
||||
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
"oi(a, 2+2)",
|
||||
expr(Call {
|
||||
f: bx(expr(Value(qn!(oi)))),
|
||||
@ -767,9 +767,9 @@ fn functions() {
|
||||
]
|
||||
})
|
||||
);
|
||||
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_fail!("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!(
|
||||
assert_ast!(
|
||||
"fn a(b, c: Int): Int",
|
||||
vec![stmt(StatementKind::Declaration(Declaration::FuncSig(Signature {
|
||||
name: rc("a"),
|
||||
@ -794,7 +794,7 @@ fn functions() {
|
||||
|
||||
}"#;
|
||||
|
||||
assert_ast2!(
|
||||
assert_ast!(
|
||||
source,
|
||||
vec![fn_decl(
|
||||
Signature { name: rc("some_function"), operator: false, type_anno: None, params: vec![] },
|
||||
@ -810,9 +810,9 @@ fn max_function_params() {
|
||||
write!(buf, "a{}, ", n).unwrap();
|
||||
}
|
||||
write!(buf, ") {{ return 20 }}").unwrap();
|
||||
//assert_fail2!(&buf, "A function cannot have more than 255 arguments");
|
||||
//assert_fail!(&buf, "A function cannot have more than 255 arguments");
|
||||
//TODO better errors again
|
||||
assert_fail2!(&buf, "error at 1:1439: expected ['a' ..= 'z' | 'A' ..= 'Z' | '_']");
|
||||
assert_fail!(&buf, "error at 1:1439: expected ['a' ..= 'z' | 'A' ..= 'Z' | '_']");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -834,7 +834,7 @@ fn functions_with_different_whitespace() {
|
||||
"#;
|
||||
|
||||
for item in [a, b, c].iter() {
|
||||
assert_ast2!(
|
||||
assert_ast!(
|
||||
item,
|
||||
vec![fn_decl(
|
||||
Signature {
|
||||
@ -857,7 +857,7 @@ fn functions_with_different_whitespace() {
|
||||
fn functions_with_default_args() {
|
||||
use ExpressionKind::*;
|
||||
|
||||
assert_ast2!(
|
||||
assert_ast!(
|
||||
"fn func(x: Int, y: Int = 4) { }",
|
||||
vec![fn_decl(
|
||||
Signature {
|
||||
@ -881,7 +881,7 @@ fn functions_with_default_args() {
|
||||
#[test]
|
||||
fn interface() {
|
||||
let glue = TypeIdentifier::Singleton(TypeSingletonName { name: rc("Glue"), params: vec![] });
|
||||
assert_ast2!(
|
||||
assert_ast!(
|
||||
"interface Unglueable { fn unglue(a: Glue); fn mar(): Glue }",
|
||||
vec![decl(Declaration::Interface {
|
||||
name: rc("Unglueable"),
|
||||
@ -913,13 +913,13 @@ fn impls() {
|
||||
),
|
||||
];
|
||||
|
||||
assert_ast2!(
|
||||
assert_ast!(
|
||||
"impl Heh { fn yolo() { }; fn swagg() { } }",
|
||||
vec![decl(Impl { type_name: ty_simple("Heh"), interface_name: None, block: block.clone() })]
|
||||
);
|
||||
|
||||
//TODO `"impl Heh<X> { fn yolo() { }; fn swagg() { }; }"` ought to work
|
||||
assert_ast2!(
|
||||
assert_ast!(
|
||||
"impl Heh<X> { fn yolo() { }; fn swagg() { } }",
|
||||
vec![decl(Impl {
|
||||
type_name: TypeIdentifier::Singleton(TypeSingletonName {
|
||||
@ -931,7 +931,7 @@ fn impls() {
|
||||
})]
|
||||
);
|
||||
|
||||
assert_ast2!(
|
||||
assert_ast!(
|
||||
"impl Heh for Saraz { fn yolo() {}; fn swagg() {} }",
|
||||
vec![decl(Impl {
|
||||
type_name: ty_simple("Saraz"),
|
||||
@ -940,7 +940,7 @@ fn impls() {
|
||||
})]
|
||||
);
|
||||
|
||||
assert_ast2!(
|
||||
assert_ast!(
|
||||
"impl Heh<T> for (Int, Codepoint) {}",
|
||||
vec![decl(Impl {
|
||||
type_name: TypeIdentifier::Tuple(vec![ty_simple("Int"), ty_simple("Codepoint")]),
|
||||
@ -959,7 +959,7 @@ fn annotations() {
|
||||
vec![].into(),
|
||||
));
|
||||
|
||||
assert_ast2! {
|
||||
assert_ast! {
|
||||
r#"
|
||||
@test_annotation
|
||||
fn some_function() {
|
||||
@ -973,7 +973,7 @@ fn annotations() {
|
||||
]
|
||||
};
|
||||
|
||||
assert_ast2! {
|
||||
assert_ast! {
|
||||
r#"
|
||||
@test_annotation(some,value)
|
||||
@another_annotation
|
||||
@ -993,7 +993,7 @@ fn annotations() {
|
||||
|
||||
#[test]
|
||||
fn modules() {
|
||||
assert_ast2! {
|
||||
assert_ast! {
|
||||
r#"
|
||||
module ephraim {
|
||||
let mut a = 10
|
||||
@ -1016,7 +1016,7 @@ fn modules() {
|
||||
|
||||
#[test]
|
||||
fn imports() {
|
||||
assert_ast2! {
|
||||
assert_ast! {
|
||||
"import harbinger::draughts::Norgleheim",
|
||||
vec![stmt(StatementKind::Import(ImportSpecifier {
|
||||
id: ItemId::default(),
|
||||
@ -1025,7 +1025,7 @@ fn imports() {
|
||||
}))]
|
||||
};
|
||||
|
||||
assert_ast2! {
|
||||
assert_ast! {
|
||||
"import harbinger::draughts::{Norgleheim, Xraksenlaigar}",
|
||||
vec![stmt(StatementKind::Import(ImportSpecifier {
|
||||
id: ItemId::default(),
|
||||
@ -1035,7 +1035,7 @@ fn imports() {
|
||||
}))]
|
||||
};
|
||||
|
||||
assert_ast2! {
|
||||
assert_ast! {
|
||||
"import bespouri::{}",
|
||||
vec![stmt(StatementKind::Import(ImportSpecifier {
|
||||
id: Default::default(),
|
||||
@ -1044,7 +1044,7 @@ fn imports() {
|
||||
}))]
|
||||
};
|
||||
|
||||
assert_ast2! {
|
||||
assert_ast! {
|
||||
"import bespouri::*",
|
||||
vec![stmt(StatementKind::Import(ImportSpecifier {
|
||||
id: Default::default(),
|
||||
@ -1057,7 +1057,7 @@ fn imports() {
|
||||
#[test]
|
||||
fn if_exprs() {
|
||||
use ExpressionKind::*;
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
"if a() then { tuah(); }",
|
||||
expr(IfExpression {
|
||||
discriminator: Some(bx(expr(Call { f: bx(expr(Value(qn!(a)))), arguments: vec![] }))),
|
||||
@ -1068,7 +1068,7 @@ fn if_exprs() {
|
||||
})
|
||||
);
|
||||
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
"if a then b else c",
|
||||
expr(IfExpression {
|
||||
discriminator: Some(bx(expr(Value(qn!(a))))),
|
||||
@ -1079,7 +1079,7 @@ fn if_exprs() {
|
||||
})
|
||||
);
|
||||
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
r#"
|
||||
if true then {
|
||||
let a = 10
|
||||
@ -1111,7 +1111,7 @@ fn pattern_matching() {
|
||||
use ExpressionKind::*;
|
||||
|
||||
for item in ["if x is Some(a) then { 4 } else { 9 }", "if x is Some(a) then 4 else 9"] {
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
item,
|
||||
expr(IfExpression {
|
||||
discriminator: Some(bx(expr(Value(qn!(x))))),
|
||||
@ -1124,7 +1124,7 @@ fn pattern_matching() {
|
||||
);
|
||||
}
|
||||
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
"if x is Something { a, b: x } then { 4 } else { 9 }",
|
||||
expr(IfExpression {
|
||||
discriminator: Some(bx(expr(Value(qn!(x))))),
|
||||
@ -1139,7 +1139,7 @@ fn pattern_matching() {
|
||||
})
|
||||
);
|
||||
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
"if x is -1 then 1 else 2",
|
||||
expr(IfExpression {
|
||||
discriminator: Some(bx(expr(Value(qn!(x))))),
|
||||
@ -1151,7 +1151,7 @@ fn pattern_matching() {
|
||||
})
|
||||
);
|
||||
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
"if x is true then 1 else 2",
|
||||
expr(IfExpression {
|
||||
discriminator: Some(bx(expr(Value(qn!(x))))),
|
||||
@ -1163,7 +1163,7 @@ fn pattern_matching() {
|
||||
})
|
||||
);
|
||||
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
"if x { is 1 then 5; else 20 }",
|
||||
expr(IfExpression {
|
||||
discriminator: Some(bx(expr(Value(qn!(x))))),
|
||||
@ -1185,7 +1185,7 @@ fn pattern_matching() {
|
||||
})
|
||||
);
|
||||
|
||||
assert_expr2!(
|
||||
assert_expr!(
|
||||
r#"if x is "gnosticism" then 1 else 2"#,
|
||||
expr(IfExpression {
|
||||
discriminator: Some(bx(expr(Value(qn!(x))))),
|
||||
@ -1197,7 +1197,7 @@ fn pattern_matching() {
|
||||
})
|
||||
);
|
||||
|
||||
assert_expr2! {
|
||||
assert_expr! {
|
||||
r#"
|
||||
if (45, "panda", false, 2.2) {
|
||||
is (49, "pablo", _, 28.4) then "no"
|
||||
@ -1259,7 +1259,7 @@ fn flow_control() {
|
||||
return 10;
|
||||
}"#;
|
||||
|
||||
assert_ast2!(
|
||||
assert_ast!(
|
||||
source,
|
||||
vec![fn_decl(
|
||||
Signature { name: rc("test"), operator: false, type_anno: None, params: vec![] },
|
||||
@ -1297,12 +1297,12 @@ fn comments() {
|
||||
use ExpressionKind::*;
|
||||
|
||||
let source = "1 + /* hella /* bro */ */ 2";
|
||||
assert_expr2!(source, binop("+", expr(NatLiteral(1)), expr(NatLiteral(2))));
|
||||
assert_expr!(source, binop("+", expr(NatLiteral(1)), expr(NatLiteral(2))));
|
||||
|
||||
//TODO make sure this error message makes sense
|
||||
let source = "1 + /* hella /* bro */ 2";
|
||||
assert_fail_expr2!(source, "foo");
|
||||
assert_fail_expr!(source, "foo");
|
||||
|
||||
let source = "1 + /* hella */ bro */ 2";
|
||||
assert_fail_expr2!(source, binop("+", expr(NatLiteral(1)), expr(NatLiteral(2))));
|
||||
assert_fail_expr!(source, binop("+", expr(NatLiteral(1)), expr(NatLiteral(2))));
|
||||
}
|
||||
|
@ -133,7 +133,6 @@ impl SourceReference {
|
||||
}
|
||||
|
||||
fn load_new_source(&mut self, source: &str) {
|
||||
|
||||
for (offset, ch) in source.as_bytes().iter().enumerate() {
|
||||
if *ch == ('\n' as u8) {
|
||||
self.newline_offsets.push(offset);
|
||||
|
@ -226,9 +226,7 @@ pub fn tokenize(input: &str) -> Vec<Token> {
|
||||
c if is_operator(&c) => handle_operator(c, &mut input),
|
||||
unknown => Error(format!("Unexpected character: {}", unknown)),
|
||||
};
|
||||
let location = Location {
|
||||
offset: 0,
|
||||
};
|
||||
let location = Location { offset: 0 };
|
||||
tokens.push(Token { kind: cur_tok_kind, location });
|
||||
}
|
||||
tokens
|
||||
|
Loading…
Reference in New Issue
Block a user