Skip to content

Commit

Permalink
Support more literals inside macros. (#9811)
Browse files Browse the repository at this point in the history
  • Loading branch information
toddsundsted authored Nov 24, 2020
1 parent 48ebe7b commit 43cac83
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
6 changes: 5 additions & 1 deletion spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1564,9 +1564,13 @@ 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}"))
it_parses "{% begin %}%Q#{open} %s #{close}{% end %}", MacroIf.new(true.bool, MacroLiteral.new("%Q#{open} %s #{close}"))
it_parses "{% begin %}%i#{open} %s #{close}{% end %}", MacroIf.new(true.bool, MacroLiteral.new("%i#{open} %s #{close}"))
it_parses "{% begin %}%w#{open} %s #{close}{% end %}", MacroIf.new(true.bool, MacroLiteral.new("%w#{open} %s #{close}"))
it_parses "{% begin %}%x#{open} %s #{close}{% end %}", MacroIf.new(true.bool, MacroLiteral.new("%x#{open} %s #{close}"))
it_parses "{% begin %}%r#{open}\\A#{close}{% end %}", MacroIf.new(true.bool, MacroLiteral.new("%r#{open}\\A#{close}"))
end

it_parses %(foo(bar:"a", baz:"b")), Call.new(nil, "foo", named_args: [NamedArgument.new("bar", "a".string), NamedArgument.new("baz", "b".string)])
Expand Down
16 changes: 16 additions & 0 deletions src/compiler/crystal/syntax/lexer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2323,6 +2323,22 @@ module Crystal
next_char
delimiter_state = Token::DelimiterState.new(:string, char, closing_char, 1)
next_char
elsif char == 'Q' && (peek = peek_next_char) && peek.in?('(', '<', '[', '{', '|')
next_char
delimiter_state = Token::DelimiterState.new(:string, char, closing_char, 1)
next_char
elsif char == 'i' && (peek = peek_next_char) && peek.in?('(', '<', '[', '{', '|')
next_char
delimiter_state = Token::DelimiterState.new(:symbol_array, char, closing_char, 1)
next_char
elsif char == 'w' && (peek = peek_next_char) && peek.in?('(', '<', '[', '{', '|')
next_char
delimiter_state = Token::DelimiterState.new(:string_array, char, closing_char, 1)
next_char
elsif char == 'x' && (peek = peek_next_char) && peek.in?('(', '<', '[', '{', '|')
next_char
delimiter_state = Token::DelimiterState.new(:command, char, closing_char, 1)
next_char
elsif char == 'r' && (peek = peek_next_char) && peek.in?('(', '<', '[', '{', '|')
next_char
delimiter_state = Token::DelimiterState.new(:regex, char, closing_char, 1)
Expand Down

0 comments on commit 43cac83

Please sign in to comment.