Skip to content

Report parser errors instead of panicking - #243

Merged
vito merged 7 commits into
mainfrom
fix-parser-panics
Nov 1, 2026
Merged

vito merged 7 commits into
mainfrom
fix-parser-panics

Conversation

@vito

@vito vito commented Nov 1, 2026 •

Copy link
Copy Markdown
Owner

Several inputs crashed the parser with a Go interface-conversion panic instead of reporting a syntax error, and a few related literal and escape forms were rejected or reported in the wrong place.

%word{...} literals

Before:

$ echo 'print(%raw{hello})' > x.dang && dang x.dang
x.dang:1:18 (17): rule PositionalValue: interface conversion: *dang.Quoted is not dang.Node: missing method Body

After:

$ echo 'let x = %raw{hello}' > x.dang && dang x.dang
Error: syntax error: invalid expression after '='
  --> x.dang:1:1

The Quoted rule came from the October 2023 grammar sketch and never got semantics: it had no Infer, no Eval and no formatter case. The only documentation was a single line in the grammar reference. Since no program can depend on it, this removes the rule, the AST type and the doc line (no version gate), and regenerates the pigeon and tree-sitter parsers.

Out-of-range / invalid literals

Before:

$ echo 'let x = 9223372036854775808' > x.dang && dang x.dang
x.dang:2:0 (27): rule ValueOnlyField: interface conversion: interface is nil, not dang.Node

After:

Error: syntax error: integer literal out of range
  --> x.dang:1:9
   |
 1 | let x = 9223372036854775808
   |         ^^^^^^^^^^^^^^^^^^^

The same change covers out-of-range floats (1.0e309, which used to give a misleading unexpected '1.0') and strings with escapes that encode no character ("\777", which panicked the same way). These actions now panic with a literalError, and pigeon's recover turns that into a parse error. Returning the error isn't enough: pigeon drops it when an enclosing left-recursive rule rolls back its error list, and the parse then "succeeds". ParseWithRecovery shows the literal error with its source, ahead of tree-sitter's diagnosis (tree-sitter finds nothing wrong with these literals). This applies to the non-cgo build too.

-9223372036854775808 still gives the out-of-range error, because - is an operator applied to the literal. test_unary_minus now shows the bounds: 9223372036854775807 and -9223372036854775807 - 1.

Two block parameters

Before:

$ dang x.dang   # pub f(&a: Int!, &b: Int!): Int! { 1 }
rule TypeAndArgsAndBlockField: function can only have one block parameter
rule Dang: interface conversion: interface is nil, not dang.Node

After: syntax error: function can only have one block parameter, pointing at the second block parameter. new(&a: ..., &b: ...) gets the same fix (constructor can only have one block parameter). Both actions go through the same reject(loc, msg) helper as the literals.

The bodiless forms used to parse and silently drop one of the two block parameters. They are now rejected the same way: an interface field twice(&a: Int!, &b: Int!): Int! gives function can only have one block parameter, and a block parameter's own type &blk(&a: Int!, &b: Int!): Int! gives block can only have one block parameter.

1e5

Before: let a = 1e5 gave syntax error: unexpected '1', while 10e5 parsed, because the exponent-only form needed two mantissa digits. After: 1e5 is a float (see test_float_literals).

\/ in strings

Before: print("a\/b") gave syntax error: unexpected 'print("a\/b")'. After: it prints a/b. \/ is an escape for /, as in JSON. The old bytes.Replace in the String action also turned the escaped backslash in "a\\/b" into \/; a small unquoteString now rewrites only real \/ escapes. An unknown escape such as "C:\Users" is now reported at the escape (syntax error: invalid escape sequence in string literal) instead of at the start of the statement. Tree-sitter leaves that rule out, so editors still flag the bad escape.

Closes #236

Stale tree-sitter builds

pkg/dang/danglang #includes treesitter/src/parser.c from another directory, and the Go build cache doesn't track that file, so a cached build could keep linking the old tree-sitter parser after a regeneration. The treesitter go generate step now ends by writing pkg/dang/danglang/sources_digest.go, which holds a SHA-256 of the included C sources. A regenerated parser therefore changes the package and forces a rebuild, and the treesitter generate/stale check fails if the stamp is out of date.

Known gap

Tree-sitter (highlighting and syntax-error placement only) still mis-lexes exponents written e<digit>, such as 1e5 and 1.5e10: it reads e5 as an identifier because it treats the grammar's "e" as a keyword. This happens on main too. Fixing it in the generated grammar broke x.field parsing in other corpus tests, so it is left for a follow-up.

vito added 2 commits November 1, 2026 18:34
`%raw{hello}` parsed into a Quoted value that was never a Node: it had no
Infer, Eval or formatter case, so any use of it panicked the parser with
"*dang.Quoted is not dang.Node". The rule dates from the October 2023
checkpoint that sketched the grammar ("mostly just guessing my way along")
and never gained semantics; the grammar reference's one-line mention is the
only documentation it ever had. Remove the rule, the AST type and the doc
line, so `%word{...}` is an ordinary syntax error. No program could rely on
it, so no version gate is needed.

Regenerates the pigeon parser and the tree-sitter grammar.

Signed-off-by: Alex Suraci <[email protected]>
`9223372036854775808` (one past the largest Int) panicked the parser with
"rule ValueOnlyField: interface conversion: interface is nil": the Int action
returned a nil node along with ParseInt's error, and pigeon keeps the rule's
match, so the nil reached a rule asserting it to a Node. Strings whose escapes
encode no character (`"\777"`) panicked the same way, and out-of-range floats
(`1.0e309`) came out as a misleading "unexpected '1.0'".

These actions now abort the parse with "syntax error: integer literal out of
range" (or the float/string equivalent) pointing at the literal. They panic
with the error, which pigeon turns into a parse error, because an error an
action returns is lost when an enclosing left-recursive rule rolls back its
error list, leaving a parse that "succeeds". ParseWithRecovery renders the
error with its source ahead of tree-sitter's diagnosis, which sees nothing
wrong with these literals, in the non-cgo build as well.

`-9223372036854775808` is still an error: `-` is an operator applied to the
literal, so the minimum Int is written `-9223372036854775807 - 1`, as
test_unary_minus now shows.

Signed-off-by: Alex Suraci <[email protected]>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

Latest commit: da1cf8c
Status: ✅  Deploy successful!
Preview URL: https://2f9528d3.dang-3kk.pages.dev
Branch Preview URL: https://fix-parser-panics.dang-3kk.pages.dev

View logs

vito added 3 commits November 1, 2026 19:08
Declaring two block parameters, as in `pub f(&a: Int!, &b: Int!): Int! { 1 }`
or `new(&a: Int!, &b: Int!) { ... }`, made the action return a nil node with
its error, which crashed the enclosing rule with "interface conversion:
interface is nil, not dang.Node". Both actions now reject the parse like the
literals do, pointing at the second block parameter, so the literal-specific
helper becomes a general reject(loc, msg).

Signed-off-by: Alex Suraci <[email protected]>
The exponent-only Float form required at least two mantissa digits
(`NonZeroDecimalDigit DecimalDigit+`), so `10e5` parsed while `1e5` was a
syntax error. It now takes any number, like Int.

Signed-off-by: Alex Suraci <[email protected]>
The String action already rewrote `\/` to `/` before unquoting, but
ImmediateEscape never admitted `\/`, so `"a\/b"` was a syntax error reported
at the start of the statement. Admit it, as JSON does, and decode it in
unquoteString, which rewrites only escapes that really are `\/`: the old
bytes.Replace also turned the escaped backslash in `"a\\/b"` into a bogus
`\/`. It also leaves c.text alone, so the literal's location keeps its length.

An unknown escape (`"C:\Users"`) is now rejected at the escape itself by a new
InvalidEscape rule, rather than reported by tree-sitter at the start of the
form. Tree-sitter skips that rule, so editors don't highlight an invalid
escape as a valid one.

The `\/` cases are a Go test rather than a tests/*.dang file because the
formatter normalizes `\/` to `/`.

Closes #236

Signed-off-by: Alex Suraci <[email protected]>
vito added 2 commits November 1, 2026 22:13
pkg/dang/danglang compiles treesitter/src/parser.c and scanner.c by
#include from another directory, and the Go build cache doesn't track
such files: after a grammar regeneration, the CLI, the tests and the docs
build (which share a cache volume) could keep linking the old tree-sitter
parser, misplacing syntax errors.

The treesitter `go generate` now ends with a step that writes
pkg/dang/danglang/sources_digest.go, a constant holding a SHA-256 of the
included C sources. It is a Go file of the package, so a new parser.c
changes the package's cache key and forces a rebuild. Being generated, it
is covered by the treesitter generate/stale check: regenerating parser.c
without the stamp fails CI. This keeps the #include setup and adds no
build tooling, unlike copying or symlinking the sources into the package.

The editor-syntaxes skill's manual `go test -a` workaround is replaced by a
note on the stamp.

Signed-off-by: Alex Suraci <[email protected]>
A second block parameter was rejected only by the two field forms with a
body. An interface field (`twice(&a: Int!, &b: Int!): Int!`) and a block
parameter's own type (`&blk(&a: Int!, &b: Int!): Int!`) parsed, and
FunTypeNode kept only the last block parameter, silently dropping the
other. TypeAndArgsField and BlockParamDecl now reject it at the second
block parameter through a shared rejectSecondBlockParam.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito force-pushed the fix-parser-panics branch from c15bacb to da1cf8c Compare November 1, 2026 22:15
@vito
vito merged commit 42f87b2 into main Nov 1, 2026
9 checks passed
sipsma added a commit to grouville/dagger that referenced this pull request Nov 3, 2026
The engine pinned Guillaume's fork of Dang for vito/dang#211, which adds
PrepareDir and PreparedDirectory.Evaluate. vito/dang#250 carried that
change onto Dang main and merged, so Dang now moves to its merge commit
and the replace of the fork goes away.

Since the merge of vito/dang#213 that Dang was on before, Dang's type
checker became stricter (vito/dang#230 to #235, #246 and #249): programs
it accepted that could fail at runtime are now type errors, so a module
that loaded before may now fail to load. Among them, operators need
non-null operands, Map.get is typed as nullable (vito/dang#215), and a
decode result must be given a type before it is used. Function
parameters match by name, so a positional call through an interface
binds in the interface's order. YAML << merge keys are resolved when
decoding (vito/dang#217), and malformed syntax is reported instead of
panicking the parser (vito/dang#243). The release note describes what
module authors will see.

Signed-off-by: Erik Sipsma <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"\/" escape doesn't parse

1 participant