Initial Commit
This commit is contained in:
782 files changed
+145156
No files matched your search
@@ -0,0 +1,141 @@
|
||||
#! @file recovery.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the tests of how the parser recovers from mistakes: going on past an
|
||||
#! item, a parameter, a statement, or a declaration that fails, with one note
|
||||
#! for each mistake.
|
||||
|
||||
import std::testing;
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
@test def a_list_goes_on_past_an_item_that_fails(t: &mut tester): void {
|
||||
val made := parse("def f(): void { g(1 +, 2, h(3 *), [4, , 5]); }\nval ok := 1;\n");
|
||||
t.expects(made.notes.length == 3);
|
||||
t.expects(noted(&made, 1, 22, ec::expr));
|
||||
t.expects(noted(&made, 1, 32, ec::expr));
|
||||
t.expects(noted(&made, 1, 39, ec::expr));
|
||||
t.expects(made.syntax.print(made.root, &made.source, &made.names).span ==
|
||||
"(document [(function:f [] _ [] [] type_name:void (block [(expr_stmt:g (call name:g [(binary:+ number:1 error) number:2 (call name:h [(binary:* number:3 error)]) (array [number:4 error number:5])]))])) (variable:ok [] _ _ number:1)])");
|
||||
}
|
||||
|
||||
@test def parameters_go_on_past_one_that_fails(t: &mut tester): void {
|
||||
val made := parse("def f(a: , b: i64, 7, c: u8): void {}");
|
||||
t.expects(made.notes.length == 2);
|
||||
t.expects(noted(&made, 1, 10, ec::kind));
|
||||
t.expects(noted(&made, 1, 20, ec::ident));
|
||||
}
|
||||
|
||||
@test def statements_and_declarations_resume_after_errors(t: &mut tester): void {
|
||||
val made := parse("def f(): void { val x := 1 }\ndef g(): void { y = ; z(); }\nstruct s { var a: i64 }\nval ok := 1;\n");
|
||||
t.expects(made.notes.length == 3);
|
||||
t.expects(noted(&made, 1, 28, ec::token));
|
||||
t.expects(noted(&made, 2, 21, ec::expr));
|
||||
t.expects(noted(&made, 3, 23, ec::token));
|
||||
t.expects(made.syntax.print(made.root, &made.source, &made.names).span ==
|
||||
"(document [(function:f [] _ [] [] type_name:void (block [(variable:x [] _ _ number:1)])) (function:g [] _ [] [] type_name:void (block [(expr_stmt:y (assign:= name:y error))])) (struct_decl:s [] _ [] [] [(field:a [] _ type_name:i64 _ {mutable})]) (variable:ok [] _ _ number:1)])");
|
||||
}
|
||||
|
||||
@test def members_resume_after_errors(t: &mut tester): void {
|
||||
val made := parse("trait t { pub def f(): void; var x: i64; }\nenum e { a = , b, 3, c }\n");
|
||||
t.expects(noted(&made, 1, 11, ec::token));
|
||||
t.expects(noted(&made, 1, 30, ec::token));
|
||||
t.expects(noted(&made, 2, 14, ec::expr));
|
||||
t.expects(noted(&made, 2, 19, ec::ident));
|
||||
}
|
||||
|
||||
@test def what_belongs_nowhere_is_kept_within_errors(t: &mut tester): void {
|
||||
# A trait's `var` that is no property, attributes before no member, and those
|
||||
# within a function, each kept under an `error` node rather than dropped.
|
||||
t.expects(tree("trait t { var x: i64; }").span ==
|
||||
"(document [(trait_decl:t [] _ [] [] [(error [(variable:x [] _ type_name:i64 _ {mutable})])])])");
|
||||
t.expects(tree("struct s { @a }").span ==
|
||||
"(document [(struct_decl:s [] _ [] [] [(error [(attribute type_name:a [])])])])");
|
||||
t.expects(statements("@a val x := 1;").span ==
|
||||
"(error [(attribute type_name:a []) (variable:x [] _ _ number:1)])");
|
||||
}
|
||||
|
||||
@test def declarations_are_pub_once_and_after_their_attributes(t: &mut tester): void {
|
||||
val made := parse("pub pub def f(): void {}\n@a pub @b def g(): void {}\nimport x\nmodule m;\n");
|
||||
t.expects(noted(&made, 1, 5, ec::token));
|
||||
t.expects(noted(&made, 2, 8, ec::token));
|
||||
t.expects(noted(&made, 4, 1, ec::token));
|
||||
|
||||
# The attributes written after `pub` still describe the function.
|
||||
t.expects(tree("@a pub @b def g(): void {}").span ==
|
||||
"(document [(function:g [(attribute type_name:a []) (attribute type_name:b [])] _ [] [] type_name:void block {exported})])");
|
||||
}
|
||||
|
||||
@test def nothing_is_nullable(t: &mut tester): void {
|
||||
val made := parse("def f(p: ?i64): void { g(p!); }\n");
|
||||
t.expects(made.notes.length == 2);
|
||||
t.expects(noted(&made, 1, 10, ec::kind));
|
||||
t.expects(noted(&made, 1, 27, ec::token));
|
||||
}
|
||||
|
||||
@test def else_takes_a_value_in_parentheses_before_anything_but_a_block(t: &mut tester): void {
|
||||
val made := parse("val n := f() else (e) 1;\n");
|
||||
t.expects(made.notes.length == 1);
|
||||
t.expects(noted(&made, 1, 23, ec::token));
|
||||
}
|
||||
|
||||
@test def a_comparison_after_a_cast_needs_parentheses(t: &mut tester): void {
|
||||
val made := parse("val ok := n as u64 < limit;\n");
|
||||
t.expects(made.notes.length == 1);
|
||||
t.expects(noted(&made, 1, 27, ec::token));
|
||||
t.expects(made.notes.at(0).detail.starts_with("Expected `>` to close the type arguments. A `<` after the type of `as` or `is`"));
|
||||
|
||||
# Anywhere else the plain message is given.
|
||||
val plain := parse("val v: list<i64;\n");
|
||||
t.expects(plain.notes.at(0).detail == "Expected `>` to close the type arguments.");
|
||||
}
|
||||
|
||||
@test def too_deep_gives_up_on_only_its_declaration(t: &mut tester): void {
|
||||
var text := string::from("val x := ", nothing {});
|
||||
for _ in 0..150 { text.append("("); }
|
||||
text.append("1");
|
||||
for _ in 0..150 { text.append(")"); }
|
||||
text.append(";\nval y := (;\nval z := 1;\n");
|
||||
|
||||
val made := parse(text.span);
|
||||
t.expects(made.notes.length == 2);
|
||||
t.expects(noted(&made, 1, 110, ec::syntax));
|
||||
t.expects(noted(&made, 2, 11, ec::expr));
|
||||
|
||||
# The declarations after it are parsed as though nothing went wrong.
|
||||
val decls := made.syntax.node_lefts.at(made.root);
|
||||
t.expects(made.syntax.run_length(decls) == 3);
|
||||
t.expects(made.syntax.print(made.syntax.run_at(decls, 2), &made.source, &made.names).span == "(variable:z [] _ _ number:1)");
|
||||
}
|
||||
|
||||
@test def notes_are_bounded(t: &mut tester): void {
|
||||
var text := string::new(nothing {});
|
||||
for _ in 0..150 { text.append("val v := ;\n"); }
|
||||
t.expects(parse(text.span).notes.length == parser::max_notes as u64);
|
||||
}
|
||||
|
||||
@test def the_walker_follows_the_shapes(t: &mut tester): void {
|
||||
val made := parse("def f(a: i64): i64 { return a + 1; }");
|
||||
val function := made.syntax.run_at(made.syntax.node_lefts.at(made.root), 0);
|
||||
|
||||
var kids := vector::<u32>::new();
|
||||
made.syntax.children(function, &mut kids);
|
||||
|
||||
# The record's runs and nodes in order: the parameter, the result, the body.
|
||||
t.expects(kids.length == 3);
|
||||
t.expects(made.syntax.node_kinds.at(kids.at(0)) == nk::param);
|
||||
t.expects(made.syntax.node_kinds.at(kids.at(1)) == nk::type_name);
|
||||
t.expects(made.syntax.node_kinds.at(kids.at(2)) == nk::block);
|
||||
|
||||
# Every node is reached once from the document.
|
||||
var todo := vector::<u32>::new();
|
||||
todo.append(made.root);
|
||||
var walked := 0u;
|
||||
while walked < todo.length {
|
||||
made.syntax.children(todo.at(walked), &mut todo);
|
||||
walked++;
|
||||
}
|
||||
t.expects(todo.length == made.syntax.node_count - 1);
|
||||
}
|
||||
Reference in new issue
Block a user