Skip to content

fix(lexer): validate rule names are valid Go identifiers - #468

Open
Pastalikek65 wants to merge 1 commit into
alecthomas:masterfrom
Pastalikek65:fix-202-rule-ident-validation
Open

fix(lexer): validate rule names are valid Go identifiers#468
Pastalikek65 wants to merge 1 commit into
alecthomas:masterfrom
Pastalikek65:fix-202-rule-ident-validation

Conversation

@Pastalikek65

Copy link
Copy Markdown
Contributor

Closes #202

Rule names are interpolated verbatim into generated codegen function names (match<Lexer><Rule>), so invalid identifiers such as match:, ;, | or @ produced uncompilable source (see #202 for the original report).

Rather than escaping names in codegen (which would silently rename tokens behind the parser's back), this validates at lexer.New construction time — the earliest point where a malformed name can be caught, and consistent with @alecthomas's assessment: "they are not valid rule names as only identifiers can be matched by the parser. This is really a validation error."

Changes:

  • lexer/stateful.go: reject non-identifier rule names in New() with a clear error (lexer: <state>.<idx>: invalid rule name ...)
  • lexer/stateful_test.go: table test covering invalid (match:, ;, 1two, has space, ünïcode-rune) and valid (Ident, identifier, _private, ünïcode) names

No behavior change for existing valid lexers; names that were never usable now fail fast instead of emitting broken codegen output.

Rule names are interpolated verbatim into generated codegen function
names (match<Lexer><Rule>), so invalid identifiers such as "match:"
or ";" produced uncompilable source. Reject them at lexer
construction time with a clear error instead. Fixes alecthomas#202.
@alecthomas

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 24b31bfadf

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread lexer/stateful.go
Comment on lines +255 to +256
if rule.Name != "" && !isValidIdent(rule.Name) {
return nil, fmt.Errorf("lexer: %s.%d: invalid rule name %q: must be a valid Go identifier", key, i, rule.Name)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve non-identifier names for runtime lexers

Restricting every rule passed to lexer.New breaks previously valid runtime lexers even when code generation and named grammar references are never used. For example, a lowercase rule named comment-line was accepted and automatically elided by StatefulLexer.Next, while an arbitrary named token can also be consumed through the lexer API or matched by its literal value; after this check, both New and NewSimple reject such definitions solely because the optional generator cannot interpolate the name. Apply this restriction in the generator (or encode generated function names) rather than changing the runtime lexer contract.

Useful? React with 👍 / 👎.

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.

bug: lexer codegen should validate token identifiers are valid

2 participants