From ff2a072201fd4ec3cf1783e1ab0ecec6d3e7cb94 Mon Sep 17 00:00:00 2001 From: Dirk Pranke Date: Sun, 14 May 2023 18:44:54 -0700 Subject: [PATCH] Fix GitHub issue #63 - Handle `+Infinity` properly. The code handled `Infinity` and `-Infinity` but not `+Infinity`. This change fixes that so all three are handled. --- json5/json5.g | 3 ++- json5/parser.py | 23 +++++++++++++---------- tests/lib_test.py | 1 + 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/json5/json5.g b/json5/json5.g index a33ccd2..2c5c05b 100644 --- a/json5/json5.g +++ b/json5/json5.g @@ -98,7 +98,8 @@ id_continue = ascii_id_start | '\u200D' num_literal = '-' num_literal:n -> '-' + n - | '+'? dec_literal:d ~id_start -> d + | '+' num_literal:n -> n + | dec_literal:d ~id_start -> d | hex_literal | 'Infinity' | 'NaN' diff --git a/json5/parser.py b/json5/parser.py index 86bc194..eb297fb 100644 --- a/json5/parser.py +++ b/json5/parser.py @@ -734,8 +734,8 @@ def _id_continue__c9_(self): def _num_literal_(self): self._choose([self._num_literal__c0_, self._num_literal__c1_, - self._hex_literal_, self._num_literal__c3_, - self._num_literal__c4_]) + self._num_literal__c2_, self._hex_literal_, + self._num_literal__c4_, self._num_literal__c5_]) def _num_literal__c0_(self): self._push('num_literal__c0') @@ -746,19 +746,22 @@ def _num_literal__c0_(self): def _num_literal__c1_(self): self._push('num_literal__c1') - self._seq([self._num_literal__c1__s0_, - lambda: self._bind(self._dec_literal_, 'd'), - lambda: self._not(self._id_start_), - lambda: self._succeed(self._get('d'))]) + self._seq([lambda: self._ch('+'), + lambda: self._bind(self._num_literal_, 'n'), + lambda: self._succeed(self._get('n'))]) self._pop('num_literal__c1') - def _num_literal__c1__s0_(self): - self._opt(lambda: self._ch('+')) + def _num_literal__c2_(self): + self._push('num_literal__c2') + self._seq([lambda: self._bind(self._dec_literal_, 'd'), + lambda: self._not(self._id_start_), + lambda: self._succeed(self._get('d'))]) + self._pop('num_literal__c2') - def _num_literal__c3_(self): + def _num_literal__c4_(self): self._str('Infinity') - def _num_literal__c4_(self): + def _num_literal__c5_(self): self._str('NaN') def _dec_literal_(self): diff --git a/tests/lib_test.py b/tests/lib_test.py index 68842e7..90e5f56 100644 --- a/tests/lib_test.py +++ b/tests/lib_test.py @@ -120,6 +120,7 @@ def test_numbers(self): # names self.check('Infinity', float('inf')) + self.check('+Infinity', float('inf')) self.check('-Infinity', float('-inf')) self.assertTrue(math.isnan(json5.loads('NaN'))) self.assertTrue(math.isnan(json5.loads('-NaN')))