Skip to content

Commit 75c3d8d

Browse files
use regex package instead of built-in re (#240)
1 parent e159a5a commit 75c3d8d

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

hcl2/reconstructor.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from lark.reconstruct import Reconstructor
1010
from lark.tree_matcher import is_discarded_terminal
1111
from lark.visitors import Transformer_InPlace
12+
from regex import regex
1213

1314
from hcl2.const import START_LINE_KEY, END_LINE_KEY
1415
from hcl2.parser import reconstruction_parser
@@ -439,14 +440,10 @@ def _build_string_rule(self, string: str, level: int = 0) -> Tree:
439440
#
440441
result = []
441442

442-
pattern = re.compile(r"(\${1,2}\{(?:[^{}]|\{[^{}]*})*})")
443-
parts = re.split(pattern, string)
443+
pattern = regex.compile(r"(\${1,2}\{(?:[^{}]|(?R))*\})")
444+
parts = [part for part in pattern.split(string) if part != ""]
444445
# e.g. 'aaa$${bbb}ccc${"ddd-${eee}"}' -> ['aaa', '$${bbb}', 'ccc', '${"ddd-${eee}"}']
445-
446-
if parts[-1] == "":
447-
parts.pop()
448-
if len(parts) > 0 and parts[0] == "":
449-
parts.pop(0)
446+
# 'aa-${"bb-${"cc-${"dd-${5 + 5}"}"}"}' -> ['aa-', '${"bb-${"cc-${"dd-${5 + 5}"}"}"}']
450447

451448
for part in parts:
452449
if part.startswith("$${") and part.endswith("}"):

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# Place dependencies in this file, following the distutils format:
22
# http://docs.python.org/2/distutils/setupscript.html#relationships-between-distributions-and-packages
33
lark>=1.1.5,<2.0
4+
regex>=2024.4.16

0 commit comments

Comments
 (0)