Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1 | /* |
| 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> |
Gabriel Martinez | df4909e | 2014-11-04 10:00:56 -0800 | [diff] [blame] | 18 | #include <list> |
Christian Helmich | 89a6894 | 2017-07-29 02:27:51 +0900 | [diff] [blame] | 19 | #include <iostream> |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 20 | |
Wouter van Oortmerssen | d3ac0bc | 2016-06-15 13:54:17 -0700 | [diff] [blame] | 21 | #ifdef _WIN32 |
| 22 | #if !defined(_USE_MATH_DEFINES) |
| 23 | #define _USE_MATH_DEFINES // For M_PI. |
| 24 | #endif // !defined(_USE_MATH_DEFINES) |
| 25 | #endif // _WIN32 |
| 26 | |
| 27 | #include <math.h> |
| 28 | |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 29 | #include "flatbuffers/idl.h" |
| 30 | #include "flatbuffers/util.h" |
| 31 | |
| 32 | namespace flatbuffers { |
| 33 | |
| 34 | const char *const kTypeNames[] = { |
MikkelFJ | f2b3705 | 2017-08-02 17:07:43 +0200 | [diff] [blame] | 35 | #define FLATBUFFERS_TD(ENUM, IDLTYPE, ALIASTYPE, \ |
| 36 | CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \ |
rw | 48dfc69 | 2014-12-16 00:32:11 -0800 | [diff] [blame] | 37 | IDLTYPE, |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 38 | FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD) |
| 39 | #undef FLATBUFFERS_TD |
| 40 | nullptr |
| 41 | }; |
| 42 | |
| 43 | const char kTypeSizes[] = { |
MikkelFJ | f2b3705 | 2017-08-02 17:07:43 +0200 | [diff] [blame] | 44 | #define FLATBUFFERS_TD(ENUM, IDLTYPE, ALIASTYPE, \ |
| 45 | CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \ |
Wouter van Oortmerssen | 557c88c | 2014-09-16 17:37:17 -0700 | [diff] [blame] | 46 | sizeof(CTYPE), |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 47 | FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD) |
| 48 | #undef FLATBUFFERS_TD |
| 49 | }; |
| 50 | |
Wouter van Oortmerssen | 81312c2 | 2015-05-21 16:33:29 -0700 | [diff] [blame] | 51 | // The enums in the reflection schema should match the ones we use internally. |
| 52 | // Compare the last element to check if these go out of sync. |
| 53 | static_assert(BASE_TYPE_UNION == |
Wouter van Oortmerssen | 622b8d0 | 2015-06-15 15:57:48 -0700 | [diff] [blame] | 54 | static_cast<BaseType>(reflection::Union), |
Wouter van Oortmerssen | 81312c2 | 2015-05-21 16:33:29 -0700 | [diff] [blame] | 55 | "enums don't match"); |
| 56 | |
Wouter van Oortmerssen | 451272b | 2015-12-29 16:33:00 -0800 | [diff] [blame] | 57 | // Any parsing calls have to be wrapped in this macro, which automates |
| 58 | // handling of recursive error checking a bit. It will check the received |
| 59 | // CheckedError object, and return straight away on error. |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 60 | #define ECHECK(call) { auto ce = (call); if (ce.Check()) return ce; } |
Wouter van Oortmerssen | 451272b | 2015-12-29 16:33:00 -0800 | [diff] [blame] | 61 | |
| 62 | // These two functions are called hundreds of times below, so define a short |
| 63 | // form: |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 64 | #define NEXT() ECHECK(Next()) |
| 65 | #define EXPECT(tok) ECHECK(Expect(tok)) |
| 66 | |
Ben Hamilton | f6416d8 | 2016-08-01 14:04:51 -0700 | [diff] [blame] | 67 | static bool ValidateUTF8(const std::string &str) { |
| 68 | const char *s = &str[0]; |
| 69 | const char * const sEnd = s + str.length(); |
| 70 | while (s < sEnd) { |
| 71 | if (FromUTF8(&s) < 0) { |
| 72 | return false; |
| 73 | } |
| 74 | } |
| 75 | return true; |
| 76 | } |
| 77 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 78 | CheckedError Parser::Error(const std::string &msg) { |
| 79 | error_ = file_being_parsed_.length() ? AbsolutePath(file_being_parsed_) : ""; |
| 80 | #ifdef _WIN32 |
| 81 | error_ += "(" + NumToString(line_) + ")"; // MSVC alike |
| 82 | #else |
| 83 | if (file_being_parsed_.length()) error_ += ":"; |
| 84 | error_ += NumToString(line_) + ":0"; // gcc alike |
| 85 | #endif |
| 86 | error_ += ": error: " + msg; |
| 87 | return CheckedError(true); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 90 | inline CheckedError NoError() { return CheckedError(false); } |
| 91 | |
Jason Stubbs | a07f0d4 | 2017-04-18 04:19:43 +1000 | [diff] [blame] | 92 | inline std::string OutOfRangeErrorMsg(int64_t val, const std::string &op, |
| 93 | int64_t limit) { |
| 94 | const std::string cause = NumToString(val) + op + NumToString(limit); |
| 95 | return "constant does not fit (" + cause + ")"; |
| 96 | } |
| 97 | |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 98 | // Ensure that integer values we parse fit inside the declared integer type. |
Jason Stubbs | a07f0d4 | 2017-04-18 04:19:43 +1000 | [diff] [blame] | 99 | CheckedError Parser::CheckInRange(int64_t val, int64_t min, int64_t max) { |
| 100 | if (val < min) |
| 101 | return Error(OutOfRangeErrorMsg(val, " < ", min)); |
| 102 | else if (val > max) |
| 103 | return Error(OutOfRangeErrorMsg(val, " > ", max)); |
| 104 | else |
| 105 | return NoError(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // atot: templated version of atoi/atof: convert a string to an instance of T. |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 109 | template<typename T> inline CheckedError atot(const char *s, Parser &parser, |
| 110 | T *val) { |
| 111 | int64_t i = StringToInt(s); |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 112 | const int64_t min = flatbuffers::numeric_limits<T>::min(); |
| 113 | const int64_t max = flatbuffers::numeric_limits<T>::max(); |
Jason Stubbs | a07f0d4 | 2017-04-18 04:19:43 +1000 | [diff] [blame] | 114 | ECHECK(parser.CheckInRange(i, min, max)); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 115 | *val = (T)i; |
| 116 | return NoError(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 117 | } |
Wouter van Oortmerssen | 2957428 | 2017-01-30 17:07:55 -0800 | [diff] [blame] | 118 | template<> inline CheckedError atot<uint64_t>(const char *s, Parser &parser, |
| 119 | uint64_t *val) { |
| 120 | (void)parser; |
| 121 | *val = StringToUInt(s); |
| 122 | return NoError(); |
| 123 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 124 | template<> inline CheckedError atot<bool>(const char *s, Parser &parser, |
| 125 | bool *val) { |
| 126 | (void)parser; |
| 127 | *val = 0 != atoi(s); |
| 128 | return NoError(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 129 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 130 | template<> inline CheckedError atot<float>(const char *s, Parser &parser, |
| 131 | float *val) { |
| 132 | (void)parser; |
| 133 | *val = static_cast<float>(strtod(s, nullptr)); |
| 134 | return NoError(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 135 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 136 | template<> inline CheckedError atot<double>(const char *s, Parser &parser, |
| 137 | double *val) { |
| 138 | (void)parser; |
| 139 | *val = strtod(s, nullptr); |
| 140 | return NoError(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 143 | template<> inline CheckedError atot<Offset<void>>(const char *s, Parser &parser, |
| 144 | Offset<void> *val) { |
| 145 | (void)parser; |
| 146 | *val = Offset<void>(atoi(s)); |
| 147 | return NoError(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 150 | std::string Namespace::GetFullyQualifiedName(const std::string &name, |
| 151 | size_t max_components) const { |
| 152 | // Early exit if we don't have a defined namespace. |
| 153 | if (components.size() == 0 || !max_components) { |
| 154 | return name; |
| 155 | } |
| 156 | std::stringstream stream; |
| 157 | for (size_t i = 0; i < std::min(components.size(), max_components); |
| 158 | i++) { |
| 159 | if (i) { |
| 160 | stream << "."; |
| 161 | } |
| 162 | stream << components[i]; |
| 163 | } |
Wouter van Oortmerssen | 8c1a723 | 2017-01-09 15:54:31 -0800 | [diff] [blame] | 164 | if (name.length()) stream << "." << name; |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 165 | return stream.str(); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 170 | // Declare tokens we'll use. Single character tokens are represented by their |
| 171 | // ascii character code (e.g. '{'), others above 256. |
| 172 | #define FLATBUFFERS_GEN_TOKENS(TD) \ |
| 173 | TD(Eof, 256, "end of file") \ |
| 174 | TD(StringConstant, 257, "string constant") \ |
| 175 | TD(IntegerConstant, 258, "integer constant") \ |
| 176 | TD(FloatConstant, 259, "float constant") \ |
| 177 | TD(Identifier, 260, "identifier") \ |
| 178 | TD(Table, 261, "table") \ |
| 179 | TD(Struct, 262, "struct") \ |
| 180 | TD(Enum, 263, "enum") \ |
| 181 | TD(Union, 264, "union") \ |
| 182 | TD(NameSpace, 265, "namespace") \ |
Wouter van Oortmerssen | 5da7bda | 2014-07-31 15:11:03 -0700 | [diff] [blame] | 183 | TD(RootType, 266, "root_type") \ |
| 184 | TD(FileIdentifier, 267, "file_identifier") \ |
Wouter van Oortmerssen | be894f0 | 2014-08-19 14:20:05 -0700 | [diff] [blame] | 185 | TD(FileExtension, 268, "file_extension") \ |
Wouter van Oortmerssen | 0952143 | 2014-11-17 17:27:26 -0800 | [diff] [blame] | 186 | TD(Include, 269, "include") \ |
Wouter van Oortmerssen | 049f3f7 | 2016-01-08 15:18:51 -0800 | [diff] [blame] | 187 | TD(Attribute, 270, "attribute") \ |
Wouter van Oortmerssen | 1a63eb4 | 2016-03-07 18:07:10 -0800 | [diff] [blame] | 188 | TD(Null, 271, "null") \ |
Wouter van Oortmerssen | 3f936c5 | 2017-01-18 16:23:35 -0800 | [diff] [blame] | 189 | TD(Service, 272, "rpc_service") \ |
rouzier | a2b1bfc | 2017-08-04 11:04:28 -0400 | [diff] [blame] | 190 | TD(NativeInclude, 273, "native_include") \ |
| 191 | TD(BooleanConstant, 274, "boolean constant") |
Wouter van Oortmerssen | 8f80fec | 2014-07-29 10:29:38 -0700 | [diff] [blame] | 192 | #ifdef __GNUC__ |
| 193 | __extension__ // Stop GCC complaining about trailing comma with -Wpendantic. |
| 194 | #endif |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 195 | enum { |
Wouter van Oortmerssen | 75349ae | 2014-07-09 11:44:26 -0700 | [diff] [blame] | 196 | #define FLATBUFFERS_TOKEN(NAME, VALUE, STRING) kToken ## NAME = VALUE, |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 197 | FLATBUFFERS_GEN_TOKENS(FLATBUFFERS_TOKEN) |
| 198 | #undef FLATBUFFERS_TOKEN |
MikkelFJ | f2b3705 | 2017-08-02 17:07:43 +0200 | [diff] [blame] | 199 | #define FLATBUFFERS_TD(ENUM, IDLTYPE, ALIASTYPE, \ |
| 200 | CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \ |
Wouter van Oortmerssen | 557c88c | 2014-09-16 17:37:17 -0700 | [diff] [blame] | 201 | kToken ## ENUM, |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 202 | FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD) |
| 203 | #undef FLATBUFFERS_TD |
| 204 | }; |
| 205 | |
| 206 | static std::string TokenToString(int t) { |
| 207 | static const char *tokens[] = { |
| 208 | #define FLATBUFFERS_TOKEN(NAME, VALUE, STRING) STRING, |
| 209 | FLATBUFFERS_GEN_TOKENS(FLATBUFFERS_TOKEN) |
| 210 | #undef FLATBUFFERS_TOKEN |
MikkelFJ | f2b3705 | 2017-08-02 17:07:43 +0200 | [diff] [blame] | 211 | #define FLATBUFFERS_TD(ENUM, IDLTYPE, ALIASTYPE, \ |
| 212 | CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \ |
rw | 48dfc69 | 2014-12-16 00:32:11 -0800 | [diff] [blame] | 213 | IDLTYPE, |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 214 | FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD) |
| 215 | #undef FLATBUFFERS_TD |
| 216 | }; |
| 217 | if (t < 256) { // A single ascii char token. |
| 218 | std::string s; |
Wouter van Oortmerssen | 8e40902 | 2014-09-02 17:32:12 -0700 | [diff] [blame] | 219 | s.append(1, static_cast<char>(t)); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 220 | return s; |
| 221 | } else { // Other tokens. |
| 222 | return tokens[t - 256]; |
| 223 | } |
| 224 | } |
| 225 | |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 226 | std::string Parser::TokenToStringId(int t) { |
| 227 | return TokenToString(t) + (t == kTokenIdentifier ? ": " + attribute_ : ""); |
| 228 | } |
| 229 | |
Wouter van Oortmerssen | ebac1e1 | 2014-08-20 13:22:16 -0700 | [diff] [blame] | 230 | // Parses exactly nibbles worth of hex digits into a number, or error. |
Wouter van Oortmerssen | 2957428 | 2017-01-30 17:07:55 -0800 | [diff] [blame] | 231 | CheckedError Parser::ParseHexNum(int nibbles, uint64_t *val) { |
Wouter van Oortmerssen | ebac1e1 | 2014-08-20 13:22:16 -0700 | [diff] [blame] | 232 | for (int i = 0; i < nibbles; i++) |
Chris Pickett | 30013b4 | 2016-01-05 13:38:03 -0600 | [diff] [blame] | 233 | if (!isxdigit(static_cast<const unsigned char>(cursor_[i]))) |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 234 | return Error("escape code must be followed by " + NumToString(nibbles) + |
| 235 | " hex digits"); |
Hiroshi Matsunaga | 7cf74cb | 2015-01-09 03:38:14 +0900 | [diff] [blame] | 236 | std::string target(cursor_, cursor_ + nibbles); |
Sahil Jain | b6ba322 | 2016-08-25 23:37:30 -0400 | [diff] [blame] | 237 | *val = StringToUInt(target.c_str(), nullptr, 16); |
Wouter van Oortmerssen | ebac1e1 | 2014-08-20 13:22:16 -0700 | [diff] [blame] | 238 | cursor_ += nibbles; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 239 | return NoError(); |
Wouter van Oortmerssen | ebac1e1 | 2014-08-20 13:22:16 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Oli Wilkinson | cbe8747 | 2016-01-18 18:58:53 +0000 | [diff] [blame] | 242 | CheckedError Parser::SkipByteOrderMark() { |
| 243 | if (static_cast<unsigned char>(*cursor_) != 0xef) return NoError(); |
| 244 | cursor_++; |
Wouter van Oortmerssen | f6330ab | 2016-04-22 11:26:47 -0700 | [diff] [blame] | 245 | if (static_cast<unsigned char>(*cursor_) != 0xbb) return Error("invalid utf-8 byte order mark"); |
| 246 | cursor_++; |
| 247 | if (static_cast<unsigned char>(*cursor_) != 0xbf) return Error("invalid utf-8 byte order mark"); |
| 248 | cursor_++; |
Oli Wilkinson | cbe8747 | 2016-01-18 18:58:53 +0000 | [diff] [blame] | 249 | return NoError(); |
| 250 | } |
| 251 | |
Wouter van Oortmerssen | fbc8af4 | 2016-03-07 17:22:51 -0800 | [diff] [blame] | 252 | bool IsIdentifierStart(char c) { |
| 253 | return isalpha(static_cast<unsigned char>(c)) || c == '_'; |
| 254 | } |
| 255 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 256 | CheckedError Parser::Next() { |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 257 | doc_comment_.clear(); |
| 258 | bool seen_newline = false; |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 259 | attribute_.clear(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 260 | for (;;) { |
| 261 | char c = *cursor_++; |
| 262 | token_ = c; |
| 263 | switch (c) { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 264 | case '\0': cursor_--; token_ = kTokenEof; return NoError(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 265 | case ' ': case '\r': case '\t': break; |
| 266 | case '\n': line_++; seen_newline = true; break; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 267 | case '{': case '}': case '(': case ')': case '[': case ']': |
| 268 | case ',': case ':': case ';': case '=': return NoError(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 269 | case '.': |
Chris Pickett | 30013b4 | 2016-01-05 13:38:03 -0600 | [diff] [blame] | 270 | if(!isdigit(static_cast<const unsigned char>(*cursor_))) return NoError(); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 271 | return Error("floating point constant can\'t start with \".\""); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 272 | case '\"': |
Ben Gertzfield | 6704b19 | 2016-04-28 12:27:38 -0700 | [diff] [blame] | 273 | case '\'': { |
| 274 | int unicode_high_surrogate = -1; |
| 275 | |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 276 | while (*cursor_ != c) { |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 277 | if (*cursor_ < ' ' && *cursor_ >= 0) |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 278 | return Error("illegal character in string constant"); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 279 | if (*cursor_ == '\\') { |
| 280 | cursor_++; |
Ben Gertzfield | 6704b19 | 2016-04-28 12:27:38 -0700 | [diff] [blame] | 281 | if (unicode_high_surrogate != -1 && |
| 282 | *cursor_ != 'u') { |
| 283 | return Error( |
| 284 | "illegal Unicode sequence (unpaired high surrogate)"); |
| 285 | } |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 286 | switch (*cursor_) { |
| 287 | case 'n': attribute_ += '\n'; cursor_++; break; |
| 288 | case 't': attribute_ += '\t'; cursor_++; break; |
| 289 | case 'r': attribute_ += '\r'; cursor_++; break; |
Wouter van Oortmerssen | ebac1e1 | 2014-08-20 13:22:16 -0700 | [diff] [blame] | 290 | case 'b': attribute_ += '\b'; cursor_++; break; |
| 291 | case 'f': attribute_ += '\f'; cursor_++; break; |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 292 | case '\"': attribute_ += '\"'; cursor_++; break; |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 293 | case '\'': attribute_ += '\''; cursor_++; break; |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 294 | case '\\': attribute_ += '\\'; cursor_++; break; |
Wouter van Oortmerssen | ebac1e1 | 2014-08-20 13:22:16 -0700 | [diff] [blame] | 295 | case '/': attribute_ += '/'; cursor_++; break; |
| 296 | case 'x': { // Not in the JSON standard |
| 297 | cursor_++; |
Wouter van Oortmerssen | 2957428 | 2017-01-30 17:07:55 -0800 | [diff] [blame] | 298 | uint64_t val; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 299 | ECHECK(ParseHexNum(2, &val)); |
| 300 | attribute_ += static_cast<char>(val); |
Wouter van Oortmerssen | ebac1e1 | 2014-08-20 13:22:16 -0700 | [diff] [blame] | 301 | break; |
| 302 | } |
| 303 | case 'u': { |
| 304 | cursor_++; |
Wouter van Oortmerssen | 2957428 | 2017-01-30 17:07:55 -0800 | [diff] [blame] | 305 | uint64_t val; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 306 | ECHECK(ParseHexNum(4, &val)); |
Ben Gertzfield | 6704b19 | 2016-04-28 12:27:38 -0700 | [diff] [blame] | 307 | if (val >= 0xD800 && val <= 0xDBFF) { |
| 308 | if (unicode_high_surrogate != -1) { |
| 309 | return Error( |
| 310 | "illegal Unicode sequence (multiple high surrogates)"); |
| 311 | } else { |
Wouter van Oortmerssen | e92ae51 | 2016-06-02 14:55:35 -0700 | [diff] [blame] | 312 | unicode_high_surrogate = static_cast<int>(val); |
Ben Gertzfield | 6704b19 | 2016-04-28 12:27:38 -0700 | [diff] [blame] | 313 | } |
| 314 | } else if (val >= 0xDC00 && val <= 0xDFFF) { |
| 315 | if (unicode_high_surrogate == -1) { |
| 316 | return Error( |
| 317 | "illegal Unicode sequence (unpaired low surrogate)"); |
| 318 | } else { |
| 319 | int code_point = 0x10000 + |
| 320 | ((unicode_high_surrogate & 0x03FF) << 10) + |
| 321 | (val & 0x03FF); |
| 322 | ToUTF8(code_point, &attribute_); |
| 323 | unicode_high_surrogate = -1; |
| 324 | } |
| 325 | } else { |
| 326 | if (unicode_high_surrogate != -1) { |
| 327 | return Error( |
| 328 | "illegal Unicode sequence (unpaired high surrogate)"); |
| 329 | } |
| 330 | ToUTF8(static_cast<int>(val), &attribute_); |
| 331 | } |
Wouter van Oortmerssen | ebac1e1 | 2014-08-20 13:22:16 -0700 | [diff] [blame] | 332 | break; |
| 333 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 334 | default: return Error("unknown escape code in string constant"); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 335 | } |
| 336 | } else { // printable chars + UTF-8 bytes |
Ben Gertzfield | 6704b19 | 2016-04-28 12:27:38 -0700 | [diff] [blame] | 337 | if (unicode_high_surrogate != -1) { |
| 338 | return Error( |
| 339 | "illegal Unicode sequence (unpaired high surrogate)"); |
| 340 | } |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 341 | attribute_ += *cursor_++; |
| 342 | } |
| 343 | } |
Ben Gertzfield | 6704b19 | 2016-04-28 12:27:38 -0700 | [diff] [blame] | 344 | if (unicode_high_surrogate != -1) { |
| 345 | return Error( |
| 346 | "illegal Unicode sequence (unpaired high surrogate)"); |
| 347 | } |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 348 | cursor_++; |
Ben Hamilton | f6416d8 | 2016-08-01 14:04:51 -0700 | [diff] [blame] | 349 | if (!opts.allow_non_utf8 && !ValidateUTF8(attribute_)) { |
| 350 | return Error("illegal UTF-8 sequence"); |
| 351 | } |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 352 | token_ = kTokenStringConstant; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 353 | return NoError(); |
Ben Gertzfield | 6704b19 | 2016-04-28 12:27:38 -0700 | [diff] [blame] | 354 | } |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 355 | case '/': |
| 356 | if (*cursor_ == '/') { |
| 357 | const char *start = ++cursor_; |
Mormegil | a8d6962 | 2015-04-14 18:09:08 +0200 | [diff] [blame] | 358 | while (*cursor_ && *cursor_ != '\n' && *cursor_ != '\r') cursor_++; |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 359 | if (*start == '/') { // documentation comment |
Zbigniew Mandziejewicz | 07d5965 | 2014-10-22 22:40:03 +0800 | [diff] [blame] | 360 | if (cursor_ != source_ && !seen_newline) |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 361 | return Error( |
| 362 | "a documentation comment should be on a line on its own"); |
Gabriel Martinez | 730c0ca | 2014-09-24 11:46:32 -0700 | [diff] [blame] | 363 | doc_comment_.push_back(std::string(start + 1, cursor_)); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 364 | } |
| 365 | break; |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 366 | } else if (*cursor_ == '*') { |
| 367 | cursor_++; |
| 368 | // TODO: make nested. |
| 369 | while (*cursor_ != '*' || cursor_[1] != '/') { |
Wouter van Oortmerssen | ab51b03 | 2016-10-07 17:07:39 -0700 | [diff] [blame] | 370 | if (*cursor_ == '\n') line_++; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 371 | if (!*cursor_) return Error("end of file in comment"); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 372 | cursor_++; |
| 373 | } |
| 374 | cursor_ += 2; |
| 375 | break; |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 376 | } |
| 377 | // fall thru |
| 378 | default: |
Wouter van Oortmerssen | fbc8af4 | 2016-03-07 17:22:51 -0800 | [diff] [blame] | 379 | if (IsIdentifierStart(c)) { |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 380 | // Collect all chars of an identifier: |
| 381 | const char *start = cursor_ - 1; |
| 382 | while (isalnum(static_cast<unsigned char>(*cursor_)) || |
| 383 | *cursor_ == '_') |
| 384 | cursor_++; |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 385 | attribute_.append(start, cursor_); |
| 386 | // First, see if it is a type keyword from the table of types: |
MikkelFJ | f2b3705 | 2017-08-02 17:07:43 +0200 | [diff] [blame] | 387 | #define FLATBUFFERS_TD(ENUM, IDLTYPE, ALIASTYPE, \ |
| 388 | CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \ |
| 389 | if (attribute_ == IDLTYPE || attribute_ == ALIASTYPE) { \ |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 390 | token_ = kToken ## ENUM; \ |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 391 | return NoError(); \ |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 392 | } |
| 393 | FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD) |
| 394 | #undef FLATBUFFERS_TD |
| 395 | // If it's a boolean constant keyword, turn those into integers, |
| 396 | // which simplifies our logic downstream. |
| 397 | if (attribute_ == "true" || attribute_ == "false") { |
| 398 | attribute_ = NumToString(attribute_ == "true"); |
rouzier | a2b1bfc | 2017-08-04 11:04:28 -0400 | [diff] [blame] | 399 | token_ = kTokenBooleanConstant; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 400 | return NoError(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 401 | } |
| 402 | // Check for declaration keywords: |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 403 | if (attribute_ == "table") { |
| 404 | token_ = kTokenTable; |
| 405 | return NoError(); |
| 406 | } |
| 407 | if (attribute_ == "struct") { |
| 408 | token_ = kTokenStruct; |
| 409 | return NoError(); |
| 410 | } |
| 411 | if (attribute_ == "enum") { |
| 412 | token_ = kTokenEnum; |
| 413 | return NoError(); |
| 414 | } |
| 415 | if (attribute_ == "union") { |
| 416 | token_ = kTokenUnion; |
| 417 | return NoError(); |
| 418 | } |
| 419 | if (attribute_ == "namespace") { |
| 420 | token_ = kTokenNameSpace; |
| 421 | return NoError(); |
| 422 | } |
| 423 | if (attribute_ == "root_type") { |
| 424 | token_ = kTokenRootType; |
| 425 | return NoError(); |
| 426 | } |
| 427 | if (attribute_ == "include") { |
| 428 | token_ = kTokenInclude; |
| 429 | return NoError(); |
| 430 | } |
| 431 | if (attribute_ == "attribute") { |
| 432 | token_ = kTokenAttribute; |
| 433 | return NoError(); |
| 434 | } |
Wouter van Oortmerssen | 5da7bda | 2014-07-31 15:11:03 -0700 | [diff] [blame] | 435 | if (attribute_ == "file_identifier") { |
| 436 | token_ = kTokenFileIdentifier; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 437 | return NoError(); |
Wouter van Oortmerssen | 5da7bda | 2014-07-31 15:11:03 -0700 | [diff] [blame] | 438 | } |
| 439 | if (attribute_ == "file_extension") { |
| 440 | token_ = kTokenFileExtension; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 441 | return NoError(); |
Wouter van Oortmerssen | 5da7bda | 2014-07-31 15:11:03 -0700 | [diff] [blame] | 442 | } |
Wouter van Oortmerssen | 049f3f7 | 2016-01-08 15:18:51 -0800 | [diff] [blame] | 443 | if (attribute_ == "null") { |
| 444 | token_ = kTokenNull; |
| 445 | return NoError(); |
| 446 | } |
Wouter van Oortmerssen | 1a63eb4 | 2016-03-07 18:07:10 -0800 | [diff] [blame] | 447 | if (attribute_ == "rpc_service") { |
| 448 | token_ = kTokenService; |
| 449 | return NoError(); |
| 450 | } |
Wouter van Oortmerssen | 3f936c5 | 2017-01-18 16:23:35 -0800 | [diff] [blame] | 451 | if (attribute_ == "native_include") { |
| 452 | token_ = kTokenNativeInclude; |
| 453 | return NoError(); |
| 454 | } |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 455 | // If not, it is a user-defined identifier: |
| 456 | token_ = kTokenIdentifier; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 457 | return NoError(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 458 | } else if (isdigit(static_cast<unsigned char>(c)) || c == '-') { |
| 459 | const char *start = cursor_ - 1; |
Wouter van Oortmerssen | 2957428 | 2017-01-30 17:07:55 -0800 | [diff] [blame] | 460 | if (c == '-' && *cursor_ == '0' && |
| 461 | (cursor_[1] == 'x' || cursor_[1] == 'X')) { |
Raman | f6f88e5 | 2016-07-12 19:47:53 +0200 | [diff] [blame] | 462 | ++start; |
| 463 | ++cursor_; |
| 464 | attribute_.append(&c, &c + 1); |
| 465 | c = '0'; |
| 466 | } |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 467 | if (c == '0' && (*cursor_ == 'x' || *cursor_ == 'X')) { |
| 468 | cursor_++; |
| 469 | while (isxdigit(static_cast<unsigned char>(*cursor_))) cursor_++; |
| 470 | attribute_.append(start + 2, cursor_); |
Wouter van Oortmerssen | 2957428 | 2017-01-30 17:07:55 -0800 | [diff] [blame] | 471 | attribute_ = NumToString(static_cast<int64_t>( |
| 472 | StringToUInt(attribute_.c_str(), nullptr, 16))); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 473 | token_ = kTokenIntegerConstant; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 474 | return NoError(); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 475 | } |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 476 | while (isdigit(static_cast<unsigned char>(*cursor_))) cursor_++; |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 477 | if (*cursor_ == '.' || *cursor_ == 'e' || *cursor_ == 'E') { |
| 478 | if (*cursor_ == '.') { |
| 479 | cursor_++; |
| 480 | while (isdigit(static_cast<unsigned char>(*cursor_))) cursor_++; |
| 481 | } |
Wouter van Oortmerssen | 93df569 | 2014-07-10 13:40:55 -0700 | [diff] [blame] | 482 | // See if this float has a scientific notation suffix. Both JSON |
| 483 | // and C++ (through strtod() we use) have the same format: |
| 484 | if (*cursor_ == 'e' || *cursor_ == 'E') { |
| 485 | cursor_++; |
| 486 | if (*cursor_ == '+' || *cursor_ == '-') cursor_++; |
| 487 | while (isdigit(static_cast<unsigned char>(*cursor_))) cursor_++; |
| 488 | } |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 489 | token_ = kTokenFloatConstant; |
| 490 | } else { |
| 491 | token_ = kTokenIntegerConstant; |
| 492 | } |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 493 | attribute_.append(start, cursor_); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 494 | return NoError(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 495 | } |
| 496 | std::string ch; |
| 497 | ch = c; |
| 498 | if (c < ' ' || c > '~') ch = "code: " + NumToString(c); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 499 | return Error("illegal character: " + ch); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 504 | // Check if a given token is next. |
| 505 | bool Parser::Is(int t) { |
| 506 | return t == token_; |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | // Expect a given token to be next, consume it, or error if not present. |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 510 | CheckedError Parser::Expect(int t) { |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 511 | if (t != token_) { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 512 | return Error("expecting: " + TokenToString(t) + " instead got: " + |
| 513 | TokenToStringId(token_)); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 514 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 515 | NEXT(); |
| 516 | return NoError(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 517 | } |
| 518 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 519 | CheckedError Parser::ParseNamespacing(std::string *id, std::string *last) { |
| 520 | while (Is('.')) { |
| 521 | NEXT(); |
Wouter van Oortmerssen | 39833d7 | 2015-05-08 15:04:53 -0700 | [diff] [blame] | 522 | *id += "."; |
| 523 | *id += attribute_; |
| 524 | if (last) *last = attribute_; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 525 | EXPECT(kTokenIdentifier); |
Wouter van Oortmerssen | 39833d7 | 2015-05-08 15:04:53 -0700 | [diff] [blame] | 526 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 527 | return NoError(); |
Wouter van Oortmerssen | 39833d7 | 2015-05-08 15:04:53 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | EnumDef *Parser::LookupEnum(const std::string &id) { |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 531 | // Search thru parent namespaces. |
Wouter van Oortmerssen | 321a1c9 | 2017-08-24 12:55:35 -0700 | [diff] [blame^] | 532 | for (int components = static_cast<int>(current_namespace_->components.size()); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 533 | components >= 0; components--) { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 534 | auto ed = enums_.Lookup( |
Wouter van Oortmerssen | 321a1c9 | 2017-08-24 12:55:35 -0700 | [diff] [blame^] | 535 | current_namespace_->GetFullyQualifiedName(id, components)); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 536 | if (ed) return ed; |
| 537 | } |
| 538 | return nullptr; |
Wouter van Oortmerssen | 39833d7 | 2015-05-08 15:04:53 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 541 | CheckedError Parser::ParseTypeIdent(Type &type) { |
Wouter van Oortmerssen | 39833d7 | 2015-05-08 15:04:53 -0700 | [diff] [blame] | 542 | std::string id = attribute_; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 543 | EXPECT(kTokenIdentifier); |
| 544 | ECHECK(ParseNamespacing(&id, nullptr)); |
Wouter van Oortmerssen | 39833d7 | 2015-05-08 15:04:53 -0700 | [diff] [blame] | 545 | auto enum_def = LookupEnum(id); |
Wouter van Oortmerssen | d38b9af | 2014-09-26 16:46:30 -0700 | [diff] [blame] | 546 | if (enum_def) { |
| 547 | type = enum_def->underlying_type; |
| 548 | if (enum_def->is_union) type.base_type = BASE_TYPE_UNION; |
| 549 | } else { |
| 550 | type.base_type = BASE_TYPE_STRUCT; |
Wouter van Oortmerssen | 39833d7 | 2015-05-08 15:04:53 -0700 | [diff] [blame] | 551 | type.struct_def = LookupCreateStruct(id); |
Wouter van Oortmerssen | d38b9af | 2014-09-26 16:46:30 -0700 | [diff] [blame] | 552 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 553 | return NoError(); |
Wouter van Oortmerssen | d38b9af | 2014-09-26 16:46:30 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 556 | // Parse any IDL type. |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 557 | CheckedError Parser::ParseType(Type &type) { |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 558 | if (token_ >= kTokenBOOL && token_ <= kTokenSTRING) { |
| 559 | type.base_type = static_cast<BaseType>(token_ - kTokenNONE); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 560 | NEXT(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 561 | } else { |
| 562 | if (token_ == kTokenIdentifier) { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 563 | ECHECK(ParseTypeIdent(type)); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 564 | } else if (token_ == '[') { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 565 | NEXT(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 566 | Type subtype; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 567 | ECHECK(ParseType(subtype)); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 568 | if (subtype.base_type == BASE_TYPE_VECTOR) { |
| 569 | // We could support this, but it will complicate things, and it's |
| 570 | // easier to work around with a struct around the inner vector. |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 571 | return Error( |
| 572 | "nested vector types not supported (wrap in table first)."); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 573 | } |
Wouter van Oortmerssen | 3fb6a86 | 2014-07-14 17:49:04 -0700 | [diff] [blame] | 574 | type = Type(BASE_TYPE_VECTOR, subtype.struct_def, subtype.enum_def); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 575 | type.element = subtype.base_type; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 576 | EXPECT(']'); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 577 | } else { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 578 | return Error("illegal type syntax"); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 579 | } |
| 580 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 581 | return NoError(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 582 | } |
| 583 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 584 | CheckedError Parser::AddField(StructDef &struct_def, const std::string &name, |
| 585 | const Type &type, FieldDef **dest) { |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 586 | auto &field = *new FieldDef(); |
| 587 | field.value.offset = |
| 588 | FieldIndexToOffset(static_cast<voffset_t>(struct_def.fields.vec.size())); |
| 589 | field.name = name; |
Gabriel Martinez | df4909e | 2014-11-04 10:00:56 -0800 | [diff] [blame] | 590 | field.file = struct_def.file; |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 591 | field.value.type = type; |
| 592 | if (struct_def.fixed) { // statically compute the field offset |
| 593 | auto size = InlineSize(type); |
| 594 | auto alignment = InlineAlignment(type); |
| 595 | // structs_ need to have a predictable format, so we need to align to |
| 596 | // the largest scalar |
| 597 | struct_def.minalign = std::max(struct_def.minalign, alignment); |
| 598 | struct_def.PadLastField(alignment); |
Wouter van Oortmerssen | 1256307 | 2014-06-30 15:56:31 -0700 | [diff] [blame] | 599 | field.value.offset = static_cast<voffset_t>(struct_def.bytesize); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 600 | struct_def.bytesize += size; |
| 601 | } |
| 602 | if (struct_def.fields.Add(name, &field)) |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 603 | return Error("field already exists: " + name); |
| 604 | *dest = &field; |
| 605 | return NoError(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 606 | } |
| 607 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 608 | CheckedError Parser::ParseField(StructDef &struct_def) { |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 609 | std::string name = attribute_; |
Wouter van Oortmerssen | e9f1f4d | 2017-06-02 15:55:02 -0700 | [diff] [blame] | 610 | |
Wouter van Oortmerssen | 8a58aaf | 2017-08-10 14:22:03 -0700 | [diff] [blame] | 611 | if (structs_.Lookup(name)) |
Wouter van Oortmerssen | e9f1f4d | 2017-06-02 15:55:02 -0700 | [diff] [blame] | 612 | return Error("field name can not be the same as table/struct name"); |
| 613 | |
Gabriel Martinez | 730c0ca | 2014-09-24 11:46:32 -0700 | [diff] [blame] | 614 | std::vector<std::string> dc = doc_comment_; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 615 | EXPECT(kTokenIdentifier); |
| 616 | EXPECT(':'); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 617 | Type type; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 618 | ECHECK(ParseType(type)); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 619 | |
| 620 | if (struct_def.fixed && !IsScalar(type.base_type) && !IsStruct(type)) |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 621 | return Error("structs_ may contain only scalar or struct fields"); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 622 | |
Wouter van Oortmerssen | 9140144 | 2014-07-07 17:34:23 -0700 | [diff] [blame] | 623 | FieldDef *typefield = nullptr; |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 624 | if (type.base_type == BASE_TYPE_UNION) { |
| 625 | // For union fields, add a second auto-generated field to hold the type, |
Wouter van Oortmerssen | 9e6c5f9 | 2016-06-20 16:06:30 -0700 | [diff] [blame] | 626 | // with a special suffix. |
| 627 | ECHECK(AddField(struct_def, name + UnionTypeFieldSuffix(), |
| 628 | type.enum_def->underlying_type, &typefield)); |
Bei Li | 68bbe98 | 2017-01-24 11:52:36 -0800 | [diff] [blame] | 629 | } else if (type.base_type == BASE_TYPE_VECTOR && |
| 630 | type.element == BASE_TYPE_UNION) { |
Kamil Rojewski | 46bb05d | 2017-08-11 18:24:36 +0200 | [diff] [blame] | 631 | // Only cpp, js and ts supports the union vector feature so far. |
| 632 | if (!SupportsVectorOfUnions()) { |
Bei Li | 68bbe98 | 2017-01-24 11:52:36 -0800 | [diff] [blame] | 633 | return Error("Vectors of unions are not yet supported in all " |
| 634 | "the specified programming languages."); |
| 635 | } |
| 636 | // For vector of union fields, add a second auto-generated vector field to |
| 637 | // hold the types, with a special suffix. |
| 638 | Type union_vector(BASE_TYPE_VECTOR, nullptr, type.enum_def); |
| 639 | union_vector.element = BASE_TYPE_UTYPE; |
| 640 | ECHECK(AddField(struct_def, name + UnionTypeFieldSuffix(), |
| 641 | union_vector, &typefield)); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 642 | } |
| 643 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 644 | FieldDef *field; |
| 645 | ECHECK(AddField(struct_def, name, type, &field)); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 646 | |
| 647 | if (token_ == '=') { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 648 | NEXT(); |
Wouter van Oortmerssen | 15dc1a8 | 2014-09-03 12:23:15 -0700 | [diff] [blame] | 649 | if (!IsScalar(type.base_type)) |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 650 | return Error("default values currently only supported for scalars"); |
| 651 | ECHECK(ParseSingleValue(field->value)); |
Wouter van Oortmerssen | fd542c7 | 2016-04-20 12:05:21 -0700 | [diff] [blame] | 652 | } |
| 653 | if (IsFloat(field->value.type.base_type)) { |
| 654 | if (!strpbrk(field->value.constant.c_str(), ".eE")) |
| 655 | field->value.constant += ".0"; |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 656 | } |
| 657 | |
Wouter van Oortmerssen | 7b80535 | 2014-09-23 11:55:42 -0700 | [diff] [blame] | 658 | if (type.enum_def && |
| 659 | IsScalar(type.base_type) && |
| 660 | !struct_def.fixed && |
| 661 | !type.enum_def->attributes.Lookup("bit_flags") && |
Wouter van Oortmerssen | d38b9af | 2014-09-26 16:46:30 -0700 | [diff] [blame] | 662 | !type.enum_def->ReverseLookup(static_cast<int>( |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 663 | StringToInt(field->value.constant.c_str())))) |
| 664 | return Error("enum " + type.enum_def->name + |
Wouter van Oortmerssen | 7b80535 | 2014-09-23 11:55:42 -0700 | [diff] [blame] | 665 | " does not have a declaration for this field\'s default of " + |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 666 | field->value.constant); |
Wouter van Oortmerssen | 7b80535 | 2014-09-23 11:55:42 -0700 | [diff] [blame] | 667 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 668 | field->doc_comment = dc; |
Wouter van Oortmerssen | e6b79f0 | 2016-03-09 15:03:05 -0800 | [diff] [blame] | 669 | ECHECK(ParseMetaData(&field->attributes)); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 670 | field->deprecated = field->attributes.Lookup("deprecated") != nullptr; |
| 671 | auto hash_name = field->attributes.Lookup("hash"); |
Alex Ames | d575321 | 2015-02-13 15:58:29 -0800 | [diff] [blame] | 672 | if (hash_name) { |
| 673 | switch (type.base_type) { |
| 674 | case BASE_TYPE_INT: |
| 675 | case BASE_TYPE_UINT: { |
| 676 | if (FindHashFunction32(hash_name->constant.c_str()) == nullptr) |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 677 | return Error("Unknown hashing algorithm for 32 bit types: " + |
Alex Ames | d575321 | 2015-02-13 15:58:29 -0800 | [diff] [blame] | 678 | hash_name->constant); |
| 679 | break; |
| 680 | } |
| 681 | case BASE_TYPE_LONG: |
| 682 | case BASE_TYPE_ULONG: { |
| 683 | if (FindHashFunction64(hash_name->constant.c_str()) == nullptr) |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 684 | return Error("Unknown hashing algorithm for 64 bit types: " + |
Alex Ames | d575321 | 2015-02-13 15:58:29 -0800 | [diff] [blame] | 685 | hash_name->constant); |
| 686 | break; |
| 687 | } |
| 688 | default: |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 689 | return Error( |
| 690 | "only int, uint, long and ulong data types support hashing."); |
Alex Ames | d575321 | 2015-02-13 15:58:29 -0800 | [diff] [blame] | 691 | } |
| 692 | } |
Wouter van Oortmerssen | dc2fa21 | 2016-10-05 16:59:15 -0700 | [diff] [blame] | 693 | auto cpp_type = field->attributes.Lookup("cpp_type"); |
| 694 | if (cpp_type) { |
| 695 | if (!hash_name) |
| 696 | return Error("cpp_type can only be used with a hashed field"); |
| 697 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 698 | if (field->deprecated && struct_def.fixed) |
| 699 | return Error("can't deprecate fields in a struct"); |
| 700 | field->required = field->attributes.Lookup("required") != nullptr; |
| 701 | if (field->required && (struct_def.fixed || |
| 702 | IsScalar(field->value.type.base_type))) |
| 703 | return Error("only non-scalar fields in tables may be 'required'"); |
| 704 | field->key = field->attributes.Lookup("key") != nullptr; |
| 705 | if (field->key) { |
Wouter van Oortmerssen | 3550899 | 2015-01-07 17:51:31 -0800 | [diff] [blame] | 706 | if (struct_def.has_key) |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 707 | return Error("only one field may be set as 'key'"); |
Wouter van Oortmerssen | 3550899 | 2015-01-07 17:51:31 -0800 | [diff] [blame] | 708 | struct_def.has_key = true; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 709 | if (!IsScalar(field->value.type.base_type)) { |
| 710 | field->required = true; |
| 711 | if (field->value.type.base_type != BASE_TYPE_STRING) |
| 712 | return Error("'key' field must be string or scalar type"); |
Wouter van Oortmerssen | 3550899 | 2015-01-07 17:51:31 -0800 | [diff] [blame] | 713 | } |
| 714 | } |
Wouter van Oortmerssen | 641b397 | 2016-12-02 14:25:08 -0800 | [diff] [blame] | 715 | |
| 716 | field->native_inline = field->attributes.Lookup("native_inline") != nullptr; |
| 717 | if (field->native_inline && !IsStruct(field->value.type)) |
| 718 | return Error("native_inline can only be defined on structs'"); |
| 719 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 720 | auto nested = field->attributes.Lookup("nested_flatbuffer"); |
Wouter van Oortmerssen | 3e201a9 | 2014-07-15 17:50:22 -0700 | [diff] [blame] | 721 | if (nested) { |
| 722 | if (nested->type.base_type != BASE_TYPE_STRING) |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 723 | return Error( |
| 724 | "nested_flatbuffer attribute must be a string (the root type)"); |
| 725 | if (field->value.type.base_type != BASE_TYPE_VECTOR || |
| 726 | field->value.type.element != BASE_TYPE_UCHAR) |
| 727 | return Error( |
| 728 | "nested_flatbuffer attribute may only apply to a vector of ubyte"); |
Wouter van Oortmerssen | 3e201a9 | 2014-07-15 17:50:22 -0700 | [diff] [blame] | 729 | // This will cause an error if the root type of the nested flatbuffer |
| 730 | // wasn't defined elsewhere. |
| 731 | LookupCreateStruct(nested->constant); |
Christian Helmich | 89a6894 | 2017-07-29 02:27:51 +0900 | [diff] [blame] | 732 | |
| 733 | // Keep a pointer to StructDef in FieldDef to simplify re-use later |
Wouter van Oortmerssen | 321a1c9 | 2017-08-24 12:55:35 -0700 | [diff] [blame^] | 734 | auto nested_qualified_name = |
| 735 | current_namespace_->GetFullyQualifiedName(nested->constant); |
Christian Helmich | 89a6894 | 2017-07-29 02:27:51 +0900 | [diff] [blame] | 736 | field->nested_flatbuffer = structs_.Lookup(nested_qualified_name); |
Wouter van Oortmerssen | 3e201a9 | 2014-07-15 17:50:22 -0700 | [diff] [blame] | 737 | } |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 738 | |
Wouter van Oortmerssen | dddd086 | 2017-06-02 16:41:22 -0700 | [diff] [blame] | 739 | if (field->attributes.Lookup("flexbuffer")) { |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 740 | field->flexbuffer = true; |
Wouter van Oortmerssen | dddd086 | 2017-06-02 16:41:22 -0700 | [diff] [blame] | 741 | uses_flexbuffers_ = true; |
| 742 | if (field->value.type.base_type != BASE_TYPE_VECTOR || |
| 743 | field->value.type.element != BASE_TYPE_UCHAR) |
| 744 | return Error( |
| 745 | "flexbuffer attribute may only apply to a vector of ubyte"); |
| 746 | } |
| 747 | |
Wouter van Oortmerssen | 9140144 | 2014-07-07 17:34:23 -0700 | [diff] [blame] | 748 | if (typefield) { |
Kamil Rojewski | 7cc72e4 | 2017-08-11 18:19:28 +0200 | [diff] [blame] | 749 | if (!IsScalar(typefield->value.type.base_type)) { |
| 750 | // this is a union vector field |
| 751 | typefield->required = field->required; |
| 752 | } |
Wouter van Oortmerssen | 9140144 | 2014-07-07 17:34:23 -0700 | [diff] [blame] | 753 | // If this field is a union, and it has a manually assigned id, |
| 754 | // the automatically added type field should have an id as well (of N - 1). |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 755 | auto attr = field->attributes.Lookup("id"); |
Wouter van Oortmerssen | 9140144 | 2014-07-07 17:34:23 -0700 | [diff] [blame] | 756 | if (attr) { |
| 757 | auto id = atoi(attr->constant.c_str()); |
| 758 | auto val = new Value(); |
| 759 | val->type = attr->type; |
| 760 | val->constant = NumToString(id - 1); |
| 761 | typefield->attributes.Add("id", val); |
| 762 | } |
| 763 | } |
| 764 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 765 | EXPECT(';'); |
| 766 | return NoError(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 767 | } |
| 768 | |
Wouter van Oortmerssen | b0752e1 | 2017-04-10 15:56:51 -0700 | [diff] [blame] | 769 | CheckedError Parser::ParseString(Value &val) { |
| 770 | auto s = attribute_; |
| 771 | EXPECT(kTokenStringConstant); |
| 772 | val.constant = NumToString(builder_.CreateString(s).o); |
| 773 | return NoError(); |
| 774 | } |
| 775 | |
Wouter van Oortmerssen | f325cce | 2017-06-16 11:57:58 -0700 | [diff] [blame] | 776 | CheckedError Parser::ParseComma() { |
| 777 | if (!opts.protobuf_ascii_alike) EXPECT(','); |
| 778 | return NoError(); |
| 779 | } |
| 780 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 781 | CheckedError Parser::ParseAnyValue(Value &val, FieldDef *field, |
Wouter van Oortmerssen | 9e6c5f9 | 2016-06-20 16:06:30 -0700 | [diff] [blame] | 782 | size_t parent_fieldn, |
| 783 | const StructDef *parent_struct_def) { |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 784 | switch (val.type.base_type) { |
| 785 | case BASE_TYPE_UNION: { |
| 786 | assert(field); |
Wouter van Oortmerssen | 9e6c5f9 | 2016-06-20 16:06:30 -0700 | [diff] [blame] | 787 | std::string constant; |
Wouter van Oortmerssen | eac2905 | 2017-01-18 12:02:31 -0800 | [diff] [blame] | 788 | // Find corresponding type field we may have already parsed. |
| 789 | for (auto elem = field_stack_.rbegin(); |
| 790 | elem != field_stack_.rbegin() + parent_fieldn; ++elem) { |
| 791 | auto &type = elem->second->value.type; |
| 792 | if (type.base_type == BASE_TYPE_UTYPE && |
| 793 | type.enum_def == val.type.enum_def) { |
| 794 | constant = elem->first.constant; |
| 795 | break; |
| 796 | } |
| 797 | } |
| 798 | if (constant.empty()) { |
Wouter van Oortmerssen | 9e6c5f9 | 2016-06-20 16:06:30 -0700 | [diff] [blame] | 799 | // We haven't seen the type field yet. Sadly a lot of JSON writers |
| 800 | // output these in alphabetical order, meaning it comes after this |
| 801 | // value. So we scan past the value to find it, then come back here. |
| 802 | auto type_name = field->name + UnionTypeFieldSuffix(); |
| 803 | assert(parent_struct_def); |
| 804 | auto type_field = parent_struct_def->fields.Lookup(type_name); |
| 805 | assert(type_field); // Guaranteed by ParseField(). |
| 806 | // Remember where we are in the source file, so we can come back here. |
| 807 | auto backup = *static_cast<ParserState *>(this); |
| 808 | ECHECK(SkipAnyJsonValue()); // The table. |
Wouter van Oortmerssen | f325cce | 2017-06-16 11:57:58 -0700 | [diff] [blame] | 809 | ECHECK(ParseComma()); |
Wouter van Oortmerssen | 9e6c5f9 | 2016-06-20 16:06:30 -0700 | [diff] [blame] | 810 | auto next_name = attribute_; |
| 811 | if (Is(kTokenStringConstant)) { |
| 812 | NEXT(); |
| 813 | } else { |
| 814 | EXPECT(kTokenIdentifier); |
| 815 | } |
| 816 | if (next_name != type_name) |
| 817 | return Error("missing type field after this union value: " + |
| 818 | type_name); |
| 819 | EXPECT(':'); |
| 820 | Value type_val = type_field->value; |
| 821 | ECHECK(ParseAnyValue(type_val, type_field, 0, nullptr)); |
| 822 | constant = type_val.constant; |
| 823 | // Got the information we needed, now rewind: |
| 824 | *static_cast<ParserState *>(this) = backup; |
Wouter van Oortmerssen | 9e6c5f9 | 2016-06-20 16:06:30 -0700 | [diff] [blame] | 825 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 826 | uint8_t enum_idx; |
Wouter van Oortmerssen | 9e6c5f9 | 2016-06-20 16:06:30 -0700 | [diff] [blame] | 827 | ECHECK(atot(constant.c_str(), *this, &enum_idx)); |
Wouter van Oortmerssen | 3fb6a86 | 2014-07-14 17:49:04 -0700 | [diff] [blame] | 828 | auto enum_val = val.type.enum_def->ReverseLookup(enum_idx); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 829 | if (!enum_val) return Error("illegal type id for: " + field->name); |
Wouter van Oortmerssen | b0752e1 | 2017-04-10 15:56:51 -0700 | [diff] [blame] | 830 | if (enum_val->union_type.base_type == BASE_TYPE_STRUCT) { |
| 831 | ECHECK(ParseTable(*enum_val->union_type.struct_def, &val.constant, |
| 832 | nullptr)); |
| 833 | if (enum_val->union_type.struct_def->fixed) { |
| 834 | // All BASE_TYPE_UNION values are offsets, so turn this into one. |
| 835 | SerializeStruct(*enum_val->union_type.struct_def, val); |
| 836 | builder_.ClearOffsets(); |
| 837 | val.constant = NumToString(builder_.GetSize()); |
| 838 | } |
| 839 | } else if (enum_val->union_type.base_type == BASE_TYPE_STRING) { |
| 840 | ECHECK(ParseString(val)); |
| 841 | } else { |
| 842 | assert(false); |
| 843 | } |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 844 | break; |
| 845 | } |
| 846 | case BASE_TYPE_STRUCT: |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 847 | ECHECK(ParseTable(*val.type.struct_def, &val.constant, nullptr)); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 848 | break; |
| 849 | case BASE_TYPE_STRING: { |
Wouter van Oortmerssen | b0752e1 | 2017-04-10 15:56:51 -0700 | [diff] [blame] | 850 | ECHECK(ParseString(val)); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 851 | break; |
| 852 | } |
| 853 | case BASE_TYPE_VECTOR: { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 854 | uoffset_t off; |
| 855 | ECHECK(ParseVector(val.type.VectorType(), &off)); |
| 856 | val.constant = NumToString(off); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 857 | break; |
| 858 | } |
Alex Ames | d575321 | 2015-02-13 15:58:29 -0800 | [diff] [blame] | 859 | case BASE_TYPE_INT: |
| 860 | case BASE_TYPE_UINT: |
| 861 | case BASE_TYPE_LONG: |
| 862 | case BASE_TYPE_ULONG: { |
| 863 | if (field && field->attributes.Lookup("hash") && |
| 864 | (token_ == kTokenIdentifier || token_ == kTokenStringConstant)) { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 865 | ECHECK(ParseHash(val, field)); |
Alex Ames | d575321 | 2015-02-13 15:58:29 -0800 | [diff] [blame] | 866 | } else { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 867 | ECHECK(ParseSingleValue(val)); |
Alex Ames | d575321 | 2015-02-13 15:58:29 -0800 | [diff] [blame] | 868 | } |
| 869 | break; |
| 870 | } |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 871 | default: |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 872 | ECHECK(ParseSingleValue(val)); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 873 | break; |
| 874 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 875 | return NoError(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | void Parser::SerializeStruct(const StructDef &struct_def, const Value &val) { |
Wouter van Oortmerssen | 4d78104 | 2015-09-28 15:02:41 -0700 | [diff] [blame] | 879 | assert(val.constant.length() == struct_def.bytesize); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 880 | builder_.Align(struct_def.minalign); |
Wouter van Oortmerssen | 4d78104 | 2015-09-28 15:02:41 -0700 | [diff] [blame] | 881 | builder_.PushBytes(reinterpret_cast<const uint8_t *>(val.constant.c_str()), |
| 882 | struct_def.bytesize); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 883 | builder_.AddStructOffset(val.offset, builder_.GetSize()); |
| 884 | } |
| 885 | |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 886 | CheckedError Parser::ParseTableDelimiters(size_t &fieldn, |
| 887 | const StructDef *struct_def, |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 888 | ParseTableDelimitersBody body, |
| 889 | void *state) { |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 890 | // We allow tables both as JSON object{ .. } with field names |
Guillaume Giraud | b174068 | 2017-06-08 01:58:19 +0200 | [diff] [blame] | 891 | // or vector[..] with all fields in order |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 892 | char terminator = '}'; |
| 893 | bool is_nested_vector = struct_def && Is('['); |
| 894 | if (is_nested_vector) { |
Guillaume Giraud | b174068 | 2017-06-08 01:58:19 +0200 | [diff] [blame] | 895 | NEXT(); |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 896 | terminator = ']'; |
Guillaume Giraud | b174068 | 2017-06-08 01:58:19 +0200 | [diff] [blame] | 897 | } else { |
| 898 | EXPECT('{'); |
| 899 | } |
Wouter van Oortmerssen | 6c2dc41 | 2015-01-16 16:57:04 -0800 | [diff] [blame] | 900 | for (;;) { |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 901 | if ((!opts.strict_json || !fieldn) && Is(terminator)) break; |
Guillaume Giraud | b174068 | 2017-06-08 01:58:19 +0200 | [diff] [blame] | 902 | std::string name; |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 903 | if (is_nested_vector) { |
| 904 | if (fieldn > struct_def->fields.vec.size()) { |
Guillaume Giraud | b174068 | 2017-06-08 01:58:19 +0200 | [diff] [blame] | 905 | return Error("too many unnamed fields in nested array"); |
| 906 | } |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 907 | name = struct_def->fields.vec[fieldn]->name; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 908 | } else { |
Guillaume Giraud | b174068 | 2017-06-08 01:58:19 +0200 | [diff] [blame] | 909 | name = attribute_; |
| 910 | if (Is(kTokenStringConstant)) { |
| 911 | NEXT(); |
| 912 | } else { |
| 913 | EXPECT(opts.strict_json ? kTokenStringConstant : kTokenIdentifier); |
| 914 | } |
Wouter van Oortmerssen | f325cce | 2017-06-16 11:57:58 -0700 | [diff] [blame] | 915 | if (!opts.protobuf_ascii_alike || !(Is('{') || Is('['))) EXPECT(':'); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 916 | } |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 917 | ECHECK(body(name, fieldn, struct_def, state)); |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 918 | if (Is(terminator)) break; |
Wouter van Oortmerssen | f325cce | 2017-06-16 11:57:58 -0700 | [diff] [blame] | 919 | ECHECK(ParseComma()); |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 920 | } |
| 921 | NEXT(); |
| 922 | if (is_nested_vector && fieldn != struct_def->fields.vec.size()) { |
| 923 | return Error("wrong number of unnamed fields in table vector"); |
| 924 | } |
| 925 | return NoError(); |
| 926 | } |
| 927 | |
| 928 | CheckedError Parser::ParseTable(const StructDef &struct_def, std::string *value, |
| 929 | uoffset_t *ovalue) { |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 930 | size_t fieldn_outer = 0; |
| 931 | auto err = ParseTableDelimiters(fieldn_outer, &struct_def, |
| 932 | [](const std::string &name, size_t &fieldn, |
| 933 | const StructDef *struct_def_inner, |
| 934 | void *state) -> CheckedError { |
| 935 | Parser *parser = static_cast<Parser *>(state); |
schoetbi | 2e2063c | 2017-07-19 17:58:48 +0200 | [diff] [blame] | 936 | if (name == "$schema") { |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 937 | ECHECK(parser->Expect(kTokenStringConstant)); |
schoetbi | 2e2063c | 2017-07-19 17:58:48 +0200 | [diff] [blame] | 938 | return NoError(); |
| 939 | } |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 940 | auto field = struct_def_inner->fields.Lookup(name); |
Nalinichandra Penke | 13d0594 | 2015-12-22 00:02:19 -0800 | [diff] [blame] | 941 | if (!field) { |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 942 | if (!parser->opts.skip_unexpected_fields_in_json) { |
| 943 | return parser->Error("unknown field: " + name); |
Nalinichandra Penke | 13d0594 | 2015-12-22 00:02:19 -0800 | [diff] [blame] | 944 | } else { |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 945 | ECHECK(parser->SkipAnyJsonValue()); |
Nalinichandra Penke | 13d0594 | 2015-12-22 00:02:19 -0800 | [diff] [blame] | 946 | } |
| 947 | } else { |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 948 | if (parser->Is(kTokenNull)) { |
| 949 | ECHECK(parser->Next()); // Ignore this field. |
Wouter van Oortmerssen | 049f3f7 | 2016-01-08 15:18:51 -0800 | [diff] [blame] | 950 | } else { |
| 951 | Value val = field->value; |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 952 | if (field->flexbuffer) { |
| 953 | flexbuffers::Builder builder(1024, |
| 954 | flexbuffers::BUILDER_FLAG_SHARE_ALL); |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 955 | ECHECK(parser->ParseFlexBufferValue(&builder)); |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 956 | builder.Finish(); |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 957 | auto off = parser->builder_.CreateVector(builder.GetBuffer()); |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 958 | val.constant = NumToString(off.o); |
Christian Helmich | 89a6894 | 2017-07-29 02:27:51 +0900 | [diff] [blame] | 959 | } else if (field->nested_flatbuffer) { |
| 960 | ECHECK(parser->ParseNestedFlatbuffer(val, field, fieldn, struct_def_inner)); |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 961 | } else { |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 962 | ECHECK(parser->ParseAnyValue(val, field, fieldn, struct_def_inner)); |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 963 | } |
Wouter van Oortmerssen | 049f3f7 | 2016-01-08 15:18:51 -0800 | [diff] [blame] | 964 | // Hardcoded insertion-sort with error-check. |
| 965 | // If fields are specified in order, then this loop exits immediately. |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 966 | auto elem = parser->field_stack_.rbegin(); |
| 967 | for (; elem != parser->field_stack_.rbegin() + fieldn; ++elem) { |
Wouter van Oortmerssen | eac2905 | 2017-01-18 12:02:31 -0800 | [diff] [blame] | 968 | auto existing_field = elem->second; |
Wouter van Oortmerssen | 049f3f7 | 2016-01-08 15:18:51 -0800 | [diff] [blame] | 969 | if (existing_field == field) |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 970 | return parser->Error("field set more than once: " + field->name); |
Wouter van Oortmerssen | 049f3f7 | 2016-01-08 15:18:51 -0800 | [diff] [blame] | 971 | if (existing_field->value.offset < field->value.offset) break; |
| 972 | } |
Wouter van Oortmerssen | eac2905 | 2017-01-18 12:02:31 -0800 | [diff] [blame] | 973 | // Note: elem points to before the insertion point, thus .base() points |
| 974 | // to the correct spot. |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 975 | parser->field_stack_.insert(elem.base(), |
| 976 | std::make_pair(val, field)); |
Wouter van Oortmerssen | 049f3f7 | 2016-01-08 15:18:51 -0800 | [diff] [blame] | 977 | fieldn++; |
Nalinichandra Penke | 13d0594 | 2015-12-22 00:02:19 -0800 | [diff] [blame] | 978 | } |
Wouter van Oortmerssen | 4d78104 | 2015-09-28 15:02:41 -0700 | [diff] [blame] | 979 | } |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 980 | return NoError(); |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 981 | }, this); |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 982 | ECHECK(err); |
Nalinichandra Penke | 13d0594 | 2015-12-22 00:02:19 -0800 | [diff] [blame] | 983 | |
sfariv | 55dec4d | 2017-05-18 09:32:04 -0700 | [diff] [blame] | 984 | // Check if all required fields are parsed. |
| 985 | for (auto field_it = struct_def.fields.vec.begin(); |
| 986 | field_it != struct_def.fields.vec.end(); |
| 987 | ++field_it) { |
| 988 | auto required_field = *field_it; |
| 989 | if (!required_field->required) { |
| 990 | continue; |
| 991 | } |
| 992 | bool found = false; |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 993 | for (auto pf_it = field_stack_.end() - fieldn_outer; |
sfariv | 55dec4d | 2017-05-18 09:32:04 -0700 | [diff] [blame] | 994 | pf_it != field_stack_.end(); |
| 995 | ++pf_it) { |
| 996 | auto parsed_field = pf_it->second; |
| 997 | if (parsed_field == required_field) { |
| 998 | found = true; |
| 999 | break; |
| 1000 | } |
| 1001 | } |
| 1002 | if (!found) { |
| 1003 | return Error("required field is missing: " + required_field->name + " in " + struct_def.name); |
| 1004 | } |
| 1005 | } |
| 1006 | |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 1007 | if (struct_def.fixed && fieldn_outer != struct_def.fields.vec.size()) |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1008 | return Error("struct: wrong number of initializers: " + struct_def.name); |
Wouter van Oortmerssen | 4d78104 | 2015-09-28 15:02:41 -0700 | [diff] [blame] | 1009 | |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1010 | auto start = struct_def.fixed |
| 1011 | ? builder_.StartStruct(struct_def.minalign) |
| 1012 | : builder_.StartTable(); |
| 1013 | |
| 1014 | for (size_t size = struct_def.sortbysize ? sizeof(largest_scalar_t) : 1; |
| 1015 | size; |
| 1016 | size /= 2) { |
| 1017 | // Go through elements in reverse, since we're building the data backwards. |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 1018 | for (auto it = field_stack_.rbegin(); it != field_stack_.rbegin() + |
| 1019 | fieldn_outer; |
| 1020 | ++it) { |
Shuhei Tanuma | 721d219 | 2015-11-06 07:14:21 +0900 | [diff] [blame] | 1021 | auto &field_value = it->first; |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1022 | auto field = it->second; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1023 | if (!struct_def.sortbysize || |
| 1024 | size == SizeOf(field_value.type.base_type)) { |
Shuhei Tanuma | 721d219 | 2015-11-06 07:14:21 +0900 | [diff] [blame] | 1025 | switch (field_value.type.base_type) { |
MikkelFJ | f2b3705 | 2017-08-02 17:07:43 +0200 | [diff] [blame] | 1026 | #define FLATBUFFERS_TD(ENUM, IDLTYPE, ALIASTYPE, \ |
| 1027 | CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \ |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1028 | case BASE_TYPE_ ## ENUM: \ |
| 1029 | builder_.Pad(field->padding); \ |
Wouter van Oortmerssen | be3c874 | 2014-08-11 17:38:54 -0700 | [diff] [blame] | 1030 | if (struct_def.fixed) { \ |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1031 | CTYPE val; \ |
| 1032 | ECHECK(atot(field_value.constant.c_str(), *this, &val)); \ |
| 1033 | builder_.PushElement(val); \ |
Wouter van Oortmerssen | be3c874 | 2014-08-11 17:38:54 -0700 | [diff] [blame] | 1034 | } else { \ |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1035 | CTYPE val, valdef; \ |
| 1036 | ECHECK(atot(field_value.constant.c_str(), *this, &val)); \ |
| 1037 | ECHECK(atot(field->value.constant.c_str(), *this, &valdef)); \ |
| 1038 | builder_.AddElement(field_value.offset, val, valdef); \ |
Wouter van Oortmerssen | be3c874 | 2014-08-11 17:38:54 -0700 | [diff] [blame] | 1039 | } \ |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1040 | break; |
| 1041 | FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD); |
| 1042 | #undef FLATBUFFERS_TD |
MikkelFJ | f2b3705 | 2017-08-02 17:07:43 +0200 | [diff] [blame] | 1043 | #define FLATBUFFERS_TD(ENUM, IDLTYPE, ALIASTYPE, \ |
| 1044 | CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \ |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1045 | case BASE_TYPE_ ## ENUM: \ |
| 1046 | builder_.Pad(field->padding); \ |
| 1047 | if (IsStruct(field->value.type)) { \ |
Shuhei Tanuma | 721d219 | 2015-11-06 07:14:21 +0900 | [diff] [blame] | 1048 | SerializeStruct(*field->value.type.struct_def, field_value); \ |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1049 | } else { \ |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1050 | CTYPE val; \ |
| 1051 | ECHECK(atot(field_value.constant.c_str(), *this, &val)); \ |
| 1052 | builder_.AddOffset(field_value.offset, val); \ |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1053 | } \ |
| 1054 | break; |
| 1055 | FLATBUFFERS_GEN_TYPES_POINTER(FLATBUFFERS_TD); |
| 1056 | #undef FLATBUFFERS_TD |
| 1057 | } |
| 1058 | } |
| 1059 | } |
| 1060 | } |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 1061 | for (size_t i = 0; i < fieldn_outer; i++) field_stack_.pop_back(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1062 | |
| 1063 | if (struct_def.fixed) { |
| 1064 | builder_.ClearOffsets(); |
| 1065 | builder_.EndStruct(); |
Wouter van Oortmerssen | 4d78104 | 2015-09-28 15:02:41 -0700 | [diff] [blame] | 1066 | assert(value); |
| 1067 | // Temporarily store this struct in the value string, since it is to |
| 1068 | // be serialized in-place elsewhere. |
| 1069 | value->assign( |
| 1070 | reinterpret_cast<const char *>(builder_.GetCurrentBufferPointer()), |
| 1071 | struct_def.bytesize); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1072 | builder_.PopBytes(struct_def.bytesize); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1073 | assert(!ovalue); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1074 | } else { |
Wouter van Oortmerssen | ac1015e | 2017-08-21 13:44:23 -0700 | [diff] [blame] | 1075 | auto val = builder_.EndTable(start); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1076 | if (ovalue) *ovalue = val; |
| 1077 | if (value) *value = NumToString(val); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1078 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1079 | return NoError(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1080 | } |
| 1081 | |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 1082 | CheckedError Parser::ParseVectorDelimiters(size_t &count, |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 1083 | ParseVectorDelimitersBody body, |
| 1084 | void *state) { |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 1085 | EXPECT('['); |
Wouter van Oortmerssen | 6c2dc41 | 2015-01-16 16:57:04 -0800 | [diff] [blame] | 1086 | for (;;) { |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 1087 | if ((!opts.strict_json || !count) && Is(']')) break; |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 1088 | ECHECK(body(count, state)); |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 1089 | count++; |
| 1090 | if (Is(']')) break; |
Wouter van Oortmerssen | f325cce | 2017-06-16 11:57:58 -0700 | [diff] [blame] | 1091 | ECHECK(ParseComma()); |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 1092 | } |
| 1093 | NEXT(); |
| 1094 | return NoError(); |
| 1095 | } |
| 1096 | |
| 1097 | CheckedError Parser::ParseVector(const Type &type, uoffset_t *ovalue) { |
| 1098 | size_t count = 0; |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 1099 | std::pair<Parser *, const Type &> parser_and_type_state(this, type); |
| 1100 | auto err = ParseVectorDelimiters(count, |
| 1101 | [](size_t &, void *state) -> CheckedError { |
| 1102 | auto *parser_and_type = |
| 1103 | static_cast<std::pair<Parser *, const Type &> *>(state); |
| 1104 | auto *parser = parser_and_type->first; |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1105 | Value val; |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 1106 | val.type = parser_and_type->second; |
| 1107 | ECHECK(parser->ParseAnyValue(val, nullptr, 0, nullptr)); |
| 1108 | parser->field_stack_.push_back(std::make_pair(val, nullptr)); |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 1109 | return NoError(); |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 1110 | }, &parser_and_type_state); |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 1111 | ECHECK(err); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1112 | |
Wouter van Oortmerssen | be3c874 | 2014-08-11 17:38:54 -0700 | [diff] [blame] | 1113 | builder_.StartVector(count * InlineSize(type) / InlineAlignment(type), |
| 1114 | InlineAlignment(type)); |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 1115 | for (size_t i = 0; i < count; i++) { |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1116 | // start at the back, since we're building the data backwards. |
| 1117 | auto &val = field_stack_.back().first; |
| 1118 | switch (val.type.base_type) { |
MikkelFJ | f2b3705 | 2017-08-02 17:07:43 +0200 | [diff] [blame] | 1119 | #define FLATBUFFERS_TD(ENUM, IDLTYPE, ALIASTYPE, \ |
| 1120 | CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \ |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1121 | case BASE_TYPE_ ## ENUM: \ |
| 1122 | if (IsStruct(val.type)) SerializeStruct(*val.type.struct_def, val); \ |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1123 | else { \ |
| 1124 | CTYPE elem; \ |
| 1125 | ECHECK(atot(val.constant.c_str(), *this, &elem)); \ |
| 1126 | builder_.PushElement(elem); \ |
| 1127 | } \ |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1128 | break; |
| 1129 | FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD) |
| 1130 | #undef FLATBUFFERS_TD |
| 1131 | } |
| 1132 | field_stack_.pop_back(); |
| 1133 | } |
| 1134 | |
| 1135 | builder_.ClearOffsets(); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1136 | *ovalue = builder_.EndVector(count); |
| 1137 | return NoError(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1138 | } |
| 1139 | |
Christian Helmich | 89a6894 | 2017-07-29 02:27:51 +0900 | [diff] [blame] | 1140 | CheckedError Parser::ParseNestedFlatbuffer(Value &val, FieldDef *field, |
| 1141 | size_t fieldn, |
| 1142 | const StructDef *parent_struct_def) { |
| 1143 | if (token_ == '[') {// backwards compat for 'legacy' ubyte buffers |
| 1144 | ECHECK(ParseAnyValue(val, field, fieldn, parent_struct_def)); |
| 1145 | } else { |
| 1146 | auto cursor_at_value_begin = cursor_; |
| 1147 | ECHECK(SkipAnyJsonValue()); |
| 1148 | std::string substring(cursor_at_value_begin -1 , cursor_ -1); |
| 1149 | |
| 1150 | // Create and initialize new parser |
| 1151 | Parser nested_parser; |
| 1152 | assert(field->nested_flatbuffer); |
| 1153 | nested_parser.root_struct_def_ = field->nested_flatbuffer; |
| 1154 | nested_parser.enums_ = enums_; |
| 1155 | nested_parser.opts = opts; |
| 1156 | nested_parser.uses_flexbuffers_ = uses_flexbuffers_; |
| 1157 | |
| 1158 | // Parse JSON substring into new flatbuffer builder using nested_parser |
| 1159 | if (!nested_parser.Parse(substring.c_str(), nullptr, nullptr)) { |
| 1160 | ECHECK(Error(nested_parser.error_)); |
| 1161 | } |
| 1162 | auto off = builder_.CreateVector(nested_parser.builder_.GetBufferPointer(), nested_parser.builder_.GetSize()); |
| 1163 | val.constant = NumToString(off.o); |
| 1164 | |
| 1165 | // Clean nested_parser before destruction to avoid deleting the elements in the SymbolTables |
| 1166 | nested_parser.enums_.dict.clear(); |
| 1167 | nested_parser.enums_.vec.clear(); |
| 1168 | } |
| 1169 | return NoError(); |
| 1170 | } |
| 1171 | |
Wouter van Oortmerssen | e6b79f0 | 2016-03-09 15:03:05 -0800 | [diff] [blame] | 1172 | CheckedError Parser::ParseMetaData(SymbolTable<Value> *attributes) { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1173 | if (Is('(')) { |
| 1174 | NEXT(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1175 | for (;;) { |
| 1176 | auto name = attribute_; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1177 | EXPECT(kTokenIdentifier); |
Wouter van Oortmerssen | 0952143 | 2014-11-17 17:27:26 -0800 | [diff] [blame] | 1178 | if (known_attributes_.find(name) == known_attributes_.end()) |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1179 | return Error("user define attributes must be declared before use: " + |
| 1180 | name); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1181 | auto e = new Value(); |
Wouter van Oortmerssen | e6b79f0 | 2016-03-09 15:03:05 -0800 | [diff] [blame] | 1182 | attributes->Add(name, e); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1183 | if (Is(':')) { |
| 1184 | NEXT(); |
| 1185 | ECHECK(ParseSingleValue(*e)); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1186 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1187 | if (Is(')')) { NEXT(); break; } |
| 1188 | EXPECT(','); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1189 | } |
| 1190 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1191 | return NoError(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1192 | } |
| 1193 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1194 | CheckedError Parser::TryTypedValue(int dtoken, bool check, Value &e, |
| 1195 | BaseType req, bool *destmatch) { |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1196 | bool match = dtoken == token_; |
| 1197 | if (match) { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1198 | *destmatch = true; |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1199 | e.constant = attribute_; |
| 1200 | if (!check) { |
| 1201 | if (e.type.base_type == BASE_TYPE_NONE) { |
| 1202 | e.type.base_type = req; |
| 1203 | } else { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1204 | return Error(std::string("type mismatch: expecting: ") + |
| 1205 | kTypeNames[e.type.base_type] + |
| 1206 | ", found: " + |
| 1207 | kTypeNames[req]); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1208 | } |
| 1209 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1210 | NEXT(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1211 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1212 | return NoError(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1213 | } |
| 1214 | |
Wouter van Oortmerssen | fbc8af4 | 2016-03-07 17:22:51 -0800 | [diff] [blame] | 1215 | CheckedError Parser::ParseEnumFromString(Type &type, int64_t *result) { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1216 | *result = 0; |
Wouter van Oortmerssen | 9c3de1e | 2014-07-25 15:04:35 -0700 | [diff] [blame] | 1217 | // Parse one or more enum identifiers, separated by spaces. |
| 1218 | const char *next = attribute_.c_str(); |
| 1219 | do { |
| 1220 | const char *divider = strchr(next, ' '); |
| 1221 | std::string word; |
| 1222 | if (divider) { |
| 1223 | word = std::string(next, divider); |
| 1224 | next = divider + strspn(divider, " "); |
| 1225 | } else { |
| 1226 | word = next; |
| 1227 | next += word.length(); |
| 1228 | } |
| 1229 | if (type.enum_def) { // The field has an enum type |
| 1230 | auto enum_val = type.enum_def->vals.Lookup(word); |
| 1231 | if (!enum_val) |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1232 | return Error("unknown enum value: " + word + |
Wouter van Oortmerssen | 9c3de1e | 2014-07-25 15:04:35 -0700 | [diff] [blame] | 1233 | ", for enum: " + type.enum_def->name); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1234 | *result |= enum_val->value; |
Wouter van Oortmerssen | 9c3de1e | 2014-07-25 15:04:35 -0700 | [diff] [blame] | 1235 | } else { // No enum type, probably integral field. |
| 1236 | if (!IsInteger(type.base_type)) |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1237 | return Error("not a valid value for this field: " + word); |
Wouter van Oortmerssen | 9c3de1e | 2014-07-25 15:04:35 -0700 | [diff] [blame] | 1238 | // TODO: could check if its a valid number constant here. |
Wouter van Oortmerssen | 39833d7 | 2015-05-08 15:04:53 -0700 | [diff] [blame] | 1239 | const char *dot = strrchr(word.c_str(), '.'); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1240 | if (!dot) |
| 1241 | return Error("enum values need to be qualified by an enum type"); |
Wouter van Oortmerssen | 9c3de1e | 2014-07-25 15:04:35 -0700 | [diff] [blame] | 1242 | std::string enum_def_str(word.c_str(), dot); |
| 1243 | std::string enum_val_str(dot + 1, word.c_str() + word.length()); |
Wouter van Oortmerssen | 39833d7 | 2015-05-08 15:04:53 -0700 | [diff] [blame] | 1244 | auto enum_def = LookupEnum(enum_def_str); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1245 | if (!enum_def) return Error("unknown enum: " + enum_def_str); |
Wouter van Oortmerssen | 9c3de1e | 2014-07-25 15:04:35 -0700 | [diff] [blame] | 1246 | auto enum_val = enum_def->vals.Lookup(enum_val_str); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1247 | if (!enum_val) return Error("unknown enum value: " + enum_val_str); |
| 1248 | *result |= enum_val->value; |
Wouter van Oortmerssen | 9c3de1e | 2014-07-25 15:04:35 -0700 | [diff] [blame] | 1249 | } |
| 1250 | } while(*next); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1251 | return NoError(); |
Wouter van Oortmerssen | 9c3de1e | 2014-07-25 15:04:35 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
Alex Ames | d575321 | 2015-02-13 15:58:29 -0800 | [diff] [blame] | 1254 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1255 | CheckedError Parser::ParseHash(Value &e, FieldDef* field) { |
Alex Ames | d575321 | 2015-02-13 15:58:29 -0800 | [diff] [blame] | 1256 | assert(field); |
| 1257 | Value *hash_name = field->attributes.Lookup("hash"); |
| 1258 | switch (e.type.base_type) { |
Wouter van Oortmerssen | 46497e4 | 2017-04-17 17:40:02 -0700 | [diff] [blame] | 1259 | case BASE_TYPE_INT: { |
| 1260 | auto hash = FindHashFunction32(hash_name->constant.c_str()); |
| 1261 | int32_t hashed_value = static_cast<int32_t>(hash(attribute_.c_str())); |
| 1262 | e.constant = NumToString(hashed_value); |
| 1263 | break; |
| 1264 | } |
Alex Ames | d575321 | 2015-02-13 15:58:29 -0800 | [diff] [blame] | 1265 | case BASE_TYPE_UINT: { |
| 1266 | auto hash = FindHashFunction32(hash_name->constant.c_str()); |
| 1267 | uint32_t hashed_value = hash(attribute_.c_str()); |
| 1268 | e.constant = NumToString(hashed_value); |
| 1269 | break; |
| 1270 | } |
Wouter van Oortmerssen | 46497e4 | 2017-04-17 17:40:02 -0700 | [diff] [blame] | 1271 | case BASE_TYPE_LONG: { |
| 1272 | auto hash = FindHashFunction64(hash_name->constant.c_str()); |
| 1273 | int64_t hashed_value = static_cast<int64_t>(hash(attribute_.c_str())); |
| 1274 | e.constant = NumToString(hashed_value); |
| 1275 | break; |
| 1276 | } |
Alex Ames | d575321 | 2015-02-13 15:58:29 -0800 | [diff] [blame] | 1277 | case BASE_TYPE_ULONG: { |
| 1278 | auto hash = FindHashFunction64(hash_name->constant.c_str()); |
| 1279 | uint64_t hashed_value = hash(attribute_.c_str()); |
| 1280 | e.constant = NumToString(hashed_value); |
| 1281 | break; |
| 1282 | } |
| 1283 | default: |
| 1284 | assert(0); |
| 1285 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1286 | NEXT(); |
| 1287 | return NoError(); |
Alex Ames | d575321 | 2015-02-13 15:58:29 -0800 | [diff] [blame] | 1288 | } |
| 1289 | |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 1290 | CheckedError Parser::TokenError() { |
| 1291 | return Error("cannot parse value starting with: " + |
| 1292 | TokenToStringId(token_)); |
| 1293 | } |
| 1294 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1295 | CheckedError Parser::ParseSingleValue(Value &e) { |
Wouter van Oortmerssen | d3ac0bc | 2016-06-15 13:54:17 -0700 | [diff] [blame] | 1296 | // First see if this could be a conversion function: |
| 1297 | if (token_ == kTokenIdentifier && *cursor_ == '(') { |
| 1298 | auto functionname = attribute_; |
| 1299 | NEXT(); |
| 1300 | EXPECT('('); |
| 1301 | ECHECK(ParseSingleValue(e)); |
| 1302 | EXPECT(')'); |
| 1303 | #define FLATBUFFERS_FN_DOUBLE(name, op) \ |
| 1304 | if (functionname == name) { \ |
| 1305 | auto x = strtod(e.constant.c_str(), nullptr); \ |
| 1306 | e.constant = NumToString(op); \ |
| 1307 | } |
| 1308 | FLATBUFFERS_FN_DOUBLE("deg", x / M_PI * 180); |
| 1309 | FLATBUFFERS_FN_DOUBLE("rad", x * M_PI / 180); |
| 1310 | FLATBUFFERS_FN_DOUBLE("sin", sin(x)); |
| 1311 | FLATBUFFERS_FN_DOUBLE("cos", cos(x)); |
| 1312 | FLATBUFFERS_FN_DOUBLE("tan", tan(x)); |
| 1313 | FLATBUFFERS_FN_DOUBLE("asin", asin(x)); |
| 1314 | FLATBUFFERS_FN_DOUBLE("acos", acos(x)); |
| 1315 | FLATBUFFERS_FN_DOUBLE("atan", atan(x)); |
| 1316 | // TODO(wvo): add more useful conversion functions here. |
| 1317 | #undef FLATBUFFERS_FN_DOUBLE |
| 1318 | // Then check if this could be a string/identifier enum value: |
| 1319 | } else if (e.type.base_type != BASE_TYPE_STRING && |
Wouter van Oortmerssen | 9c3de1e | 2014-07-25 15:04:35 -0700 | [diff] [blame] | 1320 | e.type.base_type != BASE_TYPE_NONE && |
| 1321 | (token_ == kTokenIdentifier || token_ == kTokenStringConstant)) { |
Wouter van Oortmerssen | fbc8af4 | 2016-03-07 17:22:51 -0800 | [diff] [blame] | 1322 | if (IsIdentifierStart(attribute_[0])) { // Enum value. |
| 1323 | int64_t val; |
| 1324 | ECHECK(ParseEnumFromString(e.type, &val)); |
| 1325 | e.constant = NumToString(val); |
| 1326 | NEXT(); |
| 1327 | } else { // Numeric constant in string. |
| 1328 | if (IsInteger(e.type.base_type)) { |
Sahil Jain | b6ba322 | 2016-08-25 23:37:30 -0400 | [diff] [blame] | 1329 | char *end; |
| 1330 | e.constant = NumToString(StringToInt(attribute_.c_str(), &end)); |
| 1331 | if (*end) |
| 1332 | return Error("invalid integer: " + attribute_); |
Wouter van Oortmerssen | fbc8af4 | 2016-03-07 17:22:51 -0800 | [diff] [blame] | 1333 | } else if (IsFloat(e.type.base_type)) { |
Sahil Jain | b6ba322 | 2016-08-25 23:37:30 -0400 | [diff] [blame] | 1334 | char *end; |
| 1335 | e.constant = NumToString(strtod(attribute_.c_str(), &end)); |
| 1336 | if (*end) |
| 1337 | return Error("invalid float: " + attribute_); |
Wouter van Oortmerssen | fbc8af4 | 2016-03-07 17:22:51 -0800 | [diff] [blame] | 1338 | } else { |
| 1339 | assert(0); // Shouldn't happen, we covered all types. |
| 1340 | e.constant = "0"; |
| 1341 | } |
Wouter van Oortmerssen | 83dc5ed | 2016-04-11 11:08:09 -0700 | [diff] [blame] | 1342 | NEXT(); |
Wouter van Oortmerssen | fbc8af4 | 2016-03-07 17:22:51 -0800 | [diff] [blame] | 1343 | } |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1344 | } else { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1345 | bool match = false; |
| 1346 | ECHECK(TryTypedValue(kTokenIntegerConstant, |
| 1347 | IsScalar(e.type.base_type), |
| 1348 | e, |
| 1349 | BASE_TYPE_INT, |
| 1350 | &match)); |
rouzier | a2b1bfc | 2017-08-04 11:04:28 -0400 | [diff] [blame] | 1351 | ECHECK(TryTypedValue(kTokenBooleanConstant, |
| 1352 | IsBool(e.type.base_type), |
| 1353 | e, |
| 1354 | BASE_TYPE_BOOL, |
| 1355 | &match)); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1356 | ECHECK(TryTypedValue(kTokenFloatConstant, |
| 1357 | IsFloat(e.type.base_type), |
| 1358 | e, |
| 1359 | BASE_TYPE_FLOAT, |
| 1360 | &match)); |
| 1361 | ECHECK(TryTypedValue(kTokenStringConstant, |
| 1362 | e.type.base_type == BASE_TYPE_STRING, |
| 1363 | e, |
| 1364 | BASE_TYPE_STRING, |
| 1365 | &match)); |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 1366 | if (!match) return TokenError(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1367 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1368 | return NoError(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1369 | } |
| 1370 | |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1371 | StructDef *Parser::LookupCreateStruct(const std::string &name, |
| 1372 | bool create_if_new, bool definition) { |
Wouter van Oortmerssen | 321a1c9 | 2017-08-24 12:55:35 -0700 | [diff] [blame^] | 1373 | std::string qualified_name = current_namespace_->GetFullyQualifiedName(name); |
Wouter van Oortmerssen | 20c0082 | 2016-02-12 16:20:33 -0800 | [diff] [blame] | 1374 | // See if it exists pre-declared by an unqualified use. |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1375 | auto struct_def = structs_.Lookup(name); |
| 1376 | if (struct_def && struct_def->predecl) { |
| 1377 | if (definition) { |
Wouter van Oortmerssen | 20c0082 | 2016-02-12 16:20:33 -0800 | [diff] [blame] | 1378 | // Make sure it has the current namespace, and is registered under its |
| 1379 | // qualified name. |
Wouter van Oortmerssen | 321a1c9 | 2017-08-24 12:55:35 -0700 | [diff] [blame^] | 1380 | struct_def->defined_namespace = current_namespace_; |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1381 | structs_.Move(name, qualified_name); |
| 1382 | } |
| 1383 | return struct_def; |
| 1384 | } |
Wouter van Oortmerssen | 20c0082 | 2016-02-12 16:20:33 -0800 | [diff] [blame] | 1385 | // See if it exists pre-declared by an qualified use. |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1386 | struct_def = structs_.Lookup(qualified_name); |
Wouter van Oortmerssen | 20c0082 | 2016-02-12 16:20:33 -0800 | [diff] [blame] | 1387 | if (struct_def && struct_def->predecl) { |
| 1388 | if (definition) { |
| 1389 | // Make sure it has the current namespace. |
Wouter van Oortmerssen | 321a1c9 | 2017-08-24 12:55:35 -0700 | [diff] [blame^] | 1390 | struct_def->defined_namespace = current_namespace_; |
Wouter van Oortmerssen | 20c0082 | 2016-02-12 16:20:33 -0800 | [diff] [blame] | 1391 | } |
| 1392 | return struct_def; |
| 1393 | } |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1394 | if (!definition) { |
| 1395 | // Search thru parent namespaces. |
Wouter van Oortmerssen | 321a1c9 | 2017-08-24 12:55:35 -0700 | [diff] [blame^] | 1396 | for (size_t components = current_namespace_->components.size(); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1397 | components && !struct_def; components--) { |
| 1398 | struct_def = structs_.Lookup( |
Wouter van Oortmerssen | 321a1c9 | 2017-08-24 12:55:35 -0700 | [diff] [blame^] | 1399 | current_namespace_->GetFullyQualifiedName(name, components - 1)); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1400 | } |
| 1401 | } |
| 1402 | if (!struct_def && create_if_new) { |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1403 | struct_def = new StructDef(); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1404 | if (definition) { |
| 1405 | structs_.Add(qualified_name, struct_def); |
| 1406 | struct_def->name = name; |
Wouter van Oortmerssen | 321a1c9 | 2017-08-24 12:55:35 -0700 | [diff] [blame^] | 1407 | struct_def->defined_namespace = current_namespace_; |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1408 | } else { |
| 1409 | // Not a definition. |
| 1410 | // Rather than failing, we create a "pre declared" StructDef, due to |
| 1411 | // circular references, and check for errors at the end of parsing. |
| 1412 | // It is defined in the root namespace, since we don't know what the |
| 1413 | // final namespace will be. |
| 1414 | // TODO: maybe safer to use special namespace? |
| 1415 | structs_.Add(name, struct_def); |
| 1416 | struct_def->name = name; |
Wouter van Oortmerssen | 321a1c9 | 2017-08-24 12:55:35 -0700 | [diff] [blame^] | 1417 | struct_def->defined_namespace = empty_namespace_; |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1418 | } |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1419 | } |
| 1420 | return struct_def; |
| 1421 | } |
| 1422 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1423 | CheckedError Parser::ParseEnum(bool is_union, EnumDef **dest) { |
Max Galkin | c3807fa | 2015-03-07 08:49:55 -0800 | [diff] [blame] | 1424 | std::vector<std::string> enum_comment = doc_comment_; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1425 | NEXT(); |
Max Galkin | c3807fa | 2015-03-07 08:49:55 -0800 | [diff] [blame] | 1426 | std::string enum_name = attribute_; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1427 | EXPECT(kTokenIdentifier); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1428 | auto &enum_def = *new EnumDef(); |
Max Galkin | c3807fa | 2015-03-07 08:49:55 -0800 | [diff] [blame] | 1429 | enum_def.name = enum_name; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1430 | enum_def.file = file_being_parsed_; |
Max Galkin | c3807fa | 2015-03-07 08:49:55 -0800 | [diff] [blame] | 1431 | enum_def.doc_comment = enum_comment; |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1432 | enum_def.is_union = is_union; |
Wouter van Oortmerssen | 321a1c9 | 2017-08-24 12:55:35 -0700 | [diff] [blame^] | 1433 | enum_def.defined_namespace = current_namespace_; |
| 1434 | if (enums_.Add(current_namespace_->GetFullyQualifiedName(enum_name), |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1435 | &enum_def)) |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1436 | return Error("enum already exists: " + enum_name); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1437 | if (is_union) { |
| 1438 | enum_def.underlying_type.base_type = BASE_TYPE_UTYPE; |
| 1439 | enum_def.underlying_type.enum_def = &enum_def; |
Wouter van Oortmerssen | a5f5001 | 2014-07-02 12:01:21 -0700 | [diff] [blame] | 1440 | } else { |
Wouter van Oortmerssen | 45bda6e | 2015-11-30 17:42:19 -0800 | [diff] [blame] | 1441 | if (opts.proto_mode) { |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1442 | enum_def.underlying_type.base_type = BASE_TYPE_INT; |
Wouter van Oortmerssen | d38b9af | 2014-09-26 16:46:30 -0700 | [diff] [blame] | 1443 | } else { |
| 1444 | // Give specialized error message, since this type spec used to |
| 1445 | // be optional in the first FlatBuffers release. |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1446 | if (!Is(':')) { |
| 1447 | return Error("must specify the underlying integer type for this" |
| 1448 | " enum (e.g. \': short\', which was the default)."); |
| 1449 | } else { |
| 1450 | NEXT(); |
| 1451 | } |
Wouter van Oortmerssen | d38b9af | 2014-09-26 16:46:30 -0700 | [diff] [blame] | 1452 | // Specify the integer type underlying this enum. |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1453 | ECHECK(ParseType(enum_def.underlying_type)); |
Wouter van Oortmerssen | d38b9af | 2014-09-26 16:46:30 -0700 | [diff] [blame] | 1454 | if (!IsInteger(enum_def.underlying_type.base_type)) |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1455 | return Error("underlying enum type must be integral"); |
Wouter van Oortmerssen | d38b9af | 2014-09-26 16:46:30 -0700 | [diff] [blame] | 1456 | } |
Wouter van Oortmerssen | 3fb6a86 | 2014-07-14 17:49:04 -0700 | [diff] [blame] | 1457 | // Make this type refer back to the enum it was derived from. |
| 1458 | enum_def.underlying_type.enum_def = &enum_def; |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1459 | } |
Wouter van Oortmerssen | e6b79f0 | 2016-03-09 15:03:05 -0800 | [diff] [blame] | 1460 | ECHECK(ParseMetaData(&enum_def.attributes)); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1461 | EXPECT('{'); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1462 | if (is_union) enum_def.vals.Add("NONE", new EnumVal("NONE", 0)); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1463 | for (;;) { |
Wouter van Oortmerssen | 45bda6e | 2015-11-30 17:42:19 -0800 | [diff] [blame] | 1464 | if (opts.proto_mode && attribute_ == "option") { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1465 | ECHECK(ParseProtoOption()); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1466 | } else { |
| 1467 | auto value_name = attribute_; |
| 1468 | auto full_name = value_name; |
| 1469 | std::vector<std::string> value_comment = doc_comment_; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1470 | EXPECT(kTokenIdentifier); |
Wouter van Oortmerssen | 3639032 | 2016-06-17 18:16:11 -0700 | [diff] [blame] | 1471 | if (is_union) { |
| 1472 | ECHECK(ParseNamespacing(&full_name, &value_name)); |
Wouter van Oortmerssen | d70f5ac | 2016-07-29 10:45:32 -0700 | [diff] [blame] | 1473 | if (opts.union_value_namespacing) { |
| 1474 | // Since we can't namespace the actual enum identifiers, turn |
| 1475 | // namespace parts into part of the identifier. |
| 1476 | value_name = full_name; |
| 1477 | std::replace(value_name.begin(), value_name.end(), '.', '_'); |
| 1478 | } |
Wouter van Oortmerssen | 3639032 | 2016-06-17 18:16:11 -0700 | [diff] [blame] | 1479 | } |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1480 | auto prevsize = enum_def.vals.vec.size(); |
| 1481 | auto value = enum_def.vals.vec.size() |
| 1482 | ? enum_def.vals.vec.back()->value + 1 |
| 1483 | : 0; |
| 1484 | auto &ev = *new EnumVal(value_name, value); |
| 1485 | if (enum_def.vals.Add(value_name, &ev)) |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1486 | return Error("enum value already exists: " + value_name); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1487 | ev.doc_comment = value_comment; |
| 1488 | if (is_union) { |
Wouter van Oortmerssen | b0752e1 | 2017-04-10 15:56:51 -0700 | [diff] [blame] | 1489 | if (Is(':')) { |
| 1490 | NEXT(); |
| 1491 | ECHECK(ParseType(ev.union_type)); |
| 1492 | if (ev.union_type.base_type != BASE_TYPE_STRUCT && |
| 1493 | ev.union_type.base_type != BASE_TYPE_STRING) |
| 1494 | return Error("union value type may only be table/struct/string"); |
| 1495 | enum_def.uses_type_aliases = true; |
| 1496 | } else { |
| 1497 | ev.union_type = Type(BASE_TYPE_STRUCT, LookupCreateStruct(full_name)); |
| 1498 | } |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1499 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1500 | if (Is('=')) { |
| 1501 | NEXT(); |
Wouter van Oortmerssen | d779308 | 2016-02-10 11:25:31 -0800 | [diff] [blame] | 1502 | ev.value = StringToInt(attribute_.c_str()); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1503 | EXPECT(kTokenIntegerConstant); |
Wouter van Oortmerssen | 45bda6e | 2015-11-30 17:42:19 -0800 | [diff] [blame] | 1504 | if (!opts.proto_mode && prevsize && |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1505 | enum_def.vals.vec[prevsize - 1]->value >= ev.value) |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1506 | return Error("enum values must be specified in ascending order"); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1507 | } |
Wouter van Oortmerssen | 9d01bfa | 2017-05-10 17:21:47 -0700 | [diff] [blame] | 1508 | if (is_union) { |
| 1509 | if (ev.value < 0 || ev.value >= 256) |
| 1510 | return Error("union enum value must fit in a ubyte"); |
| 1511 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1512 | if (opts.proto_mode && Is('[')) { |
| 1513 | NEXT(); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1514 | // ignore attributes on enums. |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1515 | while (token_ != ']') NEXT(); |
| 1516 | NEXT(); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1517 | } |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1518 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1519 | if (!Is(opts.proto_mode ? ';' : ',')) break; |
| 1520 | NEXT(); |
| 1521 | if (Is('}')) break; |
| 1522 | } |
| 1523 | EXPECT('}'); |
Wouter van Oortmerssen | 127d350 | 2014-07-17 15:12:37 -0700 | [diff] [blame] | 1524 | if (enum_def.attributes.Lookup("bit_flags")) { |
| 1525 | for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); |
| 1526 | ++it) { |
| 1527 | if (static_cast<size_t>((*it)->value) >= |
| 1528 | SizeOf(enum_def.underlying_type.base_type) * 8) |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1529 | return Error("bit flag out of range of underlying integral type"); |
Wouter van Oortmerssen | 9c3de1e | 2014-07-25 15:04:35 -0700 | [diff] [blame] | 1530 | (*it)->value = 1LL << (*it)->value; |
Wouter van Oortmerssen | 127d350 | 2014-07-17 15:12:37 -0700 | [diff] [blame] | 1531 | } |
| 1532 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1533 | if (dest) *dest = &enum_def; |
Wouter van Oortmerssen | 321a1c9 | 2017-08-24 12:55:35 -0700 | [diff] [blame^] | 1534 | types_.Add(current_namespace_->GetFullyQualifiedName(enum_def.name), |
Wouter van Oortmerssen | 9b3d8b3 | 2017-01-27 15:28:57 -0800 | [diff] [blame] | 1535 | new Type(BASE_TYPE_UNION, nullptr, &enum_def)); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1536 | return NoError(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1537 | } |
| 1538 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1539 | CheckedError Parser::StartStruct(const std::string &name, StructDef **dest) { |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1540 | auto &struct_def = *LookupCreateStruct(name, true, true); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1541 | if (!struct_def.predecl) return Error("datatype already exists: " + name); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1542 | struct_def.predecl = false; |
| 1543 | struct_def.name = name; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1544 | struct_def.file = file_being_parsed_; |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1545 | // Move this struct to the back of the vector just in case it was predeclared, |
Wouter van Oortmerssen | d38b9af | 2014-09-26 16:46:30 -0700 | [diff] [blame] | 1546 | // to preserve declaration order. |
Dmitry Ermolov | 370693a | 2017-04-20 02:55:41 +0300 | [diff] [blame] | 1547 | *std::remove(structs_.vec.begin(), structs_.vec.end(), &struct_def) = &struct_def; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1548 | *dest = &struct_def; |
| 1549 | return NoError(); |
Wouter van Oortmerssen | d38b9af | 2014-09-26 16:46:30 -0700 | [diff] [blame] | 1550 | } |
| 1551 | |
Chandra Penke | b63ebad | 2016-01-06 08:31:53 -0800 | [diff] [blame] | 1552 | CheckedError Parser::CheckClash(std::vector<FieldDef*> &fields, |
| 1553 | StructDef *struct_def, |
| 1554 | const char *suffix, |
| 1555 | BaseType basetype) { |
| 1556 | auto len = strlen(suffix); |
| 1557 | for (auto it = fields.begin(); it != fields.end(); ++it) { |
| 1558 | auto &fname = (*it)->name; |
| 1559 | if (fname.length() > len && |
| 1560 | fname.compare(fname.length() - len, len, suffix) == 0 && |
| 1561 | (*it)->value.type.base_type != BASE_TYPE_UTYPE) { |
| 1562 | auto field = struct_def->fields.Lookup( |
| 1563 | fname.substr(0, fname.length() - len)); |
| 1564 | if (field && field->value.type.base_type == basetype) |
| 1565 | return Error("Field " + fname + |
| 1566 | " would clash with generated functions for field " + |
| 1567 | field->name); |
| 1568 | } |
| 1569 | } |
| 1570 | return NoError(); |
| 1571 | } |
Wouter van Oortmerssen | d779308 | 2016-02-10 11:25:31 -0800 | [diff] [blame] | 1572 | |
Kamil Rojewski | 46bb05d | 2017-08-11 18:24:36 +0200 | [diff] [blame] | 1573 | bool Parser::SupportsVectorOfUnions() const { |
| 1574 | return opts.lang_to_generate != 0 && (opts.lang_to_generate & |
| 1575 | ~(IDLOptions::kCpp | IDLOptions::kJs | IDLOptions::kTs | IDLOptions::kPhp)) == 0; |
| 1576 | } |
| 1577 | |
Wouter van Oortmerssen | 321a1c9 | 2017-08-24 12:55:35 -0700 | [diff] [blame^] | 1578 | Namespace *Parser::UniqueNamespace(Namespace *ns) { |
| 1579 | for (auto it = namespaces_.begin(); it != namespaces_.end(); ++it) { |
| 1580 | if (ns->components == (*it)->components) { |
| 1581 | delete ns; |
| 1582 | return *it; |
| 1583 | } |
| 1584 | } |
| 1585 | namespaces_.push_back(ns); |
| 1586 | return ns; |
| 1587 | } |
| 1588 | |
Chandra Penke | b63ebad | 2016-01-06 08:31:53 -0800 | [diff] [blame] | 1589 | static bool compareFieldDefs(const FieldDef *a, const FieldDef *b) { |
| 1590 | auto a_id = atoi(a->attributes.Lookup("id")->constant.c_str()); |
| 1591 | auto b_id = atoi(b->attributes.Lookup("id")->constant.c_str()); |
| 1592 | return a_id < b_id; |
| 1593 | } |
| 1594 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1595 | CheckedError Parser::ParseDecl() { |
Wouter van Oortmerssen | d38b9af | 2014-09-26 16:46:30 -0700 | [diff] [blame] | 1596 | std::vector<std::string> dc = doc_comment_; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1597 | bool fixed = Is(kTokenStruct); |
| 1598 | if (fixed) NEXT() else EXPECT(kTokenTable); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1599 | std::string name = attribute_; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1600 | EXPECT(kTokenIdentifier); |
| 1601 | StructDef *struct_def; |
| 1602 | ECHECK(StartStruct(name, &struct_def)); |
| 1603 | struct_def->doc_comment = dc; |
| 1604 | struct_def->fixed = fixed; |
Wouter van Oortmerssen | e6b79f0 | 2016-03-09 15:03:05 -0800 | [diff] [blame] | 1605 | ECHECK(ParseMetaData(&struct_def->attributes)); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1606 | struct_def->sortbysize = |
| 1607 | struct_def->attributes.Lookup("original_order") == nullptr && !fixed; |
| 1608 | EXPECT('{'); |
| 1609 | while (token_ != '}') ECHECK(ParseField(*struct_def)); |
| 1610 | auto force_align = struct_def->attributes.Lookup("force_align"); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1611 | if (fixed && force_align) { |
| 1612 | auto align = static_cast<size_t>(atoi(force_align->constant.c_str())); |
| 1613 | if (force_align->type.base_type != BASE_TYPE_INT || |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1614 | align < struct_def->minalign || |
Wouter van Oortmerssen | 6862b2f | 2016-10-17 18:02:19 -0700 | [diff] [blame] | 1615 | align > FLATBUFFERS_MAX_ALIGNMENT || |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1616 | align & (align - 1)) |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1617 | return Error("force_align must be a power of two integer ranging from the" |
Wouter van Oortmerssen | 6862b2f | 2016-10-17 18:02:19 -0700 | [diff] [blame] | 1618 | "struct\'s natural alignment to " + |
| 1619 | NumToString(FLATBUFFERS_MAX_ALIGNMENT)); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1620 | struct_def->minalign = align; |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1621 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1622 | struct_def->PadLastField(struct_def->minalign); |
Wouter van Oortmerssen | 9140144 | 2014-07-07 17:34:23 -0700 | [diff] [blame] | 1623 | // Check if this is a table that has manual id assignments |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1624 | auto &fields = struct_def->fields.vec; |
| 1625 | if (!struct_def->fixed && fields.size()) { |
Wouter van Oortmerssen | 7fcbe72 | 2014-07-08 16:35:14 -0700 | [diff] [blame] | 1626 | size_t num_id_fields = 0; |
Wouter van Oortmerssen | 9140144 | 2014-07-07 17:34:23 -0700 | [diff] [blame] | 1627 | for (auto it = fields.begin(); it != fields.end(); ++it) { |
| 1628 | if ((*it)->attributes.Lookup("id")) num_id_fields++; |
| 1629 | } |
| 1630 | // If any fields have ids.. |
| 1631 | if (num_id_fields) { |
| 1632 | // Then all fields must have them. |
| 1633 | if (num_id_fields != fields.size()) |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1634 | return Error( |
| 1635 | "either all fields or no fields must have an 'id' attribute"); |
Wouter van Oortmerssen | 9140144 | 2014-07-07 17:34:23 -0700 | [diff] [blame] | 1636 | // Simply sort by id, then the fields are the same as if no ids had |
| 1637 | // been specified. |
Chandra Penke | b63ebad | 2016-01-06 08:31:53 -0800 | [diff] [blame] | 1638 | std::sort(fields.begin(), fields.end(), compareFieldDefs); |
Wouter van Oortmerssen | 9140144 | 2014-07-07 17:34:23 -0700 | [diff] [blame] | 1639 | // Verify we have a contiguous set, and reassign vtable offsets. |
| 1640 | for (int i = 0; i < static_cast<int>(fields.size()); i++) { |
| 1641 | if (i != atoi(fields[i]->attributes.Lookup("id")->constant.c_str())) |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1642 | return Error("field id\'s must be consecutive from 0, id " + |
Wouter van Oortmerssen | 9140144 | 2014-07-07 17:34:23 -0700 | [diff] [blame] | 1643 | NumToString(i) + " missing or set twice"); |
| 1644 | fields[i]->value.offset = FieldIndexToOffset(static_cast<voffset_t>(i)); |
| 1645 | } |
| 1646 | } |
| 1647 | } |
Chandra Penke | b63ebad | 2016-01-06 08:31:53 -0800 | [diff] [blame] | 1648 | |
Wouter van Oortmerssen | 9e6c5f9 | 2016-06-20 16:06:30 -0700 | [diff] [blame] | 1649 | ECHECK(CheckClash(fields, struct_def, UnionTypeFieldSuffix(), |
| 1650 | BASE_TYPE_UNION)); |
Chandra Penke | b63ebad | 2016-01-06 08:31:53 -0800 | [diff] [blame] | 1651 | ECHECK(CheckClash(fields, struct_def, "Type", BASE_TYPE_UNION)); |
| 1652 | ECHECK(CheckClash(fields, struct_def, "_length", BASE_TYPE_VECTOR)); |
| 1653 | ECHECK(CheckClash(fields, struct_def, "Length", BASE_TYPE_VECTOR)); |
| 1654 | ECHECK(CheckClash(fields, struct_def, "_byte_vector", BASE_TYPE_STRING)); |
| 1655 | ECHECK(CheckClash(fields, struct_def, "ByteVector", BASE_TYPE_STRING)); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1656 | EXPECT('}'); |
Wouter van Oortmerssen | 321a1c9 | 2017-08-24 12:55:35 -0700 | [diff] [blame^] | 1657 | types_.Add(current_namespace_->GetFullyQualifiedName(struct_def->name), |
Wouter van Oortmerssen | 9b3d8b3 | 2017-01-27 15:28:57 -0800 | [diff] [blame] | 1658 | new Type(BASE_TYPE_STRUCT, struct_def, nullptr)); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1659 | return NoError(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1660 | } |
| 1661 | |
Wouter van Oortmerssen | 1a63eb4 | 2016-03-07 18:07:10 -0800 | [diff] [blame] | 1662 | CheckedError Parser::ParseService() { |
| 1663 | std::vector<std::string> service_comment = doc_comment_; |
| 1664 | NEXT(); |
| 1665 | auto service_name = attribute_; |
| 1666 | EXPECT(kTokenIdentifier); |
| 1667 | auto &service_def = *new ServiceDef(); |
| 1668 | service_def.name = service_name; |
| 1669 | service_def.file = file_being_parsed_; |
| 1670 | service_def.doc_comment = service_comment; |
Wouter van Oortmerssen | 321a1c9 | 2017-08-24 12:55:35 -0700 | [diff] [blame^] | 1671 | service_def.defined_namespace = current_namespace_; |
| 1672 | if (services_.Add(current_namespace_->GetFullyQualifiedName(service_name), |
Wouter van Oortmerssen | 1a63eb4 | 2016-03-07 18:07:10 -0800 | [diff] [blame] | 1673 | &service_def)) |
| 1674 | return Error("service already exists: " + service_name); |
Wouter van Oortmerssen | e6b79f0 | 2016-03-09 15:03:05 -0800 | [diff] [blame] | 1675 | ECHECK(ParseMetaData(&service_def.attributes)); |
Wouter van Oortmerssen | 1a63eb4 | 2016-03-07 18:07:10 -0800 | [diff] [blame] | 1676 | EXPECT('{'); |
| 1677 | do { |
| 1678 | auto rpc_name = attribute_; |
| 1679 | EXPECT(kTokenIdentifier); |
| 1680 | EXPECT('('); |
| 1681 | Type reqtype, resptype; |
| 1682 | ECHECK(ParseTypeIdent(reqtype)); |
| 1683 | EXPECT(')'); |
| 1684 | EXPECT(':'); |
| 1685 | ECHECK(ParseTypeIdent(resptype)); |
| 1686 | if (reqtype.base_type != BASE_TYPE_STRUCT || reqtype.struct_def->fixed || |
| 1687 | resptype.base_type != BASE_TYPE_STRUCT || resptype.struct_def->fixed) |
| 1688 | return Error("rpc request and response types must be tables"); |
| 1689 | auto &rpc = *new RPCCall(); |
| 1690 | rpc.name = rpc_name; |
| 1691 | rpc.request = reqtype.struct_def; |
| 1692 | rpc.response = resptype.struct_def; |
| 1693 | if (service_def.calls.Add(rpc_name, &rpc)) |
| 1694 | return Error("rpc already exists: " + rpc_name); |
Wouter van Oortmerssen | e6b79f0 | 2016-03-09 15:03:05 -0800 | [diff] [blame] | 1695 | ECHECK(ParseMetaData(&rpc.attributes)); |
Wouter van Oortmerssen | 1a63eb4 | 2016-03-07 18:07:10 -0800 | [diff] [blame] | 1696 | EXPECT(';'); |
| 1697 | } while (token_ != '}'); |
| 1698 | NEXT(); |
| 1699 | return NoError(); |
| 1700 | } |
| 1701 | |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1702 | bool Parser::SetRootType(const char *name) { |
Wouter van Oortmerssen | 4dcaec7 | 2015-12-09 16:41:12 -0800 | [diff] [blame] | 1703 | root_struct_def_ = structs_.Lookup(name); |
| 1704 | if (!root_struct_def_) |
| 1705 | root_struct_def_ = structs_.Lookup( |
Wouter van Oortmerssen | 321a1c9 | 2017-08-24 12:55:35 -0700 | [diff] [blame^] | 1706 | current_namespace_->GetFullyQualifiedName(name)); |
Wouter van Oortmerssen | 81312c2 | 2015-05-21 16:33:29 -0700 | [diff] [blame] | 1707 | return root_struct_def_ != nullptr; |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 1708 | } |
| 1709 | |
Wouter van Oortmerssen | be894f0 | 2014-08-19 14:20:05 -0700 | [diff] [blame] | 1710 | void Parser::MarkGenerated() { |
Wouter van Oortmerssen | 3881bbd | 2015-11-30 16:42:48 -0800 | [diff] [blame] | 1711 | // This function marks all existing definitions as having already |
| 1712 | // been generated, which signals no code for included files should be |
| 1713 | // generated. |
Wouter van Oortmerssen | be894f0 | 2014-08-19 14:20:05 -0700 | [diff] [blame] | 1714 | for (auto it = enums_.vec.begin(); |
| 1715 | it != enums_.vec.end(); ++it) { |
| 1716 | (*it)->generated = true; |
| 1717 | } |
| 1718 | for (auto it = structs_.vec.begin(); |
| 1719 | it != structs_.vec.end(); ++it) { |
Robbie McElrath | 0e85eee | 2017-06-26 09:07:02 -0700 | [diff] [blame] | 1720 | if (!(*it)->predecl) { |
| 1721 | (*it)->generated = true; |
| 1722 | } |
Wouter van Oortmerssen | be894f0 | 2014-08-19 14:20:05 -0700 | [diff] [blame] | 1723 | } |
Wouter van Oortmerssen | 48f37f9 | 2016-04-13 18:16:05 -0700 | [diff] [blame] | 1724 | for (auto it = services_.vec.begin(); |
| 1725 | it != services_.vec.end(); ++it) { |
| 1726 | (*it)->generated = true; |
| 1727 | } |
Wouter van Oortmerssen | be894f0 | 2014-08-19 14:20:05 -0700 | [diff] [blame] | 1728 | } |
| 1729 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1730 | CheckedError Parser::ParseNamespace() { |
| 1731 | NEXT(); |
Wouter van Oortmerssen | d38b9af | 2014-09-26 16:46:30 -0700 | [diff] [blame] | 1732 | auto ns = new Namespace(); |
Wouter van Oortmerssen | 321a1c9 | 2017-08-24 12:55:35 -0700 | [diff] [blame^] | 1733 | namespaces_.push_back(ns); // Store it here to not leak upon error. |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1734 | if (token_ != ';') { |
| 1735 | for (;;) { |
| 1736 | ns->components.push_back(attribute_); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1737 | EXPECT(kTokenIdentifier); |
| 1738 | if (Is('.')) NEXT() else break; |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1739 | } |
Wouter van Oortmerssen | d38b9af | 2014-09-26 16:46:30 -0700 | [diff] [blame] | 1740 | } |
Wouter van Oortmerssen | 321a1c9 | 2017-08-24 12:55:35 -0700 | [diff] [blame^] | 1741 | namespaces_.pop_back(); |
| 1742 | current_namespace_ = UniqueNamespace(ns); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1743 | EXPECT(';'); |
| 1744 | return NoError(); |
Wouter van Oortmerssen | d38b9af | 2014-09-26 16:46:30 -0700 | [diff] [blame] | 1745 | } |
| 1746 | |
Chandra Penke | b63ebad | 2016-01-06 08:31:53 -0800 | [diff] [blame] | 1747 | static bool compareEnumVals(const EnumVal *a, const EnumVal* b) { |
| 1748 | return a->value < b->value; |
| 1749 | } |
| 1750 | |
Wouter van Oortmerssen | d38b9af | 2014-09-26 16:46:30 -0700 | [diff] [blame] | 1751 | // Best effort parsing of .proto declarations, with the aim to turn them |
| 1752 | // in the closest corresponding FlatBuffer equivalent. |
| 1753 | // We parse everything as identifiers instead of keywords, since we don't |
| 1754 | // want protobuf keywords to become invalid identifiers in FlatBuffers. |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1755 | CheckedError Parser::ParseProtoDecl() { |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1756 | bool isextend = attribute_ == "extend"; |
Wouter van Oortmerssen | d38b9af | 2014-09-26 16:46:30 -0700 | [diff] [blame] | 1757 | if (attribute_ == "package") { |
| 1758 | // These are identical in syntax to FlatBuffer's namespace decl. |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1759 | ECHECK(ParseNamespace()); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1760 | } else if (attribute_ == "message" || isextend) { |
Advay Mengle | 3ad8536 | 2015-03-31 02:03:11 -0700 | [diff] [blame] | 1761 | std::vector<std::string> struct_comment = doc_comment_; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1762 | NEXT(); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1763 | StructDef *struct_def = nullptr; |
Wouter van Oortmerssen | 321a1c9 | 2017-08-24 12:55:35 -0700 | [diff] [blame^] | 1764 | Namespace *parent_namespace = nullptr; |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1765 | if (isextend) { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1766 | if (Is('.')) NEXT(); // qualified names may start with a . ? |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1767 | auto id = attribute_; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1768 | EXPECT(kTokenIdentifier); |
| 1769 | ECHECK(ParseNamespacing(&id, nullptr)); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1770 | struct_def = LookupCreateStruct(id, false); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1771 | if (!struct_def) |
| 1772 | return Error("cannot extend unknown message type: " + id); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1773 | } else { |
| 1774 | std::string name = attribute_; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1775 | EXPECT(kTokenIdentifier); |
| 1776 | ECHECK(StartStruct(name, &struct_def)); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1777 | // Since message definitions can be nested, we create a new namespace. |
| 1778 | auto ns = new Namespace(); |
| 1779 | // Copy of current namespace. |
Wouter van Oortmerssen | 321a1c9 | 2017-08-24 12:55:35 -0700 | [diff] [blame^] | 1780 | *ns = *current_namespace_; |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1781 | // But with current message name. |
| 1782 | ns->components.push_back(name); |
Wouter van Oortmerssen | 321a1c9 | 2017-08-24 12:55:35 -0700 | [diff] [blame^] | 1783 | parent_namespace = current_namespace_; |
| 1784 | current_namespace_ = UniqueNamespace(ns); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1785 | } |
| 1786 | struct_def->doc_comment = struct_comment; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1787 | ECHECK(ParseProtoFields(struct_def, isextend, false)); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1788 | if (!isextend) { |
Wouter van Oortmerssen | 321a1c9 | 2017-08-24 12:55:35 -0700 | [diff] [blame^] | 1789 | current_namespace_ = parent_namespace; |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1790 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1791 | if (Is(';')) NEXT(); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1792 | } else if (attribute_ == "enum") { |
| 1793 | // These are almost the same, just with different terminator: |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1794 | EnumDef *enum_def; |
| 1795 | ECHECK(ParseEnum(false, &enum_def)); |
| 1796 | if (Is(';')) NEXT(); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1797 | // Protobuf allows them to be specified in any order, so sort afterwards. |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1798 | auto &v = enum_def->vals.vec; |
Wouter van Oortmerssen | d779308 | 2016-02-10 11:25:31 -0800 | [diff] [blame] | 1799 | std::sort(v.begin(), v.end(), compareEnumVals); |
Chandra Penke | b63ebad | 2016-01-06 08:31:53 -0800 | [diff] [blame] | 1800 | |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1801 | // Temp: remove any duplicates, as .fbs files can't handle them. |
| 1802 | for (auto it = v.begin(); it != v.end(); ) { |
| 1803 | if (it != v.begin() && it[0]->value == it[-1]->value) it = v.erase(it); |
| 1804 | else ++it; |
| 1805 | } |
| 1806 | } else if (attribute_ == "syntax") { // Skip these. |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1807 | NEXT(); |
| 1808 | EXPECT('='); |
| 1809 | EXPECT(kTokenStringConstant); |
| 1810 | EXPECT(';'); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1811 | } else if (attribute_ == "option") { // Skip these. |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1812 | ECHECK(ParseProtoOption()); |
| 1813 | EXPECT(';'); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1814 | } else if (attribute_ == "service") { // Skip these. |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1815 | NEXT(); |
| 1816 | EXPECT(kTokenIdentifier); |
| 1817 | ECHECK(ParseProtoCurliesOrIdent()); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1818 | } else { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1819 | return Error("don\'t know how to parse .proto declaration starting with " + |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1820 | TokenToStringId(token_)); |
| 1821 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1822 | return NoError(); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1823 | } |
| 1824 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1825 | CheckedError Parser::ParseProtoFields(StructDef *struct_def, bool isextend, |
| 1826 | bool inside_oneof) { |
| 1827 | EXPECT('{'); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1828 | while (token_ != '}') { |
| 1829 | if (attribute_ == "message" || attribute_ == "extend" || |
| 1830 | attribute_ == "enum") { |
| 1831 | // Nested declarations. |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1832 | ECHECK(ParseProtoDecl()); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1833 | } else if (attribute_ == "extensions") { // Skip these. |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1834 | NEXT(); |
| 1835 | EXPECT(kTokenIntegerConstant); |
| 1836 | if (Is(kTokenIdentifier)) { |
| 1837 | NEXT(); // to |
| 1838 | NEXT(); // num |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1839 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1840 | EXPECT(';'); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1841 | } else if (attribute_ == "option") { // Skip these. |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1842 | ECHECK(ParseProtoOption()); |
| 1843 | EXPECT(';'); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1844 | } else if (attribute_ == "reserved") { // Skip these. |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1845 | NEXT(); |
| 1846 | EXPECT(kTokenIntegerConstant); |
| 1847 | while (Is(',')) { NEXT(); EXPECT(kTokenIntegerConstant); } |
| 1848 | EXPECT(';'); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1849 | } else { |
| 1850 | std::vector<std::string> field_comment = doc_comment_; |
| 1851 | // Parse the qualifier. |
| 1852 | bool required = false; |
| 1853 | bool repeated = false; |
| 1854 | bool oneof = false; |
| 1855 | if (!inside_oneof) { |
Wouter van Oortmerssen | 2abe24b | 2015-09-28 09:42:45 -0700 | [diff] [blame] | 1856 | if (attribute_ == "optional") { |
| 1857 | // This is the default. |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1858 | EXPECT(kTokenIdentifier); |
Wouter van Oortmerssen | 2abe24b | 2015-09-28 09:42:45 -0700 | [diff] [blame] | 1859 | } else if (attribute_ == "required") { |
| 1860 | required = true; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1861 | EXPECT(kTokenIdentifier); |
Wouter van Oortmerssen | 2abe24b | 2015-09-28 09:42:45 -0700 | [diff] [blame] | 1862 | } else if (attribute_ == "repeated") { |
| 1863 | repeated = true; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1864 | EXPECT(kTokenIdentifier); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1865 | } else if (attribute_ == "oneof") { |
| 1866 | oneof = true; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1867 | EXPECT(kTokenIdentifier); |
Wouter van Oortmerssen | 2abe24b | 2015-09-28 09:42:45 -0700 | [diff] [blame] | 1868 | } else { |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1869 | // can't error, proto3 allows decls without any of the above. |
Wouter van Oortmerssen | 2abe24b | 2015-09-28 09:42:45 -0700 | [diff] [blame] | 1870 | } |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1871 | } |
| 1872 | StructDef *anonymous_struct = nullptr; |
| 1873 | Type type; |
| 1874 | if (attribute_ == "group" || oneof) { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1875 | if (!oneof) EXPECT(kTokenIdentifier); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1876 | auto name = "Anonymous" + NumToString(anonymous_counter++); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1877 | ECHECK(StartStruct(name, &anonymous_struct)); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1878 | type = Type(BASE_TYPE_STRUCT, anonymous_struct); |
| 1879 | } else { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1880 | ECHECK(ParseTypeFromProtoType(&type)); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1881 | } |
| 1882 | // Repeated elements get mapped to a vector. |
| 1883 | if (repeated) { |
| 1884 | type.element = type.base_type; |
| 1885 | type.base_type = BASE_TYPE_VECTOR; |
| 1886 | } |
| 1887 | std::string name = attribute_; |
| 1888 | // Protos may use our keywords "attribute" & "namespace" as an identifier. |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1889 | if (Is(kTokenAttribute) || Is(kTokenNameSpace)) { |
| 1890 | NEXT(); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1891 | // TODO: simpler to just not make these keywords? |
| 1892 | name += "_"; // Have to make it not a keyword. |
| 1893 | } else { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1894 | EXPECT(kTokenIdentifier); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1895 | } |
| 1896 | if (!oneof) { |
Wouter van Oortmerssen | 2abe24b | 2015-09-28 09:42:45 -0700 | [diff] [blame] | 1897 | // Parse the field id. Since we're just translating schemas, not |
| 1898 | // any kind of binary compatibility, we can safely ignore these, and |
| 1899 | // assign our own. |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1900 | EXPECT('='); |
| 1901 | EXPECT(kTokenIntegerConstant); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1902 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1903 | FieldDef *field = nullptr; |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1904 | if (isextend) { |
| 1905 | // We allow a field to be re-defined when extending. |
| 1906 | // TODO: are there situations where that is problematic? |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1907 | field = struct_def->fields.Lookup(name); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1908 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1909 | if (!field) ECHECK(AddField(*struct_def, name, type, &field)); |
| 1910 | field->doc_comment = field_comment; |
| 1911 | if (!IsScalar(type.base_type)) field->required = required; |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1912 | // See if there's a default specified. |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1913 | if (Is('[')) { |
| 1914 | NEXT(); |
| 1915 | for (;;) { |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1916 | auto key = attribute_; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1917 | ECHECK(ParseProtoKey()); |
| 1918 | EXPECT('='); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1919 | auto val = attribute_; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1920 | ECHECK(ParseProtoCurliesOrIdent()); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1921 | if (key == "default") { |
| 1922 | // Temp: skip non-numeric defaults (enums). |
| 1923 | auto numeric = strpbrk(val.c_str(), "0123456789-+."); |
| 1924 | if (IsScalar(type.base_type) && numeric == val.c_str()) |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1925 | field->value.constant = val; |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1926 | } else if (key == "deprecated") { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1927 | field->deprecated = val == "true"; |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1928 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1929 | if (!Is(',')) break; |
| 1930 | NEXT(); |
| 1931 | } |
| 1932 | EXPECT(']'); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1933 | } |
| 1934 | if (anonymous_struct) { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1935 | ECHECK(ParseProtoFields(anonymous_struct, false, oneof)); |
| 1936 | if (Is(';')) NEXT(); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1937 | } else { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1938 | EXPECT(';'); |
Wouter van Oortmerssen | d38b9af | 2014-09-26 16:46:30 -0700 | [diff] [blame] | 1939 | } |
Wouter van Oortmerssen | d38b9af | 2014-09-26 16:46:30 -0700 | [diff] [blame] | 1940 | } |
Wouter van Oortmerssen | d38b9af | 2014-09-26 16:46:30 -0700 | [diff] [blame] | 1941 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1942 | NEXT(); |
| 1943 | return NoError(); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1944 | } |
| 1945 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1946 | CheckedError Parser::ParseProtoKey() { |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1947 | if (token_ == '(') { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1948 | NEXT(); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1949 | // Skip "(a.b)" style custom attributes. |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1950 | while (token_ == '.' || token_ == kTokenIdentifier) NEXT(); |
| 1951 | EXPECT(')'); |
| 1952 | while (Is('.')) { NEXT(); EXPECT(kTokenIdentifier); } |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1953 | } else { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1954 | EXPECT(kTokenIdentifier); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1955 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1956 | return NoError(); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1957 | } |
| 1958 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1959 | CheckedError Parser::ParseProtoCurliesOrIdent() { |
| 1960 | if (Is('{')) { |
| 1961 | NEXT(); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1962 | for (int nesting = 1; nesting; ) { |
| 1963 | if (token_ == '{') nesting++; |
| 1964 | else if (token_ == '}') nesting--; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1965 | NEXT(); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1966 | } |
| 1967 | } else { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1968 | NEXT(); // Any single token. |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1969 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1970 | return NoError(); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 1971 | } |
| 1972 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1973 | CheckedError Parser::ParseProtoOption() { |
| 1974 | NEXT(); |
| 1975 | ECHECK(ParseProtoKey()); |
| 1976 | EXPECT('='); |
| 1977 | ECHECK(ParseProtoCurliesOrIdent()); |
| 1978 | return NoError(); |
Wouter van Oortmerssen | d38b9af | 2014-09-26 16:46:30 -0700 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | // Parse a protobuf type, and map it to the corresponding FlatBuffer one. |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 1982 | CheckedError Parser::ParseTypeFromProtoType(Type *type) { |
Wouter van Oortmerssen | 69776b9 | 2017-08-14 14:45:29 -0700 | [diff] [blame] | 1983 | struct type_lookup { const char *proto_type; BaseType fb_type, element; }; |
Wouter van Oortmerssen | d38b9af | 2014-09-26 16:46:30 -0700 | [diff] [blame] | 1984 | static type_lookup lookup[] = { |
Wouter van Oortmerssen | 69776b9 | 2017-08-14 14:45:29 -0700 | [diff] [blame] | 1985 | { "float", BASE_TYPE_FLOAT, BASE_TYPE_NONE }, |
| 1986 | { "double", BASE_TYPE_DOUBLE, BASE_TYPE_NONE }, |
| 1987 | { "int32", BASE_TYPE_INT, BASE_TYPE_NONE }, |
| 1988 | { "int64", BASE_TYPE_LONG, BASE_TYPE_NONE }, |
| 1989 | { "uint32", BASE_TYPE_UINT, BASE_TYPE_NONE }, |
| 1990 | { "uint64", BASE_TYPE_ULONG, BASE_TYPE_NONE }, |
| 1991 | { "sint32", BASE_TYPE_INT, BASE_TYPE_NONE }, |
| 1992 | { "sint64", BASE_TYPE_LONG, BASE_TYPE_NONE }, |
| 1993 | { "fixed32", BASE_TYPE_UINT, BASE_TYPE_NONE }, |
| 1994 | { "fixed64", BASE_TYPE_ULONG, BASE_TYPE_NONE }, |
| 1995 | { "sfixed32", BASE_TYPE_INT, BASE_TYPE_NONE }, |
| 1996 | { "sfixed64", BASE_TYPE_LONG, BASE_TYPE_NONE }, |
| 1997 | { "bool", BASE_TYPE_BOOL, BASE_TYPE_NONE }, |
| 1998 | { "string", BASE_TYPE_STRING, BASE_TYPE_NONE }, |
| 1999 | { "bytes", BASE_TYPE_VECTOR, BASE_TYPE_UCHAR }, |
| 2000 | { nullptr, BASE_TYPE_NONE, BASE_TYPE_NONE } |
Wouter van Oortmerssen | d38b9af | 2014-09-26 16:46:30 -0700 | [diff] [blame] | 2001 | }; |
Wouter van Oortmerssen | d38b9af | 2014-09-26 16:46:30 -0700 | [diff] [blame] | 2002 | for (auto tl = lookup; tl->proto_type; tl++) { |
| 2003 | if (attribute_ == tl->proto_type) { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 2004 | type->base_type = tl->fb_type; |
Wouter van Oortmerssen | 69776b9 | 2017-08-14 14:45:29 -0700 | [diff] [blame] | 2005 | type->element = tl->element; |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 2006 | NEXT(); |
| 2007 | return NoError(); |
Wouter van Oortmerssen | d38b9af | 2014-09-26 16:46:30 -0700 | [diff] [blame] | 2008 | } |
| 2009 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 2010 | if (Is('.')) NEXT(); // qualified names may start with a . ? |
| 2011 | ECHECK(ParseTypeIdent(*type)); |
| 2012 | return NoError(); |
Wouter van Oortmerssen | d38b9af | 2014-09-26 16:46:30 -0700 | [diff] [blame] | 2013 | } |
| 2014 | |
Nalinichandra Penke | 13d0594 | 2015-12-22 00:02:19 -0800 | [diff] [blame] | 2015 | CheckedError Parser::SkipAnyJsonValue() { |
| 2016 | switch (token_) { |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 2017 | case '{': { |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 2018 | size_t fieldn_outer = 0; |
| 2019 | return ParseTableDelimiters(fieldn_outer, nullptr, |
| 2020 | [](const std::string &, |
| 2021 | size_t &fieldn, const StructDef *, |
| 2022 | void *state) -> CheckedError { |
| 2023 | auto *parser = static_cast<Parser *>(state); |
| 2024 | ECHECK(parser->SkipAnyJsonValue()); |
| 2025 | fieldn++; |
| 2026 | return NoError(); |
| 2027 | }, |
| 2028 | this); |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 2029 | } |
| 2030 | case '[': { |
| 2031 | size_t count = 0; |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 2032 | return ParseVectorDelimiters(count, [](size_t &, |
| 2033 | void *state) -> CheckedError { |
| 2034 | return static_cast<Parser *>(state)->SkipAnyJsonValue(); |
| 2035 | }, |
| 2036 | this); |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 2037 | } |
Nalinichandra Penke | 13d0594 | 2015-12-22 00:02:19 -0800 | [diff] [blame] | 2038 | case kTokenStringConstant: |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 2039 | EXPECT(kTokenStringConstant); |
Nalinichandra Penke | 13d0594 | 2015-12-22 00:02:19 -0800 | [diff] [blame] | 2040 | break; |
| 2041 | case kTokenIntegerConstant: |
| 2042 | EXPECT(kTokenIntegerConstant); |
| 2043 | break; |
| 2044 | case kTokenFloatConstant: |
| 2045 | EXPECT(kTokenFloatConstant); |
| 2046 | break; |
rouzier | a2b1bfc | 2017-08-04 11:04:28 -0400 | [diff] [blame] | 2047 | case kTokenBooleanConstant: |
| 2048 | EXPECT(kTokenBooleanConstant); |
| 2049 | break; |
Nalinichandra Penke | 13d0594 | 2015-12-22 00:02:19 -0800 | [diff] [blame] | 2050 | default: |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 2051 | return TokenError(); |
Nalinichandra Penke | 13d0594 | 2015-12-22 00:02:19 -0800 | [diff] [blame] | 2052 | } |
| 2053 | return NoError(); |
| 2054 | } |
| 2055 | |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 2056 | CheckedError Parser::ParseFlexBufferValue(flexbuffers::Builder *builder) { |
| 2057 | switch (token_) { |
| 2058 | case '{': { |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 2059 | std::pair<Parser *, flexbuffers::Builder *> parser_and_builder_state( |
| 2060 | this, builder); |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 2061 | auto start = builder->StartMap(); |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 2062 | size_t fieldn_outer = 0; |
| 2063 | auto err = ParseTableDelimiters(fieldn_outer, nullptr, |
| 2064 | [](const std::string &name, |
| 2065 | size_t &fieldn, const StructDef *, |
| 2066 | void *state) -> CheckedError { |
| 2067 | auto *parser_and_builder = |
| 2068 | static_cast<std::pair<Parser *, flexbuffers::Builder *> *>( |
| 2069 | state); |
| 2070 | auto *parser = parser_and_builder->first; |
| 2071 | auto *current_builder = parser_and_builder->second; |
| 2072 | current_builder->Key(name); |
| 2073 | ECHECK(parser->ParseFlexBufferValue(current_builder)); |
| 2074 | fieldn++; |
| 2075 | return NoError(); |
| 2076 | }, |
| 2077 | &parser_and_builder_state); |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 2078 | ECHECK(err); |
| 2079 | builder->EndMap(start); |
| 2080 | break; |
Nalinichandra Penke | cbab266 | 2016-02-22 14:27:08 -0600 | [diff] [blame] | 2081 | } |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 2082 | case '[':{ |
| 2083 | auto start = builder->StartVector(); |
| 2084 | size_t count = 0; |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 2085 | std::pair<Parser *, flexbuffers::Builder *> parser_and_builder_state( |
| 2086 | this, builder); |
| 2087 | ECHECK(ParseVectorDelimiters(count, [](size_t &, |
| 2088 | void *state) -> CheckedError { |
| 2089 | auto *parser_and_builder = |
| 2090 | static_cast<std::pair<Parser *, flexbuffers::Builder *> *>( |
| 2091 | state); |
| 2092 | return parser_and_builder->first->ParseFlexBufferValue( |
| 2093 | parser_and_builder->second); |
| 2094 | }, |
| 2095 | &parser_and_builder_state)); |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 2096 | builder->EndVector(start, false, false); |
| 2097 | break; |
Nalinichandra Penke | cbab266 | 2016-02-22 14:27:08 -0600 | [diff] [blame] | 2098 | } |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 2099 | case kTokenStringConstant: |
| 2100 | builder->String(attribute_); |
| 2101 | EXPECT(kTokenStringConstant); |
| 2102 | break; |
| 2103 | case kTokenIntegerConstant: |
| 2104 | builder->Int(StringToInt(attribute_.c_str())); |
| 2105 | EXPECT(kTokenIntegerConstant); |
| 2106 | break; |
rouzier | a2b1bfc | 2017-08-04 11:04:28 -0400 | [diff] [blame] | 2107 | case kTokenBooleanConstant: |
| 2108 | builder->Bool(StringToInt(attribute_.c_str()) != 0); |
| 2109 | EXPECT(kTokenBooleanConstant); |
| 2110 | break; |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 2111 | case kTokenFloatConstant: |
| 2112 | builder->Double(strtod(attribute_.c_str(), nullptr)); |
| 2113 | EXPECT(kTokenFloatConstant); |
| 2114 | break; |
| 2115 | default: |
| 2116 | return TokenError(); |
Nalinichandra Penke | 13d0594 | 2015-12-22 00:02:19 -0800 | [diff] [blame] | 2117 | } |
Nalinichandra Penke | 13d0594 | 2015-12-22 00:02:19 -0800 | [diff] [blame] | 2118 | return NoError(); |
| 2119 | } |
| 2120 | |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 2121 | bool Parser::ParseFlexBuffer(const char *source, const char *source_filename, |
| 2122 | flexbuffers::Builder *builder) { |
| 2123 | auto ok = !StartParseFile(source, source_filename).Check() && |
| 2124 | !ParseFlexBufferValue(builder).Check(); |
| 2125 | if (ok) builder->Finish(); |
| 2126 | return ok; |
Nalinichandra Penke | 13d0594 | 2015-12-22 00:02:19 -0800 | [diff] [blame] | 2127 | } |
| 2128 | |
Wouter van Oortmerssen | 30642c5 | 2014-09-22 15:49:43 -0700 | [diff] [blame] | 2129 | bool Parser::Parse(const char *source, const char **include_paths, |
| 2130 | const char *source_filename) { |
Robbie McElrath | 0e85eee | 2017-06-26 09:07:02 -0700 | [diff] [blame] | 2131 | return !ParseRoot(source, include_paths, source_filename).Check(); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 2132 | } |
| 2133 | |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 2134 | CheckedError Parser::StartParseFile(const char *source, const char *source_filename) { |
| 2135 | file_being_parsed_ = source_filename ? source_filename : ""; |
| 2136 | source_ = cursor_ = source; |
| 2137 | line_ = 1; |
| 2138 | error_.clear(); |
| 2139 | ECHECK(SkipByteOrderMark()); |
| 2140 | NEXT(); |
| 2141 | if (Is(kTokenEof)) |
| 2142 | return Error("input file is empty"); |
| 2143 | return NoError(); |
| 2144 | } |
| 2145 | |
Robbie McElrath | 0e85eee | 2017-06-26 09:07:02 -0700 | [diff] [blame] | 2146 | CheckedError Parser::ParseRoot(const char *source, const char **include_paths, |
| 2147 | const char *source_filename) { |
| 2148 | ECHECK(DoParse(source, include_paths, source_filename, nullptr)); |
| 2149 | |
| 2150 | // Check that all types were defined. |
| 2151 | for (auto it = structs_.vec.begin(); it != structs_.vec.end(); ++it) { |
| 2152 | if ((*it)->predecl) { |
Wouter van Oortmerssen | 321a1c9 | 2017-08-24 12:55:35 -0700 | [diff] [blame^] | 2153 | return Error("type referenced but not defined (check namespace): " + |
| 2154 | (*it)->name); |
Robbie McElrath | 0e85eee | 2017-06-26 09:07:02 -0700 | [diff] [blame] | 2155 | } |
| 2156 | } |
| 2157 | |
| 2158 | // This check has to happen here and not earlier, because only now do we |
| 2159 | // know for sure what the type of these are. |
| 2160 | for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) { |
| 2161 | auto &enum_def = **it; |
| 2162 | if (enum_def.is_union) { |
| 2163 | for (auto val_it = enum_def.vals.vec.begin(); |
| 2164 | val_it != enum_def.vals.vec.end(); |
| 2165 | ++val_it) { |
| 2166 | auto &val = **val_it; |
Kamil Rojewski | 46bb05d | 2017-08-11 18:24:36 +0200 | [diff] [blame] | 2167 | if (!SupportsVectorOfUnions() && |
Robbie McElrath | 0e85eee | 2017-06-26 09:07:02 -0700 | [diff] [blame] | 2168 | val.union_type.struct_def && val.union_type.struct_def->fixed) |
| 2169 | return Error( |
| 2170 | "only tables can be union elements in the generated language: " |
| 2171 | + val.name); |
| 2172 | } |
| 2173 | } |
| 2174 | } |
| 2175 | return NoError(); |
| 2176 | } |
| 2177 | |
| 2178 | CheckedError Parser::DoParse(const char *source, |
| 2179 | const char **include_paths, |
| 2180 | const char *source_filename, |
| 2181 | const char *include_filename) { |
Gabriel Martinez | df4909e | 2014-11-04 10:00:56 -0800 | [diff] [blame] | 2182 | if (source_filename && |
Pavel Kalinnikov | 642254b | 2017-06-02 17:50:18 +0200 | [diff] [blame] | 2183 | included_files_.find(source_filename) == included_files_.end()) { |
| 2184 | included_files_[source_filename] = include_filename ? include_filename : ""; |
Gabriel Martinez | df4909e | 2014-11-04 10:00:56 -0800 | [diff] [blame] | 2185 | files_included_per_file_[source_filename] = std::set<std::string>(); |
Gabriel Martinez | df4909e | 2014-11-04 10:00:56 -0800 | [diff] [blame] | 2186 | } |
| 2187 | if (!include_paths) { |
Wouter van Oortmerssen | 1e6f8f5 | 2015-06-22 10:23:42 -0700 | [diff] [blame] | 2188 | static const char *current_directory[] = { "", nullptr }; |
Gabriel Martinez | df4909e | 2014-11-04 10:00:56 -0800 | [diff] [blame] | 2189 | include_paths = current_directory; |
| 2190 | } |
Yonggang Li | fea6b52 | 2017-01-03 13:54:15 -0800 | [diff] [blame] | 2191 | field_stack_.clear(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 2192 | builder_.Clear(); |
Wouter van Oortmerssen | 94680f5 | 2015-10-05 17:39:08 -0700 | [diff] [blame] | 2193 | // Start with a blank namespace just in case this file doesn't have one. |
Wouter van Oortmerssen | 321a1c9 | 2017-08-24 12:55:35 -0700 | [diff] [blame^] | 2194 | current_namespace_ = empty_namespace_; |
Louis-Paul CORDIER | e7e4dc7 | 2017-03-10 17:50:44 +0100 | [diff] [blame] | 2195 | |
Wouter van Oortmerssen | 8f864aa | 2017-06-05 17:45:44 -0700 | [diff] [blame] | 2196 | ECHECK(StartParseFile(source, source_filename)); |
Louis-Paul CORDIER | e7e4dc7 | 2017-03-10 17:50:44 +0100 | [diff] [blame] | 2197 | |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 2198 | // Includes must come before type declarations: |
| 2199 | for (;;) { |
| 2200 | // Parse pre-include proto statements if any: |
| 2201 | if (opts.proto_mode && |
| 2202 | (attribute_ == "option" || attribute_ == "syntax" || |
| 2203 | attribute_ == "package")) { |
| 2204 | ECHECK(ParseProtoDecl()); |
Wouter van Oortmerssen | 3f936c5 | 2017-01-18 16:23:35 -0800 | [diff] [blame] | 2205 | } else if (Is(kTokenNativeInclude)) { |
| 2206 | NEXT(); |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 2207 | vector_emplace_back(&native_included_files_, attribute_); |
Wouter van Oortmerssen | 3f936c5 | 2017-01-18 16:23:35 -0800 | [diff] [blame] | 2208 | EXPECT(kTokenStringConstant); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 2209 | } else if (Is(kTokenInclude) || |
| 2210 | (opts.proto_mode && |
| 2211 | attribute_ == "import" && |
| 2212 | Is(kTokenIdentifier))) { |
| 2213 | NEXT(); |
| 2214 | if (opts.proto_mode && attribute_ == "public") NEXT(); |
Wouter van Oortmerssen | aaf5598 | 2017-05-18 17:18:43 -0700 | [diff] [blame] | 2215 | auto name = flatbuffers::PosixPath(attribute_.c_str()); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 2216 | EXPECT(kTokenStringConstant); |
| 2217 | // Look for the file in include_paths. |
| 2218 | std::string filepath; |
| 2219 | for (auto paths = include_paths; paths && *paths; paths++) { |
| 2220 | filepath = flatbuffers::ConCatPathFileName(*paths, name); |
| 2221 | if(FileExists(filepath.c_str())) break; |
Wouter van Oortmerssen | be894f0 | 2014-08-19 14:20:05 -0700 | [diff] [blame] | 2222 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 2223 | if (filepath.empty()) |
| 2224 | return Error("unable to locate include file: " + name); |
| 2225 | if (source_filename) |
| 2226 | files_included_per_file_[source_filename].insert(filepath); |
Pavel Kalinnikov | 642254b | 2017-06-02 17:50:18 +0200 | [diff] [blame] | 2227 | if (included_files_.find(filepath) == included_files_.end()) { |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 2228 | // We found an include file that we have not parsed yet. |
| 2229 | // Load it and parse it. |
| 2230 | std::string contents; |
| 2231 | if (!LoadFile(filepath.c_str(), true, &contents)) |
| 2232 | return Error("unable to load include file: " + name); |
Wouter van Oortmerssen | 22743ca | 2017-05-24 15:21:26 -0700 | [diff] [blame] | 2233 | ECHECK(DoParse(contents.c_str(), include_paths, filepath.c_str(), |
| 2234 | name.c_str())); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 2235 | // We generally do not want to output code for any included files: |
| 2236 | if (!opts.generate_all) MarkGenerated(); |
Wouter van Oortmerssen | 432e758 | 2017-08-14 10:41:39 -0700 | [diff] [blame] | 2237 | // Reset these just in case the included file had them, and the |
Wouter van Oortmerssen | dca33dd | 2017-08-14 09:22:12 -0700 | [diff] [blame] | 2238 | // parent doesn't. |
| 2239 | root_struct_def_ = nullptr; |
Wouter van Oortmerssen | 432e758 | 2017-08-14 10:41:39 -0700 | [diff] [blame] | 2240 | file_identifier_.clear(); |
| 2241 | file_extension_.clear(); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 2242 | // This is the easiest way to continue this file after an include: |
| 2243 | // instead of saving and restoring all the state, we simply start the |
| 2244 | // file anew. This will cause it to encounter the same include |
| 2245 | // statement again, but this time it will skip it, because it was |
| 2246 | // entered into included_files_. |
| 2247 | // This is recursive, but only go as deep as the number of include |
| 2248 | // statements. |
Wouter van Oortmerssen | 22743ca | 2017-05-24 15:21:26 -0700 | [diff] [blame] | 2249 | return DoParse(source, include_paths, source_filename, include_filename); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 2250 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 2251 | EXPECT(';'); |
| 2252 | } else { |
| 2253 | break; |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 2254 | } |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 2255 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 2256 | // Now parse all other kinds of declarations: |
| 2257 | while (token_ != kTokenEof) { |
| 2258 | if (opts.proto_mode) { |
| 2259 | ECHECK(ParseProtoDecl()); |
| 2260 | } else if (token_ == kTokenNameSpace) { |
| 2261 | ECHECK(ParseNamespace()); |
| 2262 | } else if (token_ == '{') { |
| 2263 | if (!root_struct_def_) |
| 2264 | return Error("no root type set to parse json with"); |
| 2265 | if (builder_.GetSize()) { |
| 2266 | return Error("cannot have more than one json object in a file"); |
| 2267 | } |
| 2268 | uoffset_t toff; |
| 2269 | ECHECK(ParseTable(*root_struct_def_, nullptr, &toff)); |
| 2270 | builder_.Finish(Offset<Table>(toff), |
| 2271 | file_identifier_.length() ? file_identifier_.c_str() : nullptr); |
| 2272 | } else if (token_ == kTokenEnum) { |
| 2273 | ECHECK(ParseEnum(false, nullptr)); |
| 2274 | } else if (token_ == kTokenUnion) { |
| 2275 | ECHECK(ParseEnum(true, nullptr)); |
| 2276 | } else if (token_ == kTokenRootType) { |
| 2277 | NEXT(); |
| 2278 | auto root_type = attribute_; |
| 2279 | EXPECT(kTokenIdentifier); |
| 2280 | ECHECK(ParseNamespacing(&root_type, nullptr)); |
| 2281 | if (!SetRootType(root_type.c_str())) |
| 2282 | return Error("unknown root type: " + root_type); |
| 2283 | if (root_struct_def_->fixed) |
| 2284 | return Error("root type must be a table"); |
| 2285 | EXPECT(';'); |
| 2286 | } else if (token_ == kTokenFileIdentifier) { |
| 2287 | NEXT(); |
| 2288 | file_identifier_ = attribute_; |
| 2289 | EXPECT(kTokenStringConstant); |
| 2290 | if (file_identifier_.length() != |
| 2291 | FlatBufferBuilder::kFileIdentifierLength) |
| 2292 | return Error("file_identifier must be exactly " + |
| 2293 | NumToString(FlatBufferBuilder::kFileIdentifierLength) + |
| 2294 | " characters"); |
| 2295 | EXPECT(';'); |
| 2296 | } else if (token_ == kTokenFileExtension) { |
| 2297 | NEXT(); |
| 2298 | file_extension_ = attribute_; |
| 2299 | EXPECT(kTokenStringConstant); |
| 2300 | EXPECT(';'); |
| 2301 | } else if(token_ == kTokenInclude) { |
| 2302 | return Error("includes must come before declarations"); |
| 2303 | } else if(token_ == kTokenAttribute) { |
| 2304 | NEXT(); |
| 2305 | auto name = attribute_; |
| 2306 | EXPECT(kTokenStringConstant); |
| 2307 | EXPECT(';'); |
Wouter van Oortmerssen | 72fc45a | 2016-04-11 17:36:56 -0700 | [diff] [blame] | 2308 | known_attributes_[name] = false; |
Wouter van Oortmerssen | 1a63eb4 | 2016-03-07 18:07:10 -0800 | [diff] [blame] | 2309 | } else if (token_ == kTokenService) { |
| 2310 | ECHECK(ParseService()); |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 2311 | } else { |
| 2312 | ECHECK(ParseDecl()); |
| 2313 | } |
| 2314 | } |
Wouter van Oortmerssen | 40a33b1 | 2015-12-09 18:06:56 -0800 | [diff] [blame] | 2315 | return NoError(); |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 2316 | } |
| 2317 | |
Gabriel Martinez | df4909e | 2014-11-04 10:00:56 -0800 | [diff] [blame] | 2318 | std::set<std::string> Parser::GetIncludedFilesRecursive( |
| 2319 | const std::string &file_name) const { |
| 2320 | std::set<std::string> included_files; |
| 2321 | std::list<std::string> to_process; |
| 2322 | |
| 2323 | if (file_name.empty()) return included_files; |
| 2324 | to_process.push_back(file_name); |
| 2325 | |
| 2326 | while (!to_process.empty()) { |
| 2327 | std::string current = to_process.front(); |
| 2328 | to_process.pop_front(); |
| 2329 | included_files.insert(current); |
| 2330 | |
Stewart Miles | a892322 | 2017-07-13 06:27:39 -0700 | [diff] [blame] | 2331 | // Workaround the lack of const accessor in C++98 maps. |
| 2332 | auto &new_files = |
| 2333 | (*const_cast<std::map<std::string, std::set<std::string>> *>( |
| 2334 | &files_included_per_file_))[current]; |
Gabriel Martinez | df4909e | 2014-11-04 10:00:56 -0800 | [diff] [blame] | 2335 | for (auto it = new_files.begin(); it != new_files.end(); ++it) { |
| 2336 | if (included_files.find(*it) == included_files.end()) |
| 2337 | to_process.push_back(*it); |
| 2338 | } |
| 2339 | } |
| 2340 | |
| 2341 | return included_files; |
| 2342 | } |
| 2343 | |
Wouter van Oortmerssen | 81312c2 | 2015-05-21 16:33:29 -0700 | [diff] [blame] | 2344 | // Schema serialization functionality: |
| 2345 | |
Chandra Penke | b63ebad | 2016-01-06 08:31:53 -0800 | [diff] [blame] | 2346 | template<typename T> bool compareName(const T* a, const T* b) { |
Xun Liu | df0991b | 2016-09-14 10:33:06 -0700 | [diff] [blame] | 2347 | return a->defined_namespace->GetFullyQualifiedName(a->name) |
| 2348 | < b->defined_namespace->GetFullyQualifiedName(b->name); |
Chandra Penke | b63ebad | 2016-01-06 08:31:53 -0800 | [diff] [blame] | 2349 | } |
| 2350 | |
Wouter van Oortmerssen | 81312c2 | 2015-05-21 16:33:29 -0700 | [diff] [blame] | 2351 | template<typename T> void AssignIndices(const std::vector<T *> &defvec) { |
| 2352 | // Pre-sort these vectors, such that we can set the correct indices for them. |
| 2353 | auto vec = defvec; |
Chandra Penke | b63ebad | 2016-01-06 08:31:53 -0800 | [diff] [blame] | 2354 | std::sort(vec.begin(), vec.end(), compareName<T>); |
Wouter van Oortmerssen | 81312c2 | 2015-05-21 16:33:29 -0700 | [diff] [blame] | 2355 | for (int i = 0; i < static_cast<int>(vec.size()); i++) vec[i]->index = i; |
| 2356 | } |
| 2357 | |
| 2358 | void Parser::Serialize() { |
| 2359 | builder_.Clear(); |
| 2360 | AssignIndices(structs_.vec); |
| 2361 | AssignIndices(enums_.vec); |
| 2362 | std::vector<Offset<reflection::Object>> object_offsets; |
| 2363 | for (auto it = structs_.vec.begin(); it != structs_.vec.end(); ++it) { |
Wouter van Oortmerssen | 72fc45a | 2016-04-11 17:36:56 -0700 | [diff] [blame] | 2364 | auto offset = (*it)->Serialize(&builder_, *this); |
Wouter van Oortmerssen | 81312c2 | 2015-05-21 16:33:29 -0700 | [diff] [blame] | 2365 | object_offsets.push_back(offset); |
| 2366 | (*it)->serialized_location = offset.o; |
| 2367 | } |
| 2368 | std::vector<Offset<reflection::Enum>> enum_offsets; |
| 2369 | for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) { |
Wouter van Oortmerssen | 72fc45a | 2016-04-11 17:36:56 -0700 | [diff] [blame] | 2370 | auto offset = (*it)->Serialize(&builder_, *this); |
Wouter van Oortmerssen | 81312c2 | 2015-05-21 16:33:29 -0700 | [diff] [blame] | 2371 | enum_offsets.push_back(offset); |
| 2372 | (*it)->serialized_location = offset.o; |
| 2373 | } |
| 2374 | auto schema_offset = reflection::CreateSchema( |
| 2375 | builder_, |
| 2376 | builder_.CreateVectorOfSortedTables(&object_offsets), |
| 2377 | builder_.CreateVectorOfSortedTables(&enum_offsets), |
| 2378 | builder_.CreateString(file_identifier_), |
| 2379 | builder_.CreateString(file_extension_), |
Wouter van Oortmerssen | 36c7e9a | 2015-06-29 15:21:48 -0700 | [diff] [blame] | 2380 | root_struct_def_ |
| 2381 | ? root_struct_def_->serialized_location |
| 2382 | : 0); |
Wouter van Oortmerssen | 81312c2 | 2015-05-21 16:33:29 -0700 | [diff] [blame] | 2383 | builder_.Finish(schema_offset, reflection::SchemaIdentifier()); |
| 2384 | } |
| 2385 | |
Wouter van Oortmerssen | 72fc45a | 2016-04-11 17:36:56 -0700 | [diff] [blame] | 2386 | Offset<reflection::Object> StructDef::Serialize(FlatBufferBuilder *builder, |
| 2387 | const Parser &parser) const { |
Wouter van Oortmerssen | 81312c2 | 2015-05-21 16:33:29 -0700 | [diff] [blame] | 2388 | std::vector<Offset<reflection::Field>> field_offsets; |
| 2389 | for (auto it = fields.vec.begin(); it != fields.vec.end(); ++it) { |
Wouter van Oortmerssen | 622b8d0 | 2015-06-15 15:57:48 -0700 | [diff] [blame] | 2390 | field_offsets.push_back( |
| 2391 | (*it)->Serialize(builder, |
Wouter van Oortmerssen | 72fc45a | 2016-04-11 17:36:56 -0700 | [diff] [blame] | 2392 | static_cast<uint16_t>(it - fields.vec.begin()), parser)); |
Wouter van Oortmerssen | 81312c2 | 2015-05-21 16:33:29 -0700 | [diff] [blame] | 2393 | } |
Xun Liu | df0991b | 2016-09-14 10:33:06 -0700 | [diff] [blame] | 2394 | auto qualified_name = defined_namespace->GetFullyQualifiedName(name); |
Wouter van Oortmerssen | 81312c2 | 2015-05-21 16:33:29 -0700 | [diff] [blame] | 2395 | return reflection::CreateObject(*builder, |
Xun Liu | df0991b | 2016-09-14 10:33:06 -0700 | [diff] [blame] | 2396 | builder->CreateString(qualified_name), |
Wouter van Oortmerssen | 81312c2 | 2015-05-21 16:33:29 -0700 | [diff] [blame] | 2397 | builder->CreateVectorOfSortedTables( |
| 2398 | &field_offsets), |
Wouter van Oortmerssen | cb2b2be | 2015-06-23 16:06:35 -0700 | [diff] [blame] | 2399 | fixed, |
| 2400 | static_cast<int>(minalign), |
Wouter van Oortmerssen | 72fc45a | 2016-04-11 17:36:56 -0700 | [diff] [blame] | 2401 | static_cast<int>(bytesize), |
Wouter van Oortmerssen | 1fb6b9e | 2017-02-13 16:15:55 -0800 | [diff] [blame] | 2402 | SerializeAttributes(builder, parser), |
| 2403 | parser.opts.binary_schema_comments |
| 2404 | ? builder->CreateVectorOfStrings( |
| 2405 | doc_comment) |
| 2406 | : 0); |
Wouter van Oortmerssen | 81312c2 | 2015-05-21 16:33:29 -0700 | [diff] [blame] | 2407 | } |
| 2408 | |
| 2409 | Offset<reflection::Field> FieldDef::Serialize(FlatBufferBuilder *builder, |
Wouter van Oortmerssen | 72fc45a | 2016-04-11 17:36:56 -0700 | [diff] [blame] | 2410 | uint16_t id, |
| 2411 | const Parser &parser) const { |
Wouter van Oortmerssen | 81312c2 | 2015-05-21 16:33:29 -0700 | [diff] [blame] | 2412 | return reflection::CreateField(*builder, |
| 2413 | builder->CreateString(name), |
| 2414 | value.type.Serialize(builder), |
| 2415 | id, |
| 2416 | value.offset, |
| 2417 | IsInteger(value.type.base_type) |
| 2418 | ? StringToInt(value.constant.c_str()) |
| 2419 | : 0, |
| 2420 | IsFloat(value.type.base_type) |
| 2421 | ? strtod(value.constant.c_str(), nullptr) |
| 2422 | : 0.0, |
| 2423 | deprecated, |
| 2424 | required, |
Wouter van Oortmerssen | 72fc45a | 2016-04-11 17:36:56 -0700 | [diff] [blame] | 2425 | key, |
Wouter van Oortmerssen | 1fb6b9e | 2017-02-13 16:15:55 -0800 | [diff] [blame] | 2426 | SerializeAttributes(builder, parser), |
| 2427 | parser.opts.binary_schema_comments |
| 2428 | ? builder->CreateVectorOfStrings(doc_comment) |
| 2429 | : 0); |
Wouter van Oortmerssen | 81312c2 | 2015-05-21 16:33:29 -0700 | [diff] [blame] | 2430 | // TODO: value.constant is almost always "0", we could save quite a bit of |
| 2431 | // space by sharing it. Same for common values of value.type. |
| 2432 | } |
| 2433 | |
Wouter van Oortmerssen | 72fc45a | 2016-04-11 17:36:56 -0700 | [diff] [blame] | 2434 | Offset<reflection::Enum> EnumDef::Serialize(FlatBufferBuilder *builder, |
| 2435 | const Parser &parser) const { |
Wouter van Oortmerssen | 81312c2 | 2015-05-21 16:33:29 -0700 | [diff] [blame] | 2436 | std::vector<Offset<reflection::EnumVal>> enumval_offsets; |
| 2437 | for (auto it = vals.vec.begin(); it != vals.vec.end(); ++it) { |
| 2438 | enumval_offsets.push_back((*it)->Serialize(builder)); |
| 2439 | } |
Xun Liu | df0991b | 2016-09-14 10:33:06 -0700 | [diff] [blame] | 2440 | auto qualified_name = defined_namespace->GetFullyQualifiedName(name); |
Wouter van Oortmerssen | 81312c2 | 2015-05-21 16:33:29 -0700 | [diff] [blame] | 2441 | return reflection::CreateEnum(*builder, |
Xun Liu | df0991b | 2016-09-14 10:33:06 -0700 | [diff] [blame] | 2442 | builder->CreateString(qualified_name), |
Wouter van Oortmerssen | 81312c2 | 2015-05-21 16:33:29 -0700 | [diff] [blame] | 2443 | builder->CreateVector(enumval_offsets), |
| 2444 | is_union, |
Wouter van Oortmerssen | 72fc45a | 2016-04-11 17:36:56 -0700 | [diff] [blame] | 2445 | underlying_type.Serialize(builder), |
Wouter van Oortmerssen | 1fb6b9e | 2017-02-13 16:15:55 -0800 | [diff] [blame] | 2446 | SerializeAttributes(builder, parser), |
| 2447 | parser.opts.binary_schema_comments |
| 2448 | ? builder->CreateVectorOfStrings(doc_comment) |
| 2449 | : 0); |
Wouter van Oortmerssen | 81312c2 | 2015-05-21 16:33:29 -0700 | [diff] [blame] | 2450 | } |
| 2451 | |
| 2452 | Offset<reflection::EnumVal> EnumVal::Serialize(FlatBufferBuilder *builder) const |
| 2453 | { |
| 2454 | return reflection::CreateEnumVal(*builder, |
| 2455 | builder->CreateString(name), |
| 2456 | value, |
Wouter van Oortmerssen | b0752e1 | 2017-04-10 15:56:51 -0700 | [diff] [blame] | 2457 | union_type.struct_def |
| 2458 | ? union_type.struct_def-> |
| 2459 | serialized_location |
| 2460 | : 0, |
| 2461 | union_type.Serialize(builder)); |
Wouter van Oortmerssen | 81312c2 | 2015-05-21 16:33:29 -0700 | [diff] [blame] | 2462 | } |
| 2463 | |
| 2464 | Offset<reflection::Type> Type::Serialize(FlatBufferBuilder *builder) const { |
| 2465 | return reflection::CreateType(*builder, |
| 2466 | static_cast<reflection::BaseType>(base_type), |
| 2467 | static_cast<reflection::BaseType>(element), |
| 2468 | struct_def ? struct_def->index : |
| 2469 | (enum_def ? enum_def->index : -1)); |
| 2470 | } |
| 2471 | |
Wouter van Oortmerssen | 72fc45a | 2016-04-11 17:36:56 -0700 | [diff] [blame] | 2472 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset< |
| 2473 | reflection::KeyValue>>> |
| 2474 | Definition::SerializeAttributes(FlatBufferBuilder *builder, |
| 2475 | const Parser &parser) const { |
| 2476 | std::vector<flatbuffers::Offset<reflection::KeyValue>> attrs; |
Wouter van Oortmerssen | e92ae51 | 2016-06-02 14:55:35 -0700 | [diff] [blame] | 2477 | for (auto kv = attributes.dict.begin(); kv != attributes.dict.end(); ++kv) { |
| 2478 | auto it = parser.known_attributes_.find(kv->first); |
Wouter van Oortmerssen | 72fc45a | 2016-04-11 17:36:56 -0700 | [diff] [blame] | 2479 | assert(it != parser.known_attributes_.end()); |
| 2480 | if (!it->second) { // Custom attribute. |
| 2481 | attrs.push_back( |
Wouter van Oortmerssen | e92ae51 | 2016-06-02 14:55:35 -0700 | [diff] [blame] | 2482 | reflection::CreateKeyValue(*builder, builder->CreateString(kv->first), |
Wouter van Oortmerssen | 72fc45a | 2016-04-11 17:36:56 -0700 | [diff] [blame] | 2483 | builder->CreateString( |
Wouter van Oortmerssen | e92ae51 | 2016-06-02 14:55:35 -0700 | [diff] [blame] | 2484 | kv->second->constant))); |
Wouter van Oortmerssen | 72fc45a | 2016-04-11 17:36:56 -0700 | [diff] [blame] | 2485 | } |
| 2486 | } |
| 2487 | if (attrs.size()) { |
| 2488 | return builder->CreateVectorOfSortedTables(&attrs); |
| 2489 | } else { |
| 2490 | return 0; |
| 2491 | } |
| 2492 | } |
| 2493 | |
Wouter van Oortmerssen | 05b00c5 | 2016-07-20 17:24:50 -0700 | [diff] [blame] | 2494 | std::string Parser::ConformTo(const Parser &base) { |
| 2495 | for (auto sit = structs_.vec.begin(); sit != structs_.vec.end(); ++sit) { |
| 2496 | auto &struct_def = **sit; |
| 2497 | auto qualified_name = |
| 2498 | struct_def.defined_namespace->GetFullyQualifiedName(struct_def.name); |
| 2499 | auto struct_def_base = base.structs_.Lookup(qualified_name); |
| 2500 | if (!struct_def_base) continue; |
| 2501 | for (auto fit = struct_def.fields.vec.begin(); |
| 2502 | fit != struct_def.fields.vec.end(); ++fit) { |
| 2503 | auto &field = **fit; |
| 2504 | auto field_base = struct_def_base->fields.Lookup(field.name); |
| 2505 | if (field_base) { |
| 2506 | if (field.value.offset != field_base->value.offset) |
| 2507 | return "offsets differ for field: " + field.name; |
| 2508 | if (field.value.constant != field_base->value.constant) |
| 2509 | return "defaults differ for field: " + field.name; |
| 2510 | if (!EqualByName(field.value.type, field_base->value.type)) |
| 2511 | return "types differ for field: " + field.name; |
| 2512 | } else { |
| 2513 | // Doesn't have to exist, deleting fields is fine. |
| 2514 | // But we should check if there is a field that has the same offset |
| 2515 | // but is incompatible (in the case of field renaming). |
| 2516 | for (auto fbit = struct_def_base->fields.vec.begin(); |
| 2517 | fbit != struct_def_base->fields.vec.end(); ++fbit) { |
| 2518 | field_base = *fbit; |
| 2519 | if (field.value.offset == field_base->value.offset) { |
| 2520 | if (!EqualByName(field.value.type, field_base->value.type)) |
| 2521 | return "field renamed to different type: " + field.name; |
| 2522 | break; |
| 2523 | } |
| 2524 | } |
| 2525 | } |
| 2526 | } |
| 2527 | } |
| 2528 | for (auto eit = enums_.vec.begin(); eit != enums_.vec.end(); ++eit) { |
| 2529 | auto &enum_def = **eit; |
| 2530 | auto qualified_name = |
| 2531 | enum_def.defined_namespace->GetFullyQualifiedName(enum_def.name); |
| 2532 | auto enum_def_base = base.enums_.Lookup(qualified_name); |
| 2533 | if (!enum_def_base) continue; |
| 2534 | for (auto evit = enum_def.vals.vec.begin(); |
| 2535 | evit != enum_def.vals.vec.end(); ++evit) { |
| 2536 | auto &enum_val = **evit; |
| 2537 | auto enum_val_base = enum_def_base->vals.Lookup(enum_val.name); |
| 2538 | if (enum_val_base) { |
| 2539 | if (enum_val.value != enum_val_base->value) |
| 2540 | return "values differ for enum: " + enum_val.name; |
| 2541 | } |
| 2542 | } |
| 2543 | } |
| 2544 | return ""; |
| 2545 | } |
| 2546 | |
Wouter van Oortmerssen | 26a3073 | 2014-01-27 16:52:49 -0800 | [diff] [blame] | 2547 | } // namespace flatbuffers |