plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
60e4ec8
— Peter John
2026-11-10T20:58:54+05:30
Parser: a statement can't follow another on the same line
compiler/checker/tests/checker/review_tests.rs
CHANGED
|
@@ -226,3 +226,14 @@ fn a_generic_fn_calling_itself_with_a_growing_type_is_an_error() {
|
|
|
226
226
|
);
|
|
227
227
|
assert!(ok.is_empty(), "{ok:?}");
|
|
228
228
|
}
|
|
229
|
+
|
|
230
|
+
/// Two statements on one line (`0b105` is `0b10` then `5`, `12abc` is `12` then `abc`) are a
|
|
231
|
+
/// parse error, where the first one's value used to be dropped and the program ran on.
|
|
232
|
+
#[test]
|
|
233
|
+
fn two_statements_on_one_line_are_a_parse_error() {
|
|
234
|
+
for (body, tok) in [("0b105", "'5'"), ("1 2", "'2'"), ("12abc", "'abc'"), ("x := 1 2\n x", "'2'")] {
|
|
235
|
+
let src = format!("import std/Int\n\nfun k() -> Int =\n {body}\n");
|
|
236
|
+
let e = plum_test_support::load("same_line", &src).expect_err("a parse error");
|
|
237
|
+
assert!(e.contains(&format!("unexpected {tok}: a statement can't follow another on the same line")), "{body}: {e}");
|
|
238
|
+
}
|
|
239
|
+
}
|
compiler/core/src/parser.rs
CHANGED
|
@@ -738,7 +738,17 @@ impl<'a> AstParser<'a> {
|
|
|
738
738
|
fn parseBlock(&self, node: Node) -> Block {
|
|
739
739
|
let mut cursor = node.walk();
|
|
740
740
|
let mut stmts = Vec::new();
|
|
741
|
+
// The grammar has no separator between statements, so `x := 1 2`, `0b105` (`0b10`
|
|
742
|
+
// then `5`) or `12abc` would be two statements: the first one's value silently lost.
|
|
743
|
+
let mut prev_end: Option<usize> = None;
|
|
741
744
|
for n in node.named_children(&mut cursor) {
|
|
745
|
+
if n.kind() == "comment" {
|
|
746
|
+
continue;
|
|
747
|
+
}
|
|
748
|
+
if prev_end == Some(n.start_position().row) {
|
|
749
|
+
self.removed(n, &format!("unexpected '{}': a statement can't follow another on the same line", self.text(n).lines().next().unwrap_or("")));
|
|
750
|
+
}
|
|
751
|
+
prev_end = Some(n.end_position().row);
|
|
742
752
|
let u = self.unwrapExprNode(n);
|
|
743
753
|
if u.kind() == "assign" && self.isUpdate(u) {
|
|
744
754
|
stmts.extend(self.parseUpdate(u));
|