Skip to content

Commit

Permalink
Fix #355 - needed re.escape, not escape_regex_range_chars in QuotedSt…
Browse files Browse the repository at this point in the history
…ring
  • Loading branch information
ptmcg committed Jan 15, 2022
1 parent 1ccf846 commit 5fb6e0d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGES
Expand Up @@ -14,6 +14,10 @@ Version 3.0.7 -
- Fixed bug #350, in which White expressions could fail to match due to
unintended whitespace-skipping. Reported by Fu Hanxi, thank you!

- Fixed bug #355, when a QuotedString is defined with characters in its
quoteChar string containing regex-significant characters such as ., *,
?, [, ], etc.

- Fixed bug in ParserElement.run_tests where comments would be displayed
using with_line_numbers.

Expand Down
2 changes: 1 addition & 1 deletion pyparsing/__init__.py
Expand Up @@ -126,7 +126,7 @@ def __repr__(self):


__version_info__ = version_info(3, 0, 7, "final", 0)
__version_time__ = "02 Jan 2022 22:56 UTC"
__version_time__ = "15 Jan 2022 04:10 UTC"
__version__ = __version_info__.__version__
__versionTime__ = __version_time__
__author__ = "Paul McGuire <ptmcg.gm+pyparsing@gmail.com>"
Expand Down
2 changes: 1 addition & 1 deletion pyparsing/core.py
Expand Up @@ -3131,7 +3131,7 @@ def __init__(
+ "|".join(
"(?:{}(?!{}))".format(
re.escape(self.endQuoteChar[:i]),
_escape_regex_range_chars(self.endQuoteChar[i:]),
re.escape(self.endQuoteChar[i:]),
)
for i in range(len(self.endQuoteChar) - 1, 0, -1)
)
Expand Down
23 changes: 23 additions & 0 deletions tests/test_unit.py
Expand Up @@ -1807,6 +1807,29 @@ def test(label, quoteExpr, expected):
with self.assertRaises(ValueError):
pp.QuotedString("", "\\")

def testCustomQuotes2(self):

qs = pp.QuotedString(quote_char=".[", end_quote_char="].")
print(qs.reString)
self.assertParseAndCheckList(qs, ".[...].", ['...'])
self.assertParseAndCheckList(qs, ".[].", [''])
self.assertParseAndCheckList(qs, ".[]].", [']'])
self.assertParseAndCheckList(qs, ".[]]].", [']]'])

qs = pp.QuotedString(quote_char="+*", end_quote_char="*+")
print(qs.reString)
self.assertParseAndCheckList(qs, "+*...*+", ['...'])
self.assertParseAndCheckList(qs, "+**+", [''])
self.assertParseAndCheckList(qs, "+***+", ['*'])
self.assertParseAndCheckList(qs, "+****+", ['**'])

qs = pp.QuotedString(quote_char="*/", end_quote_char="/*")
print(qs.reString)
self.assertParseAndCheckList(qs, "*/.../*", ['...'])
self.assertParseAndCheckList(qs, "*//*", [''])
self.assertParseAndCheckList(qs, "*///*", ['/'])
self.assertParseAndCheckList(qs, "*////*", ['//'])

def testRepeater(self):
if ParserElement._packratEnabled or ParserElement._left_recursion_enabled:
print("skipping this test, not compatible with memoization")
Expand Down

0 comments on commit 5fb6e0d

Please sign in to comment.