forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlowParser.cpp
249 lines (205 loc) · 6.63 KB
/
FlowParser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#ifdef HERMES_USE_FLOWPARSER
#define DEBUG_TYPE "flowparser"
#include "hermes/FlowParser/FlowParser.h"
#include "hermes/AST/ASTBuilder.h"
#include "hermes/AST/ESTreeJSONDumper.h"
#include "hermes/Support/Conversions.h"
#include "llvh/Support/Debug.h"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
#include "flowparser/libflowparser.h"
#pragma GCC diagnostic pop
namespace hermes {
namespace parser {
using namespace hermes::parser;
using llvh::cast;
using llvh::dyn_cast;
using llvh::dyn_cast_or_null;
using llvh::isa;
namespace {
class JSONTranslator final
: public flowparser::AbstractTranslator<JSONValue *> {
public:
JSONTranslator(JSONFactory &factory) : factory_(factory) {}
bool getErrorEncountered() const {
return errorEncountered_;
}
virtual JSONValue *convert_string(char *str) {
return factory_.getString(str);
}
virtual JSONValue *convert_number(double n) {
return factory_.getNumber(n);
}
virtual JSONValue *convert_bool(long b) {
return factory_.getBoolean(b);
}
virtual JSONValue *convert_null() {
return factory_.getNull();
}
virtual JSONValue *convert_undefined() {
// TODO: this probably indicates an error. There is no "undefined" in JSON.
errorEncountered_ = true;
return factory_.getNull();
}
virtual JSONValue *convert_object(value props) {
llvh::SmallVector<JSONFactory::Prop, 4> propList;
while (flowparser::list_has_next(props)) {
value prop = flowparser::list_head(props);
propList.emplace_back(
factory_.getString(flowparser::get_prop_key(prop)),
convert_json(flowparser::get_prop_value(prop)));
props = flowparser::list_tail(props);
}
auto *res = factory_.newObject(propList.begin(), propList.end());
// In case of failure (caused by duplicate properties), set the error flag.
return res ? res : convert_undefined();
}
virtual JSONValue *convert_array(value items) {
llvh::SmallVector<JSONValue *, 4> storage;
while (flowparser::list_has_next(items)) {
value head = flowparser::list_head(items);
storage.push_back(convert_json(head));
items = flowparser::list_tail(items);
}
return factory_.newArray(storage.size(), storage.begin(), storage.end());
}
private:
JSONFactory &factory_;
/// Set to true if we encountered an error.
bool errorEncountered_{false};
};
#ifndef NDEBUG
void dump(llvh::raw_ostream &OS, const JSONValue *jsonValue, unsigned indent) {
if (!jsonValue) {
OS << "!!nullptr!!";
return;
}
switch (jsonValue->getKind()) {
case JSONKind::Object:
OS << "{\n";
indent += 4;
for (auto pair : *cast<JSONObject>(jsonValue)) {
OS.indent(indent);
dump(OS, pair.first, indent);
OS << ":";
dump(OS, pair.second, indent);
OS << ",\n";
}
indent -= 4;
OS.indent(indent) << "}";
break;
case JSONKind::Array:
OS << "[\n";
indent += 4;
for (auto *val : *cast<JSONArray>(jsonValue)) {
OS.indent(indent);
dump(OS, val, indent);
OS << ",\n";
}
indent -= 4;
OS.indent(indent) << "]";
break;
case JSONKind::String:
OS << "\"" << cast<JSONString>(jsonValue)->str() << "\"";
break;
case JSONKind::Number: {
char buf[NUMBER_TO_STRING_BUF_SIZE];
numberToString(cast<JSONNumber>(jsonValue)->getValue(), buf, sizeof(buf));
OS << buf;
break;
}
case JSONKind::Boolean:
OS << (cast<JSONBoolean>(jsonValue)->getValue() ? "true" : "false");
break;
case JSONKind::Null:
OS << "null";
break;
}
}
void dump(llvh::raw_ostream &OS, const JSONValue *jsonValue) {
dump(OS, jsonValue, 0);
OS << "\n";
}
#endif
/// Print all recorded errors.
/// \return true if there were no errors.
bool printErrors(Context &context, uint32_t bufferId, JSONValue *v) {
bool haveErrors = false;
auto *obj = dyn_cast<JSONObject>(v);
if (!obj)
return false;
auto *errors = dyn_cast_or_null<JSONArray>(obj->get("errors"));
if (!errors)
return false;
auto parseLoc = [&context, bufferId](JSONValue *v) -> SMLoc {
auto *obj = dyn_cast_or_null<JSONObject>(v);
if (!obj)
return {};
auto *line = dyn_cast_or_null<JSONNumber>(obj->get("line"));
if (!line)
return {};
auto *column = dyn_cast_or_null<JSONNumber>(obj->get("column"));
if (!column)
return {};
SourceErrorManager::SourceCoords coords{
bufferId, (unsigned)line->getValue(), (unsigned)column->getValue()};
return context.getSourceErrorManager().findSMLocFromCoords(coords);
};
haveErrors = errors->size() != 0;
for (auto *elem : *errors) {
auto *error = dyn_cast<JSONObject>(elem);
if (!error)
continue;
auto *loc = dyn_cast_or_null<JSONObject>(error->get("loc"));
if (!loc)
continue;
SMLoc start = parseLoc(loc->get("start"));
SMLoc end = parseLoc(loc->get("end"));
auto *message = dyn_cast_or_null<JSONString>(error->get("message"));
if (!message)
continue;
context.getSourceErrorManager().error({start, end}, message->str());
}
return !haveErrors;
}
} // anonymous namespace
llvh::Optional<ESTree::ProgramNode *> parseFlowParser(
Context &context,
uint32_t bufferId) {
static const bool initFlowParser = (flowparser::init(), true);
(void)initFlowParser;
JSONFactory factory{context.getAllocator(), &context.getStringTable()};
JSONTranslator tr{factory};
auto *buffer = context.getSourceErrorManager().getSourceBuffer(bufferId);
JSONValue *jsonValue = tr.parse(buffer->getBufferStart(), {});
if (!jsonValue || tr.getErrorEncountered()) {
context.getSourceErrorManager().error(
SMLoc::getFromPointer(buffer->getBufferStart()), "Error parsing");
return llvh::None;
}
LLVM_DEBUG(dump(llvh::dbgs(), jsonValue));
if (!printErrors(context, bufferId, jsonValue))
return llvh::None;
auto parsedRes = ESTree::buildAST(context, jsonValue, buffer);
if (!parsedRes)
return llvh::None;
ESTree::Node *parsed = *parsedRes;
if (!isa<ESTree::ProgramNode>(parsed)) {
context.getSourceErrorManager().error(
SMLoc::getFromPointer(buffer->getBufferStart()), "Unexpected AST node");
return llvh::None;
}
LLVM_DEBUG(hermes::dumpESTreeJSON(
llvh::dbgs(), parsed, true /* pretty */, ESTreeDumpMode::HideEmpty));
return cast<ESTree::ProgramNode>(parsed);
}
} // namespace parser
} // namespace hermes
#undef DEBUG_TYPE
#endif // HERMES_USE_FLOWPARSER