v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
json-parser.cc
Go to the documentation of this file.
1// Copyright 2019 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7#include <cctype>
8#include <optional>
9
11
12namespace v8::internal::torque {
13
14template <>
16 ParseResultTypeId::kJsonValue;
17
18template <>
21 ParseResultTypeId::kJsonMember;
22
23template <>
26 ParseResultTypeId::kStdVectorOfJsonValue;
27
28template <>
31 ParseResultTypeId::kStdVectorOfJsonMember;
32
33namespace ls {
34
35using JsonMember = std::pair<std::string, JsonValue>;
36
37template <bool value>
38std::optional<ParseResult> MakeBoolLiteral(ParseResultIterator* child_results) {
39 return ParseResult{JsonValue::From(value)};
40}
41
42std::optional<ParseResult> MakeNullLiteral(ParseResultIterator* child_results) {
45 return ParseResult{std::move(result)};
46}
47
48std::optional<ParseResult> MakeNumberLiteral(
49 ParseResultIterator* child_results) {
50 auto number = child_results->NextAs<std::string>();
51 double d = std::stod(number.c_str());
53}
54
55std::optional<ParseResult> MakeStringLiteral(
56 ParseResultIterator* child_results) {
57 std::string literal = child_results->NextAs<std::string>();
59}
60
61std::optional<ParseResult> MakeArray(ParseResultIterator* child_results) {
62 JsonArray array = child_results->NextAs<JsonArray>();
63 return ParseResult{JsonValue::From(std::move(array))};
64}
65
66std::optional<ParseResult> MakeMember(ParseResultIterator* child_results) {
68 std::string key = child_results->NextAs<std::string>();
70 result.second = child_results->NextAs<JsonValue>();
71 return ParseResult{std::move(result)};
72}
73
74std::optional<ParseResult> MakeObject(ParseResultIterator* child_results) {
75 using MemberList = std::vector<JsonMember>;
76 MemberList members = child_results->NextAs<MemberList>();
77
78 JsonObject object;
79 for (auto& member : members) object.insert(std::move(member));
80
81 return ParseResult{JsonValue::From(std::move(object))};
82}
83
84class JsonGrammar : public Grammar {
86 while (MatchChar(std::isspace, pos)) {
87 }
88 return true;
89 }
90
92 InputPosition current = *pos;
93 if (MatchString("\"", &current)) {
94 while (
95 (MatchString("\\", &current) && MatchAnyChar(&current)) ||
96 MatchChar([](char c) { return c != '"' && c != '\n'; }, &current)) {
97 }
98 if (MatchString("\"", &current)) {
99 *pos = current;
100 return true;
101 }
102 }
103 current = *pos;
104 if (MatchString("'", &current)) {
105 while (
106 (MatchString("\\", &current) && MatchAnyChar(&current)) ||
107 MatchChar([](char c) { return c != '\'' && c != '\n'; }, &current)) {
108 }
109 if (MatchString("'", &current)) {
110 *pos = current;
111 return true;
112 }
113 }
114 return false;
115 }
116
118 InputPosition current = *pos;
119 MatchString("-", &current);
120 if (MatchString("0x", &current) && MatchChar(std::isxdigit, &current)) {
121 while (MatchChar(std::isxdigit, &current)) {
122 }
123 *pos = current;
124 return true;
125 }
126 return false;
127 }
128
130 InputPosition current = *pos;
131 bool found_digit = false;
132 MatchString("-", &current);
133 while (MatchChar(std::isdigit, &current)) found_digit = true;
134 MatchString(".", &current);
135 while (MatchChar(std::isdigit, &current)) found_digit = true;
136 if (!found_digit) return false;
137 *pos = current;
138 if ((MatchString("e", &current) || MatchString("E", &current)) &&
139 (MatchString("+", &current) || MatchString("-", &current) || true) &&
140 MatchChar(std::isdigit, &current)) {
141 while (MatchChar(std::isdigit, &current)) {
142 }
143 *pos = current;
144 return true;
145 }
146 return true;
147 }
148
149 public:
151
152 Symbol trueLiteral = {Rule({Token("true")})};
153 Symbol falseLiteral = {Rule({Token("false")})};
154 Symbol nullLiteral = {Rule({Token("null")})};
155
159
162
164 Symbol array = {Rule({Token("["), elementList, Token("]")})};
165
168 Symbol object = {Rule({Token("{"), memberList, Token("}")})};
169
177
178 Symbol file = {Rule({&value})};
179};
180
181JsonParserResult ParseJson(const std::string& input) {
182 // Torque needs a CurrentSourceFile scope during parsing.
183 // As JSON lives in memory only, an unknown file scope is created.
184 SourceFileMap::Scope source_map_scope("");
185 TorqueMessages::Scope messages_scope;
186 CurrentSourceFile::Scope unkown_file(SourceFileMap::AddSource("<json>"));
187
189 try {
190 result.value = (*JsonGrammar().Parse(input)).Cast<JsonValue>();
191 } catch (TorqueAbortCompilation&) {
192 CHECK(!TorqueMessages::Get().empty());
193 result.error = TorqueMessages::Get().front();
194 }
195 return result;
196}
197
198} // namespace ls
199} // namespace v8::internal::torque
SourcePosition pos
void SetWhitespace(PatternFunction ws)
Symbol * Pattern(PatternFunction pattern)
static std::optional< ParseResult > YieldMatchedInput(ParseResultIterator *child_results)
std::optional< ParseResult > Parse(const std::string &input)
Symbol * List(Symbol *element, std::optional< Symbol * > separator={})
static V8_EXPORT_PRIVATE bool MatchAnyChar(InputPosition *pos)
Symbol * Token(const std::string &s)
static V8_EXPORT_PRIVATE bool MatchString(const char *s, InputPosition *pos)
static V8_EXPORT_PRIVATE bool MatchChar(int(*char_class)(int), InputPosition *pos)
static SourceId AddSource(std::string path)
static bool MatchHexLiteral(InputPosition *pos)
static bool MatchWhitespace(InputPosition *pos)
static bool MatchDecimalLiteral(InputPosition *pos)
static bool MatchStringLiteral(InputPosition *pos)
LineAndColumn current
ZoneVector< RpoNumber > & result
FunctionLiteral * literal
Definition liveedit.cc:294
std::optional< ParseResult > MakeNumberLiteral(ParseResultIterator *child_results)
std::pair< std::string, JsonValue > JsonMember
std::optional< ParseResult > MakeNullLiteral(ParseResultIterator *child_results)
std::map< std::string, JsonValue > JsonObject
Definition json.h:22
std::optional< ParseResult > MakeObject(ParseResultIterator *child_results)
std::vector< JsonValue > JsonArray
Definition json.h:23
std::optional< ParseResult > MakeMember(ParseResultIterator *child_results)
std::optional< ParseResult > MakeBoolLiteral(ParseResultIterator *child_results)
std::optional< ParseResult > MakeArray(ParseResultIterator *child_results)
JsonParserResult ParseJson(const std::string &input)
std::optional< ParseResult > MakeStringLiteral(ParseResultIterator *child_results)
const char * InputPosition
std::string StringLiteralUnquote(const std::string &s)
Definition utils.cc:23
Tagged< To > Cast(Tagged< From > value, const v8::SourceLocation &loc=INIT_SOURCE_LOCATION_IN_DEBUG)
Definition casting.h:150
#define CHECK(condition)
Definition logging.h:124
#define V8_EXPORT_PRIVATE
Definition macros.h:460
enum v8::internal::torque::ls::JsonValue::@156 tag
static JsonValue From(double number)
Definition json.h:37