blob: e0b767f21f9290eb0a95406b705cad4bfa4c070a [file] [log] [blame]
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -08001/*
2 * Copyright 2014 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <algorithm>
18
19#include "flatbuffers/flatbuffers.h"
20#include "flatbuffers/idl.h"
21#include "flatbuffers/util.h"
22
23namespace flatbuffers {
24
25const char *const kTypeNames[] = {
rw74d5f372014-07-11 16:12:35 -070026 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) IDLTYPE,
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -080027 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
28 #undef FLATBUFFERS_TD
29 nullptr
30};
31
32const char kTypeSizes[] = {
rw74d5f372014-07-11 16:12:35 -070033 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) sizeof(CTYPE),
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -080034 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
35 #undef FLATBUFFERS_TD
36};
37
38static void Error(const std::string &msg) {
39 throw msg;
40}
41
42// Ensure that integer values we parse fit inside the declared integer type.
43static void CheckBitsFit(int64_t val, size_t bits) {
44 auto mask = (1ll << bits) - 1; // Bits we allow to be used.
45 if (bits < 64 &&
46 (val & ~mask) != 0 && // Positive or unsigned.
47 (val | mask) != -1) // Negative.
48 Error("constant does not fit in a " + NumToString(bits) + "-bit field");
49}
50
51// atot: templated version of atoi/atof: convert a string to an instance of T.
52template<typename T> inline T atot(const char *s) {
53 auto val = StringToInt(s);
54 CheckBitsFit(val, sizeof(T) * 8);
55 return (T)val;
56}
57template<> inline bool atot<bool>(const char *s) {
58 return 0 != atoi(s);
59}
60template<> inline float atot<float>(const char *s) {
61 return static_cast<float>(strtod(s, nullptr));
62}
63template<> inline double atot<double>(const char *s) {
64 return strtod(s, nullptr);
65}
66
67template<> inline Offset<void> atot<Offset<void>>(const char *s) {
68 return Offset<void>(atoi(s));
69}
70
71// Declare tokens we'll use. Single character tokens are represented by their
72// ascii character code (e.g. '{'), others above 256.
73#define FLATBUFFERS_GEN_TOKENS(TD) \
74 TD(Eof, 256, "end of file") \
75 TD(StringConstant, 257, "string constant") \
76 TD(IntegerConstant, 258, "integer constant") \
77 TD(FloatConstant, 259, "float constant") \
78 TD(Identifier, 260, "identifier") \
79 TD(Table, 261, "table") \
80 TD(Struct, 262, "struct") \
81 TD(Enum, 263, "enum") \
82 TD(Union, 264, "union") \
83 TD(NameSpace, 265, "namespace") \
84 TD(RootType, 266, "root_type")
85enum {
Wouter van Oortmerssen75349ae2014-07-09 11:44:26 -070086 #define FLATBUFFERS_TOKEN(NAME, VALUE, STRING) kToken ## NAME = VALUE,
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -080087 FLATBUFFERS_GEN_TOKENS(FLATBUFFERS_TOKEN)
88 #undef FLATBUFFERS_TOKEN
rw74d5f372014-07-11 16:12:35 -070089 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) kToken ## ENUM,
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -080090 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
91 #undef FLATBUFFERS_TD
92};
93
94static std::string TokenToString(int t) {
95 static const char *tokens[] = {
96 #define FLATBUFFERS_TOKEN(NAME, VALUE, STRING) STRING,
97 FLATBUFFERS_GEN_TOKENS(FLATBUFFERS_TOKEN)
98 #undef FLATBUFFERS_TOKEN
rw74d5f372014-07-11 16:12:35 -070099 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) IDLTYPE,
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -0800100 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
101 #undef FLATBUFFERS_TD
102 };
103 if (t < 256) { // A single ascii char token.
104 std::string s;
105 s.append(1, t);
106 return s;
107 } else { // Other tokens.
108 return tokens[t - 256];
109 }
110}
111
112void Parser::Next() {
113 doc_comment_.clear();
114 bool seen_newline = false;
115 for (;;) {
116 char c = *cursor_++;
117 token_ = c;
118 switch (c) {
119 case '\0': cursor_--; token_ = kTokenEof; return;
120 case ' ': case '\r': case '\t': break;
121 case '\n': line_++; seen_newline = true; break;
122 case '{': case '}': case '(': case ')': case '[': case ']': return;
123 case ',': case ':': case ';': case '=': return;
124 case '.':
125 if(!isdigit(*cursor_)) return;
126 Error("floating point constant can\'t start with \".\"");
127 break;
128 case '\"':
129 attribute_ = "";
130 while (*cursor_ != '\"') {
131 if (*cursor_ < ' ' && *cursor_ >= 0)
132 Error("illegal character in string constant");
133 if (*cursor_ == '\\') {
134 cursor_++;
135 switch (*cursor_) {
136 case 'n': attribute_ += '\n'; cursor_++; break;
137 case 't': attribute_ += '\t'; cursor_++; break;
138 case 'r': attribute_ += '\r'; cursor_++; break;
139 case '\"': attribute_ += '\"'; cursor_++; break;
140 case '\\': attribute_ += '\\'; cursor_++; break;
141 default: Error("unknown escape code in string constant"); break;
142 }
143 } else { // printable chars + UTF-8 bytes
144 attribute_ += *cursor_++;
145 }
146 }
147 cursor_++;
148 token_ = kTokenStringConstant;
149 return;
150 case '/':
151 if (*cursor_ == '/') {
152 const char *start = ++cursor_;
153 while (*cursor_ && *cursor_ != '\n') cursor_++;
154 if (*start == '/') { // documentation comment
155 if (!seen_newline)
156 Error("a documentation comment should be on a line on its own");
157 // todo: do we want to support multiline comments instead?
158 doc_comment_ += std::string(start + 1, cursor_);
159 }
160 break;
161 }
162 // fall thru
163 default:
164 if (isalpha(static_cast<unsigned char>(c))) {
165 // Collect all chars of an identifier:
166 const char *start = cursor_ - 1;
167 while (isalnum(static_cast<unsigned char>(*cursor_)) ||
168 *cursor_ == '_')
169 cursor_++;
170 attribute_.clear();
171 attribute_.append(start, cursor_);
172 // First, see if it is a type keyword from the table of types:
rw74d5f372014-07-11 16:12:35 -0700173 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) \
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -0800174 if (attribute_ == IDLTYPE) { \
175 token_ = kToken ## ENUM; \
176 return; \
177 }
178 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
179 #undef FLATBUFFERS_TD
180 // If it's a boolean constant keyword, turn those into integers,
181 // which simplifies our logic downstream.
182 if (attribute_ == "true" || attribute_ == "false") {
183 attribute_ = NumToString(attribute_ == "true");
184 token_ = kTokenIntegerConstant;
185 return;
186 }
187 // Check for declaration keywords:
188 if (attribute_ == "table") { token_ = kTokenTable; return; }
189 if (attribute_ == "struct") { token_ = kTokenStruct; return; }
190 if (attribute_ == "enum") { token_ = kTokenEnum; return; }
191 if (attribute_ == "union") { token_ = kTokenUnion; return; }
192 if (attribute_ == "namespace") { token_ = kTokenNameSpace; return; }
193 if (attribute_ == "root_type") { token_ = kTokenRootType; return; }
194 // If not, it is a user-defined identifier:
195 token_ = kTokenIdentifier;
196 return;
197 } else if (isdigit(static_cast<unsigned char>(c)) || c == '-') {
198 const char *start = cursor_ - 1;
199 while (isdigit(static_cast<unsigned char>(*cursor_))) cursor_++;
200 if (*cursor_ == '.') {
201 cursor_++;
202 while (isdigit(static_cast<unsigned char>(*cursor_))) cursor_++;
Wouter van Oortmerssen93df5692014-07-10 13:40:55 -0700203 // See if this float has a scientific notation suffix. Both JSON
204 // and C++ (through strtod() we use) have the same format:
205 if (*cursor_ == 'e' || *cursor_ == 'E') {
206 cursor_++;
207 if (*cursor_ == '+' || *cursor_ == '-') cursor_++;
208 while (isdigit(static_cast<unsigned char>(*cursor_))) cursor_++;
209 }
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -0800210 token_ = kTokenFloatConstant;
211 } else {
212 token_ = kTokenIntegerConstant;
213 }
214 attribute_.clear();
215 attribute_.append(start, cursor_);
216 return;
217 }
218 std::string ch;
219 ch = c;
220 if (c < ' ' || c > '~') ch = "code: " + NumToString(c);
221 Error("illegal character: " + ch);
222 break;
223 }
224 }
225}
226
227// Check if a given token is next, if so, consume it as well.
228bool Parser::IsNext(int t) {
229 bool isnext = t == token_;
230 if (isnext) Next();
231 return isnext;
232}
233
234// Expect a given token to be next, consume it, or error if not present.
235void Parser::Expect(int t) {
236 if (t != token_) {
237 Error("expecting: " + TokenToString(t) + " instead got: " +
238 TokenToString(token_));
239 }
240 Next();
241}
242
243// Parse any IDL type.
244void Parser::ParseType(Type &type) {
245 if (token_ >= kTokenBOOL && token_ <= kTokenSTRING) {
246 type.base_type = static_cast<BaseType>(token_ - kTokenNONE);
247 } else {
248 if (token_ == kTokenIdentifier) {
249 auto enum_def = enums_.Lookup(attribute_);
250 if (enum_def) {
251 type = enum_def->underlying_type;
252 if (enum_def->is_union) type.base_type = BASE_TYPE_UNION;
253 } else {
254 type.base_type = BASE_TYPE_STRUCT;
255 type.struct_def = LookupCreateStruct(attribute_);
256 }
257 } else if (token_ == '[') {
258 Next();
259 Type subtype;
260 ParseType(subtype);
261 if (subtype.base_type == BASE_TYPE_VECTOR) {
262 // We could support this, but it will complicate things, and it's
263 // easier to work around with a struct around the inner vector.
264 Error("nested vector types not supported (wrap in table first).");
265 }
266 if (subtype.base_type == BASE_TYPE_UNION) {
267 // We could support this if we stored a struct of 2 elements per
268 // union element.
269 Error("vector of union types not supported (wrap in table first).");
270 }
Wouter van Oortmerssen3fb6a862014-07-14 17:49:04 -0700271 type = Type(BASE_TYPE_VECTOR, subtype.struct_def, subtype.enum_def);
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -0800272 type.element = subtype.base_type;
273 Expect(']');
274 return;
275 } else {
276 Error("illegal type syntax");
277 }
278 }
279 Next();
280}
281
282FieldDef &Parser::AddField(StructDef &struct_def,
283 const std::string &name,
284 const Type &type) {
285 auto &field = *new FieldDef();
286 field.value.offset =
287 FieldIndexToOffset(static_cast<voffset_t>(struct_def.fields.vec.size()));
288 field.name = name;
289 field.value.type = type;
290 if (struct_def.fixed) { // statically compute the field offset
291 auto size = InlineSize(type);
292 auto alignment = InlineAlignment(type);
293 // structs_ need to have a predictable format, so we need to align to
294 // the largest scalar
295 struct_def.minalign = std::max(struct_def.minalign, alignment);
296 struct_def.PadLastField(alignment);
Wouter van Oortmerssen12563072014-06-30 15:56:31 -0700297 field.value.offset = static_cast<voffset_t>(struct_def.bytesize);
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -0800298 struct_def.bytesize += size;
299 }
300 if (struct_def.fields.Add(name, &field))
301 Error("field already exists: " + name);
302 return field;
303}
304
305void Parser::ParseField(StructDef &struct_def) {
306 std::string name = attribute_;
307 std::string dc = doc_comment_;
308 Expect(kTokenIdentifier);
309 Expect(':');
310 Type type;
311 ParseType(type);
312
313 if (struct_def.fixed && !IsScalar(type.base_type) && !IsStruct(type))
314 Error("structs_ may contain only scalar or struct fields");
315
Wouter van Oortmerssen91401442014-07-07 17:34:23 -0700316 FieldDef *typefield = nullptr;
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -0800317 if (type.base_type == BASE_TYPE_UNION) {
318 // For union fields, add a second auto-generated field to hold the type,
319 // with _type appended as the name.
Wouter van Oortmerssen91401442014-07-07 17:34:23 -0700320 typefield = &AddField(struct_def, name + "_type",
321 type.enum_def->underlying_type);
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -0800322 }
323
324 auto &field = AddField(struct_def, name, type);
325
326 if (token_ == '=') {
327 Next();
328 ParseSingleValue(field.value);
329 }
330
331 field.doc_comment = dc;
332 ParseMetaData(field);
333 field.deprecated = field.attributes.Lookup("deprecated") != nullptr;
334 if (field.deprecated && struct_def.fixed)
335 Error("can't deprecate fields in a struct");
Wouter van Oortmerssen3e201a92014-07-15 17:50:22 -0700336 auto nested = field.attributes.Lookup("nested_flatbuffer");
337 if (nested) {
338 if (nested->type.base_type != BASE_TYPE_STRING)
339 Error("nested_flatbuffer attribute must be a string (the root type)");
340 if (field.value.type.base_type != BASE_TYPE_VECTOR ||
341 field.value.type.element != BASE_TYPE_UCHAR)
342 Error("nested_flatbuffer attribute may only apply to a vector of ubyte");
343 // This will cause an error if the root type of the nested flatbuffer
344 // wasn't defined elsewhere.
345 LookupCreateStruct(nested->constant);
346 }
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -0800347
Wouter van Oortmerssen91401442014-07-07 17:34:23 -0700348 if (typefield) {
349 // If this field is a union, and it has a manually assigned id,
350 // the automatically added type field should have an id as well (of N - 1).
351 auto attr = field.attributes.Lookup("id");
352 if (attr) {
353 auto id = atoi(attr->constant.c_str());
354 auto val = new Value();
355 val->type = attr->type;
356 val->constant = NumToString(id - 1);
357 typefield->attributes.Add("id", val);
358 }
359 }
360
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -0800361 Expect(';');
362}
363
364void Parser::ParseAnyValue(Value &val, FieldDef *field) {
365 switch (val.type.base_type) {
366 case BASE_TYPE_UNION: {
367 assert(field);
368 if (!field_stack_.size() ||
369 field_stack_.back().second->value.type.base_type != BASE_TYPE_UTYPE)
370 Error("missing type field before this union value: " + field->name);
371 auto enum_idx = atot<unsigned char>(
372 field_stack_.back().first.constant.c_str());
Wouter van Oortmerssen3fb6a862014-07-14 17:49:04 -0700373 auto enum_val = val.type.enum_def->ReverseLookup(enum_idx);
374 if (!enum_val) Error("illegal type id for: " + field->name);
375 val.constant = NumToString(ParseTable(*enum_val->struct_def));
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -0800376 break;
377 }
378 case BASE_TYPE_STRUCT:
379 val.constant = NumToString(ParseTable(*val.type.struct_def));
380 break;
381 case BASE_TYPE_STRING: {
382 auto s = attribute_;
383 Expect(kTokenStringConstant);
384 val.constant = NumToString(builder_.CreateString(s).o);
385 break;
386 }
387 case BASE_TYPE_VECTOR: {
388 Expect('[');
389 val.constant = NumToString(ParseVector(val.type.VectorType()));
390 break;
391 }
392 default:
393 ParseSingleValue(val);
394 break;
395 }
396}
397
398void Parser::SerializeStruct(const StructDef &struct_def, const Value &val) {
399 auto off = atot<uoffset_t>(val.constant.c_str());
400 assert(struct_stack_.size() - off == struct_def.bytesize);
401 builder_.Align(struct_def.minalign);
402 builder_.PushBytes(&struct_stack_[off], struct_def.bytesize);
403 struct_stack_.resize(struct_stack_.size() - struct_def.bytesize);
404 builder_.AddStructOffset(val.offset, builder_.GetSize());
405}
406
407uoffset_t Parser::ParseTable(const StructDef &struct_def) {
408 Expect('{');
409 size_t fieldn = 0;
410 for (;;) {
411 std::string name = attribute_;
412 if (!IsNext(kTokenStringConstant)) Expect(kTokenIdentifier);
413 auto field = struct_def.fields.Lookup(name);
414 if (!field) Error("unknown field: " + name);
415 if (struct_def.fixed && (fieldn >= struct_def.fields.vec.size()
416 || struct_def.fields.vec[fieldn] != field)) {
417 Error("struct field appearing out of order: " + name);
418 }
419 Expect(':');
420 Value val = field->value;
421 ParseAnyValue(val, field);
422 field_stack_.push_back(std::make_pair(val, field));
423 fieldn++;
424 if (IsNext('}')) break;
425 Expect(',');
426 }
427 if (struct_def.fixed && fieldn != struct_def.fields.vec.size())
428 Error("incomplete struct initialization: " + struct_def.name);
429 auto start = struct_def.fixed
430 ? builder_.StartStruct(struct_def.minalign)
431 : builder_.StartTable();
432
433 for (size_t size = struct_def.sortbysize ? sizeof(largest_scalar_t) : 1;
434 size;
435 size /= 2) {
436 // Go through elements in reverse, since we're building the data backwards.
437 for (auto it = field_stack_.rbegin();
438 it != field_stack_.rbegin() + fieldn; ++it) {
439 auto &value = it->first;
440 auto field = it->second;
441 if (!struct_def.sortbysize || size == SizeOf(value.type.base_type)) {
442 switch (value.type.base_type) {
rw74d5f372014-07-11 16:12:35 -0700443 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) \
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -0800444 case BASE_TYPE_ ## ENUM: \
445 builder_.Pad(field->padding); \
446 builder_.AddElement(value.offset, \
447 atot<CTYPE>( value.constant.c_str()), \
448 atot<CTYPE>(field->value.constant.c_str())); \
449 break;
450 FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD);
451 #undef FLATBUFFERS_TD
rw74d5f372014-07-11 16:12:35 -0700452 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) \
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -0800453 case BASE_TYPE_ ## ENUM: \
454 builder_.Pad(field->padding); \
455 if (IsStruct(field->value.type)) { \
456 SerializeStruct(*field->value.type.struct_def, value); \
457 } else { \
458 builder_.AddOffset(value.offset, \
459 atot<CTYPE>(value.constant.c_str())); \
460 } \
461 break;
462 FLATBUFFERS_GEN_TYPES_POINTER(FLATBUFFERS_TD);
463 #undef FLATBUFFERS_TD
464 }
465 }
466 }
467 }
468 for (size_t i = 0; i < fieldn; i++) field_stack_.pop_back();
469
470 if (struct_def.fixed) {
471 builder_.ClearOffsets();
472 builder_.EndStruct();
473 // Temporarily store this struct in a side buffer, since this data has to
474 // be stored in-line later in the parent object.
475 auto off = struct_stack_.size();
476 struct_stack_.insert(struct_stack_.end(),
477 builder_.GetBufferPointer(),
478 builder_.GetBufferPointer() + struct_def.bytesize);
479 builder_.PopBytes(struct_def.bytesize);
480 return static_cast<uoffset_t>(off);
481 } else {
482 return builder_.EndTable(
483 start,
484 static_cast<voffset_t>(struct_def.fields.vec.size()));
485 }
486}
487
488uoffset_t Parser::ParseVector(const Type &type) {
489 int count = 0;
490 if (token_ != ']') for (;;) {
491 Value val;
492 val.type = type;
493 ParseAnyValue(val, NULL);
494 field_stack_.push_back(std::make_pair(val, nullptr));
495 count++;
496 if (token_ == ']') break;
497 Expect(',');
498 }
499 Next();
500
501 builder_.StartVector(count * InlineSize(type), InlineAlignment((type)));
502 for (int i = 0; i < count; i++) {
503 // start at the back, since we're building the data backwards.
504 auto &val = field_stack_.back().first;
505 switch (val.type.base_type) {
rw74d5f372014-07-11 16:12:35 -0700506 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) \
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -0800507 case BASE_TYPE_ ## ENUM: \
508 if (IsStruct(val.type)) SerializeStruct(*val.type.struct_def, val); \
509 else builder_.PushElement(atot<CTYPE>(val.constant.c_str())); \
510 break;
511 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
512 #undef FLATBUFFERS_TD
513 }
514 field_stack_.pop_back();
515 }
516
517 builder_.ClearOffsets();
518 return builder_.EndVector(count);
519}
520
521void Parser::ParseMetaData(Definition &def) {
522 if (IsNext('(')) {
523 for (;;) {
524 auto name = attribute_;
525 Expect(kTokenIdentifier);
526 auto e = new Value();
527 def.attributes.Add(name, e);
528 if (IsNext(':')) {
529 ParseSingleValue(*e);
530 }
531 if (IsNext(')')) break;
532 Expect(',');
533 }
534 }
535}
536
537bool Parser::TryTypedValue(int dtoken,
538 bool check,
539 Value &e,
540 BaseType req) {
541 bool match = dtoken == token_;
542 if (match) {
543 e.constant = attribute_;
544 if (!check) {
545 if (e.type.base_type == BASE_TYPE_NONE) {
546 e.type.base_type = req;
547 } else {
548 Error(std::string("type mismatch: expecting: ") +
549 kTypeNames[e.type.base_type] +
550 ", found: " +
551 kTypeNames[req]);
552 }
553 }
554 Next();
555 }
556 return match;
557}
558
Wouter van Oortmerssen9c3de1e2014-07-25 15:04:35 -0700559int64_t Parser::ParseIntegerFromString(Type &type) {
560 int64_t result = 0;
561 // Parse one or more enum identifiers, separated by spaces.
562 const char *next = attribute_.c_str();
563 do {
564 const char *divider = strchr(next, ' ');
565 std::string word;
566 if (divider) {
567 word = std::string(next, divider);
568 next = divider + strspn(divider, " ");
569 } else {
570 word = next;
571 next += word.length();
572 }
573 if (type.enum_def) { // The field has an enum type
574 auto enum_val = type.enum_def->vals.Lookup(word);
575 if (!enum_val)
576 Error("unknown enum value: " + word +
577 ", for enum: " + type.enum_def->name);
578 result |= enum_val->value;
579 } else { // No enum type, probably integral field.
580 if (!IsInteger(type.base_type))
581 Error("not a valid value for this field: " + word);
582 // TODO: could check if its a valid number constant here.
583 const char *dot = strchr(word.c_str(), '.');
584 if (!dot) Error("enum values need to be qualified by an enum type");
585 std::string enum_def_str(word.c_str(), dot);
586 std::string enum_val_str(dot + 1, word.c_str() + word.length());
587 auto enum_def = enums_.Lookup(enum_def_str);
588 if (!enum_def) Error("unknown enum: " + enum_def_str);
589 auto enum_val = enum_def->vals.Lookup(enum_val_str);
590 if (!enum_val) Error("unknown enum value: " + enum_val_str);
591 result |= enum_val->value;
592 }
593 } while(*next);
594 return result;
595}
596
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -0800597void Parser::ParseSingleValue(Value &e) {
Wouter van Oortmerssen9c3de1e2014-07-25 15:04:35 -0700598 // First check if this could be a string/identifier enum value:
599 if (e.type.base_type != BASE_TYPE_STRING &&
600 e.type.base_type != BASE_TYPE_NONE &&
601 (token_ == kTokenIdentifier || token_ == kTokenStringConstant)) {
602 e.constant = NumToString(ParseIntegerFromString(e.type));
603 Next();
Wouter van Oortmerssen3fb6a862014-07-14 17:49:04 -0700604 } else if (TryTypedValue(kTokenIntegerConstant,
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -0800605 IsScalar(e.type.base_type),
606 e,
607 BASE_TYPE_INT) ||
608 TryTypedValue(kTokenFloatConstant,
609 IsFloat(e.type.base_type),
610 e,
611 BASE_TYPE_FLOAT) ||
612 TryTypedValue(kTokenStringConstant,
613 e.type.base_type == BASE_TYPE_STRING,
614 e,
615 BASE_TYPE_STRING)) {
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -0800616 } else {
617 Error("cannot parse value starting with: " + TokenToString(token_));
618 }
619}
620
621StructDef *Parser::LookupCreateStruct(const std::string &name) {
622 auto struct_def = structs_.Lookup(name);
623 if (!struct_def) {
624 // Rather than failing, we create a "pre declared" StructDef, due to
625 // circular references, and check for errors at the end of parsing.
626 struct_def = new StructDef();
627 structs_.Add(name, struct_def);
628 struct_def->name = name;
629 struct_def->predecl = true;
630 }
631 return struct_def;
632}
633
634void Parser::ParseEnum(bool is_union) {
635 std::string dc = doc_comment_;
636 Next();
637 std::string name = attribute_;
638 Expect(kTokenIdentifier);
639 auto &enum_def = *new EnumDef();
640 enum_def.name = name;
641 enum_def.doc_comment = dc;
642 enum_def.is_union = is_union;
643 if (enums_.Add(name, &enum_def)) Error("enum already exists: " + name);
644 if (is_union) {
645 enum_def.underlying_type.base_type = BASE_TYPE_UTYPE;
646 enum_def.underlying_type.enum_def = &enum_def;
Wouter van Oortmerssena5f50012014-07-02 12:01:21 -0700647 } else {
648 // Give specialized error message, since this type spec used to
649 // be optional in the first FlatBuffers release.
650 if (!IsNext(':')) Error("must specify the underlying integer type for this"
651 " enum (e.g. \': short\', which was the default).");
652 // Specify the integer type underlying this enum.
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -0800653 ParseType(enum_def.underlying_type);
654 if (!IsInteger(enum_def.underlying_type.base_type))
655 Error("underlying enum type must be integral");
Wouter van Oortmerssen3fb6a862014-07-14 17:49:04 -0700656 // Make this type refer back to the enum it was derived from.
657 enum_def.underlying_type.enum_def = &enum_def;
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -0800658 }
659 ParseMetaData(enum_def);
660 Expect('{');
661 if (is_union) enum_def.vals.Add("NONE", new EnumVal("NONE", 0));
662 do {
663 std::string name = attribute_;
664 std::string dc = doc_comment_;
665 Expect(kTokenIdentifier);
666 auto prevsize = enum_def.vals.vec.size();
Wouter van Oortmerssen127d3502014-07-17 15:12:37 -0700667 auto value = enum_def.vals.vec.size()
668 ? enum_def.vals.vec.back()->value + 1
669 : 0;
670 auto &ev = *new EnumVal(name, value);
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -0800671 if (enum_def.vals.Add(name, &ev))
672 Error("enum value already exists: " + name);
673 ev.doc_comment = dc;
674 if (is_union) {
675 ev.struct_def = LookupCreateStruct(name);
676 }
677 if (IsNext('=')) {
678 ev.value = atoi(attribute_.c_str());
679 Expect(kTokenIntegerConstant);
680 if (prevsize && enum_def.vals.vec[prevsize - 1]->value >= ev.value)
681 Error("enum values must be specified in ascending order");
682 }
683 } while (IsNext(','));
684 Expect('}');
Wouter van Oortmerssen127d3502014-07-17 15:12:37 -0700685 if (enum_def.attributes.Lookup("bit_flags")) {
686 for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
687 ++it) {
688 if (static_cast<size_t>((*it)->value) >=
689 SizeOf(enum_def.underlying_type.base_type) * 8)
690 Error("bit flag out of range of underlying integral type");
Wouter van Oortmerssen9c3de1e2014-07-25 15:04:35 -0700691 (*it)->value = 1LL << (*it)->value;
Wouter van Oortmerssen127d3502014-07-17 15:12:37 -0700692 }
693 }
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -0800694}
695
696void Parser::ParseDecl() {
697 std::string dc = doc_comment_;
698 bool fixed = IsNext(kTokenStruct);
699 if (!fixed) Expect(kTokenTable);
700 std::string name = attribute_;
701 Expect(kTokenIdentifier);
702 auto &struct_def = *LookupCreateStruct(name);
703 if (!struct_def.predecl) Error("datatype already exists: " + name);
704 struct_def.predecl = false;
705 struct_def.name = name;
706 struct_def.doc_comment = dc;
707 struct_def.fixed = fixed;
708 // Move this struct to the back of the vector just in case it was predeclared,
709 // to preserve declartion order.
710 remove(structs_.vec.begin(), structs_.vec.end(), &struct_def);
711 structs_.vec.back() = &struct_def;
712 ParseMetaData(struct_def);
713 struct_def.sortbysize =
714 struct_def.attributes.Lookup("original_order") == nullptr && !fixed;
715 Expect('{');
716 while (token_ != '}') ParseField(struct_def);
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -0800717 auto force_align = struct_def.attributes.Lookup("force_align");
718 if (fixed && force_align) {
719 auto align = static_cast<size_t>(atoi(force_align->constant.c_str()));
720 if (force_align->type.base_type != BASE_TYPE_INT ||
721 align < struct_def.minalign ||
722 align > 256 ||
723 align & (align - 1))
724 Error("force_align must be a power of two integer ranging from the"
725 "struct\'s natural alignment to 256");
726 struct_def.minalign = align;
727 }
Wouter van Oortmerssen65cfa182014-06-23 11:34:19 -0700728 struct_def.PadLastField(struct_def.minalign);
Wouter van Oortmerssen91401442014-07-07 17:34:23 -0700729 // Check if this is a table that has manual id assignments
730 auto &fields = struct_def.fields.vec;
731 if (!struct_def.fixed && fields.size()) {
Wouter van Oortmerssen7fcbe722014-07-08 16:35:14 -0700732 size_t num_id_fields = 0;
Wouter van Oortmerssen91401442014-07-07 17:34:23 -0700733 for (auto it = fields.begin(); it != fields.end(); ++it) {
734 if ((*it)->attributes.Lookup("id")) num_id_fields++;
735 }
736 // If any fields have ids..
737 if (num_id_fields) {
738 // Then all fields must have them.
739 if (num_id_fields != fields.size())
740 Error("either all fields or no fields must have an 'id' attribute");
741 // Simply sort by id, then the fields are the same as if no ids had
742 // been specified.
743 std::sort(fields.begin(), fields.end(),
744 [](const FieldDef *a, const FieldDef *b) -> bool {
745 auto a_id = atoi(a->attributes.Lookup("id")->constant.c_str());
746 auto b_id = atoi(b->attributes.Lookup("id")->constant.c_str());
747 return a_id < b_id;
748 });
749 // Verify we have a contiguous set, and reassign vtable offsets.
750 for (int i = 0; i < static_cast<int>(fields.size()); i++) {
751 if (i != atoi(fields[i]->attributes.Lookup("id")->constant.c_str()))
752 Error("field id\'s must be consecutive from 0, id " +
753 NumToString(i) + " missing or set twice");
754 fields[i]->value.offset = FieldIndexToOffset(static_cast<voffset_t>(i));
755 }
756 }
757 }
Wouter van Oortmerssen65cfa182014-06-23 11:34:19 -0700758 Expect('}');
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -0800759}
760
761bool Parser::SetRootType(const char *name) {
762 root_struct_def = structs_.Lookup(name);
763 return root_struct_def != nullptr;
764}
765
766bool Parser::Parse(const char *source) {
767 source_ = cursor_ = source;
768 line_ = 1;
769 error_.clear();
770 builder_.Clear();
771 try {
772 Next();
773 while (token_ != kTokenEof) {
774 if (token_ == kTokenNameSpace) {
775 Next();
Wouter van Oortmerssen2208de02014-07-09 14:21:11 -0700776 name_space_.clear();
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -0800777 for (;;) {
778 name_space_.push_back(attribute_);
779 Expect(kTokenIdentifier);
780 if (!IsNext('.')) break;
781 }
782 Expect(';');
783 } else if (token_ == '{') {
784 if (!root_struct_def) Error("no root type set to parse json with");
785 if (builder_.GetSize()) {
786 Error("cannot have more than one json object in a file");
787 }
788 builder_.Finish(Offset<Table>(ParseTable(*root_struct_def)));
789 } else if (token_ == kTokenEnum) {
790 ParseEnum(false);
791 } else if (token_ == kTokenUnion) {
792 ParseEnum(true);
793 } else if (token_ == kTokenRootType) {
794 Next();
795 auto root_type = attribute_;
796 Expect(kTokenIdentifier);
797 Expect(';');
798 if (!SetRootType(root_type.c_str()))
799 Error("unknown root type: " + root_type);
800 if (root_struct_def->fixed)
801 Error("root type must be a table");
802 } else {
803 ParseDecl();
804 }
805 }
806 for (auto it = structs_.vec.begin(); it != structs_.vec.end(); ++it) {
807 if ((*it)->predecl)
808 Error("type referenced but not defined: " + (*it)->name);
809 }
810 for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) {
811 auto &enum_def = **it;
812 if (enum_def.is_union) {
813 for (auto it = enum_def.vals.vec.begin();
814 it != enum_def.vals.vec.end();
815 ++it) {
816 auto &val = **it;
817 if (val.struct_def && val.struct_def->fixed)
818 Error("only tables can be union elements: " + val.name);
819 }
820 }
821 }
822 } catch (const std::string &msg) {
823 error_ = "line " + NumToString(line_) + ": " + msg;
824 return false;
825 }
826 assert(!struct_stack_.size());
827 return true;
828}
829
830} // namespace flatbuffers