diff --git a/spec/compiler/parser/parser_spec.cr b/spec/compiler/parser/parser_spec.cr index e0b21d4a3b2a..515195e4e498 100644 --- a/spec/compiler/parser/parser_spec.cr +++ b/spec/compiler/parser/parser_spec.cr @@ -1001,6 +1001,11 @@ module Crystal ) ) + it_parses "{{ foo.nil? }}", MacroExpression.new(Call.new(Var.new("foo"), "nil?")) + it_parses "{{ foo &.nil? }}", MacroExpression.new(Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Call.new(Var.new("__arg0"), "nil?")))) + it_parses "{{ foo.nil?(foo) }}", MacroExpression.new(Call.new(Var.new("foo"), "nil?", [Var.new("foo")] of ASTNode)) + it_parses "{{ nil?(foo) }}", MacroExpression.new(Call.new(nil, "nil?", [Var.new("foo")] of ASTNode)) + it_parses "foo.!", Not.new("foo".call) it_parses "foo.!.!", Not.new(Not.new("foo".call)) it_parses "foo.!( )", Not.new("foo".call) diff --git a/src/compiler/crystal/syntax/parser.cr b/src/compiler/crystal/syntax/parser.cr index 075ff20303f1..a45f2a8b6960 100644 --- a/src/compiler/crystal/syntax/parser.cr +++ b/src/compiler/crystal/syntax/parser.cr @@ -662,7 +662,7 @@ module Crystal atomic = parse_as?(atomic).at(location) elsif @token.value == :responds_to? atomic = parse_responds_to(atomic).at(location) - elsif @token.value == :nil? + elsif !@in_macro_expression && @token.value == :nil? atomic = parse_nil?(atomic).at(location) elsif @token.type == :"!" atomic = parse_negation_suffix(atomic).at(location) @@ -1541,7 +1541,7 @@ module Crystal elsif @token.value == :responds_to? call = parse_responds_to(obj).at(location) call = parse_atomic_method_suffix_special(call, location) - elsif @token.value == :nil? + elsif !@in_macro_expression && @token.value == :nil? call = parse_nil?(obj).at(location) call = parse_atomic_method_suffix_special(call, location) elsif @token.type == :"!" @@ -4059,8 +4059,10 @@ module Crystal obj = Var.new("self").at(location) return parse_responds_to(obj) when :nil? - obj = Var.new("self").at(location) - return parse_nil?(obj) + unless @in_macro_expression + obj = Var.new("self").at(location) + return parse_nil?(obj) + end else # Not a special call, go on end