Skip to content

Commit beb97a9

Browse files
committed
Fix issue #47: syntax error points to wrong position ...
Due to the way the parser was handing nested parts of the JSON5 grammar, we would incorrectly report errors as having happened at the start of a block of content, rather than where the error actually happened. A simple case of this would be reporting `"foo"bar` as happening at column 1 rather than column 5. The bug report (#47) has a more complicated and realistic example. To fix this we needed to rev the underlying parser generator (glop) to v0.6.4 and also rewrite the JSON5 grammar to use a positive lookahead.
1 parent 4c801bd commit beb97a9

3 files changed

Lines changed: 30 additions & 5 deletions

File tree

‎json5/json5.g‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ sp = ws*
44

55
ws = '\u0020' | eol | comment
66
| '\u0009' | '\u000B' | '\u000C' | '\u00A0' | '\uFEFF'
7-
| anything:x ?( is_unicat(x, 'Zs') ) -> x
7+
| ~~(anything:x ?( is_unicat(x, 'Zs') )) anything:x -> x
88

99
eol = '\u000D' '\u000A' | '\u000D' | '\u000A'
1010
| '\u2028' | '\u2029'

‎json5/parser.py‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,13 @@ def _bind(self, rule, var):
7070

7171
def _not(self, rule):
7272
p = self.pos
73+
errpos = self.errpos
7374
rule()
7475
if self.failed:
7576
self._succeed(None, p)
7677
else:
7778
self._rewind(p)
79+
self.errpos = errpos
7880
self._fail()
7981

8082
def _opt(self, rule):
@@ -100,8 +102,6 @@ def _star(self, rule, vs=None):
100102
rule()
101103
if self.failed:
102104
self._rewind(p)
103-
if p < self.errpos:
104-
self.errpos = p
105105
break
106106
else:
107107
vs.append(self.val)
@@ -200,11 +200,21 @@ def _ws__c7_(self):
200200

201201
def _ws__c8_(self):
202202
self._push('ws__c8')
203-
self._seq([lambda: self._bind(self._anything_, 'x'), self._ws__c8__s1_,
203+
self._seq([self._ws__c8__s0_, lambda: self._bind(self._anything_, 'x'),
204204
lambda: self._succeed(self._get('x'))])
205205
self._pop('ws__c8')
206206

207-
def _ws__c8__s1_(self):
207+
def _ws__c8__s0_(self):
208+
self._not(lambda: self._not(self._ws__c8__s0_n_n_))
209+
210+
def _ws__c8__s0_n_n_(self):
211+
(lambda: self._choose([self._ws__c8__s0_n_n_g__c0_]))()
212+
213+
def _ws__c8__s0_n_n_g__c0_(self):
214+
self._seq([lambda: self._bind(self._anything_, 'x'),
215+
self._ws__c8__s0_n_n_g__c0__s1_])
216+
217+
def _ws__c8__s0_n_n_g__c0__s1_(self):
208218
v = self._is_unicat(self._get('x'), 'Zs')
209219
if v:
210220
self._succeed(v)

‎tests/lib_test.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,21 @@ def test_whitespace(self):
252252
self.check(u'\u2028 1', 1)
253253
self.check(u'\u2029 1', 1)
254254

255+
def test_error_reporting(self):
256+
self.check_fail('[ ,]',
257+
err='<string>:1 Unexpected "," at column 3')
258+
259+
self.check_fail(
260+
'{\n'
261+
' version: "1.0",\n'
262+
' author: "John Smith",\n'
263+
' people : [\n'
264+
' "Monty",\n'
265+
' "Python"foo\n'
266+
' ]\n'
267+
'}\n',
268+
err='<string>:6 Unexpected "f" at column 17')
269+
255270

256271
class TestDump(unittest.TestCase):
257272
def test_basic(self):

0 commit comments

Comments
 (0)