Skip to content

Match percent directives exactly - #801

Open
ydah wants to merge 1 commit into
masterfrom
fix/percent-directive-boundaries
Open

Match percent directives exactly#801
ydah wants to merge 1 commit into
masterfrom
fix/percent-directive-boundaries

Conversation

@ydah

@ydah ydah commented Sep 4, 2026

Copy link
Copy Markdown
Member

Known percent directives were matched without checking their trailing boundary. Unknown directives such as %tokens and %typeof could therefore be split into valid directives and identifiers, while %expect-rr was only rejected after %expect had already been consumed.

Reject a percent directive match when it is followed by another directive-name character, including letters, digits, underscores, dots, or hyphens.

Copilot AI 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.

🟢 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.

Comment thread lib/lrama/lexer.rb
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])/)
Comment thread spec/lrama/lexer_spec.rb
end

it 'does not match known percent directives as prefixes' do
["%tokens FOO", "%typeof", "%expect-rr 2"].each do |text|
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.

2 participants