-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnodes.hpp
158 lines (147 loc) · 3.63 KB
/
nodes.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
** RFC5246 Data definition language parser
** Rich Salz, [email protected], June 2013.
** Copyright 2013, Rich Salz
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#include <string>
#include <map>
#include <set>
#include <vector>
/// Some containers
typedef std::vector<const class Node*> NodeList;
typedef std::vector<const class Arm*> ArmList;
typedef std::vector<const class Reference*> ReferenceList;
/// A set of ID's, which can have an optional value.
class IDSet
{
IDSet(const IDSet& rhs);
IDSet& operator=(const IDSet& rhs);
typedef std::map<std::string, int> Items;
Items mItems;
public:
typedef Items::iterator iterator;
IDSet(const std::string& id, int value=0)
{
mItems[id] = value;
}
bool has(const std::string& id) const
{
return mItems.find(id) != mItems.end();
}
void insert(const std::string& id, int value=0)
{
mItems[id] = value;
}
iterator begin() const
{
return const_cast<IDSet*>(this)->mItems.begin();
}
iterator end() const
{
return const_cast<IDSet*>(this)->mItems.end();
}
};
/// Base class for all parse nodes
class Node
{
Node(const Node& rhs);
Node& operator=(const Node& rhs);
const std::string* mName;
public:
Node(const std::string* name)
: mName(name)
{
}
virtual ~Node()
{
}
const std::string* Name() const
{
return mName;
}
};
/// A simple declaration (of a primitive type)
class Simple : public Node
{
int mType;
public:
Simple(const std::string* name, int type)
: Node(name), mType(type)
{
}
};
/// A named instance of a more complex type
class Reference : public Node
{
const std::string* mRef;
int mCrypto;
public:
Reference(const std::string* name, const std::string* ref, int crypto=0)
: Node(name), mRef(ref), mCrypto(crypto)
{
}
const std::string& Ref() const
{
return *mRef;
}
};
/// The arm of a select/variant
class Arm
{
const IDSet* mCases;
const ReferenceList* mReflist;
public:
Arm(const IDSet* cases, const ReferenceList* reflist)
: mCases(cases), mReflist(reflist)
{
}
const IDSet& Cases() const
{
return *mCases;
}
};
/// Constructed type -- struct, enum, variant/select
class Compound : public Node
{
int mType;
int mCrypto;
union {
const IDSet* uIDSet;
const NodeList* uNodeList;
const ArmList* uArmList;
};
public:
Compound(const std::string* name, int type, const IDSet* isp)
: Node(name), mType(type), mCrypto(0), uIDSet(isp)
{
}
Compound(const std::string* name, int type, const NodeList* nlp, int crypto=0)
: Node(name), mType(type), mCrypto(crypto), uNodeList(nlp)
{
}
Compound(const std::string* name, int type, const ArmList* alp)
: Node(name), mType(type), mCrypto(0), uArmList(alp)
{
}
};
// Variables.
extern int lineno;
// From flex.
extern char* yytext;
extern int yylex(void);
// From bison.
extern void vyyerror(const char* fmt, ...)
__attribute__ ((format (printf, 1, 2)));
extern void setyydebug(bool flag);