From 2e5dd91ee1419f5024bcb465e1febead739fed20 Mon Sep 17 00:00:00 2001 From: iefa m Date: Thu, 19 Nov 2026 21:53:32 +0530 Subject: [PATCH] require a token boundary after keyword, hex and Infinity/NaN literals Only dec_literal checked that no identifier character follows it, so with consume_trailing=False parse() returned None for 'nullable', True for 'truex', 10 for '0xAnull' and inf for 'Infinitytrue'. Add the same negative lookahead to the other literal alternatives and regenerate parser.py with glop. --- json5/json5.g | 12 +++++------ json5/parser.py | 53 +++++++++++++++++++++++++++++++++++++++++------ tests/lib_test.py | 25 ++++++++++++++++++++++ 3 files changed, 78 insertions(+), 12 deletions(-) diff --git a/json5/json5.g b/json5/json5.g index 2e00636..93799b8 100644 --- a/json5/json5.g +++ b/json5/json5.g @@ -16,9 +16,9 @@ eol = '\u000D' '\u000A' | '\u000D' | '\u000A' comment = '//' (~eol anything)* | '/*' (~'*/' anything)* '*/' -value = 'null' -> 'None' - | 'true' -> 'True' - | 'false' -> 'False' +value = 'null' ~id_continue -> 'None' + | 'true' ~id_continue -> 'True' + | 'false' ~id_continue -> 'False' | object:v -> ['object', v] | array:v -> ['array', v] | string:v -> ['string', v] @@ -122,9 +122,9 @@ num_literal = '-' unsigned_lit:n -> '-' + n | unsigned_lit:n -> n unsigned_lit = dec_literal:d ~id_start -> d - | hex_literal - | 'Infinity' - | 'NaN' + | hex_literal:h ~id_start -> h + | 'Infinity' ~id_continue -> 'Infinity' + | 'NaN' ~id_continue -> 'NaN' dec_literal = dec_int_lit:d frac:f exp:e -> d + f + e | dec_int_lit:d frac:f -> d + f diff --git a/json5/parser.py b/json5/parser.py index dd6275f..3946b77 100644 --- a/json5/parser.py +++ b/json5/parser.py @@ -339,13 +339,31 @@ def _value_(self): ) def _value__c0_(self): - self._seq([lambda: self._str('null'), lambda: self._succeed('None')]) + self._seq( + [ + lambda: self._str('null'), + lambda: self._not(self._id_continue_), + lambda: self._succeed('None'), + ] + ) def _value__c1_(self): - self._seq([lambda: self._str('true'), lambda: self._succeed('True')]) + self._seq( + [ + lambda: self._str('true'), + lambda: self._not(self._id_continue_), + lambda: self._succeed('True'), + ] + ) def _value__c2_(self): - self._seq([lambda: self._str('false'), lambda: self._succeed('False')]) + self._seq( + [ + lambda: self._str('false'), + lambda: self._not(self._id_continue_), + lambda: self._succeed('False'), + ] + ) def _value__c3_(self): self._push('value__c3') @@ -1442,7 +1460,7 @@ def _unsigned_lit_(self): self._choose( [ self._unsigned_lit__c0_, - self._hex_literal_, + self._unsigned_lit__c1_, self._unsigned_lit__c2_, self._unsigned_lit__c3_, ] @@ -1459,11 +1477,34 @@ def _unsigned_lit__c0_(self): ) self._pop('unsigned_lit__c0') + def _unsigned_lit__c1_(self): + self._push('unsigned_lit__c1') + self._seq( + [ + lambda: self._bind(self._hex_literal_, 'h'), + lambda: self._not(self._id_start_), + lambda: self._succeed(self._get('h')), + ] + ) + self._pop('unsigned_lit__c1') + def _unsigned_lit__c2_(self): - self._str('Infinity') + self._seq( + [ + lambda: self._str('Infinity'), + lambda: self._not(self._id_continue_), + lambda: self._succeed('Infinity'), + ] + ) def _unsigned_lit__c3_(self): - self._str('NaN') + self._seq( + [ + lambda: self._str('NaN'), + lambda: self._not(self._id_continue_), + lambda: self._succeed('NaN'), + ] + ) def _dec_literal_(self): self._choose( diff --git a/tests/lib_test.py b/tests/lib_test.py index fa85e7e..fa1b275 100644 --- a/tests/lib_test.py +++ b/tests/lib_test.py @@ -578,6 +578,31 @@ def test_parse(self): self.assertEqual(err, None) self.assertEqual(pos, 4) + def test_literal_must_end_at_token_boundary(self): + # 'nullable' is one identifier, not `null` followed by 'able', and + # a numeric literal can't be followed by an identifier character. + for s, pos in ( + ('nullable', 4), + ('truex', 4), + ('false0', 5), + ('Infinitytrue', 8), + ('-Infinityx', 9), + ('NaNNaN', 3), + ('0xAnull', 3), + ('0x1g', 3), + ): + with self.subTest(s=s): + val, err, actual_pos = json5.parse(s, consume_trailing=False) + self.assertIsNone(val) + self.assertIsNotNone(err) + self.assertEqual(pos, actual_pos) + + self.check('null,', None, err=None, pos=4, consume_trailing=False) + self.check( + 'Infinity//', float('inf'), err=None, pos=8, consume_trailing=False + ) + self.check('0x1F]', 31, err=None, pos=4, consume_trailing=False) + class TestDump(unittest.TestCase): def test_basic(self):