Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/lrama/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class Lexer
%start
).freeze #: Array[String]
IDENTIFIER_PATTERN = /[a-zA-Z_.][-a-zA-Z0-9_.]*/.freeze #: Regexp
TAG_PATTERN = /<[a-zA-Z_][a-zA-Z0-9_]*(?:(?:\.|->)[a-zA-Z_][a-zA-Z0-9_]*)*>/.freeze #: Regexp

# @rbs (GrammarFile grammar_file) -> void
def initialize(grammar_file)
Expand Down Expand Up @@ -126,7 +127,7 @@ def lex_token
return [@scanner.matched, Lrama::Lexer::Token::Token.new(s_value: @scanner.matched, location: location)]
when @scanner.scan(/[\?\+\*]/)
return [@scanner.matched, Lrama::Lexer::Token::Token.new(s_value: @scanner.matched, location: location)]
when @scanner.scan(/<\w+>/)
when @scanner.scan(TAG_PATTERN)
return [:TAG, Lrama::Lexer::Token::Tag.new(s_value: @scanner.matched, location: location)]
when @scanner.scan(/'.'/)
return [:CHARACTER, Lrama::Lexer::Token::Char.new(s_value: @scanner.matched, location: location)]
Expand Down
2 changes: 1 addition & 1 deletion lib/lrama/lexer/token/user_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def scan_reference(scanner)
if scanner.scan(/
# $ references
# It need to wrap an identifier with brackets to use ".-" for identifiers
\$(<[a-zA-Z0-9_]+>)?(?:
\$(#{Lrama::Lexer::TAG_PATTERN})?(?:
(\$) # $$, $<long>$
| (\d+) # $1, $2, $<long>1
| ([a-zA-Z_][a-zA-Z0-9_]*) # $foo, $expr, $<long>program (named reference without brackets)
Expand Down
2 changes: 2 additions & 0 deletions sig/generated/lrama/lexer.rbs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions spec/lrama/grammar/code_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
int rule5;
int rule6;
int rule7;
struct { int integer; } value;
}

%token <i> keyword_class
Expand Down Expand Up @@ -209,7 +210,7 @@
rule4: expr '+' expr[expr-right] { @1 + @[expr-right]; @0; }
;

rule5: expr '+' expr { $1 + $<integer>3; }
rule5: expr '+' expr { $1 + $<value.integer>3; }
;

rule6: expr '+' { $<integer>$ = $1; @$ = @1; } expr { $1 + $<integer>4; }
Expand Down Expand Up @@ -268,7 +269,7 @@

it "respects explicit tag in a rule" do
code = grammar.rules.find {|r| r.lhs.id.s_value == "rule5" }
expect(code.translated_code(grammar)).to eq(" (yyvsp[-2].expr) + (yyvsp[0].integer); ")
expect(code.translated_code(grammar)).to eq(" (yyvsp[-2].expr) + (yyvsp[0].value.integer); ")
end

context "midrule action exists" do
Expand Down
3 changes: 3 additions & 0 deletions spec/lrama/lexer/token/user_code_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
references = Lrama::Lexer::Token::UserCode.new(s_value: " $<long>1 ", location: location).references
expect(references.count).to eq 1
expect(references[0]).to eq Lrama::Grammar::Reference.new(type: :dollar, number: 1, index: 1, ex_tag: Lrama::Lexer::Token::Tag.new(s_value: "<long>"), first_column: 1, last_column: 9)
references = Lrama::Lexer::Token::UserCode.new(s_value: " $<value.integer>1 ", location: location).references
expect(references.count).to eq 1
expect(references[0]).to eq Lrama::Grammar::Reference.new(type: :dollar, number: 1, index: 1, ex_tag: Lrama::Lexer::Token::Tag.new(s_value: "<value.integer>"), first_column: 1, last_column: 18)

# $foo
references = Lrama::Lexer::Token::UserCode.new(s_value: " $foo ", location: location).references
Expand Down
19 changes: 19 additions & 0 deletions spec/lrama/lexer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,25 @@
expect(lexer.next_token).to eq([':', token_class::Token.new(s_value: ':')])
end
end

it 'lexes structured semantic value tags' do
tags = ["<value>", "<value.integer>", "<value.pointer->integer>"]
grammar_file = Lrama::Lexer::GrammarFile.new("tags.y", tags.join(" "))
lexer = Lrama::Lexer.new(grammar_file)

tags.each do |tag|
expect(lexer.next_token).to eq([:TAG, token_class::Tag.new(s_value: tag)])
end
end

it 'rejects unsupported type and default tags' do
["<int *>", "<std::vector<int>>", "<*>", "<>"].each do |tag|
grammar_file = Lrama::Lexer::GrammarFile.new("tags.y", tag)
lexer = Lrama::Lexer.new(grammar_file)

expect { lexer.next_token }.to raise_error(ParseError, /Unexpected token/)
end
end
end

context 'unexpected_token.y' do
Expand Down