Symbol.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #include "Symbol.hh"
  19. namespace avro {
  20. namespace parsing {
  21. using std::ostringstream;
  22. using std::string;
  23. using std::vector;
  24. const char *Symbol::stringValues[] = {
  25. "TerminalLow",
  26. "Null",
  27. "Bool",
  28. "Int",
  29. "Long",
  30. "Float",
  31. "Double",
  32. "String",
  33. "Bytes",
  34. "ArrayStart",
  35. "ArrayEnd",
  36. "MapStart",
  37. "MapEnd",
  38. "Fixed",
  39. "Enum",
  40. "Union",
  41. "TerminalHigh",
  42. "SizeCheck",
  43. "NameList",
  44. "Root",
  45. "Repeater",
  46. "Alternative",
  47. "Placeholder",
  48. "Indirect",
  49. "Symbolic",
  50. "EnumAdjust",
  51. "UnionAdjust",
  52. "SkipStart",
  53. "Resolve",
  54. "ImplicitActionLow",
  55. "RecordStart",
  56. "RecordEnd",
  57. "Field",
  58. "Record",
  59. "SizeList",
  60. "WriterUnion",
  61. "DefaultStart",
  62. "DefaultEnd",
  63. "ImplicitActionHigh",
  64. "Error"};
  65. Symbol Symbol::enumAdjustSymbol(const NodePtr &writer, const NodePtr &reader) {
  66. vector<string> rs;
  67. size_t rc = reader->names();
  68. for (size_t i = 0; i < rc; ++i) {
  69. rs.push_back(reader->nameAt(i));
  70. }
  71. size_t wc = writer->names();
  72. vector<int> adj;
  73. adj.reserve(wc);
  74. vector<string> err;
  75. for (size_t i = 0; i < wc; ++i) {
  76. const string &s = writer->nameAt(i);
  77. vector<string>::const_iterator it = find(rs.begin(), rs.end(), s);
  78. if (it == rs.end()) {
  79. auto pos = err.size() + 1;
  80. adj.push_back(-pos);
  81. err.push_back(s);
  82. } else {
  83. adj.push_back(it - rs.begin());
  84. }
  85. }
  86. return Symbol(Kind::EnumAdjust, make_pair(adj, err));
  87. }
  88. Symbol Symbol::error(const NodePtr &writer, const NodePtr &reader) {
  89. ostringstream oss;
  90. oss << "Cannot resolve: " << std::endl;
  91. writer->printJson(oss, 0);
  92. oss << std::endl
  93. << "with" << std::endl;
  94. reader->printJson(oss, 0);
  95. return Symbol(Kind::Error, oss.str());
  96. }
  97. } // namespace parsing
  98. } // namespace avro