Skip to content

Commit 173c9d9

Browse files
committed
Fix error reporting for nested rules.
This modifies the fix in f259ff, which wasn't quite right. We were incorrectly rolling back the `errpos` field when rules failed, which would lead to errors being reported as having happened at the beginning of a rule, rather than at the spot where something actually happened. In debugging this, I realized that part of the problem is that error reporting for semantic predicate failures doesn't quite work right, so I've filed a new bug for that and updated the tests to be clearer. See dpranke/pyjson5#26 and #3 for more on this.
1 parent 7ff67f0 commit 173c9d9

2 files changed

Lines changed: 10 additions & 6 deletions

File tree

‎glop/compiler.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,6 @@ def _star(self, rule, vs=None):
184184
rule()
185185
if self.failed:
186186
self._rewind(p)
187-
if p < self.errpos:
188-
self.errpos = p
189187
break
190188
else:
191189
vs.append(self.val)

‎glop/tests/main_test.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,17 +281,23 @@ def test_error_positions(self):
281281
'abd', returncode=1)
282282
self.assertIn('Unexpected "d" at column 3', err)
283283

284-
# Check that we roll back the error position properly when
285-
# we partially match something inside a rule that fails.
284+
def test_weird_error_reporting_for_semantic_predicates(self):
285+
# You would think that you'd get 'Unexpected "2" at column 2 here.
286+
# You don't, because the parser consumes the 2 as part of `anything:x`
287+
# and there's no good way for the semantic predicate to report what
288+
# it is looking at, so the parser have to assume the next character
289+
# is what fails, rather than the character that is actually causing
290+
# the problem. See https://github.com/dpranke/glop/issues/3 for more
291+
# on this.
286292
_, _, err = self.check_match(
287293
"grammar = (anything:x ?( is_unicat(x, 'Ll')))* '\n' end -> 'ok'",
288294
'a23', returncode=1)
289-
self.assertIn('Unexpected "2" at column 2', err)
295+
self.assertIn('Unexpected "3" at column 3', err)
290296

291297
_, _, err = self.check_match(
292298
"grammar = (anything:x ?( is_unicat(x, 'Ll')))+ '\n' end -> 'ok'",
293299
'a23', returncode=1)
294-
self.assertIn('Unexpected "2" at column 2', err)
300+
self.assertIn('Unexpected "3" at column 3', err)
295301

296302

297303
if __name__ == '__main__':

0 commit comments

Comments
 (0)