Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test for mid action rule variables #209

Merged
merged 1 commit into from
Nov 8, 2023
Merged
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
37 changes: 36 additions & 1 deletion spec/lrama/grammar/rule_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,41 @@
end
end

context "variables in mid action rule refer to correct name and correct position" do
let(:location) { Lrama::Lexer::Location.new(first_line: 1, first_column: 0, last_line: 1, last_column: 4) }
let(:token_1) { Lrama::Lexer::Token::Ident.new(s_value: "class", location: location) }
let(:token_2) { Lrama::Lexer::Token::Ident.new(s_value: "keyword_class", location: location) }
let(:token_3) { Lrama::Lexer::Token::Ident.new(s_value: "tSTRING", location: location) }
let(:token_4) { Lrama::Lexer::Token::UserCode.new(s_value: "$$ = $keyword_class + $2; @$ = @keyword_class + @2;", location: location) }
let(:token_5) { Lrama::Lexer::Token::Ident.new(s_value: "keyword_end", location: location) }

it "raises error" do
# class : keyword_class tSTRING { $$ = $keyword_class + $2; @$ = @keyword_class + @2; } keyword_end
rule_builder.lhs = token_1
rule_builder.add_rhs(token_2)
rule_builder.add_rhs(token_3)
rule_builder.user_code = token_4
rule_builder.add_rhs(token_5)
rule_builder.freeze_rhs

rule_builder.preprocess_references

expect(token_4.references.count).to eq 6
expect(token_4.references[0].type).to eq :dollar
expect(token_4.references[0].value).to eq '$'
expect(token_4.references[1].type).to eq :dollar
expect(token_4.references[1].value).to eq 1
expect(token_4.references[2].type).to eq :dollar
expect(token_4.references[2].value).to eq 2
expect(token_4.references[3].type).to eq :at
expect(token_4.references[3].value).to eq '$'
expect(token_4.references[4].type).to eq :at
expect(token_4.references[4].value).to eq 1
expect(token_4.references[5].type).to eq :at
expect(token_4.references[5].value).to eq 2
end
end

context "variables refer to wrong position" do
let(:location) { Lrama::Lexer::Location.new(first_line: 1, first_column: 0, last_line: 1, last_column: 4) }
let(:token_1) { Lrama::Lexer::Token::Ident.new(s_value: "class", location: location) }
Expand All @@ -150,7 +185,7 @@
end
end

context "variables in mid rule refer to following component" do
context "variables in mid action rule refer to following component" do
let(:location) { Lrama::Lexer::Location.new(first_line: 1, first_column: 0, last_line: 1, last_column: 4) }
let(:token_1) { Lrama::Lexer::Token::Ident.new(s_value: "class", location: location) }
let(:token_2) { Lrama::Lexer::Token::Ident.new(s_value: "keyword_class", location: location) }
Expand Down