summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorptmcg <ptmcg@austin.rr.com>2021-08-14 16:29:14 -0500
committerptmcg <ptmcg@austin.rr.com>2021-08-14 16:29:14 -0500
commite5994cc0eac6979c00d8413d7e4037289dc01d08 (patch)
treee3b72b2a5319e2601723c96fe2ddd47b1405139c
parent4f832b5918d7fd33c18874f24e92c0ba669ff819 (diff)
downloadpyparsing-git-e5994cc0eac6979c00d8413d7e4037289dc01d08.tar.gz
Fix bug in Located class when used with a results name. (Issue #294)
-rw-r--r--CHANGES2
-rw-r--r--pyparsing/core.py7
-rw-r--r--tests/test_unit.py18
3 files changed, 26 insertions, 1 deletions
diff --git a/CHANGES b/CHANGES
index 036d82d..a0af0f0 100644
--- a/CHANGES
+++ b/CHANGES
@@ -26,6 +26,8 @@ Version 3.0.0c1 -
['START', 'relevant text ', 'END']
- _skipped: ['relevant text ']
+- Fixed bug in Located class when used with a results name. (Issue #294)
+
Version 3.0.0b3 - August, 2021
------------------------------
diff --git a/pyparsing/core.py b/pyparsing/core.py
index 86d43c3..8a29b5d 100644
--- a/pyparsing/core.py
+++ b/pyparsing/core.py
@@ -4181,7 +4181,11 @@ class Located(ParseElementEnhance):
ret_tokens["locn_start"] = start
ret_tokens["value"] = tokens
ret_tokens["locn_end"] = loc
- return loc, ret_tokens
+ if self.resultsName:
+ # must return as a list, so that the name will be attached to the complete group
+ return loc, [ret_tokens]
+ else:
+ return loc, ret_tokens
class NotAny(ParseElementEnhance):
@@ -4992,6 +4996,7 @@ class Suppress(TokenConverter):
(See also :class:`delimited_list`.)
"""
+
def __init__(self, expr, savelist=False):
if expr is ...:
expr = _PendingSkip(NoMatch())
diff --git a/tests/test_unit.py b/tests/test_unit.py
index 2d06605..8f5372a 100644
--- a/tests/test_unit.py
+++ b/tests/test_unit.py
@@ -4671,6 +4671,24 @@ class Test02_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase):
[28, ["ID", "PARI12345678"], 43],
{"locn_end": 43, "locn_start": 28, "value": {"id": "PARI12345678"}},
)
+ self.assertEqual("PARI12345678", res.value.id)
+
+ # if Located has a results name, handle appropriately
+ id_ref = pp.Located("ID" + pp.Word(pp.alphanums, exact=12)("id"))("loc")
+
+ res = id_ref.searchString(samplestr1)[0]
+ print(res.dump())
+ self.assertEqual(
+ "ID PARI12345678",
+ samplestr1[res.loc.locn_start : res.loc.locn_end],
+ "incorrect location calculation",
+ )
+ self.assertParseResultsEquals(
+ res.loc,
+ [28, ["ID", "PARI12345678"], 43],
+ {"locn_end": 43, "locn_start": 28, "value": {"id": "PARI12345678"}},
+ )
+ self.assertEqual("PARI12345678", res.loc.value.id)
wd = pp.Word(pp.alphas)
test_string = "ljsdf123lksdjjf123lkkjj1222"