From 4504994014df3bc10da661d7a34092d6f4a4f2b5 Mon Sep 17 00:00:00 2001 From: Mykola Bilochub Date: Sat, 17 Jun 2017 02:54:34 +0300 Subject: [PATCH] parser: allow numeric literals as object keys Refs: https://github.com/metarhia/jstp/issues/152 PR-URL: https://github.com/metarhia/jstp/pull/223 Reviewed-By: Denys Otrishko Reviewed-By: Alexey Orlenko Reviewed-By: Dmytro Nechai --- src/jsrs_parser.cc | 15 +++++++++++---- .../json5/objects/numeric-literal-key.json5 | 3 +++ 2 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/json5/objects/numeric-literal-key.json5 diff --git a/src/jsrs_parser.cc b/src/jsrs_parser.cc index baa24e2e..d10255eb 100644 --- a/src/jsrs_parser.cc +++ b/src/jsrs_parser.cc @@ -721,6 +721,7 @@ MaybeLocal ParseObject(Isolate* isolate, bool key_mode = true; *size = end - begin; MaybeLocal current_key; + MaybeLocal current_numeric_key; MaybeLocal current_value; size_t current_length = 0; auto result = Object::New(isolate); @@ -734,10 +735,16 @@ MaybeLocal ParseObject(Isolate* isolate, has_ended = true; break; } - current_key = ParseKeyInObject(isolate, - begin + i, - end, - ¤t_length); + if (!isdigit(begin[i])) { + current_key = ParseKeyInObject(isolate, begin + i, end, + ¤t_length); + } else { + current_numeric_key = ParseNumber(isolate, begin + i, end, + ¤t_length); + current_key = current_numeric_key.IsEmpty() ? MaybeLocal() : + current_numeric_key.ToLocalChecked()->ToString( + isolate->GetCurrentContext()); + } if (current_key.IsEmpty()) { return MaybeLocal(); } diff --git a/test/fixtures/json5/objects/numeric-literal-key.json5 b/test/fixtures/json5/objects/numeric-literal-key.json5 new file mode 100644 index 00000000..8829b72d --- /dev/null +++ b/test/fixtures/json5/objects/numeric-literal-key.json5 @@ -0,0 +1,3 @@ +{ + 42: true +}