Skip to content

Commit

Permalink
🐛 Closing brackets and escaping problem
Browse files Browse the repository at this point in the history
  • Loading branch information
Aunsiels committed Mar 18, 2024
1 parent 5b47a85 commit eca9a78
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
11 changes: 3 additions & 8 deletions pyformlang/regular_expression/python_regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,11 @@ def _preprocess_brackets(self):
in_brackets = 0
in_brackets_temp = []
for symbol in self._python_regex:
if symbol == "[" and (not regex_temp or regex_temp[-1] != "\\") and \
(in_brackets == 0 or not in_brackets_temp[-1] or in_brackets_temp[-1][-1] != "\\"):
if symbol == "[" and not self._should_escape_next_symbol(regex_temp) and \
(in_brackets == 0 or not self._should_escape_next_symbol(in_brackets_temp[-1])):
in_brackets += 1
in_brackets_temp.append([])
elif symbol == "]" and (not regex_temp or regex_temp[-1] != "\\") and \
(in_brackets == 0 or not in_brackets_temp[-1] or
(in_brackets_temp[-1][-1] != "\\" or
(len(in_brackets_temp[-1]) > 1 and in_brackets_temp[-1][-2] == "\\"))):
elif symbol == "]" and in_brackets >= 1 and not self._should_escape_next_symbol(in_brackets_temp[-1]):
if len(in_brackets_temp) == 1:
regex_temp.append("(")
regex_temp += self._preprocess_brackets_content(
Expand Down Expand Up @@ -358,7 +355,6 @@ def _add_repetition(self, regex_list):

def _preprocess_optional(self):
regex_temp = []
print(self._python_regex)
for symbol in self._python_regex:
if symbol == "?":
if regex_temp[-1] == ")":
Expand All @@ -372,7 +368,6 @@ def _preprocess_optional(self):
regex_temp[-1] += symbol
else:
regex_temp.append(symbol)
print(regex_temp)
self._python_regex = "".join(regex_temp)

@staticmethod
Expand Down
4 changes: 3 additions & 1 deletion pyformlang/regular_expression/tests/test_python_regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,6 @@ def test_range_repetition(self):
self._test_compare(r"[a-z]{1,3}", "dpoz")

def test_error_backslash(self):
self._test_compare(r"\"([^\"\\\\]|\\\\.)*\"", '"ddd"')
self._test_compare(r"[a\\\\\\]]", "\\]")
self._test_compare(r"\"([d\"\\\\]|\\\\.)*\"", '"d\\"')
self._test_compare(r"[a\\\\]", "a")

0 comments on commit eca9a78

Please sign in to comment.