prql icon indicating copy to clipboard operation
prql copied to clipboard

Add tests for syntax grammars

Open max-sixty opened this issue 3 years ago • 1 comments

On changing the case syntax, I realize that we didn't have syntax for the previous syntax of ->, let alone the update one of =>.

In tree-sitter-prql, there are tests on some small examples; for example https://github.com/PRQL/tree-sitter-prql/blob/5f6c4e4a90633b19e2077c1d37248989789d64be/test/corpus/from_text.txt. Could we do something similar for the ones in this repo?

This would both help improve the quality of the current grammars, and gate future syntax changes on updating these. Some options of how we could do this; in increasing order of completeness but also work:

  • Simply "do a couple of canonical examples successfully parse"
  • Whether the snapshot .prql files in the repo parse
  • Snapshots of parse outputs, like tree-sitter-prql have

Something initial, on a subset of the grammars, would be very welcome

max-sixty avatar Mar 08 '23 05:03 max-sixty

Concrete evidence for the "whether the snapshot .prql files in the repo parse" option, from tonight's code survey: the lezer grammar currently rejects case [...], which is the form web/book/src/reference/syntax/case.md documents and the form both .prql files in the corpus that use case are written in (switch.prql, append_select_compute.prql). CaseExpression takes a TupleExpression only, so the braced form parses and the bracketed form does not — even though the compiler accepts both and prqlc fmt leaves each as written.

$ node -e 'import("./dist/index.js").then(({parser}) => {
    for (const src of ["derive x = case [a => 1]\n", "derive x = case {a => 1}\n"]) {
      const errs = []
      parser.parse(src).iterate({enter: n => { if (n.type.isError) errs.push(`${n.from}-${n.to}`) }})
      console.log(JSON.stringify(src), "->", errs.length ? errs.join(",") : "no errors")
    }
  })'
"derive x = case [a => 1]\n" -> 11-11,14-14,23-23
"derive x = case {a => 1}\n" -> no errors

The grammar's own corpus in grammars/prql-lezer/test/ only ever exercises the braced form, which is why nothing is red. That's the gap this issue describes: a hand-written corpus tests the syntax its author thought of, while running the repo's existing .prql files through the grammar would have caught this one on the first run.

Not fixing it here — the bot already has six PRs open, so this is a note rather than a seventh. The fix looks like widening CaseExpression to accept either bracket, but whether [...] is still the intended spelling (the opening note here is about case syntax having changed once already) is a language question worth settling before the grammar is taught both.

prql-bot avatar Sep 15 '26 07:09 prql-bot