Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions json5/json5.g
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down
53 changes: 47 additions & 6 deletions json5/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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_,
]
Expand All @@ -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(
Expand Down
25 changes: 25 additions & 0 deletions tests/lib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down