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 <cstring>
 29 #include <iostream>
 30 #include <iomanip>
 31 #include "cursor.h"
 32 
 33 
 34 struct Schema {
 35     struct Node {
 36 
 37         Node *parent;
 38         const char *type;
 39 
 40         std::vector<Node *> children;
 41 
 42         Node(Node *parent, const char* type):parent(parent), type(type) {
 43         }
 44 
 45         Node *addChild(Cursor *cursor, Node *child) {
 46             children.push_back(child);
 47             return child->parse(cursor);
 48         }
 49 
 50         virtual Node *parse(Cursor *cursor) {
 51             cursor->error(std::cerr, __FILE__, __LINE__, "In Node virtual parse!");
 52             return nullptr;
 53         };
 54 
 55         virtual ~Node() = default;
 56     };
 57 
 58 
 59 
 60     struct Array : public Node {
 61         bool flexible;
 62         long elementCount;
 63         char *elementName;
 64         Node *elementType;
 65 
 66         Array(Node *paren): Node(paren, "Array"), flexible(false), elementCount(0), elementName(nullptr), elementType(nullptr) {
 67         }
 68 
 69         Array *parse(Cursor *cursor) override;
 70 
 71         ~Array() override {
 72             if (elementType) {
 73                 delete elementType;
 74             }
 75             if (elementName) {
 76                 delete[] elementName;
 77             }
 78         }
 79     };
 80 
 81     struct AbstractNamedNode : public Node {
 82         char *name;
 83         Node *typeNode;
 84 
 85         AbstractNamedNode(Node *parent, const char *type, char *name): Node(parent, type), name(name), typeNode(nullptr) {
 86         }
 87 
 88         ~AbstractNamedNode() {
 89             if (name) {
 90                 delete[] name;
 91             }
 92 
 93             if (typeNode) {
 94                 delete typeNode;
 95             }
 96         }
 97     };
 98     struct FieldNode : public AbstractNamedNode {
 99         char *typeName;
100         FieldNode(Node *paren, char *name)
101                 : AbstractNamedNode(paren, "FieldNode", name), typeName(nullptr) {
102         }
103 
104         FieldNode *parse(Cursor *cursor) override ;
105 
106         ~FieldNode() override {
107             if (typeName) {
108                 delete[] name;
109             }
110         }
111     };
112 
113     struct AbstractStructOrUnionNode : public AbstractNamedNode {
114         char separator;
115         char terminator;
116 
117 
118         AbstractStructOrUnionNode(Node *parent, const char *type, char separator, char terminator, char *name)
119                 : AbstractNamedNode(parent, type, name), separator(separator), terminator(terminator) {
120 
121         }
122 
123          AbstractStructOrUnionNode *parse(Cursor *cursor) override;
124 
125         ~AbstractStructOrUnionNode() override =default;
126     };
127 
128     struct UnionNode : public AbstractStructOrUnionNode {
129         UnionNode(Node *parent, char *name)
130                 : AbstractStructOrUnionNode(parent, "UnionNode", '|', '>',  name) {
131         }
132 
133          UnionNode *parse(Cursor *cursor) override;
134 
135         ~UnionNode() override =default;
136     };
137 
138     struct StructNode : public AbstractStructOrUnionNode {
139          StructNode(Node *parent, const char *type, char *name)
140                 : AbstractStructOrUnionNode(parent,type, ',', '}',  name) {
141         }
142          StructNode(Node *parent, char *name)
143                 : StructNode(parent, "StructNode",  name) {
144         }
145         StructNode *parse(Cursor *cursor) override ;
146         ~StructNode() override =default;
147     };
148 
149     struct ArgStructNode : public StructNode {
150         bool complete;
151         explicit ArgStructNode(Node *parent, bool complete, char *name)
152                 : StructNode(parent, "ArgStructNode",   name), complete(complete) {
153         }
154         //virtual StructNode *parse(Cursor *cursor) ;
155         ~ArgStructNode() override =default;
156     };
157 
158 
159     struct ArgNode : public Node {
160         int idx;
161         ArgNode(Node *parent, int idx)
162                 : Node(parent, "ArgNode"), idx(idx) {
163         }
164 
165          ArgNode *parse(Cursor *cursor) override;
166 
167         virtual ~ArgNode() =default;
168     };
169 
170     struct SchemaNode : public Node {
171         SchemaNode()
172                 : Node(nullptr, "Schema") {
173         }
174 
175         virtual SchemaNode *parse(Cursor *cursor);
176 
177         virtual ~SchemaNode() =default;
178     };
179 
180     static void show(std::ostream &out, char *schema);
181     static void show(std::ostream &out, int depth, Node* node);
182     static void show(std::ostream &out, SchemaNode* schemaNode);
183 };