Skip to content

Commit

Permalink
Fix \{{...}} syntax in macro inside comments (#12175)
Browse files Browse the repository at this point in the history
Co-authored-by: Johannes Müller <[email protected]>
asterite and straight-shoota authored Jul 4, 2022
1 parent 40ea0bb commit a37e5df
Showing 2 changed files with 36 additions and 0 deletions.
30 changes: 30 additions & 0 deletions spec/compiler/lexer/lexer_macro_spec.cr
Original file line number Diff line number Diff line change
@@ -412,6 +412,36 @@ describe "Lexer macro" do
token.type.should eq(t :MACRO_END)
end

it "lexes macro with curly escape in comment" do
lexer = Lexer.new("# good \\{{world}}\nend")

token = lexer.next_macro_token(Token::MacroState.default, false)
token.type.should eq(t :MACRO_LITERAL)
token.value.should eq("# good ")

token = lexer.next_macro_token(token.macro_state, false)
token.type.should eq(t :MACRO_LITERAL)
token.value.should eq("{")

token = lexer.next_macro_token(token.macro_state, false)
token.type.should eq(t :MACRO_LITERAL)
token.value.should eq("{world}}\n")

token = lexer.next_macro_token(token.macro_state, false)
token.type.should eq(t :MACRO_END)
end

it "lexes macro with slash not followed by curly" do
lexer = Lexer.new("# good \\a\nend")

token = lexer.next_macro_token(Token::MacroState.default, false)
token.type.should eq(t :MACRO_LITERAL)
token.value.should eq("# good \\a\n")

token = lexer.next_macro_token(token.macro_state, false)
token.type.should eq(t :MACRO_END)
end

it "lexes macro with if as suffix" do
lexer = Lexer.new("foo if bar end")

6 changes: 6 additions & 0 deletions src/compiler/crystal/syntax/lexer.cr
Original file line number Diff line number Diff line change
@@ -2012,6 +2012,12 @@ module Crystal
break
when '{'
break
when '\\'
if peek_next_char == '{'
break
else
char = next_char
end
when '\0'
raise "unterminated macro"
else

0 comments on commit a37e5df

Please sign in to comment.