idl_parser.cpp 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186
  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. #include <algorithm>
  17. #include <list>
  18. #ifdef _WIN32
  19. #if !defined(_USE_MATH_DEFINES)
  20. #define _USE_MATH_DEFINES // For M_PI.
  21. #endif // !defined(_USE_MATH_DEFINES)
  22. #endif // _WIN32
  23. #include <math.h>
  24. #include "flatbuffers/idl.h"
  25. #include "flatbuffers/util.h"
  26. namespace flatbuffers {
  27. const char *const kTypeNames[] = {
  28. #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
  29. IDLTYPE,
  30. FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
  31. #undef FLATBUFFERS_TD
  32. nullptr
  33. };
  34. const char kTypeSizes[] = {
  35. #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
  36. sizeof(CTYPE),
  37. FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
  38. #undef FLATBUFFERS_TD
  39. };
  40. // The enums in the reflection schema should match the ones we use internally.
  41. // Compare the last element to check if these go out of sync.
  42. static_assert(BASE_TYPE_UNION ==
  43. static_cast<BaseType>(reflection::Union),
  44. "enums don't match");
  45. // Any parsing calls have to be wrapped in this macro, which automates
  46. // handling of recursive error checking a bit. It will check the received
  47. // CheckedError object, and return straight away on error.
  48. #define ECHECK(call) { auto ce = (call); if (ce.Check()) return ce; }
  49. // These two functions are called hundreds of times below, so define a short
  50. // form:
  51. #define NEXT() ECHECK(Next())
  52. #define EXPECT(tok) ECHECK(Expect(tok))
  53. static bool ValidateUTF8(const std::string &str) {
  54. const char *s = &str[0];
  55. const char * const sEnd = s + str.length();
  56. while (s < sEnd) {
  57. if (FromUTF8(&s) < 0) {
  58. return false;
  59. }
  60. }
  61. return true;
  62. }
  63. CheckedError Parser::Error(const std::string &msg) {
  64. error_ = file_being_parsed_.length() ? AbsolutePath(file_being_parsed_) : "";
  65. #ifdef _WIN32
  66. error_ += "(" + NumToString(line_) + ")"; // MSVC alike
  67. #else
  68. if (file_being_parsed_.length()) error_ += ":";
  69. error_ += NumToString(line_) + ":0"; // gcc alike
  70. #endif
  71. error_ += ": error: " + msg;
  72. return CheckedError(true);
  73. }
  74. inline CheckedError NoError() { return CheckedError(false); }
  75. // Ensure that integer values we parse fit inside the declared integer type.
  76. CheckedError Parser::CheckBitsFit(int64_t val, size_t bits) {
  77. // Left-shifting a 64-bit value by 64 bits or more is undefined
  78. // behavior (C99 6.5.7), so check *before* we shift.
  79. if (bits < 64) {
  80. // Bits we allow to be used.
  81. auto mask = static_cast<int64_t>((1ull << bits) - 1);
  82. if ((val & ~mask) != 0 && // Positive or unsigned.
  83. (val | mask) != -1) // Negative.
  84. return Error("constant does not fit in a " + NumToString(bits) +
  85. "-bit field");
  86. }
  87. return NoError();
  88. }
  89. // atot: templated version of atoi/atof: convert a string to an instance of T.
  90. template<typename T> inline CheckedError atot(const char *s, Parser &parser,
  91. T *val) {
  92. int64_t i = StringToInt(s);
  93. ECHECK(parser.CheckBitsFit(i, sizeof(T) * 8));
  94. *val = (T)i;
  95. return NoError();
  96. }
  97. template<> inline CheckedError atot<bool>(const char *s, Parser &parser,
  98. bool *val) {
  99. (void)parser;
  100. *val = 0 != atoi(s);
  101. return NoError();
  102. }
  103. template<> inline CheckedError atot<float>(const char *s, Parser &parser,
  104. float *val) {
  105. (void)parser;
  106. *val = static_cast<float>(strtod(s, nullptr));
  107. return NoError();
  108. }
  109. template<> inline CheckedError atot<double>(const char *s, Parser &parser,
  110. double *val) {
  111. (void)parser;
  112. *val = strtod(s, nullptr);
  113. return NoError();
  114. }
  115. template<> inline CheckedError atot<Offset<void>>(const char *s, Parser &parser,
  116. Offset<void> *val) {
  117. (void)parser;
  118. *val = Offset<void>(atoi(s));
  119. return NoError();
  120. }
  121. std::string Namespace::GetFullyQualifiedName(const std::string &name,
  122. size_t max_components) const {
  123. // Early exit if we don't have a defined namespace.
  124. if (components.size() == 0 || !max_components) {
  125. return name;
  126. }
  127. std::stringstream stream;
  128. for (size_t i = 0; i < std::min(components.size(), max_components);
  129. i++) {
  130. if (i) {
  131. stream << ".";
  132. }
  133. stream << components[i];
  134. }
  135. stream << "." << name;
  136. return stream.str();
  137. }
  138. // Declare tokens we'll use. Single character tokens are represented by their
  139. // ascii character code (e.g. '{'), others above 256.
  140. #define FLATBUFFERS_GEN_TOKENS(TD) \
  141. TD(Eof, 256, "end of file") \
  142. TD(StringConstant, 257, "string constant") \
  143. TD(IntegerConstant, 258, "integer constant") \
  144. TD(FloatConstant, 259, "float constant") \
  145. TD(Identifier, 260, "identifier") \
  146. TD(Table, 261, "table") \
  147. TD(Struct, 262, "struct") \
  148. TD(Enum, 263, "enum") \
  149. TD(Union, 264, "union") \
  150. TD(NameSpace, 265, "namespace") \
  151. TD(RootType, 266, "root_type") \
  152. TD(FileIdentifier, 267, "file_identifier") \
  153. TD(FileExtension, 268, "file_extension") \
  154. TD(Include, 269, "include") \
  155. TD(Attribute, 270, "attribute") \
  156. TD(Null, 271, "null") \
  157. TD(Service, 272, "rpc_service")
  158. #ifdef __GNUC__
  159. __extension__ // Stop GCC complaining about trailing comma with -Wpendantic.
  160. #endif
  161. enum {
  162. #define FLATBUFFERS_TOKEN(NAME, VALUE, STRING) kToken ## NAME = VALUE,
  163. FLATBUFFERS_GEN_TOKENS(FLATBUFFERS_TOKEN)
  164. #undef FLATBUFFERS_TOKEN
  165. #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
  166. kToken ## ENUM,
  167. FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
  168. #undef FLATBUFFERS_TD
  169. };
  170. static std::string TokenToString(int t) {
  171. static const char *tokens[] = {
  172. #define FLATBUFFERS_TOKEN(NAME, VALUE, STRING) STRING,
  173. FLATBUFFERS_GEN_TOKENS(FLATBUFFERS_TOKEN)
  174. #undef FLATBUFFERS_TOKEN
  175. #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
  176. IDLTYPE,
  177. FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
  178. #undef FLATBUFFERS_TD
  179. };
  180. if (t < 256) { // A single ascii char token.
  181. std::string s;
  182. s.append(1, static_cast<char>(t));
  183. return s;
  184. } else { // Other tokens.
  185. return tokens[t - 256];
  186. }
  187. }
  188. std::string Parser::TokenToStringId(int t) {
  189. return TokenToString(t) + (t == kTokenIdentifier ? ": " + attribute_ : "");
  190. }
  191. // Parses exactly nibbles worth of hex digits into a number, or error.
  192. CheckedError Parser::ParseHexNum(int nibbles, int64_t *val) {
  193. for (int i = 0; i < nibbles; i++)
  194. if (!isxdigit(static_cast<const unsigned char>(cursor_[i])))
  195. return Error("escape code must be followed by " + NumToString(nibbles) +
  196. " hex digits");
  197. std::string target(cursor_, cursor_ + nibbles);
  198. *val = StringToUInt(target.c_str(), nullptr, 16);
  199. cursor_ += nibbles;
  200. return NoError();
  201. }
  202. CheckedError Parser::SkipByteOrderMark() {
  203. if (static_cast<unsigned char>(*cursor_) != 0xef) return NoError();
  204. cursor_++;
  205. if (static_cast<unsigned char>(*cursor_) != 0xbb) return Error("invalid utf-8 byte order mark");
  206. cursor_++;
  207. if (static_cast<unsigned char>(*cursor_) != 0xbf) return Error("invalid utf-8 byte order mark");
  208. cursor_++;
  209. return NoError();
  210. }
  211. bool IsIdentifierStart(char c) {
  212. return isalpha(static_cast<unsigned char>(c)) || c == '_';
  213. }
  214. CheckedError Parser::Next() {
  215. doc_comment_.clear();
  216. bool seen_newline = false;
  217. attribute_.clear();
  218. for (;;) {
  219. char c = *cursor_++;
  220. token_ = c;
  221. switch (c) {
  222. case '\0': cursor_--; token_ = kTokenEof; return NoError();
  223. case ' ': case '\r': case '\t': break;
  224. case '\n': line_++; seen_newline = true; break;
  225. case '{': case '}': case '(': case ')': case '[': case ']':
  226. case ',': case ':': case ';': case '=': return NoError();
  227. case '.':
  228. if(!isdigit(static_cast<const unsigned char>(*cursor_))) return NoError();
  229. return Error("floating point constant can\'t start with \".\"");
  230. case '\"':
  231. case '\'': {
  232. int unicode_high_surrogate = -1;
  233. while (*cursor_ != c) {
  234. if (*cursor_ < ' ' && *cursor_ >= 0)
  235. return Error("illegal character in string constant");
  236. if (*cursor_ == '\\') {
  237. cursor_++;
  238. if (unicode_high_surrogate != -1 &&
  239. *cursor_ != 'u') {
  240. return Error(
  241. "illegal Unicode sequence (unpaired high surrogate)");
  242. }
  243. switch (*cursor_) {
  244. case 'n': attribute_ += '\n'; cursor_++; break;
  245. case 't': attribute_ += '\t'; cursor_++; break;
  246. case 'r': attribute_ += '\r'; cursor_++; break;
  247. case 'b': attribute_ += '\b'; cursor_++; break;
  248. case 'f': attribute_ += '\f'; cursor_++; break;
  249. case '\"': attribute_ += '\"'; cursor_++; break;
  250. case '\'': attribute_ += '\''; cursor_++; break;
  251. case '\\': attribute_ += '\\'; cursor_++; break;
  252. case '/': attribute_ += '/'; cursor_++; break;
  253. case 'x': { // Not in the JSON standard
  254. cursor_++;
  255. int64_t val;
  256. ECHECK(ParseHexNum(2, &val));
  257. attribute_ += static_cast<char>(val);
  258. break;
  259. }
  260. case 'u': {
  261. cursor_++;
  262. int64_t val;
  263. ECHECK(ParseHexNum(4, &val));
  264. if (val >= 0xD800 && val <= 0xDBFF) {
  265. if (unicode_high_surrogate != -1) {
  266. return Error(
  267. "illegal Unicode sequence (multiple high surrogates)");
  268. } else {
  269. unicode_high_surrogate = static_cast<int>(val);
  270. }
  271. } else if (val >= 0xDC00 && val <= 0xDFFF) {
  272. if (unicode_high_surrogate == -1) {
  273. return Error(
  274. "illegal Unicode sequence (unpaired low surrogate)");
  275. } else {
  276. int code_point = 0x10000 +
  277. ((unicode_high_surrogate & 0x03FF) << 10) +
  278. (val & 0x03FF);
  279. ToUTF8(code_point, &attribute_);
  280. unicode_high_surrogate = -1;
  281. }
  282. } else {
  283. if (unicode_high_surrogate != -1) {
  284. return Error(
  285. "illegal Unicode sequence (unpaired high surrogate)");
  286. }
  287. ToUTF8(static_cast<int>(val), &attribute_);
  288. }
  289. break;
  290. }
  291. default: return Error("unknown escape code in string constant");
  292. }
  293. } else { // printable chars + UTF-8 bytes
  294. if (unicode_high_surrogate != -1) {
  295. return Error(
  296. "illegal Unicode sequence (unpaired high surrogate)");
  297. }
  298. attribute_ += *cursor_++;
  299. }
  300. }
  301. if (unicode_high_surrogate != -1) {
  302. return Error(
  303. "illegal Unicode sequence (unpaired high surrogate)");
  304. }
  305. cursor_++;
  306. if (!opts.allow_non_utf8 && !ValidateUTF8(attribute_)) {
  307. return Error("illegal UTF-8 sequence");
  308. }
  309. token_ = kTokenStringConstant;
  310. return NoError();
  311. }
  312. case '/':
  313. if (*cursor_ == '/') {
  314. const char *start = ++cursor_;
  315. while (*cursor_ && *cursor_ != '\n' && *cursor_ != '\r') cursor_++;
  316. if (*start == '/') { // documentation comment
  317. if (cursor_ != source_ && !seen_newline)
  318. return Error(
  319. "a documentation comment should be on a line on its own");
  320. doc_comment_.push_back(std::string(start + 1, cursor_));
  321. }
  322. break;
  323. } else if (*cursor_ == '*') {
  324. cursor_++;
  325. // TODO: make nested.
  326. while (*cursor_ != '*' || cursor_[1] != '/') {
  327. if (*cursor_ == '\n') line_++;
  328. if (!*cursor_) return Error("end of file in comment");
  329. cursor_++;
  330. }
  331. cursor_ += 2;
  332. break;
  333. }
  334. // fall thru
  335. default:
  336. if (IsIdentifierStart(c)) {
  337. // Collect all chars of an identifier:
  338. const char *start = cursor_ - 1;
  339. while (isalnum(static_cast<unsigned char>(*cursor_)) ||
  340. *cursor_ == '_')
  341. cursor_++;
  342. attribute_.append(start, cursor_);
  343. // First, see if it is a type keyword from the table of types:
  344. #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, \
  345. PTYPE) \
  346. if (attribute_ == IDLTYPE) { \
  347. token_ = kToken ## ENUM; \
  348. return NoError(); \
  349. }
  350. FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
  351. #undef FLATBUFFERS_TD
  352. // If it's a boolean constant keyword, turn those into integers,
  353. // which simplifies our logic downstream.
  354. if (attribute_ == "true" || attribute_ == "false") {
  355. attribute_ = NumToString(attribute_ == "true");
  356. token_ = kTokenIntegerConstant;
  357. return NoError();
  358. }
  359. // Check for declaration keywords:
  360. if (attribute_ == "table") {
  361. token_ = kTokenTable;
  362. return NoError();
  363. }
  364. if (attribute_ == "struct") {
  365. token_ = kTokenStruct;
  366. return NoError();
  367. }
  368. if (attribute_ == "enum") {
  369. token_ = kTokenEnum;
  370. return NoError();
  371. }
  372. if (attribute_ == "union") {
  373. token_ = kTokenUnion;
  374. return NoError();
  375. }
  376. if (attribute_ == "namespace") {
  377. token_ = kTokenNameSpace;
  378. return NoError();
  379. }
  380. if (attribute_ == "root_type") {
  381. token_ = kTokenRootType;
  382. return NoError();
  383. }
  384. if (attribute_ == "include") {
  385. token_ = kTokenInclude;
  386. return NoError();
  387. }
  388. if (attribute_ == "attribute") {
  389. token_ = kTokenAttribute;
  390. return NoError();
  391. }
  392. if (attribute_ == "file_identifier") {
  393. token_ = kTokenFileIdentifier;
  394. return NoError();
  395. }
  396. if (attribute_ == "file_extension") {
  397. token_ = kTokenFileExtension;
  398. return NoError();
  399. }
  400. if (attribute_ == "null") {
  401. token_ = kTokenNull;
  402. return NoError();
  403. }
  404. if (attribute_ == "rpc_service") {
  405. token_ = kTokenService;
  406. return NoError();
  407. }
  408. // If not, it is a user-defined identifier:
  409. token_ = kTokenIdentifier;
  410. return NoError();
  411. } else if (isdigit(static_cast<unsigned char>(c)) || c == '-') {
  412. const char *start = cursor_ - 1;
  413. if (c == '-' && *cursor_ == '0' && (cursor_[1] == 'x' || cursor_[1] == 'X')) {
  414. ++start;
  415. ++cursor_;
  416. attribute_.append(&c, &c + 1);
  417. c = '0';
  418. }
  419. if (c == '0' && (*cursor_ == 'x' || *cursor_ == 'X')) {
  420. cursor_++;
  421. while (isxdigit(static_cast<unsigned char>(*cursor_))) cursor_++;
  422. attribute_.append(start + 2, cursor_);
  423. attribute_ = NumToString(StringToUInt(attribute_.c_str(), nullptr, 16));
  424. token_ = kTokenIntegerConstant;
  425. return NoError();
  426. }
  427. while (isdigit(static_cast<unsigned char>(*cursor_))) cursor_++;
  428. if (*cursor_ == '.' || *cursor_ == 'e' || *cursor_ == 'E') {
  429. if (*cursor_ == '.') {
  430. cursor_++;
  431. while (isdigit(static_cast<unsigned char>(*cursor_))) cursor_++;
  432. }
  433. // See if this float has a scientific notation suffix. Both JSON
  434. // and C++ (through strtod() we use) have the same format:
  435. if (*cursor_ == 'e' || *cursor_ == 'E') {
  436. cursor_++;
  437. if (*cursor_ == '+' || *cursor_ == '-') cursor_++;
  438. while (isdigit(static_cast<unsigned char>(*cursor_))) cursor_++;
  439. }
  440. token_ = kTokenFloatConstant;
  441. } else {
  442. token_ = kTokenIntegerConstant;
  443. }
  444. attribute_.append(start, cursor_);
  445. return NoError();
  446. }
  447. std::string ch;
  448. ch = c;
  449. if (c < ' ' || c > '~') ch = "code: " + NumToString(c);
  450. return Error("illegal character: " + ch);
  451. }
  452. }
  453. }
  454. // Check if a given token is next.
  455. bool Parser::Is(int t) {
  456. return t == token_;
  457. }
  458. // Expect a given token to be next, consume it, or error if not present.
  459. CheckedError Parser::Expect(int t) {
  460. if (t != token_) {
  461. return Error("expecting: " + TokenToString(t) + " instead got: " +
  462. TokenToStringId(token_));
  463. }
  464. NEXT();
  465. return NoError();
  466. }
  467. CheckedError Parser::ParseNamespacing(std::string *id, std::string *last) {
  468. while (Is('.')) {
  469. NEXT();
  470. *id += ".";
  471. *id += attribute_;
  472. if (last) *last = attribute_;
  473. EXPECT(kTokenIdentifier);
  474. }
  475. return NoError();
  476. }
  477. EnumDef *Parser::LookupEnum(const std::string &id) {
  478. // Search thru parent namespaces.
  479. for (int components = static_cast<int>(namespaces_.back()->components.size());
  480. components >= 0; components--) {
  481. auto ed = enums_.Lookup(
  482. namespaces_.back()->GetFullyQualifiedName(id, components));
  483. if (ed) return ed;
  484. }
  485. return nullptr;
  486. }
  487. CheckedError Parser::ParseTypeIdent(Type &type) {
  488. std::string id = attribute_;
  489. EXPECT(kTokenIdentifier);
  490. ECHECK(ParseNamespacing(&id, nullptr));
  491. auto enum_def = LookupEnum(id);
  492. if (enum_def) {
  493. type = enum_def->underlying_type;
  494. if (enum_def->is_union) type.base_type = BASE_TYPE_UNION;
  495. } else {
  496. type.base_type = BASE_TYPE_STRUCT;
  497. type.struct_def = LookupCreateStruct(id);
  498. }
  499. return NoError();
  500. }
  501. // Parse any IDL type.
  502. CheckedError Parser::ParseType(Type &type) {
  503. if (token_ >= kTokenBOOL && token_ <= kTokenSTRING) {
  504. type.base_type = static_cast<BaseType>(token_ - kTokenNONE);
  505. NEXT();
  506. } else {
  507. if (token_ == kTokenIdentifier) {
  508. ECHECK(ParseTypeIdent(type));
  509. } else if (token_ == '[') {
  510. NEXT();
  511. Type subtype;
  512. ECHECK(ParseType(subtype));
  513. if (subtype.base_type == BASE_TYPE_VECTOR) {
  514. // We could support this, but it will complicate things, and it's
  515. // easier to work around with a struct around the inner vector.
  516. return Error(
  517. "nested vector types not supported (wrap in table first).");
  518. }
  519. if (subtype.base_type == BASE_TYPE_UNION) {
  520. // We could support this if we stored a struct of 2 elements per
  521. // union element.
  522. return Error(
  523. "vector of union types not supported (wrap in table first).");
  524. }
  525. type = Type(BASE_TYPE_VECTOR, subtype.struct_def, subtype.enum_def);
  526. type.element = subtype.base_type;
  527. EXPECT(']');
  528. } else {
  529. return Error("illegal type syntax");
  530. }
  531. }
  532. return NoError();
  533. }
  534. CheckedError Parser::AddField(StructDef &struct_def, const std::string &name,
  535. const Type &type, FieldDef **dest) {
  536. auto &field = *new FieldDef();
  537. field.value.offset =
  538. FieldIndexToOffset(static_cast<voffset_t>(struct_def.fields.vec.size()));
  539. field.name = name;
  540. field.file = struct_def.file;
  541. field.value.type = type;
  542. if (struct_def.fixed) { // statically compute the field offset
  543. auto size = InlineSize(type);
  544. auto alignment = InlineAlignment(type);
  545. // structs_ need to have a predictable format, so we need to align to
  546. // the largest scalar
  547. struct_def.minalign = std::max(struct_def.minalign, alignment);
  548. struct_def.PadLastField(alignment);
  549. field.value.offset = static_cast<voffset_t>(struct_def.bytesize);
  550. struct_def.bytesize += size;
  551. }
  552. if (struct_def.fields.Add(name, &field))
  553. return Error("field already exists: " + name);
  554. *dest = &field;
  555. return NoError();
  556. }
  557. CheckedError Parser::ParseField(StructDef &struct_def) {
  558. std::string name = attribute_;
  559. std::vector<std::string> dc = doc_comment_;
  560. EXPECT(kTokenIdentifier);
  561. EXPECT(':');
  562. Type type;
  563. ECHECK(ParseType(type));
  564. if (struct_def.fixed && !IsScalar(type.base_type) && !IsStruct(type))
  565. return Error("structs_ may contain only scalar or struct fields");
  566. FieldDef *typefield = nullptr;
  567. if (type.base_type == BASE_TYPE_UNION) {
  568. // For union fields, add a second auto-generated field to hold the type,
  569. // with a special suffix.
  570. ECHECK(AddField(struct_def, name + UnionTypeFieldSuffix(),
  571. type.enum_def->underlying_type, &typefield));
  572. }
  573. FieldDef *field;
  574. ECHECK(AddField(struct_def, name, type, &field));
  575. if (token_ == '=') {
  576. NEXT();
  577. if (!IsScalar(type.base_type))
  578. return Error("default values currently only supported for scalars");
  579. ECHECK(ParseSingleValue(field->value));
  580. }
  581. if (IsFloat(field->value.type.base_type)) {
  582. if (!strpbrk(field->value.constant.c_str(), ".eE"))
  583. field->value.constant += ".0";
  584. }
  585. if (type.enum_def &&
  586. IsScalar(type.base_type) &&
  587. !struct_def.fixed &&
  588. !type.enum_def->attributes.Lookup("bit_flags") &&
  589. !type.enum_def->ReverseLookup(static_cast<int>(
  590. StringToInt(field->value.constant.c_str()))))
  591. return Error("enum " + type.enum_def->name +
  592. " does not have a declaration for this field\'s default of " +
  593. field->value.constant);
  594. field->doc_comment = dc;
  595. ECHECK(ParseMetaData(&field->attributes));
  596. field->deprecated = field->attributes.Lookup("deprecated") != nullptr;
  597. auto hash_name = field->attributes.Lookup("hash");
  598. if (hash_name) {
  599. switch (type.base_type) {
  600. case BASE_TYPE_INT:
  601. case BASE_TYPE_UINT: {
  602. if (FindHashFunction32(hash_name->constant.c_str()) == nullptr)
  603. return Error("Unknown hashing algorithm for 32 bit types: " +
  604. hash_name->constant);
  605. break;
  606. }
  607. case BASE_TYPE_LONG:
  608. case BASE_TYPE_ULONG: {
  609. if (FindHashFunction64(hash_name->constant.c_str()) == nullptr)
  610. return Error("Unknown hashing algorithm for 64 bit types: " +
  611. hash_name->constant);
  612. break;
  613. }
  614. default:
  615. return Error(
  616. "only int, uint, long and ulong data types support hashing.");
  617. }
  618. }
  619. auto cpp_type = field->attributes.Lookup("cpp_type");
  620. if (cpp_type) {
  621. if (!hash_name)
  622. return Error("cpp_type can only be used with a hashed field");
  623. }
  624. if (field->deprecated && struct_def.fixed)
  625. return Error("can't deprecate fields in a struct");
  626. field->required = field->attributes.Lookup("required") != nullptr;
  627. if (field->required && (struct_def.fixed ||
  628. IsScalar(field->value.type.base_type)))
  629. return Error("only non-scalar fields in tables may be 'required'");
  630. field->key = field->attributes.Lookup("key") != nullptr;
  631. if (field->key) {
  632. if (struct_def.has_key)
  633. return Error("only one field may be set as 'key'");
  634. struct_def.has_key = true;
  635. if (!IsScalar(field->value.type.base_type)) {
  636. field->required = true;
  637. if (field->value.type.base_type != BASE_TYPE_STRING)
  638. return Error("'key' field must be string or scalar type");
  639. }
  640. }
  641. field->native_inline = field->attributes.Lookup("native_inline") != nullptr;
  642. if (field->native_inline && !IsStruct(field->value.type))
  643. return Error("native_inline can only be defined on structs'");
  644. auto nested = field->attributes.Lookup("nested_flatbuffer");
  645. if (nested) {
  646. if (nested->type.base_type != BASE_TYPE_STRING)
  647. return Error(
  648. "nested_flatbuffer attribute must be a string (the root type)");
  649. if (field->value.type.base_type != BASE_TYPE_VECTOR ||
  650. field->value.type.element != BASE_TYPE_UCHAR)
  651. return Error(
  652. "nested_flatbuffer attribute may only apply to a vector of ubyte");
  653. // This will cause an error if the root type of the nested flatbuffer
  654. // wasn't defined elsewhere.
  655. LookupCreateStruct(nested->constant);
  656. }
  657. if (typefield) {
  658. // If this field is a union, and it has a manually assigned id,
  659. // the automatically added type field should have an id as well (of N - 1).
  660. auto attr = field->attributes.Lookup("id");
  661. if (attr) {
  662. auto id = atoi(attr->constant.c_str());
  663. auto val = new Value();
  664. val->type = attr->type;
  665. val->constant = NumToString(id - 1);
  666. typefield->attributes.Add("id", val);
  667. }
  668. }
  669. EXPECT(';');
  670. return NoError();
  671. }
  672. CheckedError Parser::ParseAnyValue(Value &val, FieldDef *field,
  673. size_t parent_fieldn,
  674. const StructDef *parent_struct_def) {
  675. switch (val.type.base_type) {
  676. case BASE_TYPE_UNION: {
  677. assert(field);
  678. std::string constant;
  679. if (!parent_fieldn ||
  680. field_stack_.back().second->value.type.base_type != BASE_TYPE_UTYPE) {
  681. // We haven't seen the type field yet. Sadly a lot of JSON writers
  682. // output these in alphabetical order, meaning it comes after this
  683. // value. So we scan past the value to find it, then come back here.
  684. auto type_name = field->name + UnionTypeFieldSuffix();
  685. assert(parent_struct_def);
  686. auto type_field = parent_struct_def->fields.Lookup(type_name);
  687. assert(type_field); // Guaranteed by ParseField().
  688. // Remember where we are in the source file, so we can come back here.
  689. auto backup = *static_cast<ParserState *>(this);
  690. ECHECK(SkipAnyJsonValue()); // The table.
  691. EXPECT(',');
  692. auto next_name = attribute_;
  693. if (Is(kTokenStringConstant)) {
  694. NEXT();
  695. } else {
  696. EXPECT(kTokenIdentifier);
  697. }
  698. if (next_name != type_name)
  699. return Error("missing type field after this union value: " +
  700. type_name);
  701. EXPECT(':');
  702. Value type_val = type_field->value;
  703. ECHECK(ParseAnyValue(type_val, type_field, 0, nullptr));
  704. constant = type_val.constant;
  705. // Got the information we needed, now rewind:
  706. *static_cast<ParserState *>(this) = backup;
  707. } else {
  708. constant = field_stack_.back().first.constant;
  709. }
  710. uint8_t enum_idx;
  711. ECHECK(atot(constant.c_str(), *this, &enum_idx));
  712. auto enum_val = val.type.enum_def->ReverseLookup(enum_idx);
  713. if (!enum_val) return Error("illegal type id for: " + field->name);
  714. ECHECK(ParseTable(*enum_val->struct_def, &val.constant, nullptr));
  715. break;
  716. }
  717. case BASE_TYPE_STRUCT:
  718. ECHECK(ParseTable(*val.type.struct_def, &val.constant, nullptr));
  719. break;
  720. case BASE_TYPE_STRING: {
  721. auto s = attribute_;
  722. EXPECT(kTokenStringConstant);
  723. val.constant = NumToString(builder_.CreateString(s).o);
  724. break;
  725. }
  726. case BASE_TYPE_VECTOR: {
  727. EXPECT('[');
  728. uoffset_t off;
  729. ECHECK(ParseVector(val.type.VectorType(), &off));
  730. val.constant = NumToString(off);
  731. break;
  732. }
  733. case BASE_TYPE_INT:
  734. case BASE_TYPE_UINT:
  735. case BASE_TYPE_LONG:
  736. case BASE_TYPE_ULONG: {
  737. if (field && field->attributes.Lookup("hash") &&
  738. (token_ == kTokenIdentifier || token_ == kTokenStringConstant)) {
  739. ECHECK(ParseHash(val, field));
  740. } else {
  741. ECHECK(ParseSingleValue(val));
  742. }
  743. break;
  744. }
  745. default:
  746. ECHECK(ParseSingleValue(val));
  747. break;
  748. }
  749. return NoError();
  750. }
  751. void Parser::SerializeStruct(const StructDef &struct_def, const Value &val) {
  752. assert(val.constant.length() == struct_def.bytesize);
  753. builder_.Align(struct_def.minalign);
  754. builder_.PushBytes(reinterpret_cast<const uint8_t *>(val.constant.c_str()),
  755. struct_def.bytesize);
  756. builder_.AddStructOffset(val.offset, builder_.GetSize());
  757. }
  758. CheckedError Parser::ParseTable(const StructDef &struct_def, std::string *value,
  759. uoffset_t *ovalue) {
  760. EXPECT('{');
  761. size_t fieldn = 0;
  762. for (;;) {
  763. if ((!opts.strict_json || !fieldn) && Is('}')) { NEXT(); break; }
  764. std::string name = attribute_;
  765. if (Is(kTokenStringConstant)) {
  766. NEXT();
  767. } else {
  768. EXPECT(opts.strict_json ? kTokenStringConstant : kTokenIdentifier);
  769. }
  770. auto field = struct_def.fields.Lookup(name);
  771. if (!field) {
  772. if (!opts.skip_unexpected_fields_in_json) {
  773. return Error("unknown field: " + name);
  774. } else {
  775. EXPECT(':');
  776. ECHECK(SkipAnyJsonValue());
  777. }
  778. } else {
  779. EXPECT(':');
  780. if (Is(kTokenNull)) {
  781. NEXT(); // Ignore this field.
  782. } else {
  783. Value val = field->value;
  784. ECHECK(ParseAnyValue(val, field, fieldn, &struct_def));
  785. size_t i = field_stack_.size();
  786. // Hardcoded insertion-sort with error-check.
  787. // If fields are specified in order, then this loop exits immediately.
  788. for (; i > field_stack_.size() - fieldn; i--) {
  789. auto existing_field = field_stack_[i - 1].second;
  790. if (existing_field == field)
  791. return Error("field set more than once: " + field->name);
  792. if (existing_field->value.offset < field->value.offset) break;
  793. }
  794. field_stack_.insert(field_stack_.begin() + i, std::make_pair(val, field));
  795. fieldn++;
  796. }
  797. }
  798. if (Is('}')) { NEXT(); break; }
  799. EXPECT(',');
  800. }
  801. if (struct_def.fixed && fieldn != struct_def.fields.vec.size())
  802. return Error("struct: wrong number of initializers: " + struct_def.name);
  803. auto start = struct_def.fixed
  804. ? builder_.StartStruct(struct_def.minalign)
  805. : builder_.StartTable();
  806. for (size_t size = struct_def.sortbysize ? sizeof(largest_scalar_t) : 1;
  807. size;
  808. size /= 2) {
  809. // Go through elements in reverse, since we're building the data backwards.
  810. for (auto it = field_stack_.rbegin();
  811. it != field_stack_.rbegin() + fieldn; ++it) {
  812. auto &field_value = it->first;
  813. auto field = it->second;
  814. if (!struct_def.sortbysize ||
  815. size == SizeOf(field_value.type.base_type)) {
  816. switch (field_value.type.base_type) {
  817. #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, \
  818. PTYPE) \
  819. case BASE_TYPE_ ## ENUM: \
  820. builder_.Pad(field->padding); \
  821. if (struct_def.fixed) { \
  822. CTYPE val; \
  823. ECHECK(atot(field_value.constant.c_str(), *this, &val)); \
  824. builder_.PushElement(val); \
  825. } else { \
  826. CTYPE val, valdef; \
  827. ECHECK(atot(field_value.constant.c_str(), *this, &val)); \
  828. ECHECK(atot(field->value.constant.c_str(), *this, &valdef)); \
  829. builder_.AddElement(field_value.offset, val, valdef); \
  830. } \
  831. break;
  832. FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD);
  833. #undef FLATBUFFERS_TD
  834. #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, \
  835. PTYPE) \
  836. case BASE_TYPE_ ## ENUM: \
  837. builder_.Pad(field->padding); \
  838. if (IsStruct(field->value.type)) { \
  839. SerializeStruct(*field->value.type.struct_def, field_value); \
  840. } else { \
  841. CTYPE val; \
  842. ECHECK(atot(field_value.constant.c_str(), *this, &val)); \
  843. builder_.AddOffset(field_value.offset, val); \
  844. } \
  845. break;
  846. FLATBUFFERS_GEN_TYPES_POINTER(FLATBUFFERS_TD);
  847. #undef FLATBUFFERS_TD
  848. }
  849. }
  850. }
  851. }
  852. for (size_t i = 0; i < fieldn; i++) field_stack_.pop_back();
  853. if (struct_def.fixed) {
  854. builder_.ClearOffsets();
  855. builder_.EndStruct();
  856. assert(value);
  857. // Temporarily store this struct in the value string, since it is to
  858. // be serialized in-place elsewhere.
  859. value->assign(
  860. reinterpret_cast<const char *>(builder_.GetCurrentBufferPointer()),
  861. struct_def.bytesize);
  862. builder_.PopBytes(struct_def.bytesize);
  863. assert(!ovalue);
  864. } else {
  865. auto val = builder_.EndTable(start,
  866. static_cast<voffset_t>(struct_def.fields.vec.size()));
  867. if (ovalue) *ovalue = val;
  868. if (value) *value = NumToString(val);
  869. }
  870. return NoError();
  871. }
  872. CheckedError Parser::ParseVector(const Type &type, uoffset_t *ovalue) {
  873. int count = 0;
  874. for (;;) {
  875. if ((!opts.strict_json || !count) && Is(']')) { NEXT(); break; }
  876. Value val;
  877. val.type = type;
  878. ECHECK(ParseAnyValue(val, nullptr, 0, nullptr));
  879. field_stack_.push_back(std::make_pair(val, nullptr));
  880. count++;
  881. if (Is(']')) { NEXT(); break; }
  882. EXPECT(',');
  883. }
  884. builder_.StartVector(count * InlineSize(type) / InlineAlignment(type),
  885. InlineAlignment(type));
  886. for (int i = 0; i < count; i++) {
  887. // start at the back, since we're building the data backwards.
  888. auto &val = field_stack_.back().first;
  889. switch (val.type.base_type) {
  890. #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
  891. case BASE_TYPE_ ## ENUM: \
  892. if (IsStruct(val.type)) SerializeStruct(*val.type.struct_def, val); \
  893. else { \
  894. CTYPE elem; \
  895. ECHECK(atot(val.constant.c_str(), *this, &elem)); \
  896. builder_.PushElement(elem); \
  897. } \
  898. break;
  899. FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
  900. #undef FLATBUFFERS_TD
  901. }
  902. field_stack_.pop_back();
  903. }
  904. builder_.ClearOffsets();
  905. *ovalue = builder_.EndVector(count);
  906. return NoError();
  907. }
  908. CheckedError Parser::ParseMetaData(SymbolTable<Value> *attributes) {
  909. if (Is('(')) {
  910. NEXT();
  911. for (;;) {
  912. auto name = attribute_;
  913. EXPECT(kTokenIdentifier);
  914. if (known_attributes_.find(name) == known_attributes_.end())
  915. return Error("user define attributes must be declared before use: " +
  916. name);
  917. auto e = new Value();
  918. attributes->Add(name, e);
  919. if (Is(':')) {
  920. NEXT();
  921. ECHECK(ParseSingleValue(*e));
  922. }
  923. if (Is(')')) { NEXT(); break; }
  924. EXPECT(',');
  925. }
  926. }
  927. return NoError();
  928. }
  929. CheckedError Parser::TryTypedValue(int dtoken, bool check, Value &e,
  930. BaseType req, bool *destmatch) {
  931. bool match = dtoken == token_;
  932. if (match) {
  933. *destmatch = true;
  934. e.constant = attribute_;
  935. if (!check) {
  936. if (e.type.base_type == BASE_TYPE_NONE) {
  937. e.type.base_type = req;
  938. } else {
  939. return Error(std::string("type mismatch: expecting: ") +
  940. kTypeNames[e.type.base_type] +
  941. ", found: " +
  942. kTypeNames[req]);
  943. }
  944. }
  945. NEXT();
  946. }
  947. return NoError();
  948. }
  949. CheckedError Parser::ParseEnumFromString(Type &type, int64_t *result) {
  950. *result = 0;
  951. // Parse one or more enum identifiers, separated by spaces.
  952. const char *next = attribute_.c_str();
  953. do {
  954. const char *divider = strchr(next, ' ');
  955. std::string word;
  956. if (divider) {
  957. word = std::string(next, divider);
  958. next = divider + strspn(divider, " ");
  959. } else {
  960. word = next;
  961. next += word.length();
  962. }
  963. if (type.enum_def) { // The field has an enum type
  964. auto enum_val = type.enum_def->vals.Lookup(word);
  965. if (!enum_val)
  966. return Error("unknown enum value: " + word +
  967. ", for enum: " + type.enum_def->name);
  968. *result |= enum_val->value;
  969. } else { // No enum type, probably integral field.
  970. if (!IsInteger(type.base_type))
  971. return Error("not a valid value for this field: " + word);
  972. // TODO: could check if its a valid number constant here.
  973. const char *dot = strrchr(word.c_str(), '.');
  974. if (!dot)
  975. return Error("enum values need to be qualified by an enum type");
  976. std::string enum_def_str(word.c_str(), dot);
  977. std::string enum_val_str(dot + 1, word.c_str() + word.length());
  978. auto enum_def = LookupEnum(enum_def_str);
  979. if (!enum_def) return Error("unknown enum: " + enum_def_str);
  980. auto enum_val = enum_def->vals.Lookup(enum_val_str);
  981. if (!enum_val) return Error("unknown enum value: " + enum_val_str);
  982. *result |= enum_val->value;
  983. }
  984. } while(*next);
  985. return NoError();
  986. }
  987. CheckedError Parser::ParseHash(Value &e, FieldDef* field) {
  988. assert(field);
  989. Value *hash_name = field->attributes.Lookup("hash");
  990. switch (e.type.base_type) {
  991. case BASE_TYPE_INT:
  992. case BASE_TYPE_UINT: {
  993. auto hash = FindHashFunction32(hash_name->constant.c_str());
  994. uint32_t hashed_value = hash(attribute_.c_str());
  995. e.constant = NumToString(hashed_value);
  996. break;
  997. }
  998. case BASE_TYPE_LONG:
  999. case BASE_TYPE_ULONG: {
  1000. auto hash = FindHashFunction64(hash_name->constant.c_str());
  1001. uint64_t hashed_value = hash(attribute_.c_str());
  1002. e.constant = NumToString(hashed_value);
  1003. break;
  1004. }
  1005. default:
  1006. assert(0);
  1007. }
  1008. NEXT();
  1009. return NoError();
  1010. }
  1011. CheckedError Parser::ParseSingleValue(Value &e) {
  1012. // First see if this could be a conversion function:
  1013. if (token_ == kTokenIdentifier && *cursor_ == '(') {
  1014. auto functionname = attribute_;
  1015. NEXT();
  1016. EXPECT('(');
  1017. ECHECK(ParseSingleValue(e));
  1018. EXPECT(')');
  1019. #define FLATBUFFERS_FN_DOUBLE(name, op) \
  1020. if (functionname == name) { \
  1021. auto x = strtod(e.constant.c_str(), nullptr); \
  1022. e.constant = NumToString(op); \
  1023. }
  1024. FLATBUFFERS_FN_DOUBLE("deg", x / M_PI * 180);
  1025. FLATBUFFERS_FN_DOUBLE("rad", x * M_PI / 180);
  1026. FLATBUFFERS_FN_DOUBLE("sin", sin(x));
  1027. FLATBUFFERS_FN_DOUBLE("cos", cos(x));
  1028. FLATBUFFERS_FN_DOUBLE("tan", tan(x));
  1029. FLATBUFFERS_FN_DOUBLE("asin", asin(x));
  1030. FLATBUFFERS_FN_DOUBLE("acos", acos(x));
  1031. FLATBUFFERS_FN_DOUBLE("atan", atan(x));
  1032. // TODO(wvo): add more useful conversion functions here.
  1033. #undef FLATBUFFERS_FN_DOUBLE
  1034. // Then check if this could be a string/identifier enum value:
  1035. } else if (e.type.base_type != BASE_TYPE_STRING &&
  1036. e.type.base_type != BASE_TYPE_NONE &&
  1037. (token_ == kTokenIdentifier || token_ == kTokenStringConstant)) {
  1038. if (IsIdentifierStart(attribute_[0])) { // Enum value.
  1039. int64_t val;
  1040. ECHECK(ParseEnumFromString(e.type, &val));
  1041. e.constant = NumToString(val);
  1042. NEXT();
  1043. } else { // Numeric constant in string.
  1044. if (IsInteger(e.type.base_type)) {
  1045. char *end;
  1046. e.constant = NumToString(StringToInt(attribute_.c_str(), &end));
  1047. if (*end)
  1048. return Error("invalid integer: " + attribute_);
  1049. } else if (IsFloat(e.type.base_type)) {
  1050. char *end;
  1051. e.constant = NumToString(strtod(attribute_.c_str(), &end));
  1052. if (*end)
  1053. return Error("invalid float: " + attribute_);
  1054. } else {
  1055. assert(0); // Shouldn't happen, we covered all types.
  1056. e.constant = "0";
  1057. }
  1058. NEXT();
  1059. }
  1060. } else {
  1061. bool match = false;
  1062. ECHECK(TryTypedValue(kTokenIntegerConstant,
  1063. IsScalar(e.type.base_type),
  1064. e,
  1065. BASE_TYPE_INT,
  1066. &match));
  1067. ECHECK(TryTypedValue(kTokenFloatConstant,
  1068. IsFloat(e.type.base_type),
  1069. e,
  1070. BASE_TYPE_FLOAT,
  1071. &match));
  1072. ECHECK(TryTypedValue(kTokenStringConstant,
  1073. e.type.base_type == BASE_TYPE_STRING,
  1074. e,
  1075. BASE_TYPE_STRING,
  1076. &match));
  1077. if (!match)
  1078. return Error("cannot parse value starting with: " +
  1079. TokenToStringId(token_));
  1080. }
  1081. return NoError();
  1082. }
  1083. StructDef *Parser::LookupCreateStruct(const std::string &name,
  1084. bool create_if_new, bool definition) {
  1085. std::string qualified_name = namespaces_.back()->GetFullyQualifiedName(name);
  1086. // See if it exists pre-declared by an unqualified use.
  1087. auto struct_def = structs_.Lookup(name);
  1088. if (struct_def && struct_def->predecl) {
  1089. if (definition) {
  1090. // Make sure it has the current namespace, and is registered under its
  1091. // qualified name.
  1092. struct_def->defined_namespace = namespaces_.back();
  1093. structs_.Move(name, qualified_name);
  1094. }
  1095. return struct_def;
  1096. }
  1097. // See if it exists pre-declared by an qualified use.
  1098. struct_def = structs_.Lookup(qualified_name);
  1099. if (struct_def && struct_def->predecl) {
  1100. if (definition) {
  1101. // Make sure it has the current namespace.
  1102. struct_def->defined_namespace = namespaces_.back();
  1103. }
  1104. return struct_def;
  1105. }
  1106. if (!definition) {
  1107. // Search thru parent namespaces.
  1108. for (size_t components = namespaces_.back()->components.size();
  1109. components && !struct_def; components--) {
  1110. struct_def = structs_.Lookup(
  1111. namespaces_.back()->GetFullyQualifiedName(name, components - 1));
  1112. }
  1113. }
  1114. if (!struct_def && create_if_new) {
  1115. struct_def = new StructDef();
  1116. if (definition) {
  1117. structs_.Add(qualified_name, struct_def);
  1118. struct_def->name = name;
  1119. struct_def->defined_namespace = namespaces_.back();
  1120. } else {
  1121. // Not a definition.
  1122. // Rather than failing, we create a "pre declared" StructDef, due to
  1123. // circular references, and check for errors at the end of parsing.
  1124. // It is defined in the root namespace, since we don't know what the
  1125. // final namespace will be.
  1126. // TODO: maybe safer to use special namespace?
  1127. structs_.Add(name, struct_def);
  1128. struct_def->name = name;
  1129. struct_def->defined_namespace = new Namespace();
  1130. namespaces_.insert(namespaces_.begin(), struct_def->defined_namespace);
  1131. }
  1132. }
  1133. return struct_def;
  1134. }
  1135. CheckedError Parser::ParseEnum(bool is_union, EnumDef **dest) {
  1136. std::vector<std::string> enum_comment = doc_comment_;
  1137. NEXT();
  1138. std::string enum_name = attribute_;
  1139. EXPECT(kTokenIdentifier);
  1140. auto &enum_def = *new EnumDef();
  1141. enum_def.name = enum_name;
  1142. enum_def.file = file_being_parsed_;
  1143. enum_def.doc_comment = enum_comment;
  1144. enum_def.is_union = is_union;
  1145. enum_def.defined_namespace = namespaces_.back();
  1146. if (enums_.Add(namespaces_.back()->GetFullyQualifiedName(enum_name),
  1147. &enum_def))
  1148. return Error("enum already exists: " + enum_name);
  1149. if (is_union) {
  1150. enum_def.underlying_type.base_type = BASE_TYPE_UTYPE;
  1151. enum_def.underlying_type.enum_def = &enum_def;
  1152. } else {
  1153. if (opts.proto_mode) {
  1154. enum_def.underlying_type.base_type = BASE_TYPE_INT;
  1155. } else {
  1156. // Give specialized error message, since this type spec used to
  1157. // be optional in the first FlatBuffers release.
  1158. if (!Is(':')) {
  1159. return Error("must specify the underlying integer type for this"
  1160. " enum (e.g. \': short\', which was the default).");
  1161. } else {
  1162. NEXT();
  1163. }
  1164. // Specify the integer type underlying this enum.
  1165. ECHECK(ParseType(enum_def.underlying_type));
  1166. if (!IsInteger(enum_def.underlying_type.base_type))
  1167. return Error("underlying enum type must be integral");
  1168. }
  1169. // Make this type refer back to the enum it was derived from.
  1170. enum_def.underlying_type.enum_def = &enum_def;
  1171. }
  1172. ECHECK(ParseMetaData(&enum_def.attributes));
  1173. EXPECT('{');
  1174. if (is_union) enum_def.vals.Add("NONE", new EnumVal("NONE", 0));
  1175. for (;;) {
  1176. if (opts.proto_mode && attribute_ == "option") {
  1177. ECHECK(ParseProtoOption());
  1178. } else {
  1179. auto value_name = attribute_;
  1180. auto full_name = value_name;
  1181. std::vector<std::string> value_comment = doc_comment_;
  1182. EXPECT(kTokenIdentifier);
  1183. if (is_union) {
  1184. ECHECK(ParseNamespacing(&full_name, &value_name));
  1185. if (opts.union_value_namespacing) {
  1186. // Since we can't namespace the actual enum identifiers, turn
  1187. // namespace parts into part of the identifier.
  1188. value_name = full_name;
  1189. std::replace(value_name.begin(), value_name.end(), '.', '_');
  1190. }
  1191. }
  1192. auto prevsize = enum_def.vals.vec.size();
  1193. auto value = enum_def.vals.vec.size()
  1194. ? enum_def.vals.vec.back()->value + 1
  1195. : 0;
  1196. auto &ev = *new EnumVal(value_name, value);
  1197. if (enum_def.vals.Add(value_name, &ev))
  1198. return Error("enum value already exists: " + value_name);
  1199. ev.doc_comment = value_comment;
  1200. if (is_union) {
  1201. ev.struct_def = LookupCreateStruct(full_name);
  1202. }
  1203. if (Is('=')) {
  1204. NEXT();
  1205. ev.value = StringToInt(attribute_.c_str());
  1206. EXPECT(kTokenIntegerConstant);
  1207. if (!opts.proto_mode && prevsize &&
  1208. enum_def.vals.vec[prevsize - 1]->value >= ev.value)
  1209. return Error("enum values must be specified in ascending order");
  1210. }
  1211. if (opts.proto_mode && Is('[')) {
  1212. NEXT();
  1213. // ignore attributes on enums.
  1214. while (token_ != ']') NEXT();
  1215. NEXT();
  1216. }
  1217. }
  1218. if (!Is(opts.proto_mode ? ';' : ',')) break;
  1219. NEXT();
  1220. if (Is('}')) break;
  1221. }
  1222. EXPECT('}');
  1223. if (enum_def.attributes.Lookup("bit_flags")) {
  1224. for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
  1225. ++it) {
  1226. if (static_cast<size_t>((*it)->value) >=
  1227. SizeOf(enum_def.underlying_type.base_type) * 8)
  1228. return Error("bit flag out of range of underlying integral type");
  1229. (*it)->value = 1LL << (*it)->value;
  1230. }
  1231. }
  1232. if (dest) *dest = &enum_def;
  1233. return NoError();
  1234. }
  1235. CheckedError Parser::StartStruct(const std::string &name, StructDef **dest) {
  1236. auto &struct_def = *LookupCreateStruct(name, true, true);
  1237. if (!struct_def.predecl) return Error("datatype already exists: " + name);
  1238. struct_def.predecl = false;
  1239. struct_def.name = name;
  1240. struct_def.file = file_being_parsed_;
  1241. // Move this struct to the back of the vector just in case it was predeclared,
  1242. // to preserve declaration order.
  1243. *remove(structs_.vec.begin(), structs_.vec.end(), &struct_def) = &struct_def;
  1244. *dest = &struct_def;
  1245. return NoError();
  1246. }
  1247. CheckedError Parser::CheckClash(std::vector<FieldDef*> &fields,
  1248. StructDef *struct_def,
  1249. const char *suffix,
  1250. BaseType basetype) {
  1251. auto len = strlen(suffix);
  1252. for (auto it = fields.begin(); it != fields.end(); ++it) {
  1253. auto &fname = (*it)->name;
  1254. if (fname.length() > len &&
  1255. fname.compare(fname.length() - len, len, suffix) == 0 &&
  1256. (*it)->value.type.base_type != BASE_TYPE_UTYPE) {
  1257. auto field = struct_def->fields.Lookup(
  1258. fname.substr(0, fname.length() - len));
  1259. if (field && field->value.type.base_type == basetype)
  1260. return Error("Field " + fname +
  1261. " would clash with generated functions for field " +
  1262. field->name);
  1263. }
  1264. }
  1265. return NoError();
  1266. }
  1267. static bool compareFieldDefs(const FieldDef *a, const FieldDef *b) {
  1268. auto a_id = atoi(a->attributes.Lookup("id")->constant.c_str());
  1269. auto b_id = atoi(b->attributes.Lookup("id")->constant.c_str());
  1270. return a_id < b_id;
  1271. }
  1272. CheckedError Parser::ParseDecl() {
  1273. std::vector<std::string> dc = doc_comment_;
  1274. bool fixed = Is(kTokenStruct);
  1275. if (fixed) NEXT() else EXPECT(kTokenTable);
  1276. std::string name = attribute_;
  1277. EXPECT(kTokenIdentifier);
  1278. StructDef *struct_def;
  1279. ECHECK(StartStruct(name, &struct_def));
  1280. struct_def->doc_comment = dc;
  1281. struct_def->fixed = fixed;
  1282. ECHECK(ParseMetaData(&struct_def->attributes));
  1283. struct_def->sortbysize =
  1284. struct_def->attributes.Lookup("original_order") == nullptr && !fixed;
  1285. EXPECT('{');
  1286. while (token_ != '}') ECHECK(ParseField(*struct_def));
  1287. auto force_align = struct_def->attributes.Lookup("force_align");
  1288. if (fixed && force_align) {
  1289. auto align = static_cast<size_t>(atoi(force_align->constant.c_str()));
  1290. if (force_align->type.base_type != BASE_TYPE_INT ||
  1291. align < struct_def->minalign ||
  1292. align > FLATBUFFERS_MAX_ALIGNMENT ||
  1293. align & (align - 1))
  1294. return Error("force_align must be a power of two integer ranging from the"
  1295. "struct\'s natural alignment to " +
  1296. NumToString(FLATBUFFERS_MAX_ALIGNMENT));
  1297. struct_def->minalign = align;
  1298. }
  1299. struct_def->PadLastField(struct_def->minalign);
  1300. // Check if this is a table that has manual id assignments
  1301. auto &fields = struct_def->fields.vec;
  1302. if (!struct_def->fixed && fields.size()) {
  1303. size_t num_id_fields = 0;
  1304. for (auto it = fields.begin(); it != fields.end(); ++it) {
  1305. if ((*it)->attributes.Lookup("id")) num_id_fields++;
  1306. }
  1307. // If any fields have ids..
  1308. if (num_id_fields) {
  1309. // Then all fields must have them.
  1310. if (num_id_fields != fields.size())
  1311. return Error(
  1312. "either all fields or no fields must have an 'id' attribute");
  1313. // Simply sort by id, then the fields are the same as if no ids had
  1314. // been specified.
  1315. std::sort(fields.begin(), fields.end(), compareFieldDefs);
  1316. // Verify we have a contiguous set, and reassign vtable offsets.
  1317. for (int i = 0; i < static_cast<int>(fields.size()); i++) {
  1318. if (i != atoi(fields[i]->attributes.Lookup("id")->constant.c_str()))
  1319. return Error("field id\'s must be consecutive from 0, id " +
  1320. NumToString(i) + " missing or set twice");
  1321. fields[i]->value.offset = FieldIndexToOffset(static_cast<voffset_t>(i));
  1322. }
  1323. }
  1324. }
  1325. ECHECK(CheckClash(fields, struct_def, UnionTypeFieldSuffix(),
  1326. BASE_TYPE_UNION));
  1327. ECHECK(CheckClash(fields, struct_def, "Type", BASE_TYPE_UNION));
  1328. ECHECK(CheckClash(fields, struct_def, "_length", BASE_TYPE_VECTOR));
  1329. ECHECK(CheckClash(fields, struct_def, "Length", BASE_TYPE_VECTOR));
  1330. ECHECK(CheckClash(fields, struct_def, "_byte_vector", BASE_TYPE_STRING));
  1331. ECHECK(CheckClash(fields, struct_def, "ByteVector", BASE_TYPE_STRING));
  1332. EXPECT('}');
  1333. return NoError();
  1334. }
  1335. CheckedError Parser::ParseService() {
  1336. std::vector<std::string> service_comment = doc_comment_;
  1337. NEXT();
  1338. auto service_name = attribute_;
  1339. EXPECT(kTokenIdentifier);
  1340. auto &service_def = *new ServiceDef();
  1341. service_def.name = service_name;
  1342. service_def.file = file_being_parsed_;
  1343. service_def.doc_comment = service_comment;
  1344. service_def.defined_namespace = namespaces_.back();
  1345. if (services_.Add(namespaces_.back()->GetFullyQualifiedName(service_name),
  1346. &service_def))
  1347. return Error("service already exists: " + service_name);
  1348. ECHECK(ParseMetaData(&service_def.attributes));
  1349. EXPECT('{');
  1350. do {
  1351. auto rpc_name = attribute_;
  1352. EXPECT(kTokenIdentifier);
  1353. EXPECT('(');
  1354. Type reqtype, resptype;
  1355. ECHECK(ParseTypeIdent(reqtype));
  1356. EXPECT(')');
  1357. EXPECT(':');
  1358. ECHECK(ParseTypeIdent(resptype));
  1359. if (reqtype.base_type != BASE_TYPE_STRUCT || reqtype.struct_def->fixed ||
  1360. resptype.base_type != BASE_TYPE_STRUCT || resptype.struct_def->fixed)
  1361. return Error("rpc request and response types must be tables");
  1362. auto &rpc = *new RPCCall();
  1363. rpc.name = rpc_name;
  1364. rpc.request = reqtype.struct_def;
  1365. rpc.response = resptype.struct_def;
  1366. if (service_def.calls.Add(rpc_name, &rpc))
  1367. return Error("rpc already exists: " + rpc_name);
  1368. ECHECK(ParseMetaData(&rpc.attributes));
  1369. EXPECT(';');
  1370. } while (token_ != '}');
  1371. NEXT();
  1372. return NoError();
  1373. }
  1374. bool Parser::SetRootType(const char *name) {
  1375. root_struct_def_ = structs_.Lookup(name);
  1376. if (!root_struct_def_)
  1377. root_struct_def_ = structs_.Lookup(
  1378. namespaces_.back()->GetFullyQualifiedName(name));
  1379. return root_struct_def_ != nullptr;
  1380. }
  1381. void Parser::MarkGenerated() {
  1382. // This function marks all existing definitions as having already
  1383. // been generated, which signals no code for included files should be
  1384. // generated.
  1385. for (auto it = enums_.vec.begin();
  1386. it != enums_.vec.end(); ++it) {
  1387. (*it)->generated = true;
  1388. }
  1389. for (auto it = structs_.vec.begin();
  1390. it != structs_.vec.end(); ++it) {
  1391. (*it)->generated = true;
  1392. }
  1393. for (auto it = services_.vec.begin();
  1394. it != services_.vec.end(); ++it) {
  1395. (*it)->generated = true;
  1396. }
  1397. }
  1398. CheckedError Parser::ParseNamespace() {
  1399. NEXT();
  1400. auto ns = new Namespace();
  1401. namespaces_.push_back(ns);
  1402. if (token_ != ';') {
  1403. for (;;) {
  1404. ns->components.push_back(attribute_);
  1405. EXPECT(kTokenIdentifier);
  1406. if (Is('.')) NEXT() else break;
  1407. }
  1408. }
  1409. EXPECT(';');
  1410. return NoError();
  1411. }
  1412. static bool compareEnumVals(const EnumVal *a, const EnumVal* b) {
  1413. return a->value < b->value;
  1414. }
  1415. // Best effort parsing of .proto declarations, with the aim to turn them
  1416. // in the closest corresponding FlatBuffer equivalent.
  1417. // We parse everything as identifiers instead of keywords, since we don't
  1418. // want protobuf keywords to become invalid identifiers in FlatBuffers.
  1419. CheckedError Parser::ParseProtoDecl() {
  1420. bool isextend = attribute_ == "extend";
  1421. if (attribute_ == "package") {
  1422. // These are identical in syntax to FlatBuffer's namespace decl.
  1423. ECHECK(ParseNamespace());
  1424. } else if (attribute_ == "message" || isextend) {
  1425. std::vector<std::string> struct_comment = doc_comment_;
  1426. NEXT();
  1427. StructDef *struct_def = nullptr;
  1428. if (isextend) {
  1429. if (Is('.')) NEXT(); // qualified names may start with a . ?
  1430. auto id = attribute_;
  1431. EXPECT(kTokenIdentifier);
  1432. ECHECK(ParseNamespacing(&id, nullptr));
  1433. struct_def = LookupCreateStruct(id, false);
  1434. if (!struct_def)
  1435. return Error("cannot extend unknown message type: " + id);
  1436. } else {
  1437. std::string name = attribute_;
  1438. EXPECT(kTokenIdentifier);
  1439. ECHECK(StartStruct(name, &struct_def));
  1440. // Since message definitions can be nested, we create a new namespace.
  1441. auto ns = new Namespace();
  1442. // Copy of current namespace.
  1443. *ns = *namespaces_.back();
  1444. // But with current message name.
  1445. ns->components.push_back(name);
  1446. namespaces_.push_back(ns);
  1447. }
  1448. struct_def->doc_comment = struct_comment;
  1449. ECHECK(ParseProtoFields(struct_def, isextend, false));
  1450. if (!isextend) {
  1451. // We have to remove the nested namespace, but we can't just throw it
  1452. // away, so put it at the beginning of the vector.
  1453. auto ns = namespaces_.back();
  1454. namespaces_.pop_back();
  1455. namespaces_.insert(namespaces_.begin(), ns);
  1456. }
  1457. if (Is(';')) NEXT();
  1458. } else if (attribute_ == "enum") {
  1459. // These are almost the same, just with different terminator:
  1460. EnumDef *enum_def;
  1461. ECHECK(ParseEnum(false, &enum_def));
  1462. if (Is(';')) NEXT();
  1463. // Protobuf allows them to be specified in any order, so sort afterwards.
  1464. auto &v = enum_def->vals.vec;
  1465. std::sort(v.begin(), v.end(), compareEnumVals);
  1466. // Temp: remove any duplicates, as .fbs files can't handle them.
  1467. for (auto it = v.begin(); it != v.end(); ) {
  1468. if (it != v.begin() && it[0]->value == it[-1]->value) it = v.erase(it);
  1469. else ++it;
  1470. }
  1471. } else if (attribute_ == "syntax") { // Skip these.
  1472. NEXT();
  1473. EXPECT('=');
  1474. EXPECT(kTokenStringConstant);
  1475. EXPECT(';');
  1476. } else if (attribute_ == "option") { // Skip these.
  1477. ECHECK(ParseProtoOption());
  1478. EXPECT(';');
  1479. } else if (attribute_ == "service") { // Skip these.
  1480. NEXT();
  1481. EXPECT(kTokenIdentifier);
  1482. ECHECK(ParseProtoCurliesOrIdent());
  1483. } else {
  1484. return Error("don\'t know how to parse .proto declaration starting with " +
  1485. TokenToStringId(token_));
  1486. }
  1487. return NoError();
  1488. }
  1489. CheckedError Parser::ParseProtoFields(StructDef *struct_def, bool isextend,
  1490. bool inside_oneof) {
  1491. EXPECT('{');
  1492. while (token_ != '}') {
  1493. if (attribute_ == "message" || attribute_ == "extend" ||
  1494. attribute_ == "enum") {
  1495. // Nested declarations.
  1496. ECHECK(ParseProtoDecl());
  1497. } else if (attribute_ == "extensions") { // Skip these.
  1498. NEXT();
  1499. EXPECT(kTokenIntegerConstant);
  1500. if (Is(kTokenIdentifier)) {
  1501. NEXT(); // to
  1502. NEXT(); // num
  1503. }
  1504. EXPECT(';');
  1505. } else if (attribute_ == "option") { // Skip these.
  1506. ECHECK(ParseProtoOption());
  1507. EXPECT(';');
  1508. } else if (attribute_ == "reserved") { // Skip these.
  1509. NEXT();
  1510. EXPECT(kTokenIntegerConstant);
  1511. while (Is(',')) { NEXT(); EXPECT(kTokenIntegerConstant); }
  1512. EXPECT(';');
  1513. } else {
  1514. std::vector<std::string> field_comment = doc_comment_;
  1515. // Parse the qualifier.
  1516. bool required = false;
  1517. bool repeated = false;
  1518. bool oneof = false;
  1519. if (!inside_oneof) {
  1520. if (attribute_ == "optional") {
  1521. // This is the default.
  1522. EXPECT(kTokenIdentifier);
  1523. } else if (attribute_ == "required") {
  1524. required = true;
  1525. EXPECT(kTokenIdentifier);
  1526. } else if (attribute_ == "repeated") {
  1527. repeated = true;
  1528. EXPECT(kTokenIdentifier);
  1529. } else if (attribute_ == "oneof") {
  1530. oneof = true;
  1531. EXPECT(kTokenIdentifier);
  1532. } else {
  1533. // can't error, proto3 allows decls without any of the above.
  1534. }
  1535. }
  1536. StructDef *anonymous_struct = nullptr;
  1537. Type type;
  1538. if (attribute_ == "group" || oneof) {
  1539. if (!oneof) EXPECT(kTokenIdentifier);
  1540. auto name = "Anonymous" + NumToString(anonymous_counter++);
  1541. ECHECK(StartStruct(name, &anonymous_struct));
  1542. type = Type(BASE_TYPE_STRUCT, anonymous_struct);
  1543. } else {
  1544. ECHECK(ParseTypeFromProtoType(&type));
  1545. }
  1546. // Repeated elements get mapped to a vector.
  1547. if (repeated) {
  1548. type.element = type.base_type;
  1549. type.base_type = BASE_TYPE_VECTOR;
  1550. }
  1551. std::string name = attribute_;
  1552. // Protos may use our keywords "attribute" & "namespace" as an identifier.
  1553. if (Is(kTokenAttribute) || Is(kTokenNameSpace)) {
  1554. NEXT();
  1555. // TODO: simpler to just not make these keywords?
  1556. name += "_"; // Have to make it not a keyword.
  1557. } else {
  1558. EXPECT(kTokenIdentifier);
  1559. }
  1560. if (!oneof) {
  1561. // Parse the field id. Since we're just translating schemas, not
  1562. // any kind of binary compatibility, we can safely ignore these, and
  1563. // assign our own.
  1564. EXPECT('=');
  1565. EXPECT(kTokenIntegerConstant);
  1566. }
  1567. FieldDef *field = nullptr;
  1568. if (isextend) {
  1569. // We allow a field to be re-defined when extending.
  1570. // TODO: are there situations where that is problematic?
  1571. field = struct_def->fields.Lookup(name);
  1572. }
  1573. if (!field) ECHECK(AddField(*struct_def, name, type, &field));
  1574. field->doc_comment = field_comment;
  1575. if (!IsScalar(type.base_type)) field->required = required;
  1576. // See if there's a default specified.
  1577. if (Is('[')) {
  1578. NEXT();
  1579. for (;;) {
  1580. auto key = attribute_;
  1581. ECHECK(ParseProtoKey());
  1582. EXPECT('=');
  1583. auto val = attribute_;
  1584. ECHECK(ParseProtoCurliesOrIdent());
  1585. if (key == "default") {
  1586. // Temp: skip non-numeric defaults (enums).
  1587. auto numeric = strpbrk(val.c_str(), "0123456789-+.");
  1588. if (IsScalar(type.base_type) && numeric == val.c_str())
  1589. field->value.constant = val;
  1590. } else if (key == "deprecated") {
  1591. field->deprecated = val == "true";
  1592. }
  1593. if (!Is(',')) break;
  1594. NEXT();
  1595. }
  1596. EXPECT(']');
  1597. }
  1598. if (anonymous_struct) {
  1599. ECHECK(ParseProtoFields(anonymous_struct, false, oneof));
  1600. if (Is(';')) NEXT();
  1601. } else {
  1602. EXPECT(';');
  1603. }
  1604. }
  1605. }
  1606. NEXT();
  1607. return NoError();
  1608. }
  1609. CheckedError Parser::ParseProtoKey() {
  1610. if (token_ == '(') {
  1611. NEXT();
  1612. // Skip "(a.b)" style custom attributes.
  1613. while (token_ == '.' || token_ == kTokenIdentifier) NEXT();
  1614. EXPECT(')');
  1615. while (Is('.')) { NEXT(); EXPECT(kTokenIdentifier); }
  1616. } else {
  1617. EXPECT(kTokenIdentifier);
  1618. }
  1619. return NoError();
  1620. }
  1621. CheckedError Parser::ParseProtoCurliesOrIdent() {
  1622. if (Is('{')) {
  1623. NEXT();
  1624. for (int nesting = 1; nesting; ) {
  1625. if (token_ == '{') nesting++;
  1626. else if (token_ == '}') nesting--;
  1627. NEXT();
  1628. }
  1629. } else {
  1630. NEXT(); // Any single token.
  1631. }
  1632. return NoError();
  1633. }
  1634. CheckedError Parser::ParseProtoOption() {
  1635. NEXT();
  1636. ECHECK(ParseProtoKey());
  1637. EXPECT('=');
  1638. ECHECK(ParseProtoCurliesOrIdent());
  1639. return NoError();
  1640. }
  1641. // Parse a protobuf type, and map it to the corresponding FlatBuffer one.
  1642. CheckedError Parser::ParseTypeFromProtoType(Type *type) {
  1643. struct type_lookup { const char *proto_type; BaseType fb_type; };
  1644. static type_lookup lookup[] = {
  1645. { "float", BASE_TYPE_FLOAT }, { "double", BASE_TYPE_DOUBLE },
  1646. { "int32", BASE_TYPE_INT }, { "int64", BASE_TYPE_LONG },
  1647. { "uint32", BASE_TYPE_UINT }, { "uint64", BASE_TYPE_ULONG },
  1648. { "sint32", BASE_TYPE_INT }, { "sint64", BASE_TYPE_LONG },
  1649. { "fixed32", BASE_TYPE_UINT }, { "fixed64", BASE_TYPE_ULONG },
  1650. { "sfixed32", BASE_TYPE_INT }, { "sfixed64", BASE_TYPE_LONG },
  1651. { "bool", BASE_TYPE_BOOL },
  1652. { "string", BASE_TYPE_STRING },
  1653. { "bytes", BASE_TYPE_STRING },
  1654. { nullptr, BASE_TYPE_NONE }
  1655. };
  1656. for (auto tl = lookup; tl->proto_type; tl++) {
  1657. if (attribute_ == tl->proto_type) {
  1658. type->base_type = tl->fb_type;
  1659. NEXT();
  1660. return NoError();
  1661. }
  1662. }
  1663. if (Is('.')) NEXT(); // qualified names may start with a . ?
  1664. ECHECK(ParseTypeIdent(*type));
  1665. return NoError();
  1666. }
  1667. CheckedError Parser::SkipAnyJsonValue() {
  1668. switch (token_) {
  1669. case '{':
  1670. ECHECK(SkipJsonObject());
  1671. break;
  1672. case kTokenStringConstant:
  1673. ECHECK(SkipJsonString());
  1674. break;
  1675. case '[':
  1676. ECHECK(SkipJsonArray());
  1677. break;
  1678. case kTokenIntegerConstant:
  1679. EXPECT(kTokenIntegerConstant);
  1680. break;
  1681. case kTokenFloatConstant:
  1682. EXPECT(kTokenFloatConstant);
  1683. break;
  1684. default:
  1685. return Error(std::string("Unexpected token:") + std::string(1, static_cast<char>(token_)));
  1686. }
  1687. return NoError();
  1688. }
  1689. CheckedError Parser::SkipJsonObject() {
  1690. EXPECT('{');
  1691. size_t fieldn = 0;
  1692. for (;;) {
  1693. if ((!opts.strict_json || !fieldn) && Is('}')) break;
  1694. if (!Is(kTokenStringConstant)) {
  1695. EXPECT(opts.strict_json ? kTokenStringConstant : kTokenIdentifier);
  1696. }
  1697. else {
  1698. NEXT();
  1699. }
  1700. EXPECT(':');
  1701. ECHECK(SkipAnyJsonValue());
  1702. fieldn++;
  1703. if (Is('}')) break;
  1704. EXPECT(',');
  1705. }
  1706. NEXT();
  1707. return NoError();
  1708. }
  1709. CheckedError Parser::SkipJsonArray() {
  1710. EXPECT('[');
  1711. for (;;) {
  1712. if (Is(']')) break;
  1713. ECHECK(SkipAnyJsonValue());
  1714. if (Is(']')) break;
  1715. EXPECT(',');
  1716. }
  1717. NEXT();
  1718. return NoError();
  1719. }
  1720. CheckedError Parser::SkipJsonString() {
  1721. EXPECT(kTokenStringConstant);
  1722. return NoError();
  1723. }
  1724. bool Parser::Parse(const char *source, const char **include_paths,
  1725. const char *source_filename) {
  1726. return !DoParse(source, include_paths, source_filename).Check();
  1727. }
  1728. CheckedError Parser::DoParse(const char *source, const char **include_paths,
  1729. const char *source_filename) {
  1730. file_being_parsed_ = source_filename ? source_filename : "";
  1731. if (source_filename &&
  1732. included_files_.find(source_filename) == included_files_.end()) {
  1733. included_files_[source_filename] = true;
  1734. files_included_per_file_[source_filename] = std::set<std::string>();
  1735. }
  1736. if (!include_paths) {
  1737. static const char *current_directory[] = { "", nullptr };
  1738. include_paths = current_directory;
  1739. }
  1740. source_ = cursor_ = source;
  1741. line_ = 1;
  1742. error_.clear();
  1743. field_stack_.clear();
  1744. builder_.Clear();
  1745. // Start with a blank namespace just in case this file doesn't have one.
  1746. namespaces_.push_back(new Namespace());
  1747. ECHECK(SkipByteOrderMark());
  1748. NEXT();
  1749. // Includes must come before type declarations:
  1750. for (;;) {
  1751. // Parse pre-include proto statements if any:
  1752. if (opts.proto_mode &&
  1753. (attribute_ == "option" || attribute_ == "syntax" ||
  1754. attribute_ == "package")) {
  1755. ECHECK(ParseProtoDecl());
  1756. } else if (Is(kTokenInclude) ||
  1757. (opts.proto_mode &&
  1758. attribute_ == "import" &&
  1759. Is(kTokenIdentifier))) {
  1760. NEXT();
  1761. if (opts.proto_mode && attribute_ == "public") NEXT();
  1762. auto name = attribute_;
  1763. EXPECT(kTokenStringConstant);
  1764. // Look for the file in include_paths.
  1765. std::string filepath;
  1766. for (auto paths = include_paths; paths && *paths; paths++) {
  1767. filepath = flatbuffers::ConCatPathFileName(*paths, name);
  1768. if(FileExists(filepath.c_str())) break;
  1769. }
  1770. if (filepath.empty())
  1771. return Error("unable to locate include file: " + name);
  1772. if (source_filename)
  1773. files_included_per_file_[source_filename].insert(filepath);
  1774. if (included_files_.find(filepath) == included_files_.end()) {
  1775. // We found an include file that we have not parsed yet.
  1776. // Load it and parse it.
  1777. std::string contents;
  1778. if (!LoadFile(filepath.c_str(), true, &contents))
  1779. return Error("unable to load include file: " + name);
  1780. ECHECK(DoParse(contents.c_str(), include_paths, filepath.c_str()));
  1781. // We generally do not want to output code for any included files:
  1782. if (!opts.generate_all) MarkGenerated();
  1783. // This is the easiest way to continue this file after an include:
  1784. // instead of saving and restoring all the state, we simply start the
  1785. // file anew. This will cause it to encounter the same include
  1786. // statement again, but this time it will skip it, because it was
  1787. // entered into included_files_.
  1788. // This is recursive, but only go as deep as the number of include
  1789. // statements.
  1790. return DoParse(source, include_paths, source_filename);
  1791. }
  1792. EXPECT(';');
  1793. } else {
  1794. break;
  1795. }
  1796. }
  1797. // Now parse all other kinds of declarations:
  1798. while (token_ != kTokenEof) {
  1799. if (opts.proto_mode) {
  1800. ECHECK(ParseProtoDecl());
  1801. } else if (token_ == kTokenNameSpace) {
  1802. ECHECK(ParseNamespace());
  1803. } else if (token_ == '{') {
  1804. if (!root_struct_def_)
  1805. return Error("no root type set to parse json with");
  1806. if (builder_.GetSize()) {
  1807. return Error("cannot have more than one json object in a file");
  1808. }
  1809. uoffset_t toff;
  1810. ECHECK(ParseTable(*root_struct_def_, nullptr, &toff));
  1811. builder_.Finish(Offset<Table>(toff),
  1812. file_identifier_.length() ? file_identifier_.c_str() : nullptr);
  1813. } else if (token_ == kTokenEnum) {
  1814. ECHECK(ParseEnum(false, nullptr));
  1815. } else if (token_ == kTokenUnion) {
  1816. ECHECK(ParseEnum(true, nullptr));
  1817. } else if (token_ == kTokenRootType) {
  1818. NEXT();
  1819. auto root_type = attribute_;
  1820. EXPECT(kTokenIdentifier);
  1821. ECHECK(ParseNamespacing(&root_type, nullptr));
  1822. if (!SetRootType(root_type.c_str()))
  1823. return Error("unknown root type: " + root_type);
  1824. if (root_struct_def_->fixed)
  1825. return Error("root type must be a table");
  1826. EXPECT(';');
  1827. } else if (token_ == kTokenFileIdentifier) {
  1828. NEXT();
  1829. file_identifier_ = attribute_;
  1830. EXPECT(kTokenStringConstant);
  1831. if (file_identifier_.length() !=
  1832. FlatBufferBuilder::kFileIdentifierLength)
  1833. return Error("file_identifier must be exactly " +
  1834. NumToString(FlatBufferBuilder::kFileIdentifierLength) +
  1835. " characters");
  1836. EXPECT(';');
  1837. } else if (token_ == kTokenFileExtension) {
  1838. NEXT();
  1839. file_extension_ = attribute_;
  1840. EXPECT(kTokenStringConstant);
  1841. EXPECT(';');
  1842. } else if(token_ == kTokenInclude) {
  1843. return Error("includes must come before declarations");
  1844. } else if(token_ == kTokenAttribute) {
  1845. NEXT();
  1846. auto name = attribute_;
  1847. EXPECT(kTokenStringConstant);
  1848. EXPECT(';');
  1849. known_attributes_[name] = false;
  1850. } else if (token_ == kTokenService) {
  1851. ECHECK(ParseService());
  1852. } else {
  1853. ECHECK(ParseDecl());
  1854. }
  1855. }
  1856. for (auto it = structs_.vec.begin(); it != structs_.vec.end(); ++it) {
  1857. if ((*it)->predecl) {
  1858. return Error("type referenced but not defined: " + (*it)->name);
  1859. }
  1860. }
  1861. for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) {
  1862. auto &enum_def = **it;
  1863. if (enum_def.is_union) {
  1864. for (auto val_it = enum_def.vals.vec.begin();
  1865. val_it != enum_def.vals.vec.end();
  1866. ++val_it) {
  1867. auto &val = **val_it;
  1868. if (val.struct_def && val.struct_def->fixed)
  1869. return Error("only tables can be union elements: " + val.name);
  1870. }
  1871. }
  1872. }
  1873. return NoError();
  1874. }
  1875. std::set<std::string> Parser::GetIncludedFilesRecursive(
  1876. const std::string &file_name) const {
  1877. std::set<std::string> included_files;
  1878. std::list<std::string> to_process;
  1879. if (file_name.empty()) return included_files;
  1880. to_process.push_back(file_name);
  1881. while (!to_process.empty()) {
  1882. std::string current = to_process.front();
  1883. to_process.pop_front();
  1884. included_files.insert(current);
  1885. auto new_files = files_included_per_file_.at(current);
  1886. for (auto it = new_files.begin(); it != new_files.end(); ++it) {
  1887. if (included_files.find(*it) == included_files.end())
  1888. to_process.push_back(*it);
  1889. }
  1890. }
  1891. return included_files;
  1892. }
  1893. // Schema serialization functionality:
  1894. template<typename T> bool compareName(const T* a, const T* b) {
  1895. return a->defined_namespace->GetFullyQualifiedName(a->name)
  1896. < b->defined_namespace->GetFullyQualifiedName(b->name);
  1897. }
  1898. template<typename T> void AssignIndices(const std::vector<T *> &defvec) {
  1899. // Pre-sort these vectors, such that we can set the correct indices for them.
  1900. auto vec = defvec;
  1901. std::sort(vec.begin(), vec.end(), compareName<T>);
  1902. for (int i = 0; i < static_cast<int>(vec.size()); i++) vec[i]->index = i;
  1903. }
  1904. void Parser::Serialize() {
  1905. builder_.Clear();
  1906. AssignIndices(structs_.vec);
  1907. AssignIndices(enums_.vec);
  1908. std::vector<Offset<reflection::Object>> object_offsets;
  1909. for (auto it = structs_.vec.begin(); it != structs_.vec.end(); ++it) {
  1910. auto offset = (*it)->Serialize(&builder_, *this);
  1911. object_offsets.push_back(offset);
  1912. (*it)->serialized_location = offset.o;
  1913. }
  1914. std::vector<Offset<reflection::Enum>> enum_offsets;
  1915. for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) {
  1916. auto offset = (*it)->Serialize(&builder_, *this);
  1917. enum_offsets.push_back(offset);
  1918. (*it)->serialized_location = offset.o;
  1919. }
  1920. auto schema_offset = reflection::CreateSchema(
  1921. builder_,
  1922. builder_.CreateVectorOfSortedTables(&object_offsets),
  1923. builder_.CreateVectorOfSortedTables(&enum_offsets),
  1924. builder_.CreateString(file_identifier_),
  1925. builder_.CreateString(file_extension_),
  1926. root_struct_def_
  1927. ? root_struct_def_->serialized_location
  1928. : 0);
  1929. builder_.Finish(schema_offset, reflection::SchemaIdentifier());
  1930. }
  1931. Offset<reflection::Object> StructDef::Serialize(FlatBufferBuilder *builder,
  1932. const Parser &parser) const {
  1933. std::vector<Offset<reflection::Field>> field_offsets;
  1934. for (auto it = fields.vec.begin(); it != fields.vec.end(); ++it) {
  1935. field_offsets.push_back(
  1936. (*it)->Serialize(builder,
  1937. static_cast<uint16_t>(it - fields.vec.begin()), parser));
  1938. }
  1939. auto qualified_name = defined_namespace->GetFullyQualifiedName(name);
  1940. return reflection::CreateObject(*builder,
  1941. builder->CreateString(qualified_name),
  1942. builder->CreateVectorOfSortedTables(
  1943. &field_offsets),
  1944. fixed,
  1945. static_cast<int>(minalign),
  1946. static_cast<int>(bytesize),
  1947. SerializeAttributes(builder, parser));
  1948. }
  1949. Offset<reflection::Field> FieldDef::Serialize(FlatBufferBuilder *builder,
  1950. uint16_t id,
  1951. const Parser &parser) const {
  1952. return reflection::CreateField(*builder,
  1953. builder->CreateString(name),
  1954. value.type.Serialize(builder),
  1955. id,
  1956. value.offset,
  1957. IsInteger(value.type.base_type)
  1958. ? StringToInt(value.constant.c_str())
  1959. : 0,
  1960. IsFloat(value.type.base_type)
  1961. ? strtod(value.constant.c_str(), nullptr)
  1962. : 0.0,
  1963. deprecated,
  1964. required,
  1965. key,
  1966. SerializeAttributes(builder, parser));
  1967. // TODO: value.constant is almost always "0", we could save quite a bit of
  1968. // space by sharing it. Same for common values of value.type.
  1969. }
  1970. Offset<reflection::Enum> EnumDef::Serialize(FlatBufferBuilder *builder,
  1971. const Parser &parser) const {
  1972. std::vector<Offset<reflection::EnumVal>> enumval_offsets;
  1973. for (auto it = vals.vec.begin(); it != vals.vec.end(); ++it) {
  1974. enumval_offsets.push_back((*it)->Serialize(builder));
  1975. }
  1976. auto qualified_name = defined_namespace->GetFullyQualifiedName(name);
  1977. return reflection::CreateEnum(*builder,
  1978. builder->CreateString(qualified_name),
  1979. builder->CreateVector(enumval_offsets),
  1980. is_union,
  1981. underlying_type.Serialize(builder),
  1982. SerializeAttributes(builder, parser));
  1983. }
  1984. Offset<reflection::EnumVal> EnumVal::Serialize(FlatBufferBuilder *builder) const
  1985. {
  1986. return reflection::CreateEnumVal(*builder,
  1987. builder->CreateString(name),
  1988. value,
  1989. struct_def
  1990. ? struct_def->serialized_location
  1991. : 0);
  1992. }
  1993. Offset<reflection::Type> Type::Serialize(FlatBufferBuilder *builder) const {
  1994. return reflection::CreateType(*builder,
  1995. static_cast<reflection::BaseType>(base_type),
  1996. static_cast<reflection::BaseType>(element),
  1997. struct_def ? struct_def->index :
  1998. (enum_def ? enum_def->index : -1));
  1999. }
  2000. flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<
  2001. reflection::KeyValue>>>
  2002. Definition::SerializeAttributes(FlatBufferBuilder *builder,
  2003. const Parser &parser) const {
  2004. std::vector<flatbuffers::Offset<reflection::KeyValue>> attrs;
  2005. for (auto kv = attributes.dict.begin(); kv != attributes.dict.end(); ++kv) {
  2006. auto it = parser.known_attributes_.find(kv->first);
  2007. assert(it != parser.known_attributes_.end());
  2008. if (!it->second) { // Custom attribute.
  2009. attrs.push_back(
  2010. reflection::CreateKeyValue(*builder, builder->CreateString(kv->first),
  2011. builder->CreateString(
  2012. kv->second->constant)));
  2013. }
  2014. }
  2015. if (attrs.size()) {
  2016. return builder->CreateVectorOfSortedTables(&attrs);
  2017. } else {
  2018. return 0;
  2019. }
  2020. }
  2021. std::string Parser::ConformTo(const Parser &base) {
  2022. for (auto sit = structs_.vec.begin(); sit != structs_.vec.end(); ++sit) {
  2023. auto &struct_def = **sit;
  2024. auto qualified_name =
  2025. struct_def.defined_namespace->GetFullyQualifiedName(struct_def.name);
  2026. auto struct_def_base = base.structs_.Lookup(qualified_name);
  2027. if (!struct_def_base) continue;
  2028. for (auto fit = struct_def.fields.vec.begin();
  2029. fit != struct_def.fields.vec.end(); ++fit) {
  2030. auto &field = **fit;
  2031. auto field_base = struct_def_base->fields.Lookup(field.name);
  2032. if (field_base) {
  2033. if (field.value.offset != field_base->value.offset)
  2034. return "offsets differ for field: " + field.name;
  2035. if (field.value.constant != field_base->value.constant)
  2036. return "defaults differ for field: " + field.name;
  2037. if (!EqualByName(field.value.type, field_base->value.type))
  2038. return "types differ for field: " + field.name;
  2039. } else {
  2040. // Doesn't have to exist, deleting fields is fine.
  2041. // But we should check if there is a field that has the same offset
  2042. // but is incompatible (in the case of field renaming).
  2043. for (auto fbit = struct_def_base->fields.vec.begin();
  2044. fbit != struct_def_base->fields.vec.end(); ++fbit) {
  2045. field_base = *fbit;
  2046. if (field.value.offset == field_base->value.offset) {
  2047. if (!EqualByName(field.value.type, field_base->value.type))
  2048. return "field renamed to different type: " + field.name;
  2049. break;
  2050. }
  2051. }
  2052. }
  2053. }
  2054. }
  2055. for (auto eit = enums_.vec.begin(); eit != enums_.vec.end(); ++eit) {
  2056. auto &enum_def = **eit;
  2057. auto qualified_name =
  2058. enum_def.defined_namespace->GetFullyQualifiedName(enum_def.name);
  2059. auto enum_def_base = base.enums_.Lookup(qualified_name);
  2060. if (!enum_def_base) continue;
  2061. for (auto evit = enum_def.vals.vec.begin();
  2062. evit != enum_def.vals.vec.end(); ++evit) {
  2063. auto &enum_val = **evit;
  2064. auto enum_val_base = enum_def_base->vals.Lookup(enum_val.name);
  2065. if (enum_val_base) {
  2066. if (enum_val.value != enum_val_base->value)
  2067. return "values differ for enum: " + enum_val.name;
  2068. }
  2069. }
  2070. }
  2071. return "";
  2072. }
  2073. } // namespace flatbuffers