feat(grammar): reject nullable non-entry rules at definition time - #74
Open
theoephraim wants to merge 1 commit into
Open
feat(grammar): reject nullable non-entry rules at definition time#74theoephraim wants to merge 1 commit into
theoephraim wants to merge 1 commit into
Conversation
A rule never succeeds with an empty match: the longest-match loop keeps
an alternative only when it advanced (pos > bestPos). So a rule that can
derive the empty string has an unreachable empty case, and a reference
to it fails wherever it would have matched nothing. Written as a filler
rule that is a silent way to lose a whole parse:
Filler = rule(() => [[many(NL)]])
Stmt = rule(() => [[Filler, IDENT]]) // rejects a plain 'a'
defineGrammar now names the rule and the fix (inline the combinator at
the use site, or make the rule non-empty and wrap references in opt()).
The entry rule stays exempt: an empty document is handled by the driver.
YAML reaches emptiness through alternatives on purpose (key: with no
node, {a: }), so five of its rules are nullable by declaration; it sets
allowNullableRules: true, which changes no parse. The nullability
fixpoint moves out of analyzeGrammar into an exported
computeNullableRules so both consumers share one answer.
Gate: test/nullable-rules.ts.
theoephraim
force-pushed
the
feat/nullable-rule-diagnostic
branch
from
September 8, 2026 04:51
ce760bb to
a7a2278
Compare
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.
Reject nullable non-entry rules at definition time
A rule never succeeds with an empty match:
parseNonReckeeps an alternative only when it advanced (pos > bestPos), and the emitted engine mirrors that. So a rule that can derive the empty string has an unreachable empty case, and a reference to it fails wherever it would have matched nothing. Written as a "filler" rule, that is a silent way to lose a whole parse:Both engines reject
ahere (the interpreter throwsunexpected 'a', the total parser reports it as an error row), and nothing points atFiller. This is how our env-spec port lost a day.defineGrammarnow rejects a nullable non-entry rule with a message that names it and the fix: inline the combinator at the use site, or make the rule non-empty and wrap references inopt(...). The entry rule stays exempt (an empty document is handled by the driver), andentrymay still be omitted (the last rule is found the wayfindEntryRuledoes).YAML reaches emptiness through alternatives on purpose (
key:with no node,{a: }, an empty doc), soNode,FlowNode,FlowMapEntry,FlowSeqEntry, andAfterDocEndare nullable by declaration. It setsallowNullableRules: true, which changes no parse; the combinator-coverage fixture intest/tm-completeness.tsdoes the same (itsSepandNotsrules are nullable by construction). TypeScript, JavaScript, and HTML have no nullable non-entry rule.The nullability fixpoint moves out of
analyzeGrammarinto an exportedcomputeNullableRules(grammar)so both consumers share one answer;analyzeGrammar's result is unchanged.Open to flipping the default if you would rather this be opt-in (
rejectNullableRules: true); the error-by-default shape is what would have caught our case.Gate:
test/nullable-rules.ts(registered incore): the rejection and its message, the entry exemption (explicit and implicitentry), the inlined form parsing what the filler form rejected, the opt-out changing no parse in either engine, and the shipped grammars' status. README gets one paragraph under "Adding a language".npm run check: 50/50.