From d3ae522061292aa646ef916b8bcf4ec33d1b012c Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Fri, 7 Apr 2017 15:57:40 -0300 Subject: [PATCH] Fixed #4249: parse error with macros with %{} --- spec/compiler/parser/parser_spec.cr | 2 ++ src/compiler/crystal/syntax/lexer.cr | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/spec/compiler/parser/parser_spec.cr b/spec/compiler/parser/parser_spec.cr index 12fd3aa5109b..9dc222e56a25 100644 --- a/spec/compiler/parser/parser_spec.cr +++ b/spec/compiler/parser/parser_spec.cr @@ -756,6 +756,8 @@ describe "Parser" do it_parses "macro foo;bar{% begin %}body{% end %}baz;end", Macro.new("foo", [] of Arg, Expressions.from(["bar".macro_literal, MacroIf.new(true.bool, "body".macro_literal), "baz;".macro_literal] of ASTNode)) + it_parses "macro x\n%{}\nend", Macro.new("x", body: MacroLiteral.new("%{}\n")) + it_parses "def foo : Int32\n1\nend", Def.new("foo", body: 1.int32, return_type: "Int32".path) it_parses "def foo(x) : Int32\n1\nend", Def.new("foo", args: ["x".arg], body: 1.int32, return_type: "Int32".path) diff --git a/src/compiler/crystal/syntax/lexer.cr b/src/compiler/crystal/syntax/lexer.cr index 58654c75020f..3274e72572db 100644 --- a/src/compiler/crystal/syntax/lexer.cr +++ b/src/compiler/crystal/syntax/lexer.cr @@ -2118,7 +2118,12 @@ module Crystal break end when '}' - if @macro_curly_count > 0 + if delimiter_state && delimiter_state.end == '}' + delimiter_state = delimiter_state.with_open_count_delta(-1) + if delimiter_state.open_count == 0 + delimiter_state = nil + end + elsif @macro_curly_count > 0 # Once we find the final '}' that closes the interpolation, # we are back inside the delimiter if @macro_curly_count == 1