aboutsummaryrefslogtreecommitdiff
path: root/src/MoonP/parser.hpp
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2021-02-17 11:22:07 +0800
committerLi Jin <dragon-fly@qq.com>2021-02-17 11:22:07 +0800
commit7066392d1c974065181d95d93274136dcd625d43 (patch)
treecf51eafc2c52cbc12246a306bca172d799193d30 /src/MoonP/parser.hpp
parent90cd12ad9ef465f3e435e1bd034dcfbe4e19d016 (diff)
downloadyuescript-7066392d1c974065181d95d93274136dcd625d43.tar.gz
yuescript-7066392d1c974065181d95d93274136dcd625d43.tar.bz2
yuescript-7066392d1c974065181d95d93274136dcd625d43.zip
stop reusing variables, rename project.
Diffstat (limited to 'src/MoonP/parser.hpp')
-rw-r--r--src/MoonP/parser.hpp422
1 files changed, 0 insertions, 422 deletions
diff --git a/src/MoonP/parser.hpp b/src/MoonP/parser.hpp
deleted file mode 100644
index f70475f..0000000
--- a/src/MoonP/parser.hpp
+++ /dev/null
@@ -1,422 +0,0 @@
1/* Copyright (c) 2012, Achilleas Margaritis, modified by Jin Li
2All rights reserved.
3
4Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
6Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
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.*/
11
12#pragma once
13
14
15//gcc chokes without rule::rule(const rule &),
16//msvc complains when rule::rule(const rule &) is defined.
17#ifdef _MSC_VER
18#pragma warning (disable: 4521)
19#endif
20
21
22#include <vector>
23#include <string>
24#include <list>
25#include <functional>
26#include <codecvt>
27#include <locale>
28
29namespace parserlib {
30
31///type of the parser's input.
32typedef std::basic_string<wchar_t> input;
33typedef input::iterator input_it;
34typedef std::wstring_convert<std::codecvt_utf8_utf16<input::value_type>> Converter;
35
36class _private;
37class _expr;
38class _context;
39class rule;
40
41
42struct item_t {
43 input_it begin;
44 input_it end;
45 void* user_data;
46};
47typedef std::function<bool(const item_t&)> user_handler;
48
49
50///position into the input.
51class pos {
52public:
53 ///interator into the input.
54 input::iterator m_it;
55
56 ///line.
57 int m_line;
58
59 ///column.
60 int m_col;
61
62 ///null constructor.
63 pos():m_line(0),m_col(0) {}
64
65 /** constructor from input.
66 @param i input.
67 */
68 pos(input &i);
69};
70
71
72/** a grammar expression.
73*/
74class expr {
75public:
76 /** character terminal constructor.
77 @param c character.
78 */
79 expr(char c);
80
81 /** null-terminated string terminal constructor.
82 @param s null-terminated string.
83 */
84 expr(const char* s);
85
86 /** rule reference constructor.
87 @param r rule.
88 */
89 expr(rule& r);
90
91 /** creates a zero-or-more loop out of this expression.
92 @return a zero-or-more loop expression.
93 */
94 expr operator*() const;
95
96 /** creates a one-or-more loop out of this expression.
97 @return a one-or-more loop expression.
98 */
99 expr operator+() const;
100
101 /** creates an optional out of this expression.
102 @return an optional expression.
103 */
104 expr operator-() const;
105
106 /** creates an AND-expression.
107 @return an AND-expression.
108 */
109 expr operator&() const;
110
111 /** creates a NOT-expression.
112 @return a NOT-expression.
113 */
114 expr operator!() const;
115
116private:
117 //internal expression
118 _expr* m_expr;
119
120 //internal constructor from internal expression
121 expr(_expr* e) : m_expr(e) {}
122
123 //assignment not allowed
124 expr& operator=(expr&);
125
126 friend class _private;
127};
128
129
130/** type of procedure to invoke when a rule is successfully parsed.
131 @param b begin position of input.
132 @param e end position of input.
133 @param d pointer to user data.
134*/
135typedef void (*parse_proc)(const pos& b, const pos& e, void* d);
136
137
138///input range.
139class input_range {
140public:
141 virtual ~input_range() {}
142
143 ///begin position.
144 pos m_begin;
145
146 ///end position.
147 pos m_end;
148
149 ///empty constructor.
150 input_range() {}
151
152 /** constructor.
153 @param b begin position.
154 @param e end position.
155 */
156 input_range(const pos& b, const pos& e);
157};
158
159
160///enum with error types.
161enum ERROR_TYPE {
162 ///syntax error
163 ERROR_SYNTAX_ERROR = 1,
164
165 ///invalid end of file
166 ERROR_INVALID_EOF,
167
168 ///first user error
169 ERROR_USER = 100
170};
171
172
173///error.
174class error : public input_range {
175public:
176 ///type
177 int m_type;
178
179 /** constructor.
180 @param b begin position.
181 @param e end position.
182 @param t type.
183 */
184 error(const pos& b, const pos& e, int t);
185
186 /** compare on begin position.
187 @param e the other error to compare this with.
188 @return true if this comes before the previous error, false otherwise.
189 */
190 bool operator<(const error& e) const;
191};
192
193
194///type of error list.
195typedef std::list<error> error_list;
196
197
198/** represents a rule.
199*/
200class rule {
201public:
202 /** character terminal constructor.
203 @param c character.
204 */
205 rule();
206 rule(char c);
207
208 /** null-terminated string terminal constructor.
209 @param s null-terminated string.
210 */
211 rule(const char* s);
212
213 /** constructor from expression.
214 @param e expression.
215 */
216 rule(const expr& e);
217
218 /** constructor from rule.
219 @param r rule.
220 */
221 rule(rule& r);
222
223 /** invalid constructor from rule (required by gcc).
224 @param r rule.
225 @exception std::logic_error always thrown.
226 */
227 rule(const rule& r);
228
229 /** deletes the internal object that represents the expression.
230 */
231 ~rule();
232
233 /** creates a zero-or-more loop out of this rule.
234 @return a zero-or-more loop rule.
235 */
236 expr operator*();
237
238 /** creates a one-or-more loop out of this rule.
239 @return a one-or-more loop rule.
240 */
241 expr operator+();
242
243 /** creates an optional out of this rule.
244 @return an optional rule.
245 */
246 expr operator-();
247
248 /** creates an AND-expression out of this rule.
249 @return an AND-expression out of this rule.
250 */
251 expr operator&();
252
253 /** creates a NOT-expression out of this rule.
254 @return a NOT-expression out of this rule.
255 */
256 expr operator!();
257
258 /** sets the parse procedure.
259 @param p procedure.
260 */
261 void set_parse_proc(parse_proc p);
262
263 /** get the this ptr (since operator & is overloaded).
264 @return pointer to this.
265 */
266 rule* this_ptr() { return this; }
267
268 rule& operator=(rule&);
269
270 rule& operator=(const expr&);
271
272private:
273 //mode
274 enum _MODE {
275 _PARSE,
276 _REJECT,
277 _ACCEPT
278 };
279
280 //state
281 struct _state {
282 //position in source code, relative to start
283 size_t m_pos;
284
285 //mode
286 _MODE m_mode;
287
288 //constructor
289 _state(size_t pos = -1, _MODE mode = _PARSE) :
290 m_pos(pos), m_mode(mode) {}
291 };
292
293 //internal expression
294 _expr* m_expr;
295
296 //associated parse procedure.
297 parse_proc m_parse_proc;
298
299 //state
300 _state m_state;
301
302 friend class _private;
303 friend class _context;
304};
305
306
307/** creates a sequence of expressions.
308 @param left left operand.
309 @param right right operand.
310 @return an expression which parses a sequence.
311*/
312expr operator>>(const expr& left, const expr& right);
313
314
315/** creates a choice of expressions.
316 @param left left operand.
317 @param right right operand.
318 @return an expression which parses a choice.
319*/
320expr operator|(const expr& left, const expr& right);
321
322
323/** converts a parser expression into a terminal.
324 @param e expression.
325 @return an expression which parses a terminal.
326*/
327expr term(const expr& e);
328
329
330/** creates a set expression from a null-terminated string.
331 @param s null-terminated string with characters of the set.
332 @return an expression which parses a single character out of a set.
333*/
334expr set(const char* s);
335
336
337/** creates a range expression.
338 @param min min character.
339 @param max max character.
340 @return an expression which parses a single character out of range.
341*/
342expr range(int min, int max);
343
344
345/** creates an expression which increments the line counter
346 and resets the column counter when the given expression
347 is parsed successfully; used for newline characters.
348 @param e expression to wrap into a newline parser.
349 @return an expression that handles newlines.
350*/
351expr nl(const expr& e);
352
353
354/** creates an expression which tests for the end of input.
355 @return an expression that handles the end of input.
356*/
357expr eof();
358
359
360/** creates a not expression.
361 @param e expression.
362 @return the appropriate expression.
363*/
364expr not_(const expr& e);
365
366
367/** creates an and expression.
368 @param e expression.
369 @return the appropriate expression.
370*/
371expr and_(const expr& e);
372
373
374/** creates an expression that parses any character.
375 @return the appropriate expression.
376*/
377expr any();
378
379
380/** parsing succeeds without consuming any input.
381*/
382expr true_();
383
384
385/** parsing fails without consuming any input.
386*/
387expr false_();
388
389
390/** parse with target expression and let user handle result.
391*/
392expr user(const expr& e, const user_handler& handler);
393
394
395/** parses the given input.
396 The parse procedures of each rule parsed are executed
397 before this function returns, if parsing succeeds.
398 @param i input.
399 @param g root rule of grammar.
400 @param el list of errors.
401 @param d user data, passed to the parse procedures.
402 @return true on parsing success, false on failure.
403*/
404bool parse(input& i, rule& g, error_list& el, void* d, void* ud);
405
406
407/** output the specific input range to the specific stream.
408 @param stream stream.
409 @param ir input range.
410 @return the stream.
411*/
412template <class T> T& operator<<(T& stream, const input_range& ir) {
413 for(input::const_iterator it = ir.m_begin.m_it;
414 it != ir.m_end.m_it;
415 ++it) {
416 stream << (typename T::char_type)*it;
417 }
418 return stream;
419}
420
421
422} //namespace parserlib