aboutsummaryrefslogtreecommitdiff
path: root/src/MoonP/parser.hpp
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2020-01-31 12:23:37 +0800
committerLi Jin <dragon-fly@qq.com>2020-01-31 12:23:37 +0800
commit57191ed45bb7bd0ffcd917f00df2e940c900fb3c (patch)
tree0b245e053030bb057113eb286348a3e4563bb20a /src/MoonP/parser.hpp
parent3f535edc133d7d6eb45ebf50627f3ee5deae1155 (diff)
downloadyuescript-57191ed45bb7bd0ffcd917f00df2e940c900fb3c.tar.gz
yuescript-57191ed45bb7bd0ffcd917f00df2e940c900fb3c.tar.bz2
yuescript-57191ed45bb7bd0ffcd917f00df2e940c900fb3c.zip
cleanup format.
Diffstat (limited to 'src/MoonP/parser.hpp')
-rw-r--r--src/MoonP/parser.hpp512
1 files changed, 256 insertions, 256 deletions
diff --git a/src/MoonP/parser.hpp b/src/MoonP/parser.hpp
index b419e8c..01d1050 100644
--- a/src/MoonP/parser.hpp
+++ b/src/MoonP/parser.hpp
@@ -1,12 +1,13 @@
1/* Copyright (c) 2012, Achilleas Margaritis, modified by Jin Li 1/* Copyright (c) 2012, Achilleas Margaritis, modified by Jin Li
2All rights reserved. 2All rights reserved.
3 3
4 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 4Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5 5
6 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 6Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8 7
9THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 8Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9
10THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
10 11
11#pragma once 12#pragma once
12 13
@@ -60,89 +61,89 @@ typedef std::function<bool(const item_t&)> user_handler;
60///position into the input. 61///position into the input.
61class pos { 62class pos {
62public: 63public:
63 ///interator into the input. 64 ///interator into the input.
64 input::iterator m_it; 65 input::iterator m_it;
65 66
66 ///line. 67 ///line.
67 int m_line; 68 int m_line;
68 69
69 ///column. 70 ///column.
70 int m_col; 71 int m_col;
71 72
72 ///null constructor. 73 ///null constructor.
73 pos():m_line(0),m_col(0) {} 74 pos():m_line(0),m_col(0) {}
74 75
75 /** constructor from input. 76 /** constructor from input.
76 @param i input. 77 @param i input.
77 */ 78 */
78 pos(input &i); 79 pos(input &i);
79}; 80};
80 81
81 82
82/** a grammar expression. 83/** a grammar expression.
83 */ 84*/
84class expr { 85class expr {
85public: 86public:
86 /** character terminal constructor. 87 /** character terminal constructor.
87 @param c character. 88 @param c character.
88 */ 89 */
89 expr(char c); 90 expr(char c);
90 91
91 /** null-terminated string terminal constructor. 92 /** null-terminated string terminal constructor.
92 @param s null-terminated string. 93 @param s null-terminated string.
93 */ 94 */
94 expr(const char *s); 95 expr(const char* s);
95 96
96 /** rule reference constructor. 97 /** rule reference constructor.
97 @param r rule. 98 @param r rule.
98 */ 99 */
99 expr(rule &r); 100 expr(rule& r);
100 101
101 /** creates a zero-or-more loop out of this expression. 102 /** creates a zero-or-more loop out of this expression.
102 @return a zero-or-more loop expression. 103 @return a zero-or-more loop expression.
103 */ 104 */
104 expr operator *() const; 105 expr operator*() const;
105 106
106 /** creates a one-or-more loop out of this expression. 107 /** creates a one-or-more loop out of this expression.
107 @return a one-or-more loop expression. 108 @return a one-or-more loop expression.
108 */ 109 */
109 expr operator +() const; 110 expr operator+() const;
110 111
111 /** creates an optional out of this expression. 112 /** creates an optional out of this expression.
112 @return an optional expression. 113 @return an optional expression.
113 */ 114 */
114 expr operator -() const; 115 expr operator-() const;
115 116
116 /** creates an AND-expression. 117 /** creates an AND-expression.
117 @return an AND-expression. 118 @return an AND-expression.
118 */ 119 */
119 expr operator &() const; 120 expr operator&() const;
120 121
121 /** creates a NOT-expression. 122 /** creates a NOT-expression.
122 @return a NOT-expression. 123 @return a NOT-expression.
123 */ 124 */
124 expr operator !() const; 125 expr operator!() const;
125 126
126private: 127private:
127 //internal expression 128 //internal expression
128 _expr *m_expr; 129 _expr* m_expr;
129 130
130 //internal constructor from internal expression 131 //internal constructor from internal expression
131 expr(_expr *e) : m_expr(e) {} 132 expr(_expr* e) : m_expr(e) {}
132 133
133 //assignment not allowed 134 //assignment not allowed
134 expr &operator = (expr &); 135 expr& operator=(expr&);
135 136
136 friend class _private; 137 friend class _private;
137}; 138};
138 139
139 140
140/** type of procedure to invoke when a rule is successfully parsed. 141/** type of procedure to invoke when a rule is successfully parsed.
141 @param b begin position of input. 142 @param b begin position of input.
142 @param e end position of input. 143 @param e end position of input.
143 @param d pointer to user data. 144 @param d pointer to user data.
144 */ 145*/
145typedef void (*parse_proc)(const pos &b, const pos &e, void *d); 146typedef void (*parse_proc)(const pos& b, const pos& e, void* d);
146 147
147 148
148///input range. 149///input range.
@@ -150,54 +151,54 @@ class input_range {
150public: 151public:
151 virtual ~input_range() {} 152 virtual ~input_range() {}
152 153
153 ///begin position. 154 ///begin position.
154 pos m_begin; 155 pos m_begin;
155 156
156 ///end position. 157 ///end position.
157 pos m_end; 158 pos m_end;
158 159
159 ///empty constructor. 160 ///empty constructor.
160 input_range() {} 161 input_range() {}
161 162
162 /** constructor. 163 /** constructor.
163 @param b begin position. 164 @param b begin position.
164 @param e end position. 165 @param e end position.
165 */ 166 */
166 input_range(const pos &b, const pos &e); 167 input_range(const pos& b, const pos& e);
167}; 168};
168 169
169 170
170///enum with error types. 171///enum with error types.
171enum ERROR_TYPE { 172enum ERROR_TYPE {
172 ///syntax error 173 ///syntax error
173 ERROR_SYNTAX_ERROR = 1, 174 ERROR_SYNTAX_ERROR = 1,
174 175
175 ///invalid end of file 176 ///invalid end of file
176 ERROR_INVALID_EOF, 177 ERROR_INVALID_EOF,
177 178
178 ///first user error 179 ///first user error
179 ERROR_USER = 100 180 ERROR_USER = 100
180}; 181};
181 182
182 183
183///error. 184///error.
184class error : public input_range { 185class error : public input_range {
185public: 186public:
186 ///type 187 ///type
187 int m_type; 188 int m_type;
188 189
189 /** constructor. 190 /** constructor.
190 @param b begin position. 191 @param b begin position.
191 @param e end position. 192 @param e end position.
192 @param t type. 193 @param t type.
193 */ 194 */
194 error(const pos &b, const pos &e, int t); 195 error(const pos& b, const pos& e, int t);
195 196
196 /** compare on begin position. 197 /** compare on begin position.
197 @param e the other error to compare this with. 198 @param e the other error to compare this with.
198 @return true if this comes before the previous error, false otherwise. 199 @return true if this comes before the previous error, false otherwise.
199 */ 200 */
200 bool operator < (const error &e) const; 201 bool operator<(const error& e) const;
201}; 202};
202 203
203 204
@@ -206,189 +207,189 @@ typedef std::list<error> error_list;
206 207
207 208
208/** represents a rule. 209/** represents a rule.
209 */ 210*/
210class rule { 211class rule {
211public: 212public:
212 /** character terminal constructor. 213 /** character terminal constructor.
213 @param c character. 214 @param c character.
214 */ 215 */
215 rule(); 216 rule();
216 rule(char c); 217 rule(char c);
217 218
218 /** null-terminated string terminal constructor. 219 /** null-terminated string terminal constructor.
219 @param s null-terminated string. 220 @param s null-terminated string.
220 */ 221 */
221 rule(const char *s); 222 rule(const char* s);
222 223
223 /** constructor from expression. 224 /** constructor from expression.
224 @param e expression. 225 @param e expression.
225 */ 226 */
226 rule(const expr &e); 227 rule(const expr& e);
227 228
228 /** constructor from rule. 229 /** constructor from rule.
229 @param r rule. 230 @param r rule.
230 */ 231 */
231 rule(rule &r); 232 rule(rule& r);
232 233
233 /** invalid constructor from rule (required by gcc). 234 /** invalid constructor from rule (required by gcc).
234 @param r rule. 235 @param r rule.
235 @exception std::logic_error always thrown. 236 @exception std::logic_error always thrown.
236 */ 237 */
237 rule(const rule &r); 238 rule(const rule& r);
238 239
239 /** deletes the internal object that represents the expression. 240 /** deletes the internal object that represents the expression.
240 */ 241 */
241 ~rule(); 242 ~rule();
242 243
243 /** creates a zero-or-more loop out of this rule. 244 /** creates a zero-or-more loop out of this rule.
244 @return a zero-or-more loop rule. 245 @return a zero-or-more loop rule.
245 */ 246 */
246 expr operator *(); 247 expr operator*();
247 248
248 /** creates a one-or-more loop out of this rule. 249 /** creates a one-or-more loop out of this rule.
249 @return a one-or-more loop rule. 250 @return a one-or-more loop rule.
250 */ 251 */
251 expr operator +(); 252 expr operator+();
252 253
253 /** creates an optional out of this rule. 254 /** creates an optional out of this rule.
254 @return an optional rule. 255 @return an optional rule.
255 */ 256 */
256 expr operator -(); 257 expr operator-();
257 258
258 /** creates an AND-expression out of this rule. 259 /** creates an AND-expression out of this rule.
259 @return an AND-expression out of this rule. 260 @return an AND-expression out of this rule.
260 */ 261 */
261 expr operator &(); 262 expr operator&();
262 263
263 /** creates a NOT-expression out of this rule. 264 /** creates a NOT-expression out of this rule.
264 @return a NOT-expression out of this rule. 265 @return a NOT-expression out of this rule.
265 */ 266 */
266 expr operator !(); 267 expr operator!();
267 268
268 /** sets the parse procedure. 269 /** sets the parse procedure.
269 @param p procedure. 270 @param p procedure.
270 */ 271 */
271 void set_parse_proc(parse_proc p); 272 void set_parse_proc(parse_proc p);
272 273
273 /** get the this ptr (since operator & is overloaded). 274 /** get the this ptr (since operator & is overloaded).
274 @return pointer to this. 275 @return pointer to this.
275 */ 276 */
276 rule *this_ptr() { return this; } 277 rule* this_ptr() { return this; }
277 278
278 rule &operator = (rule &); 279 rule& operator=(rule&);
279 280
280 rule &operator = (const expr &); 281 rule& operator=(const expr&);
281 282
282private: 283private:
283 //mode 284 //mode
284 enum _MODE { 285 enum _MODE {
285 _PARSE, 286 _PARSE,
286 _REJECT, 287 _REJECT,
287 _ACCEPT 288 _ACCEPT
288 }; 289 };
289 290
290 //state 291 //state
291 struct _state { 292 struct _state {
292 //position in source code, relative to start 293 //position in source code, relative to start
293 size_t m_pos; 294 size_t m_pos;
294 295
295 //mode 296 //mode
296 _MODE m_mode; 297 _MODE m_mode;
297 298
298 //constructor 299 //constructor
299 _state(size_t pos = -1, _MODE mode = _PARSE) : 300 _state(size_t pos = -1, _MODE mode = _PARSE) :
300 m_pos(pos), m_mode(mode) {} 301 m_pos(pos), m_mode(mode) {}
301 }; 302 };
302 303
303 //internal expression 304 //internal expression
304 _expr *m_expr; 305 _expr* m_expr;
305 306
306 //associated parse procedure. 307 //associated parse procedure.
307 parse_proc m_parse_proc; 308 parse_proc m_parse_proc;
308 309
309 //state 310 //state
310 _state m_state; 311 _state m_state;
311 312
312 friend class _private; 313 friend class _private;
313 friend class _context; 314 friend class _context;
314}; 315};
315 316
316 317
317/** creates a sequence of expressions. 318/** creates a sequence of expressions.
318 @param left left operand. 319 @param left left operand.
319 @param right right operand. 320 @param right right operand.
320 @return an expression which parses a sequence. 321 @return an expression which parses a sequence.
321 */ 322*/
322expr operator >> (const expr &left, const expr &right); 323expr operator>>(const expr& left, const expr& right);
323 324
324 325
325/** creates a choice of expressions. 326/** creates a choice of expressions.
326 @param left left operand. 327 @param left left operand.
327 @param right right operand. 328 @param right right operand.
328 @return an expression which parses a choice. 329 @return an expression which parses a choice.
329 */ 330*/
330expr operator | (const expr &left, const expr &right); 331expr operator|(const expr& left, const expr& right);
331 332
332 333
333/** converts a parser expression into a terminal. 334/** converts a parser expression into a terminal.
334 @param e expression. 335 @param e expression.
335 @return an expression which parses a terminal. 336 @return an expression which parses a terminal.
336 */ 337*/
337expr term(const expr &e); 338expr term(const expr& e);
338 339
339 340
340/** creates a set expression from a null-terminated string. 341/** creates a set expression from a null-terminated string.
341 @param s null-terminated string with characters of the set. 342 @param s null-terminated string with characters of the set.
342 @return an expression which parses a single character out of a set. 343 @return an expression which parses a single character out of a set.
343 */ 344*/
344expr set(const char *s); 345expr set(const char* s);
345 346
346 347
347/** creates a range expression. 348/** creates a range expression.
348 @param min min character. 349 @param min min character.
349 @param max max character. 350 @param max max character.
350 @return an expression which parses a single character out of range. 351 @return an expression which parses a single character out of range.
351 */ 352*/
352expr range(int min, int max); 353expr range(int min, int max);
353 354
354 355
355/** creates an expression which increments the line counter 356/** creates an expression which increments the line counter
356 and resets the column counter when the given expression 357 and resets the column counter when the given expression
357 is parsed successfully; used for newline characters. 358 is parsed successfully; used for newline characters.
358 @param e expression to wrap into a newline parser. 359 @param e expression to wrap into a newline parser.
359 @return an expression that handles newlines. 360 @return an expression that handles newlines.
360 */ 361*/
361expr nl(const expr &e); 362expr nl(const expr& e);
362 363
363 364
364/** creates an expression which tests for the end of input. 365/** creates an expression which tests for the end of input.
365 @return an expression that handles the end of input. 366 @return an expression that handles the end of input.
366 */ 367*/
367expr eof(); 368expr eof();
368 369
369 370
370/** creates a not expression. 371/** creates a not expression.
371 @param e expression. 372 @param e expression.
372 @return the appropriate expression. 373 @return the appropriate expression.
373 */ 374*/
374expr not_(const expr &e); 375expr not_(const expr& e);
375 376
376 377
377/** creates an and expression. 378/** creates an and expression.
378 @param e expression. 379 @param e expression.
379 @return the appropriate expression. 380 @return the appropriate expression.
380 */ 381*/
381expr and_(const expr &e); 382expr and_(const expr& e);
382 383
383 384
384/** creates an expression that parses any character. 385/** creates an expression that parses any character.
385 @return the appropriate expression. 386 @return the appropriate expression.
386 */ 387*/
387expr any(); 388expr any();
388 389
389 390
390/** parsing succeeds without consuming any input. 391/** parsing succeeds without consuming any input.
391 */ 392*/
392expr true_(); 393expr true_();
393 394
394 395
@@ -399,34 +400,33 @@ expr false_();
399 400
400/** parse with target expression and let user handle result. 401/** parse with target expression and let user handle result.
401*/ 402*/
402expr user(const expr &e, const user_handler& handler); 403expr user(const expr& e, const user_handler& handler);
403 404
404 405
405/** parses the given input. 406/** parses the given input.
406 The parse procedures of each rule parsed are executed 407 The parse procedures of each rule parsed are executed
407 before this function returns, if parsing succeeds. 408 before this function returns, if parsing succeeds.
408 @param i input. 409 @param i input.
409 @param g root rule of grammar. 410 @param g root rule of grammar.
410 @param el list of errors. 411 @param el list of errors.
411 @param d user data, passed to the parse procedures. 412 @param d user data, passed to the parse procedures.
412 @return true on parsing success, false on failure. 413 @return true on parsing success, false on failure.
413 */ 414*/
414bool parse(input &i, rule &g, error_list &el, void *d, void* ud); 415bool parse(input& i, rule& g, error_list& el, void* d, void* ud);
415 416
416 417
417/** output the specific input range to the specific stream. 418/** output the specific input range to the specific stream.
418 @param stream stream. 419 @param stream stream.
419 @param ir input range. 420 @param ir input range.
420 @return the stream. 421 @return the stream.
421 */ 422*/
422template <class T> T &operator << (T &stream, const input_range &ir) { 423template <class T> T& operator<<(T& stream, const input_range& ir) {
423 for(input::const_iterator it = ir.m_begin.m_it; 424 for(input::const_iterator it = ir.m_begin.m_it;
424 it != ir.m_end.m_it; 425 it != ir.m_end.m_it;
425 ++it) 426 ++it) {
426 { 427 stream << (typename T::char_type)*it;
427 stream << (typename T::char_type)*it; 428 }
428 } 429 return stream;
429 return stream;
430} 430}
431 431
432 432