Match percent directives exactly - #801
Open
ydah wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
The change is small, targeted, and aligns with the described lexer-correctness fix, with a regression spec added.
Pull request overview
This PR tightens lrama’s lexer handling of percent directives so known directives are only matched when they end at a valid boundary, preventing accidental prefix-matches (e.g., %token matching the start of %tokens).
Changes:
- Update percent-directive lexing to reject matches when followed by another directive-name character.
- Add a spec ensuring unknown directives that start with a known directive now raise
Unexpected token.
File summaries
| File | Description |
|---|---|
| spec/lrama/lexer_spec.rb | Adds coverage for unknown directives that previously could be tokenized via prefix-matches. |
| lib/lrama/lexer.rb | Adds a trailing-boundary negative lookahead to percent-directive token scanning. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| when @scanner.scan(/#{SYMBOLS.join('|')}/) | ||
| return [@scanner.matched, Lrama::Lexer::Token::Token.new(s_value: @scanner.matched, location: location)] | ||
| when @scanner.scan(/#{PERCENT_TOKENS.join('|')}/) | ||
| when @scanner.scan(/(?:#{PERCENT_TOKENS.join('|')})(?![-.\w])/) |
| end | ||
|
|
||
| it 'does not match known percent directives as prefixes' do | ||
| ["%tokens FOO", "%typeof", "%expect-rr 2"].each do |text| |
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.
Known percent directives were matched without checking their trailing boundary. Unknown directives such as
%tokensand%typeofcould therefore be split into valid directives and identifiers, while%expect-rrwas only rejected after%expecthad already been consumed.Reject a percent directive match when it is followed by another directive-name character, including letters, digits, underscores, dots, or hyphens.