Skip to content

Commit 59602e3

Browse files
authored
Merge pull request #117 from amplify-education/feature/empty-heredoc
Support empty heredoc and fix catastrophic backtracking issue
2 parents 3a20ffb + 6ff9cd9 commit 59602e3

4 files changed

Lines changed: 12 additions & 8 deletions

File tree

hcl2/hcl2.lark

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ tuple : "[" (new_line_or_comment? expression (new_line_or_comment? "," new_line_
5050
object : "{" new_line_or_comment? (object_elem (new_line_and_or_comma object_elem )* new_line_and_or_comma?)? "}"
5151
object_elem : (identifier | expression) ("=" | ":") expression
5252

53-
heredoc_template : /<<(?P<heredoc>[a-zA-Z][a-zA-Z0-9._-]+)\n(?:.|\n)+?\n+\s*(?P=heredoc)/
54-
heredoc_template_trim : /<<-(?P<heredoc_trim>[a-zA-Z][a-zA-Z0-9._-]+)\n(?:.|\n)+?\n+\s*(?P=heredoc_trim)/
53+
heredoc_template : /<<(?P<heredoc>[a-zA-Z][a-zA-Z0-9._-]+)\n(?:.|\n)*?(?P=heredoc)/
54+
heredoc_template_trim : /<<-(?P<heredoc_trim>[a-zA-Z][a-zA-Z0-9._-]+)\n(?:.|\n)*?(?P=heredoc_trim)/
5555

5656
function_call : identifier "(" new_line_or_comment? arguments? new_line_or_comment? ")"
5757
arguments : (expression (new_line_or_comment? "," new_line_or_comment? expression)* ("," | "...")? new_line_or_comment?)

hcl2/transformer.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66

77
from lark.visitors import Transformer, Discard, _DiscardType
88

9-
HEREDOC_PATTERN = re.compile(r"<<([a-zA-Z][a-zA-Z0-9._-]+)\n((.|\n)*?)\n\s*\1", re.S)
10-
HEREDOC_TRIM_PATTERN = re.compile(
11-
r"<<-([a-zA-Z][a-zA-Z0-9._-]+)\n((.|\n)*?)\n\s*\1", re.S
12-
)
9+
HEREDOC_PATTERN = re.compile(r"<<([a-zA-Z][a-zA-Z0-9._-]+)\n([\s\S]*)\1", re.S)
10+
HEREDOC_TRIM_PATTERN = re.compile(r"<<-([a-zA-Z][a-zA-Z0-9._-]+)\n([\s\S]*)\1", re.S)
1311

1412
Attribute = namedtuple("Attribute", ("key", "value"))
1513

@@ -191,7 +189,9 @@ def heredoc_template(self, args: List) -> str:
191189
match = HEREDOC_PATTERN.match(str(args[0]))
192190
if not match:
193191
raise RuntimeError(f"Invalid Heredoc token: {args[0]}")
194-
return f'"{match.group(2)}"'
192+
193+
trim_chars = "\n\t "
194+
return f'"{match.group(2).rstrip(trim_chars)}"'
195195

196196
def heredoc_template_trim(self, args: List) -> str:
197197
# See https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#template-expressions
@@ -202,7 +202,8 @@ def heredoc_template_trim(self, args: List) -> str:
202202
if not match:
203203
raise RuntimeError(f"Invalid Heredoc token: {args[0]}")
204204

205-
text = match.group(2)
205+
trim_chars = "\n\t "
206+
text = match.group(2).rstrip(trim_chars)
206207
lines = text.split("\n")
207208

208209
# calculate the min number of leading spaces in each line
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"bar": ""}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bar = <<EOF
2+
EOF

0 commit comments

Comments
 (0)