1 /* 2 * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #pragma once 27 #include <vector> 28 #include <iostream> 29 #include <iomanip> 30 #include "schema_cursor.h" 31 32 33 struct Schema { 34 struct Node { 35 Node *parent; 36 const char *type; 37 38 std::vector<Node *> children; 39 40 Node(Node *parent, const char *type): parent(parent), type(type) { 41 } 42 43 Node *addChild(SchemaCursor *cursor, Node *child) { 44 children.push_back(child); 45 return child->parse(cursor); 46 } 47 48 virtual Node *parse(SchemaCursor *cursor) { 49 cursor->error(std::cerr, __FILE__, __LINE__, "In Node virtual parse!"); 50 return nullptr; 51 }; 52 53 virtual ~Node() = default; 54 }; 55 56 57 struct Array final : Node { 58 bool flexible; 59 long elementCount; 60 char *elementName; 61 Node *elementType; 62 63 explicit Array(Node *paren): Node(paren, "Array"), flexible(false), elementCount(0), elementName(nullptr), 64 elementType(nullptr) { 65 } 66 67 Array *parse(SchemaCursor *cursor) override; 68 69 ~Array() override { 70 delete elementType; 71 delete[] elementName; 72 } 73 }; 74 75 struct AbstractNamedNode : Node { 76 char *name; 77 Node *typeNode; 78 79 AbstractNamedNode(Node *parent, const char *type, char *name): Node(parent, type), name(name), 80 typeNode(nullptr) { 81 } 82 83 ~AbstractNamedNode() override { 84 if (name) { 85 delete[] name; 86 } 87 88 if (typeNode) { 89 delete typeNode; 90 } 91 } 92 }; 93 94 struct FieldNode : AbstractNamedNode { 95 char *typeName; 96 97 FieldNode(Node *paren, char *name) 98 : AbstractNamedNode(paren, "FieldNode", name), typeName(nullptr) { 99 } 100 101 FieldNode *parse(SchemaCursor *cursor) override; 102 103 ~FieldNode() override { 104 if (typeName) { 105 delete[] name; 106 } 107 } 108 }; 109 110 struct AbstractStructOrUnionNode : AbstractNamedNode { 111 char separator; 112 char terminator; 113 114 115 AbstractStructOrUnionNode(Node *parent, const char *type, char separator, char terminator, char *name) 116 : AbstractNamedNode(parent, type, name), separator(separator), terminator(terminator) { 117 } 118 119 AbstractStructOrUnionNode *parse(SchemaCursor *cursor) override; 120 121 ~AbstractStructOrUnionNode() override = default; 122 }; 123 124 struct UnionNode final : AbstractStructOrUnionNode { 125 UnionNode(Node *parent, char *name) 126 : AbstractStructOrUnionNode(parent, "UnionNode", '|', '>', name) { 127 } 128 129 UnionNode *parse(SchemaCursor *cursor) override; 130 131 ~UnionNode() override = default; 132 }; 133 134 struct StructNode : AbstractStructOrUnionNode { 135 StructNode(Node *parent, const char *type, char *name) 136 : AbstractStructOrUnionNode(parent, type, ',', '}', name) { 137 } 138 139 StructNode(Node *parent, char *name) 140 : StructNode(parent, "StructNode", name) { 141 } 142 143 StructNode *parse(SchemaCursor *cursor) override; 144 145 ~StructNode() override = default; 146 }; 147 148 struct ArgStructNode final : StructNode { 149 bool complete; 150 151 explicit ArgStructNode(Node *parent, bool complete, char *name) 152 : StructNode(parent, "ArgStructNode", name), complete(complete) { 153 } 154 155 ~ArgStructNode() override = default; 156 }; 157 158 159 struct ArgNode : Node { 160 int idx; 161 162 ArgNode(Node *parent, int idx) 163 : Node(parent, "ArgNode"), idx(idx) { 164 } 165 166 ArgNode *parse(SchemaCursor *cursor) override; 167 168 ~ArgNode() override = default; 169 }; 170 171 struct SchemaNode : Node { 172 SchemaNode() 173 : Node(nullptr, "Schema") { 174 } 175 176 SchemaNode *parse(SchemaCursor *cursor) override; 177 178 ~SchemaNode() override = default; 179 }; 180 181 static void show(std::ostream &out, char *schema); 182 183 static void show(std::ostream &out, int depth, Node *node); 184 185 static void show(std::ostream &out, SchemaNode *schemaNode); 186 };