Skip to content

feat(grammar): reject nullable non-entry rules at definition time - #74

Open
theoephraim wants to merge 1 commit into
johnsoncodehk:masterfrom
dmno-dev:feat/nullable-rule-diagnostic
Open

feat(grammar): reject nullable non-entry rules at definition time#74
theoephraim wants to merge 1 commit into
johnsoncodehk:masterfrom
dmno-dev:feat/nullable-rule-diagnostic

Conversation

@theoephraim

@theoephraim theoephraim commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Reject nullable non-entry rules at definition time

A rule never succeeds with an empty match: parseNonRec keeps 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:

const Filler = rule(() => [[many(NL)]]);
const Stmt   = rule(() => [[Filler, IDENT]]);   // rejects a plain `a`; accepts `;a`

Both engines reject a here (the interpreter throws unexpected 'a', the total parser reports it as an error row), and nothing points at Filler. This is how our env-spec port lost a day.

defineGrammar now 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 in opt(...). The entry rule stays exempt (an empty document is handled by the driver), and entry may still be omitted (the last rule is found the way findEntryRule does).

YAML reaches emptiness through alternatives on purpose (key: with no node, {a: }, an empty doc), so Node, FlowNode, FlowMapEntry, FlowSeqEntry, and AfterDocEnd are nullable by declaration. It sets allowNullableRules: true, which changes no parse; the combinator-coverage fixture in test/tm-completeness.ts does the same (its Sep and Nots rules are nullable by construction). TypeScript, JavaScript, and HTML have no nullable non-entry rule.

The nullability fixpoint moves out of analyzeGrammar into an exported computeNullableRules(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 in core): the rejection and its message, the entry exemption (explicit and implicit entry), 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.

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
theoephraim force-pushed the feat/nullable-rule-diagnostic branch from ce760bb to a7a2278 Compare September 8, 2026 04:51
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.

1 participant