MyLang
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
myparser_ast.hpp
Go to the documentation of this file.
1 #ifndef MYPARSER_AST_HPP
2 #define MYPARSER_AST_HPP
3 
4 #include "myparser_pass.hpp"
5 
6 namespace myparser {
7 
8 using Input = std::string::const_iterator;
9 
10 class Node {
11 private:
12  const Input pos;
13 
14  inline Node() = delete;
15 
16 protected:
17  inline Node(const Input &input): pos(input) {}
18 
19 public:
20  virtual ~Node() {} // destructable (public)
21 
22  virtual inline void free() { // hack // TODO
23  delete this;
24  }
25 
26  virtual bool empty() const = 0;
27 
28  virtual void runPass(PassBase *pass) const = 0;
29 
30  virtual const std::string &getRuleName() const = 0;
31 
32  virtual size_t getLen() const = 0;
33 
34  virtual void getFullText(std::ostream &out) const = 0;
35 
36  inline const std::string getFullText() const {
37  std::ostringstream result;
38 
39  getFullText(result);
40 
41  return result.str();
42  }
43 
44  inline const Input &getPos() const {
45  return pos;
46  }
47 
48  inline const Input getTail() const {
49  return pos + getLen();
50  }
51 
52  inline Node *challengeLonger(Node *target) {
53  if (!target) {
54  return this;
55  }
56 
57  if (getTail() > target->getTail()) {
58  target->free();
59  return this;
60  } else {
61  this->free();
62  return target;
63  }
64  }
65 };
66 
67 template <class TX = void> // actually not a template
68 class NodeList: public Node {
69 private:
70  std::vector<Node *> children;
71 
72  size_t basepos;
73  NodeList<> *brother = nullptr;
74 
75 protected:
76  inline NodeList(const Input &input):
77  Node(input), children() {}
78 
79 public:
80  virtual ~NodeList() {
81  for (size_t i = basepos; i < children.size(); ++i) {
82  children[i]->free();
83  }
84  }
85 
86  virtual void free() {
87  if (brother) {
88  brother->basepos = 0;
89  brother->brother = nullptr;
90  }
91  delete this;
92  }
93 
94  virtual bool empty() const {
95  for (Node *child: children) {
96  if (!child->empty()) {
97  return false;
98  }
99  }
100 
101  return true;
102  }
103 
104  inline void bind(NodeList<> *target, const size_t pos) {
105  brother = target;
106  basepos = pos;
107  }
108 
109  inline void putChild(Node *value) {
110  children.push_back(value);
111  }
112 
113  virtual size_t getLen() const {
114  size_t result = 0;
115 
116  for (Node *child: children) {
117  result += child->getLen();
118  }
119 
120  return result;
121  }
122 
123  virtual void getFullText(std::ostream &out) const {
124  for (Node *child: children) {
125  child->getFullText(out);
126  }
127  }
128 
129  virtual size_t getIndex() const = 0;
130 
131  inline const std::vector<Node *> &getChildren() const {
132  return children;
133  }
134 };
135 
136 template <size_t I>
137 class NodeListIndexed: public NodeList<> {
138 public:
139  inline NodeListIndexed(const Input &input):
140  NodeList(input) {}
141 
142  // virtual ~NodeListIndexed() {}
143 
144  virtual size_t getIndex() const {
145  return I;
146  }
147 };
148 
149 template <class TX = void> // actually not a template
150 class NodeText: public Node {
151 private:
152  const std::string text;
153 
154 public:
155  inline NodeText(
156  const Input &input, std::string &&value
157  ): Node(input), text(std::move(value)) {}
158 
159  // virtual ~NodeText() {}
160 
161  virtual bool accepted() const {
162  return true;
163  }
164 
165  virtual bool empty() const {
166  return accepted() && text.size() == 0;
167  }
168 
169  virtual size_t getLen() const {
170  return text.size();
171  }
172 
173  virtual void getFullText(std::ostream &out) const {
174  out << text;
175  }
176 
177  inline const std::string &getText() const {
178  return text;
179  }
180 };
181 
182 template <class E>
183 class NodeTextOrError: public NodeText<> {
184 public:
186  const Input &input, std::string &&value
187  ): NodeText(input, std::move(value)) {}
188 
189 };
190 
191 template <class E>
192 class NodeError: public Node {
193 public:
194  inline NodeError(const Input &input):
195  Node(input) {}
196 
197  // virtual ~NodeError() {}
198 
199  virtual bool empty() const {
200  return false;
201  }
202 
203  virtual size_t getLen() const {
204  return 0;
205  }
206 
207  virtual void getFullText(std::ostream &out) const {
208  // nothing
209  (void) out;
210  }
211 };
212 
213 template <class N, class T, size_t I = 0>
214 class NodeTypedProto: public T {
215 public:
216  using T::T;
217 
218  virtual void runPass(PassBase *pass) const {
219  Pass<I>::call(pass, pass->getId(), this);
220  }
221 
222  virtual const std::string &getRuleName() const {
223  return N::getStr();
224  }
225 };
226 
227 // could specialization
228 template <class N, class T>
229 class NodeTyped: public NodeTypedProto<N, T> {
230 public:
232 };
233 
234 template <class NT, size_t I>
236 
237 template <class NT>
239 
240 template <class NT, class E>
242 
243 }
244 
245 #endif
const std::vector< Node * > & getChildren() const
Definition: myparser_ast.hpp:131
Node(const Input &input)
Definition: myparser_ast.hpp:17
static MYPARSER_INLINE void call(PassBase *pass, const size_t target, const T *node)
Definition: myparser_pass.hpp:48
virtual size_t getIndex() const =0
const std::string getFullText() const
Definition: myparser_ast.hpp:36
Definition: myparser_ast.hpp:229
virtual void getFullText(std::ostream &out) const
Definition: myparser_ast.hpp:123
size_t getId()
Definition: myparser_pass.hpp:18
virtual size_t getLen() const =0
virtual bool empty() const
Definition: myparser_ast.hpp:165
virtual bool empty() const
Definition: myparser_ast.hpp:94
virtual void free()
Definition: myparser_ast.hpp:86
Definition: myparser_ast.hpp:10
const Input pos
Definition: myparser_ast.hpp:12
virtual bool empty() const
Definition: myparser_ast.hpp:199
void bind(NodeList<> *target, const size_t pos)
Definition: myparser_ast.hpp:104
virtual ~NodeList()
Definition: myparser_ast.hpp:80
Definition: myparser_ast.hpp:183
const Input & getPos() const
Definition: myparser_ast.hpp:44
const std::string text
Definition: myparser_ast.hpp:152
virtual void getFullText(std::ostream &out) const
Definition: myparser_ast.hpp:173
virtual bool accepted() const
Definition: myparser_ast.hpp:161
Node * challengeLonger(Node *target)
Definition: myparser_ast.hpp:52
virtual void free()
Definition: myparser_ast.hpp:22
Definition: myparser_pass.hpp:8
const Input getTail() const
Definition: myparser_ast.hpp:48
void putChild(Node *value)
Definition: myparser_ast.hpp:109
NodeText(const Input &input, std::string &&value)
Definition: myparser_ast.hpp:155
virtual void runPass(PassBase *pass) const =0
virtual bool empty() const =0
virtual const std::string & getRuleName() const
Definition: myparser_ast.hpp:222
Definition: myparser_ast.hpp:137
Definition: myparser_ast.hpp:192
virtual size_t getLen() const
Definition: myparser_ast.hpp:203
NodeList(const Input &input)
Definition: myparser_ast.hpp:76
NodeTextOrError(const Input &input, std::string &&value)
Definition: myparser_ast.hpp:185
virtual size_t getLen() const
Definition: myparser_ast.hpp:169
virtual void runPass(PassBase *pass) const
Definition: myparser_ast.hpp:218
NodeListIndexed(const Input &input)
Definition: myparser_ast.hpp:139
std::vector< Node * > children
Definition: myparser_ast.hpp:70
virtual size_t getLen() const
Definition: myparser_ast.hpp:113
Definition: myparser_ast.hpp:150
virtual ~Node()
Definition: myparser_ast.hpp:20
virtual void getFullText(std::ostream &out) const
Definition: myparser_ast.hpp:207
std::string::const_iterator Input
Definition: myparser_ast.hpp:8
Definition: myparser_ast.hpp:214
const std::string & getText() const
Definition: myparser_ast.hpp:177
size_t basepos
Definition: myparser_ast.hpp:72
NodeList * brother
Definition: myparser_ast.hpp:73
virtual const std::string & getRuleName() const =0
virtual size_t getIndex() const
Definition: myparser_ast.hpp:144
NodeError(const Input &input)
Definition: myparser_ast.hpp:194
Definition: myparser_ast.hpp:68