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

Correctly parse \A (and others) inside %r{...} inside macros #6282

Merged
merged 2 commits into from
Jun 29, 2018
Merged
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
6 changes: 6 additions & 0 deletions spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,12 @@ module Crystal

it_parses "foo.Bar", Call.new("foo".call, "Bar")

[{'(', ')'}, {'[', ']'}, {'<', '>'}, {'{', '}'}, {'|', '|'}].each do |open, close|
it_parses "{% begin %}%r#{open}\\A#{close}{% end %}", MacroIf.new(true.bool, MacroLiteral.new("%r#{open}\\A#{close}"))
it_parses "{% begin %}%#{open} %s #{close}{% end %}", MacroIf.new(true.bool, MacroLiteral.new("%#{open} %s #{close}"))
it_parses "{% begin %}%q#{open} %s #{close}{% end %}", MacroIf.new(true.bool, MacroLiteral.new("%q#{open} %s #{close}"))
end

assert_syntax_error "return do\nend", "unexpected token: do"

%w(def macro class struct module fun alias abstract include extend lib).each do |keyword|
Expand Down
9 changes: 7 additions & 2 deletions src/compiler/crystal/syntax/lexer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2127,9 +2127,14 @@ module Crystal

if !delimiter_state && current_char == '%' && ident_start?(peek_next_char)
char = next_char
if char == 'q' && (peek = peek_next_char) && {'(', '<', '[', '{'}.includes?(peek)
if char == 'q' && (peek = peek_next_char) && {'(', '<', '[', '{', '|'}.includes?(peek)
next_char
delimiter_state = Token::DelimiterState.new(:string, char, closing_char, 1)
next_char
elsif char == 'r' && (peek = peek_next_char) && {'(', '<', '[', '{', '|'}.includes?(peek)
next_char
delimiter_state = Token::DelimiterState.new(:regex, char, closing_char, 1)
next_char
else
start = current_pos
while ident_part?(char)
Expand Down Expand Up @@ -2196,7 +2201,7 @@ module Crystal
whitespace = false
when '%'
case char = peek_next_char
when '(', '[', '<', '{'
when '(', '[', '<', '{', '|'
next_char
delimiter_state = Token::DelimiterState.new(:string, char, closing_char, 1)
else
Expand Down