Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support empty heredoc and fix catastrophic backtracking issue #117

Merged
merged 3 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions hcl2/hcl2.lark
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ tuple : "[" (new_line_or_comment? expression (new_line_or_comment? "," new_line_
object : "{" new_line_or_comment? (object_elem (new_line_and_or_comma object_elem )* new_line_and_or_comma?)? "}"
object_elem : (identifier | expression) ("=" | ":") expression

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

function_call : identifier "(" new_line_or_comment? arguments? new_line_or_comment? ")"
arguments : (expression (new_line_or_comment? "," new_line_or_comment? expression)* ("," | "...")? new_line_or_comment?)
Expand Down
13 changes: 7 additions & 6 deletions hcl2/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@

from lark.visitors import Transformer, Discard, _DiscardType

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

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

Expand Down Expand Up @@ -191,7 +189,9 @@ def heredoc_template(self, args: List) -> str:
match = HEREDOC_PATTERN.match(str(args[0]))
if not match:
raise RuntimeError(f"Invalid Heredoc token: {args[0]}")
return f'"{match.group(2)}"'

trim_chars = "\n\t "
return f'"{match.group(2).rstrip(trim_chars)}"'

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

text = match.group(2)
trim_chars = "\n\t "
text = match.group(2).rstrip(trim_chars)
lines = text.split("\n")

# calculate the min number of leading spaces in each line
Expand Down
1 change: 1 addition & 0 deletions test/helpers/terraform-config-json/empty-heredoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"bar": ""}
2 changes: 2 additions & 0 deletions test/helpers/terraform-config/empty-heredoc.hcl2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bar = <<EOF
EOF