Skip to content

Commit

Permalink
Add location info to some MacroIf nodes (#14885)
Browse files Browse the repository at this point in the history
  • Loading branch information
Blacksmoke16 authored Aug 14, 2024
1 parent 76c6b2f commit 4f31010
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
90 changes: 90 additions & 0 deletions spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2606,6 +2606,96 @@ module Crystal
node.end_location.not_nil!.line_number.should eq(5)
end

it "sets correct locations of macro if / else" do
parser = Parser.new(<<-CR)
{% if 1 == val %}
"one!"
"bar"
{% else %}
"not one"
"bar"
{% end %}
CR

node = parser.parse.as MacroIf

location = node.cond.location.should_not be_nil
location.line_number.should eq 1
location = node.cond.end_location.should_not be_nil
location.line_number.should eq 1

location = node.then.location.should_not be_nil
location.line_number.should eq 1
location = node.then.end_location.should_not be_nil
location.line_number.should eq 4

location = node.else.location.should_not be_nil
location.line_number.should eq 4
location = node.else.end_location.should_not be_nil
location.line_number.should eq 7
end

it "sets correct locations of macro if / elsif" do
parser = Parser.new(<<-CR)
{% if 1 == val %}
"one!"
"bar"
{% elsif 2 == val %}
"not one"
"bar"
{% end %}
CR

node = parser.parse.as MacroIf

location = node.cond.location.should_not be_nil
location.line_number.should eq 1
location = node.cond.end_location.should_not be_nil
location.line_number.should eq 1

location = node.then.location.should_not be_nil
location.line_number.should eq 1
location = node.then.end_location.should_not be_nil
location.line_number.should eq 4

location = node.else.location.should_not be_nil
location.line_number.should eq 4
location = node.else.end_location.should_not be_nil
location.line_number.should eq 7
end

it "sets correct locations of macro if / else / elsif" do
parser = Parser.new(<<-CR)
{% if 1 == val %}
"one!"
"bar"
{% elsif 2 == val %}
"not one"
"bar"
{% else %}
"biz"
"blah"
{% end %}
CR

node = parser.parse.as MacroIf

location = node.cond.location.should_not be_nil
location.line_number.should eq 1
location = node.cond.end_location.should_not be_nil
location.line_number.should eq 1

location = node.then.location.should_not be_nil
location.line_number.should eq 1
location = node.then.end_location.should_not be_nil
location.line_number.should eq 4

location = node.else.location.should_not be_nil
location.line_number.should eq 4
location = node.else.end_location.should_not be_nil
location.line_number.should eq 10
end

it "sets correct location of trailing ensure" do
parser = Parser.new("foo ensure bar")
node = parser.parse.as(ExceptionHandler)
Expand Down
5 changes: 3 additions & 2 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3211,7 +3211,7 @@ module Crystal

case @token.type
when .macro_literal?
pieces << MacroLiteral.new(@token.value.to_s)
pieces << MacroLiteral.new(@token.value.to_s).at(@token.location).at_end(token_end_location)
when .macro_expression_start?
pieces << MacroExpression.new(parse_macro_expression)
check_macro_expression_end
Expand Down Expand Up @@ -3475,7 +3475,8 @@ module Crystal
end
when Keyword::ELSIF
unexpected_token if is_unless
a_else = parse_macro_if(start_location, macro_state, false)
start_loc = @token.location
a_else = parse_macro_if(start_location, macro_state, false).at(start_loc)

if check_end
check_ident :end
Expand Down

0 comments on commit 4f31010

Please sign in to comment.