Repository navigation
Report parser errors instead of panicking - #243
Merged
Merged
Conversation
`%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]>
Deploying dang with
|
| 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 |
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]>
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
force-pushed
the
fix-parser-panics
branch
from
November 1, 2026 22:15
c15bacb to
da1cf8c
Compare
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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{...}literalsBefore:
After:
The
Quotedrule came from the October 2023 grammar sketch and never got semantics: it had noInfer, noEvaland 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:
After:
The same change covers out-of-range floats (
1.0e309, which used to give a misleadingunexpected '1.0') and strings with escapes that encode no character ("\777", which panicked the same way). These actions now panic with aliteralError, 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".ParseWithRecoveryshows 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.-9223372036854775808still gives the out-of-range error, because-is an operator applied to the literal.test_unary_minusnow shows the bounds:9223372036854775807and-9223372036854775807 - 1.Two block parameters
Before:
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 samereject(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!givesfunction can only have one block parameter, and a block parameter's own type&blk(&a: Int!, &b: Int!): Int!givesblock can only have one block parameter.1e5Before:
let a = 1e5gavesyntax error: unexpected '1', while10e5parsed, because the exponent-only form needed two mantissa digits. After:1e5is a float (seetest_float_literals).\/in stringsBefore:
print("a\/b")gavesyntax error: unexpected 'print("a\/b")'. After: it printsa/b.\/is an escape for/, as in JSON. The oldbytes.Replacein the String action also turned the escaped backslash in"a\\/b"into\/; a smallunquoteStringnow 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#includestreesitter/src/parser.cfrom 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 treesittergo generatestep now ends by writingpkg/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 as1e5and1.5e10: it readse5as an identifier because it treats the grammar's"e"as a keyword. This happens on main too. Fixing it in the generated grammar brokex.fieldparsing in other corpus tests, so it is left for a follow-up.