Symbol.hh 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * https://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef avro_parsing_Symbol_hh__
  19. #define avro_parsing_Symbol_hh__
  20. #include <map>
  21. #include <set>
  22. #include <sstream>
  23. #include <stack>
  24. #include <utility>
  25. #include <vector>
  26. #include <boost/any.hpp>
  27. #include <boost/tuple/tuple.hpp>
  28. #include "Decoder.hh"
  29. #include "Exception.hh"
  30. #include "Node.hh"
  31. namespace avro {
  32. namespace parsing {
  33. class Symbol;
  34. typedef std::vector<Symbol> Production;
  35. typedef std::shared_ptr<Production> ProductionPtr;
  36. typedef boost::tuple<std::stack<ssize_t>, bool, ProductionPtr, ProductionPtr> RepeaterInfo;
  37. typedef boost::tuple<ProductionPtr, ProductionPtr> RootInfo;
  38. class Symbol {
  39. public:
  40. enum class Kind {
  41. TerminalLow, // extra has nothing
  42. Null,
  43. Bool,
  44. Int,
  45. Long,
  46. Float,
  47. Double,
  48. String,
  49. Bytes,
  50. ArrayStart,
  51. ArrayEnd,
  52. MapStart,
  53. MapEnd,
  54. Fixed,
  55. Enum,
  56. Union,
  57. TerminalHigh,
  58. SizeCheck, // Extra has size
  59. NameList, // Extra has a vector<string>
  60. Root, // Root for a schema, extra is Symbol
  61. Repeater, // Array or Map, extra is symbol
  62. Alternative, // One of many (union), extra is Union
  63. Placeholder, // To be fixed up later.
  64. Indirect, // extra is shared_ptr<Production>
  65. Symbolic, // extra is weal_ptr<Production>
  66. EnumAdjust,
  67. UnionAdjust,
  68. SkipStart,
  69. Resolve,
  70. ImplicitActionLow,
  71. RecordStart,
  72. RecordEnd,
  73. Field, // extra is string
  74. Record,
  75. SizeList,
  76. WriterUnion,
  77. DefaultStart, // extra has default value in Avro binary encoding
  78. DefaultEnd,
  79. ImplicitActionHigh,
  80. Error
  81. };
  82. private:
  83. Kind kind_;
  84. boost::any extra_;
  85. explicit Symbol(Kind k) : kind_(k) {}
  86. template<typename T>
  87. Symbol(Kind k, T t) : kind_(k), extra_(t) {}
  88. public:
  89. Kind kind() const {
  90. return kind_;
  91. }
  92. template<typename T>
  93. T extra() const {
  94. return boost::any_cast<T>(extra_);
  95. }
  96. template<typename T>
  97. T *extrap() {
  98. return boost::any_cast<T>(&extra_);
  99. }
  100. template<typename T>
  101. const T *extrap() const {
  102. return boost::any_cast<T>(&extra_);
  103. }
  104. template<typename T>
  105. void extra(const T &t) {
  106. extra_ = t;
  107. }
  108. bool isTerminal() const {
  109. return kind_ > Kind::TerminalLow && kind_ < Kind::TerminalHigh;
  110. }
  111. bool isImplicitAction() const {
  112. return kind_ > Kind::ImplicitActionLow && kind_ < Kind::ImplicitActionHigh;
  113. }
  114. static const char *stringValues[];
  115. static const char *toString(Kind k) {
  116. return stringValues[static_cast<size_t>(k)];
  117. }
  118. static Symbol rootSymbol(ProductionPtr &s) {
  119. return Symbol(Kind::Root, RootInfo(s, std::make_shared<Production>()));
  120. }
  121. static Symbol rootSymbol(const ProductionPtr &main,
  122. const ProductionPtr &backup) {
  123. return Symbol(Kind::Root, RootInfo(main, backup));
  124. }
  125. static Symbol nullSymbol() {
  126. return Symbol(Kind::Null);
  127. }
  128. static Symbol boolSymbol() {
  129. return Symbol(Kind::Bool);
  130. }
  131. static Symbol intSymbol() {
  132. return Symbol(Kind::Int);
  133. }
  134. static Symbol longSymbol() {
  135. return Symbol(Kind::Long);
  136. }
  137. static Symbol floatSymbol() {
  138. return Symbol(Kind::Float);
  139. }
  140. static Symbol doubleSymbol() {
  141. return Symbol(Kind::Double);
  142. }
  143. static Symbol stringSymbol() {
  144. return Symbol(Kind::String);
  145. }
  146. static Symbol bytesSymbol() {
  147. return Symbol(Kind::Bytes);
  148. }
  149. static Symbol sizeCheckSymbol(size_t s) {
  150. return Symbol(Kind::SizeCheck, s);
  151. }
  152. static Symbol fixedSymbol() {
  153. return Symbol(Kind::Fixed);
  154. }
  155. static Symbol enumSymbol() {
  156. return Symbol(Kind::Enum);
  157. }
  158. static Symbol arrayStartSymbol() {
  159. return Symbol(Kind::ArrayStart);
  160. }
  161. static Symbol arrayEndSymbol() {
  162. return Symbol(Kind::ArrayEnd);
  163. }
  164. static Symbol mapStartSymbol() {
  165. return Symbol(Kind::MapStart);
  166. }
  167. static Symbol mapEndSymbol() {
  168. return Symbol(Kind::MapEnd);
  169. }
  170. static Symbol repeater(const ProductionPtr &p,
  171. bool isArray) {
  172. return repeater(p, p, isArray);
  173. }
  174. static Symbol repeater(const ProductionPtr &read,
  175. const ProductionPtr &skip,
  176. bool isArray) {
  177. std::stack<ssize_t> s;
  178. return Symbol(Kind::Repeater, RepeaterInfo(s, isArray, read, skip));
  179. }
  180. static Symbol defaultStartAction(std::shared_ptr<std::vector<uint8_t>> bb) {
  181. return Symbol(Kind::DefaultStart, std::move(bb));
  182. }
  183. static Symbol defaultEndAction() {
  184. return Symbol(Kind::DefaultEnd);
  185. }
  186. static Symbol alternative(
  187. const std::vector<ProductionPtr> &branches) {
  188. return Symbol(Symbol::Kind::Alternative, branches);
  189. }
  190. static Symbol unionSymbol() {
  191. return Symbol(Kind::Union);
  192. }
  193. static Symbol recordStartSymbol() {
  194. return Symbol(Kind::RecordStart);
  195. }
  196. static Symbol recordEndSymbol() {
  197. return Symbol(Kind::RecordEnd);
  198. }
  199. static Symbol fieldSymbol(const std::string &name) {
  200. return Symbol(Kind::Field, name);
  201. }
  202. static Symbol writerUnionAction() {
  203. return Symbol(Kind::WriterUnion);
  204. }
  205. static Symbol nameListSymbol(
  206. const std::vector<std::string> &v) {
  207. return Symbol(Kind::NameList, v);
  208. }
  209. template<typename T>
  210. static Symbol placeholder(const T &n) {
  211. return Symbol(Kind::Placeholder, n);
  212. }
  213. static Symbol indirect(const ProductionPtr &p) {
  214. return Symbol(Kind::Indirect, p);
  215. }
  216. static Symbol symbolic(const std::weak_ptr<Production> &p) {
  217. return Symbol(Kind::Symbolic, p);
  218. }
  219. static Symbol enumAdjustSymbol(const NodePtr &writer,
  220. const NodePtr &reader);
  221. static Symbol unionAdjustSymbol(size_t branch,
  222. const ProductionPtr &p) {
  223. return Symbol(Kind::UnionAdjust, std::make_pair(branch, p));
  224. }
  225. static Symbol sizeListAction(std::vector<size_t> order) {
  226. return Symbol(Kind::SizeList, std::move(order));
  227. }
  228. static Symbol recordAction() {
  229. return Symbol(Kind::Record);
  230. }
  231. static Symbol error(const NodePtr &writer, const NodePtr &reader);
  232. static Symbol resolveSymbol(Kind w, Kind r) {
  233. return Symbol(Kind::Resolve, std::make_pair(w, r));
  234. }
  235. static Symbol skipStart() {
  236. return Symbol(Kind::SkipStart);
  237. }
  238. };
  239. /**
  240. * Recursively replaces all placeholders in the production with the
  241. * corresponding values.
  242. */
  243. template<typename T>
  244. void fixup(const ProductionPtr &p,
  245. const std::map<T, ProductionPtr> &m) {
  246. std::set<ProductionPtr> seen;
  247. for (auto &it : *p) {
  248. fixup(it, m, seen);
  249. }
  250. }
  251. /**
  252. * Recursively replaces all placeholders in the symbol with the values with the
  253. * corresponding values.
  254. */
  255. template<typename T>
  256. void fixup_internal(const ProductionPtr &p,
  257. const std::map<T, ProductionPtr> &m,
  258. std::set<ProductionPtr> &seen) {
  259. if (seen.find(p) == seen.end()) {
  260. seen.insert(p);
  261. for (auto &it : *p) {
  262. fixup(it, m, seen);
  263. }
  264. }
  265. }
  266. template<typename T>
  267. void fixup(Symbol &s, const std::map<T, ProductionPtr> &m,
  268. std::set<ProductionPtr> &seen) {
  269. switch (s.kind()) {
  270. case Symbol::Kind::Indirect:
  271. fixup_internal(s.extra<ProductionPtr>(), m, seen);
  272. break;
  273. case Symbol::Kind::Alternative: {
  274. const std::vector<ProductionPtr> *vv =
  275. s.extrap<std::vector<ProductionPtr>>();
  276. for (const auto &it : *vv) {
  277. fixup_internal(it, m, seen);
  278. }
  279. } break;
  280. case Symbol::Kind::Repeater: {
  281. const RepeaterInfo &ri = *s.extrap<RepeaterInfo>();
  282. fixup_internal(boost::tuples::get<2>(ri), m, seen);
  283. fixup_internal(boost::tuples::get<3>(ri), m, seen);
  284. } break;
  285. case Symbol::Kind::Placeholder: {
  286. typename std::map<T, std::shared_ptr<Production>>::const_iterator it =
  287. m.find(s.extra<T>());
  288. if (it == m.end()) {
  289. throw Exception("Placeholder symbol cannot be resolved");
  290. }
  291. s = Symbol::symbolic(std::weak_ptr<Production>(it->second));
  292. } break;
  293. case Symbol::Kind::UnionAdjust:
  294. fixup_internal(s.extrap<std::pair<size_t, ProductionPtr>>()->second,
  295. m, seen);
  296. break;
  297. default:
  298. break;
  299. }
  300. }
  301. template<typename Handler>
  302. class SimpleParser {
  303. Decoder *decoder_;
  304. Handler &handler_;
  305. std::stack<Symbol> parsingStack;
  306. static void throwMismatch(Symbol::Kind actual, Symbol::Kind expected) {
  307. std::ostringstream oss;
  308. oss << "Invalid operation. Schema requires: " << Symbol::toString(expected) << ", got: " << Symbol::toString(actual);
  309. throw Exception(oss.str());
  310. }
  311. static void assertMatch(Symbol::Kind actual, Symbol::Kind expected) {
  312. if (expected != actual) {
  313. throwMismatch(actual, expected);
  314. }
  315. }
  316. void append(const ProductionPtr &ss) {
  317. for (Production::const_iterator it = ss->begin();
  318. it != ss->end(); ++it) {
  319. parsingStack.push(*it);
  320. }
  321. }
  322. size_t popSize() {
  323. const Symbol &s = parsingStack.top();
  324. assertMatch(Symbol::Kind::SizeCheck, s.kind());
  325. auto result = s.extra<size_t>();
  326. parsingStack.pop();
  327. return result;
  328. }
  329. static void assertLessThan(size_t n, size_t s) {
  330. if (n >= s) {
  331. std::ostringstream oss;
  332. oss << "Size max value. Upper bound: " << s << " found " << n;
  333. throw Exception(oss.str());
  334. }
  335. }
  336. public:
  337. Symbol::Kind advance(Symbol::Kind k) {
  338. for (;;) {
  339. Symbol &s = parsingStack.top();
  340. // std::cout << "advance: " << Symbol::toString(s.kind())
  341. // << " looking for " << Symbol::toString(k) << '\n';
  342. if (s.kind() == k) {
  343. parsingStack.pop();
  344. return k;
  345. } else if (s.isTerminal()) {
  346. throwMismatch(k, s.kind());
  347. } else {
  348. switch (s.kind()) {
  349. case Symbol::Kind::Root:
  350. append(boost::tuples::get<0>(*s.extrap<RootInfo>()));
  351. continue;
  352. case Symbol::Kind::Indirect: {
  353. ProductionPtr pp =
  354. s.extra<ProductionPtr>();
  355. parsingStack.pop();
  356. append(pp);
  357. }
  358. continue;
  359. case Symbol::Kind::Symbolic: {
  360. ProductionPtr pp(
  361. s.extra<std::weak_ptr<Production>>());
  362. parsingStack.pop();
  363. append(pp);
  364. }
  365. continue;
  366. case Symbol::Kind::Repeater: {
  367. auto *p = s.extrap<RepeaterInfo>();
  368. std::stack<ssize_t> &ns = boost::tuples::get<0>(*p);
  369. if (ns.empty()) {
  370. throw Exception(
  371. "Empty item count stack in repeater advance");
  372. }
  373. if (ns.top() == 0) {
  374. throw Exception(
  375. "Zero item count in repeater advance");
  376. }
  377. --ns.top();
  378. append(boost::tuples::get<2>(*p));
  379. }
  380. continue;
  381. case Symbol::Kind::Error:
  382. throw Exception(s.extra<std::string>());
  383. case Symbol::Kind::Resolve: {
  384. const std::pair<Symbol::Kind, Symbol::Kind> *p =
  385. s.extrap<std::pair<Symbol::Kind, Symbol::Kind>>();
  386. assertMatch(p->second, k);
  387. Symbol::Kind result = p->first;
  388. parsingStack.pop();
  389. return result;
  390. }
  391. case Symbol::Kind::SkipStart:
  392. parsingStack.pop();
  393. skip(*decoder_);
  394. break;
  395. default:
  396. if (s.isImplicitAction()) {
  397. size_t n = handler_.handle(s);
  398. if (s.kind() == Symbol::Kind::WriterUnion) {
  399. parsingStack.pop();
  400. selectBranch(n);
  401. } else {
  402. parsingStack.pop();
  403. }
  404. } else {
  405. std::ostringstream oss;
  406. oss << "Encountered " << Symbol::toString(s.kind())
  407. << " while looking for " << Symbol::toString(k);
  408. throw Exception(oss.str());
  409. }
  410. }
  411. }
  412. }
  413. }
  414. void skip(Decoder &d) {
  415. const size_t sz = parsingStack.size();
  416. if (sz == 0) {
  417. throw Exception("Nothing to skip!");
  418. }
  419. while (parsingStack.size() >= sz) {
  420. Symbol &t = parsingStack.top();
  421. // std::cout << "skip: " << Symbol::toString(t.kind()) << '\n';
  422. switch (t.kind()) {
  423. case Symbol::Kind::Null:
  424. d.decodeNull();
  425. break;
  426. case Symbol::Kind::Bool:
  427. d.decodeBool();
  428. break;
  429. case Symbol::Kind::Int:
  430. d.decodeInt();
  431. break;
  432. case Symbol::Kind::Long:
  433. d.decodeLong();
  434. break;
  435. case Symbol::Kind::Float:
  436. d.decodeFloat();
  437. break;
  438. case Symbol::Kind::Double:
  439. d.decodeDouble();
  440. break;
  441. case Symbol::Kind::String:
  442. d.skipString();
  443. break;
  444. case Symbol::Kind::Bytes:
  445. d.skipBytes();
  446. break;
  447. case Symbol::Kind::ArrayStart: {
  448. parsingStack.pop();
  449. size_t n = d.skipArray();
  450. processImplicitActions();
  451. assertMatch(Symbol::Kind::Repeater, parsingStack.top().kind());
  452. if (n == 0) {
  453. break;
  454. }
  455. Symbol &t2 = parsingStack.top();
  456. auto *p = t2.extrap<RepeaterInfo>();
  457. boost::tuples::get<0>(*p).push(n);
  458. continue;
  459. }
  460. case Symbol::Kind::ArrayEnd:
  461. break;
  462. case Symbol::Kind::MapStart: {
  463. parsingStack.pop();
  464. size_t n = d.skipMap();
  465. processImplicitActions();
  466. assertMatch(Symbol::Kind::Repeater, parsingStack.top().kind());
  467. if (n == 0) {
  468. break;
  469. }
  470. Symbol &t2 = parsingStack.top();
  471. auto *p2 = t2.extrap<RepeaterInfo>();
  472. boost::tuples::get<0>(*p2).push(n);
  473. continue;
  474. }
  475. case Symbol::Kind::MapEnd:
  476. break;
  477. case Symbol::Kind::Fixed: {
  478. parsingStack.pop();
  479. Symbol &t2 = parsingStack.top();
  480. d.decodeFixed(t2.extra<size_t>());
  481. } break;
  482. case Symbol::Kind::Enum:
  483. parsingStack.pop();
  484. d.decodeEnum();
  485. break;
  486. case Symbol::Kind::Union: {
  487. parsingStack.pop();
  488. size_t n = d.decodeUnionIndex();
  489. selectBranch(n);
  490. continue;
  491. }
  492. case Symbol::Kind::Repeater: {
  493. auto *p = t.extrap<RepeaterInfo>();
  494. std::stack<ssize_t> &ns = boost::tuples::get<0>(*p);
  495. if (ns.empty()) {
  496. throw Exception(
  497. "Empty item count stack in repeater skip");
  498. }
  499. ssize_t &n = ns.top();
  500. if (n == 0) {
  501. n = boost::tuples::get<1>(*p) ? d.arrayNext()
  502. : d.mapNext();
  503. }
  504. if (n != 0) {
  505. --n;
  506. append(boost::tuples::get<3>(*p));
  507. continue;
  508. } else {
  509. ns.pop();
  510. }
  511. break;
  512. }
  513. case Symbol::Kind::Indirect: {
  514. ProductionPtr pp =
  515. t.extra<ProductionPtr>();
  516. parsingStack.pop();
  517. append(pp);
  518. }
  519. continue;
  520. case Symbol::Kind::Symbolic: {
  521. ProductionPtr pp(
  522. t.extra<std::weak_ptr<Production>>());
  523. parsingStack.pop();
  524. append(pp);
  525. }
  526. continue;
  527. default: {
  528. std::ostringstream oss;
  529. oss << "Don't know how to skip "
  530. << Symbol::toString(t.kind());
  531. throw Exception(oss.str());
  532. }
  533. }
  534. parsingStack.pop();
  535. }
  536. }
  537. void assertSize(size_t n) {
  538. size_t s = popSize();
  539. if (s != n) {
  540. std::ostringstream oss;
  541. oss << "Incorrect size. Expected: " << s << " found " << n;
  542. throw Exception(oss.str());
  543. }
  544. }
  545. void assertLessThanSize(size_t n) {
  546. assertLessThan(n, popSize());
  547. }
  548. size_t enumAdjust(size_t n) {
  549. const Symbol &s = parsingStack.top();
  550. assertMatch(Symbol::Kind::EnumAdjust, s.kind());
  551. const auto *v = s.extrap<std::pair<std::vector<int>, std::vector<std::string>>>();
  552. assertLessThan(n, v->first.size());
  553. int result = v->first[n];
  554. if (result < 0) {
  555. std::ostringstream oss;
  556. oss << "Cannot resolve symbol: " << v->second[-result - 1]
  557. << std::endl;
  558. throw Exception(oss.str());
  559. }
  560. parsingStack.pop();
  561. return result;
  562. }
  563. size_t unionAdjust() {
  564. const Symbol &s = parsingStack.top();
  565. assertMatch(Symbol::Kind::UnionAdjust, s.kind());
  566. std::pair<size_t, ProductionPtr> p =
  567. s.extra<std::pair<size_t, ProductionPtr>>();
  568. parsingStack.pop();
  569. append(p.second);
  570. return p.first;
  571. }
  572. std::string nameForIndex(size_t e) {
  573. const Symbol &s = parsingStack.top();
  574. assertMatch(Symbol::Kind::NameList, s.kind());
  575. const std::vector<std::string> names =
  576. s.extra<std::vector<std::string>>();
  577. if (e >= names.size()) {
  578. throw Exception("Not that many names");
  579. }
  580. std::string result = names[e];
  581. parsingStack.pop();
  582. return result;
  583. }
  584. size_t indexForName(const std::string &name) {
  585. const Symbol &s = parsingStack.top();
  586. assertMatch(Symbol::Kind::NameList, s.kind());
  587. const std::vector<std::string> names =
  588. s.extra<std::vector<std::string>>();
  589. auto it = std::find(names.begin(), names.end(), name);
  590. if (it == names.end()) {
  591. throw Exception("No such enum symbol");
  592. }
  593. size_t result = it - names.begin();
  594. parsingStack.pop();
  595. return result;
  596. }
  597. void pushRepeatCount(size_t n) {
  598. processImplicitActions();
  599. Symbol &s = parsingStack.top();
  600. assertMatch(Symbol::Kind::Repeater, s.kind());
  601. auto *p = s.extrap<RepeaterInfo>();
  602. std::stack<ssize_t> &nn = boost::tuples::get<0>(*p);
  603. nn.push(n);
  604. }
  605. void nextRepeatCount(size_t n) {
  606. processImplicitActions();
  607. Symbol &s = parsingStack.top();
  608. assertMatch(Symbol::Kind::Repeater, s.kind());
  609. auto *p = s.extrap<RepeaterInfo>();
  610. std::stack<ssize_t> &nn = boost::tuples::get<0>(*p);
  611. if (nn.empty() || nn.top() != 0) {
  612. throw Exception("Wrong number of items");
  613. }
  614. nn.top() = n;
  615. }
  616. void popRepeater() {
  617. processImplicitActions();
  618. Symbol &s = parsingStack.top();
  619. assertMatch(Symbol::Kind::Repeater, s.kind());
  620. auto *p = s.extrap<RepeaterInfo>();
  621. std::stack<ssize_t> &ns = boost::tuples::get<0>(*p);
  622. if (ns.empty()) {
  623. throw Exception("Incorrect number of items (empty)");
  624. }
  625. if (ns.top() > 0) {
  626. throw Exception("Incorrect number of items (non-zero)");
  627. }
  628. ns.pop();
  629. parsingStack.pop();
  630. }
  631. void selectBranch(size_t n) {
  632. const Symbol &s = parsingStack.top();
  633. assertMatch(Symbol::Kind::Alternative, s.kind());
  634. std::vector<ProductionPtr> v =
  635. s.extra<std::vector<ProductionPtr>>();
  636. if (n >= v.size()) {
  637. throw Exception("Not that many branches");
  638. }
  639. parsingStack.pop();
  640. append(v[n]);
  641. }
  642. const std::vector<size_t> &sizeList() {
  643. const Symbol &s = parsingStack.top();
  644. assertMatch(Symbol::Kind::SizeList, s.kind());
  645. return *s.extrap<std::vector<size_t>>();
  646. }
  647. Symbol::Kind top() const {
  648. return parsingStack.top().kind();
  649. }
  650. void pop() {
  651. parsingStack.pop();
  652. }
  653. void processImplicitActions() {
  654. for (;;) {
  655. Symbol &s = parsingStack.top();
  656. if (s.isImplicitAction()) {
  657. handler_.handle(s);
  658. parsingStack.pop();
  659. } else if (s.kind() == Symbol::Kind::SkipStart) {
  660. parsingStack.pop();
  661. skip(*decoder_);
  662. } else {
  663. break;
  664. }
  665. }
  666. }
  667. SimpleParser(const Symbol &s, Decoder *d, Handler &h) : decoder_(d), handler_(h) {
  668. parsingStack.push(s);
  669. }
  670. void reset() {
  671. while (parsingStack.size() > 1) {
  672. parsingStack.pop();
  673. }
  674. }
  675. };
  676. inline std::ostream &operator<<(std::ostream &os, const Symbol &s);
  677. inline std::ostream &operator<<(std::ostream &os, const Production &p) {
  678. os << '(';
  679. for (const auto &it : p) {
  680. os << it << ", ";
  681. }
  682. os << ')';
  683. return os;
  684. }
  685. inline std::ostream &operator<<(std::ostream &os, const Symbol &s) {
  686. switch (s.kind()) {
  687. case Symbol::Kind::Repeater: {
  688. const RepeaterInfo &ri = *s.extrap<RepeaterInfo>();
  689. os << '(' << Symbol::toString(s.kind())
  690. << ' ' << *boost::tuples::get<2>(ri)
  691. << ' ' << *boost::tuples::get<3>(ri)
  692. << ')';
  693. } break;
  694. case Symbol::Kind::Indirect: {
  695. os << '(' << Symbol::toString(s.kind()) << ' '
  696. << *s.extra<std::shared_ptr<Production>>() << ')';
  697. } break;
  698. case Symbol::Kind::Alternative: {
  699. os << '(' << Symbol::toString(s.kind());
  700. for (const auto &it : *s.extrap<std::vector<ProductionPtr>>()) {
  701. os << ' ' << *it;
  702. }
  703. os << ')';
  704. } break;
  705. case Symbol::Kind::Symbolic: {
  706. os << '(' << Symbol::toString(s.kind())
  707. << ' ' << s.extra<std::weak_ptr<Production>>().lock()
  708. << ')';
  709. } break;
  710. default:
  711. os << Symbol::toString(s.kind());
  712. break;
  713. }
  714. return os;
  715. }
  716. } // namespace parsing
  717. } // namespace avro
  718. #endif