diff --git a/hcl2/reconstructor.py b/hcl2/reconstructor.py index 16fc843b..7f957d7b 100644 --- a/hcl2/reconstructor.py +++ b/hcl2/reconstructor.py @@ -9,6 +9,7 @@ from lark.reconstruct import Reconstructor from lark.tree_matcher import is_discarded_terminal from lark.visitors import Transformer_InPlace +from regex import regex from hcl2.const import START_LINE_KEY, END_LINE_KEY from hcl2.parser import reconstruction_parser @@ -439,14 +440,10 @@ def _build_string_rule(self, string: str, level: int = 0) -> Tree: # result = [] - pattern = re.compile(r"(\${1,2}\{(?:[^{}]|\{[^{}]*})*})") - parts = re.split(pattern, string) + pattern = regex.compile(r"(\${1,2}\{(?:[^{}]|(?R))*\})") + parts = [part for part in pattern.split(string) if part != ""] # e.g. 'aaa$${bbb}ccc${"ddd-${eee}"}' -> ['aaa', '$${bbb}', 'ccc', '${"ddd-${eee}"}'] - - if parts[-1] == "": - parts.pop() - if len(parts) > 0 and parts[0] == "": - parts.pop(0) + # 'aa-${"bb-${"cc-${"dd-${5 + 5}"}"}"}' -> ['aa-', '${"bb-${"cc-${"dd-${5 + 5}"}"}"}'] for part in parts: if part.startswith("$${") and part.endswith("}"): diff --git a/requirements.txt b/requirements.txt index e0c904a3..3b25f45c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ # Place dependencies in this file, following the distutils format: # http://docs.python.org/2/distutils/setupscript.html#relationships-between-distributions-and-packages lark>=1.1.5,<2.0 +regex>=2024.4.16