There is a problem with escape regular expression meta characters in the macro code

Demo code (exam01.cr):

macro foo(name)
  /\{\{{{name.id}}\}\}/
end

foo "hello"

Expand macro:

crystal tool expand -c exam01.cr:5:1 exam01.cr

Output:

1 expansion found
expansion 1:
   foo("hello")

# expand macro 'foo' (/storage/code/learn-crystal/exam01.cr:1:1)
~> /{{hello\}\}/

You can see that there are two left “{” s that have not been escaped.

The issue is that \{{ is actually valid macro syntax in that it allows you to escape a macro expression. Should be able to just double escape the \.

macro foo(name)
  /\\{\\{{{name.id}}\}\}/
end

I don’t think that explanation fits here. The first opening brace should still be escaped because it can’t be a part of \{{.
This thing looks very suspicious and unexpected. But I haven’t looked deeper into it.

1 Like