MyLang
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
myparser_pass_repr.hpp
Go to the documentation of this file.
1 #ifndef MYPARSER_PASS_REPR_HPP
2 #define MYPARSER_PASS_REPR_HPP
3 
4 #include "myparser_pass.hpp"
5 
6 namespace myparser {
7 
8 template <>
9 class Pass<PASS_REPR>: public PassProto<PASS_REPR> {
10 private:
11  const bool optionV;
12  const bool optionC;
13 
14 protected:
15  std::ostream &out;
16  size_t indent;
17 
18  virtual void putName(const std::string &name) {
19  (void) name;
20  }
21 
22  virtual void putIndex(const size_t index) {
23  (void) index;
24  }
25 
26  virtual void putText(const std::string &text) {
27  (void) text;
28  }
29 
30  virtual void putError(const std::string &error) {
31  (void) error;
32  }
33 
34  virtual void putMainBegin() {}
35 
36  virtual void putMainEnd() {}
37 
38  virtual void putPlaceHolder() {}
39 
40  virtual void putBegin() {}
41 
42  virtual void putEnd() {}
43 
44  virtual void putLnBegin() {}
45 
46  virtual void putLnEnd() {}
47 
48  virtual void putLn(const bool first) {
49  (void) first;
50  }
51 
52 public:
53  inline Pass(
54  std::ostream &target,
55  const bool verbose, const bool compact
56  ):
58  optionV(verbose), optionC(compact),
59  out(target), indent(0) {}
60 
61  // virtual ~Pass() {}
62 
63  void run(const NodeList<> *node) {
64  putMainBegin();
65 
66  putName(node->getRuleName());
67  putIndex(node->getIndex());
68 
69  std::vector<Node *> children1;
70 
71  if (!optionV) {
72  for (Node *child: node->getChildren()) {
73  if (!child->empty()) {
74  children1.push_back(child);
75  }
76  }
77  }
78 
79  const std::vector<Node *> &children =
80  optionV ? node->getChildren() : children1;
81 
82  if (optionC && children.size() == 1) {
83  putBegin();
84  children[0]->runPass(this);
85  putEnd();
86  } else {
87  putPlaceHolder();
88 
89  putLnBegin();
90  ++indent;
91 
92  bool first = true;
93  for (const Node *child: children) {
94  putLn(first);
95  first = false;
96  child->runPass(this);
97  }
98 
99  --indent;
100  putLnEnd();
101  }
102 
103  putMainEnd();
104  }
105 
106  void run(const NodeText<> *node) {
107  putMainBegin();
108 
109  putName(node->getRuleName());
110 
111  putBegin();
112  putText(node->getText());
113  putEnd();
114 
115  putMainEnd();
116  }
117 
118  template <class E>
119  void run(const NodeTextOrError<E> *node) {
120  putMainBegin();
121 
122  putName(node->getRuleName());
123 
124  putBegin();
125  if (node->accepted()) {
126  putText(node->getText());
127  } else {
128  putError(E::getStr());
129  }
130  putEnd();
131 
132  putMainEnd();
133  }
134 
135  template <class E>
136  void run(const NodeError<E> *node) {
137  putMainBegin();
138 
139  putName(node->getRuleName());
140 
141  putBegin();
142  putError(E::getStr());
143  putEnd();
144 
145  putMainEnd();
146  }
147 };
148 
149 template <class TX = void> // actually not a template
150 class PassReprText: public Pass<PASS_REPR> {
151 protected:
152  virtual void putText(const std::string &text) {
153  out << text;
154  }
155 
156 public:
157  inline PassReprText(
158  std::ostream &target
159  ): Pass<PASS_REPR>(target, true, false) {}
160 
161  // virtual ~PassReprText() {}
162 };
163 
164 template <class TX = void> // actually not a template
165 class PassReprSimple: public Pass<PASS_REPR> {
166 protected:
167  virtual void putText(const std::string &text) {
168  out << style_keyword << text << style_normal;
169  }
170 
171  virtual void putError(const std::string &error) {
172  out << style_error << "ERROR: " << style_normal << error;
173  }
174 
175  virtual void putPlaceHolder() {
176  out << "=>";
177  }
178 
179  virtual void putLn(const bool first) {
180  (void) first;
181 
182  out << '\n';
183  for (size_t i = 0; i < indent; ++i) {
184  out << " ";
185  }
186  }
187 
188 public:
190  std::ostream &target,
191  const bool verbose = false, const bool compact = true
192  ): Pass<PASS_REPR>(target, verbose, compact) {}
193 
194  // virtual ~PassReprSimple() {}
195 };
196 
197 template <class TX = void> // actually not a template
198 class PassReprFull: public Pass<PASS_REPR> {
199 protected:
200  virtual void putName(const std::string &name) {
201  out << name;
202  }
203 
204  virtual void putIndex(const size_t index) {
205  out << '[';
206  out << style_index << index << style_normal;
207  out << ']';
208  }
209 
210  virtual void putText(const std::string &text) {
211  out << style_keyword << text << style_normal;
212  }
213 
214  virtual void putError(const std::string &error) {
215  out << style_error << "ERROR: " << style_normal << error;
216  }
217 
218  virtual void putBegin() {
219  out << style_faint << " - " << style_normal;
220  }
221 
222  virtual void putLn(const bool first) {
223  (void) first;
224 
225  out << '\n';
226  for (size_t i = 0; i < indent; ++i) {
227  out << " ";
228  }
229  }
230 
231 public:
232  inline PassReprFull(
233  std::ostream &target,
234  const bool verbose = false, const bool compact = true
235  ): Pass<PASS_REPR>(target, verbose, compact) {}
236 
237  // virtual ~PassReprFull() {}
238 };
239 
240 template <class TX = void> // actually not a template
241 class PassReprJSON: public Pass<PASS_REPR> {
242 protected:
243  void putStrEscaped(const std::string &str) {
244  for (const char c: str) {
245  switch (c) {
246  // case '\0':
247  // never reach
248  case '\b':
249  out << "\\b";
250  break;
251  case '\t':
252  out << "\\t";
253  break;
254  case '\n':
255  out << "\\n";
256  break;
257  case '\v':
258  out << "\\v";
259  break;
260  case '\f':
261  out << "\\f";
262  break;
263  case '\r':
264  out << "\\r";
265  break;
266  case '\"':
267  out << "\\\"";
268  break;
269  case '\'':
270  out << "\\\'";
271  break;
272  case '\\':
273  out << "\\\\";
274  break;
275  case '\x7F':
276  out << "\\x7F";
277  break;
278  default:
279  if ('\0' <= c && c < '\x10') {
280  out << "\\x0" << ("0123456789ABCDEF"[c & 0xF]);
281  } else if ('\x10' <= c && c < '\x20') {
282  out << "\\x1" << ("0123456789ABCDEF"[c & 0xF]);
283  } else {
284  out << c;
285  }
286  break;
287  }
288  }
289  }
290 
291  virtual void putName(const std::string &name) {
292  putLn1();
293  out << "\"rulename\": \"" << name << "\",";
294  }
295 
296  virtual void putIndex(const size_t index) {
297  putLn1();
298  out << "\"ruleindex\": \"" << index << "\",";
299  }
300 
301  virtual void putText(const std::string &text) {
302  putLn1();
303  out << "\"text\": \"";
304  putStrEscaped(text);
305  out << "\",";
306  }
307 
308  virtual void putError(const std::string &error) {
309  putLn1();
310  out << "\"error\": \"";
311  putStrEscaped(error);
312  out << "\",";
313  }
314 
315  virtual void putMainBegin() {
316  out << "{";
317  ++indent; // extra
318  }
319 
320  virtual void putMainEnd() {
321  --indent; // extra
322  putLn1();
323  out << "}";
324  }
325 
326  virtual void putLnBegin() {
327  putLn1();
328  out << "\"children\": [";
329  --indent; // extra
330  }
331 
332  virtual void putLnEnd() {
333  ++indent; // extra
334  out << "],";
335  }
336 
337  virtual void putLn(const bool first) {
338  if (!first) {
339  out << ',';
340  putLn1();
341  }
342  }
343 
344  void putLn1() {
345  out << '\n';
346  for (size_t i = 0; i < indent; ++i) {
347  out << " ";
348  }
349  }
350 
351 public:
352  inline PassReprJSON(
353  std::ostream &target, const bool verbose = false
354  ): Pass<PASS_REPR>(target, verbose, false) {}
355 
356  // virtual ~PassReprJSON() {}
357 };
358 
359 }
360 
361 #endif
const std::vector< Node * > & getChildren() const
Definition: myparser_ast.hpp:131
virtual void putMainBegin()
Definition: myparser_pass_repr.hpp:315
virtual size_t getIndex() const =0
Definition: myparser_common.hpp:82
void run(const NodeList<> *node)
Definition: myparser_pass_repr.hpp:63
Definition: myparser_pass_repr.hpp:241
const bool optionV
Definition: myparser_pass_repr.hpp:11
void putStrEscaped(const std::string &str)
Definition: myparser_pass_repr.hpp:243
PassReprFull(std::ostream &target, const bool verbose=false, const bool compact=true)
Definition: myparser_pass_repr.hpp:232
Definition: myparser_pass_repr.hpp:198
Definition: myparser_pass_repr.hpp:150
Definition: myparser_ast.hpp:10
const bool optionC
Definition: myparser_pass_repr.hpp:12
const auto style_keyword
Definition: myparser_common.hpp:67
virtual void putPlaceHolder()
Definition: myparser_pass_repr.hpp:175
virtual void putName(const std::string &name)
Definition: myparser_pass_repr.hpp:291
const auto style_error
Definition: myparser_common.hpp:68
Definition: myparser_ast.hpp:183
virtual void putError(const std::string &error)
Definition: myparser_pass_repr.hpp:171
virtual bool accepted() const
Definition: myparser_ast.hpp:161
PassReprJSON(std::ostream &target, const bool verbose=false)
Definition: myparser_pass_repr.hpp:352
virtual void putMainEnd()
Definition: myparser_pass_repr.hpp:36
const auto style_normal
Definition: myparser_common.hpp:71
virtual void putBegin()
Definition: myparser_pass_repr.hpp:40
virtual void putLn(const bool first)
Definition: myparser_pass_repr.hpp:222
virtual void putLnBegin()
Definition: myparser_pass_repr.hpp:44
virtual void putError(const std::string &error)
Definition: myparser_pass_repr.hpp:308
virtual void putIndex(const size_t index)
Definition: myparser_pass_repr.hpp:22
Definition: myparser_pass.hpp:24
virtual void putName(const std::string &name)
Definition: myparser_pass_repr.hpp:200
Definition: myparser_common.hpp:92
Definition: myparser_ast.hpp:192
std::ostream & out
Definition: myparser_pass_repr.hpp:15
virtual void putMainEnd()
Definition: myparser_pass_repr.hpp:320
void run(const NodeTextOrError< E > *node)
Definition: myparser_pass_repr.hpp:119
virtual void putLn(const bool first)
Definition: myparser_pass_repr.hpp:48
const auto style_faint
Definition: myparser_common.hpp:70
const auto style_index
Definition: myparser_common.hpp:66
void putLn1()
Definition: myparser_pass_repr.hpp:344
PassReprSimple(std::ostream &target, const bool verbose=false, const bool compact=true)
Definition: myparser_pass_repr.hpp:189
PassReprText(std::ostream &target)
Definition: myparser_pass_repr.hpp:157
virtual void putText(const std::string &text)
Definition: myparser_pass_repr.hpp:152
virtual void putText(const std::string &text)
Definition: myparser_pass_repr.hpp:301
virtual void putIndex(const size_t index)
Definition: myparser_pass_repr.hpp:204
virtual void putText(const std::string &text)
Definition: myparser_pass_repr.hpp:210
virtual void putName(const std::string &name)
Definition: myparser_pass_repr.hpp:18
virtual void putPlaceHolder()
Definition: myparser_pass_repr.hpp:38
virtual void putText(const std::string &text)
Definition: myparser_pass_repr.hpp:167
virtual void putIndex(const size_t index)
Definition: myparser_pass_repr.hpp:296
Pass(std::ostream &target, const bool verbose, const bool compact)
Definition: myparser_pass_repr.hpp:53
virtual void putBegin()
Definition: myparser_pass_repr.hpp:218
virtual void putMainBegin()
Definition: myparser_pass_repr.hpp:34
virtual void putLnEnd()
Definition: myparser_pass_repr.hpp:46
virtual void putText(const std::string &text)
Definition: myparser_pass_repr.hpp:26
virtual void putLnBegin()
Definition: myparser_pass_repr.hpp:326
Definition: myparser_pass_repr.hpp:165
void run(const NodeText<> *node)
Definition: myparser_pass_repr.hpp:106
size_t indent
Definition: myparser_pass_repr.hpp:16
virtual void putLn(const bool first)
Definition: myparser_pass_repr.hpp:179
virtual void putError(const std::string &error)
Definition: myparser_pass_repr.hpp:214
const std::string & getText() const
Definition: myparser_ast.hpp:177
virtual void putEnd()
Definition: myparser_pass_repr.hpp:42
void run(const NodeError< E > *node)
Definition: myparser_pass_repr.hpp:136
virtual const std::string & getRuleName() const =0
virtual void putLnEnd()
Definition: myparser_pass_repr.hpp:332
virtual void putLn(const bool first)
Definition: myparser_pass_repr.hpp:337
virtual void putError(const std::string &error)
Definition: myparser_pass_repr.hpp:30