plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


3f715bd — Peter John 2026-11-10T19:57:38+05:30
Parse errors: a chained comparison says comparisons don't chain
compiler/checker/tests/checker/review_tests.rs CHANGED
@@ -99,6 +99,20 @@ fn an_open_local_given_one_type_checks() {
99
99
  assert!(es.is_empty(), "{es:?}");
100
100
  }
101
101
 
102
+ /// `a < b > c` is a syntax error that says comparisons don't chain, at the second operator,
103
+ /// not an "expected native" the parser's recovery made up.
104
+ #[test]
105
+ fn a_chained_comparison_says_comparisons_dont_chain() {
106
+ for src in [
107
+ "import std/Int\nimport std/Bool\n\nfun f(a: Int, b: Int) -> Bool =\n x := a < b > a\n x\n",
108
+ "import std/Int\nimport std/Bool\n\nfun f(a: Int, b: Int) -> Bool = a < b > a\n",
109
+ ] {
110
+ let e = plum_test_support::load("chain", src).expect_err("a parse error");
111
+ assert!(e.contains("comparisons don't chain"), "{e}");
112
+ assert!(!e.contains("native"), "{e}");
113
+ }
114
+ }
115
+
102
116
  /// A field whose type the first write fixes is not rebound by a second write of another
103
117
  /// type: that compiled `Some(1)` into an `Option<Str>` slot, invalid wasm.
104
118
  #[test]
compiler/core/src/loader.rs CHANGED
@@ -410,7 +410,7 @@ fn parseText(path: &Path, text: &str) -> Loaded_<Source> {
410
410
  path.display().to_string(),
411
411
  err.start.row + 1,
412
412
  err.start.column + 1,
413
- format!("{}{}{}", err.message(), groupingHint(text, err.start), angleHint(text, err.start)),
413
+ format!("{}{}{}{}", err.message(), groupingHint(text, err.start), angleHint(text, err.start), chainHint(text, err.start)),
414
414
  )]);
415
415
  }
416
416
  let ap = AstParser::new(&text);
@@ -455,6 +455,32 @@ pub fn groupingHint(text: &str, at: tree_sitter::Point) -> String {
455
455
  .to_string()
456
456
  }
457
457
 
458
+ /// `a < b > c`, `a == b == c`: comparisons don't chain. A hint line when the error is at a
459
+ /// comparison operator and the line has another one before it.
460
+ pub fn chainHint(text: &str, at: tree_sitter::Point) -> String {
461
+ let Some(line) = text.lines().nth(at.row) else { return String::new() };
462
+ let Some(rest) = line.get(at.column.min(line.len())..) else { return String::new() };
463
+ let ops = ["<=", ">=", "==", "!=", "<", ">"];
464
+ let Some(op) = ops.iter().find(|o| rest.starts_with(**o)) else { return String::new() };
465
+ // `->` and `=>` are no comparisons.
466
+ if line[..at.column].ends_with(['-', '=']) && *op == ">" {
467
+ return String::new();
468
+ }
469
+ let before = &line[..at.column];
470
+ let earlier = ops.iter().any(|o| {
471
+ before.match_indices(o).any(|(i, _)| {
472
+ let prev = before[..i].chars().last();
473
+ let next = before[i + o.len()..].chars().next();
474
+ // A spaced operator, not `->`, `=>`, `:=` or type arguments glued to a name.
475
+ prev == Some(' ') && next == Some(' ')
476
+ })
477
+ });
478
+ if !earlier {
479
+ return String::new();
480
+ }
481
+ format!("\n hint: comparisons don't chain: write `a < b && b {op} c`, or group one side with `{{ }}`")
482
+ }
483
+
458
484
  /// `Option[Int]`, `fun map[B](..)`, `Slice[Int]()`: type parameters and arguments were
459
485
  /// written in `[ ]`. A hint line when the error is at a `[` directly after a name, or on a
460
486
  /// line that has one before the error (the parser often gives up a token or two later).
@@ -579,6 +605,15 @@ fn locateError(err: tree_sitter::Node, root: tree_sitter::Node, text: &str) -> S
579
605
  // A closer the parser inserted (`foo(1, 2` + next line): blame the first token
580
606
  // that follows, or the unclosed opener when the file ends inside it.
581
607
  let missing_closer = err.is_missing() && matches!(err.kind(), ")" | "]" | "}");
608
+ // A keyword the recovery invents to finish a cut-short expression (`@native`'s
609
+ // `native`, in `x := a < b > a`) says nothing about the mistake: the token there does.
610
+ let invented = err.is_missing() && err.kind() == "native";
611
+ if invented {
612
+ if let Some(tok) = leafStartingAtOrAfter(root, err.start_byte()).filter(|t| !t.is_missing()) {
613
+ let shown = text.get(tok.start_byte()..tok.end_byte()).unwrap_or("").lines().next().unwrap_or("").to_string();
614
+ return SyntaxError { start: tok.start_position(), end: tok.end_position(), kind: SyntaxErrorKind::Unexpected(shown) };
615
+ }
616
+ }
582
617
  if err.is_missing() && !missing_closer {
583
618
  return SyntaxError {
584
619
  start: err.start_position(),