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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
/* Copyright (c) 2020 Jin Li, http://www.luvfight.me
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#pragma once
#include <string>
#include <unordered_set>
#include <stack>
#include <algorithm>
#include <vector>
#include <sstream>
#include <string_view>
using namespace std::string_view_literals;
#include "MoonP/ast.hpp"
#include "MoonP/moon_ast.h"
namespace MoonP {
struct ParseInfo {
ast_ptr<false,ast_node> node;
std::string error;
std::unique_ptr<input> codes;
std::string errorMessage(std::string_view msg, const input_range* loc) const;
};
template<typename T>
struct identity { typedef T type; };
#define AST_RULE(type) \
rule type; \
ast<type##_t> type##_impl = type; \
inline rule& getRule(identity<type##_t>) { return type; }
extern std::unordered_set<std::string> LuaKeywords;
extern std::unordered_set<std::string> Keywords;
class MoonParser {
public:
MoonParser();
template<class AST>
ParseInfo parse(const std::string& codes) {
error_list errors;
auto res = parse(codes, getRule<AST>());
if (res.node.template is<AST>()) {
return res;
}
return res;
}
template <class AST>
bool match(const std::string& codes) {
auto rEnd = rule(getRule<AST>() >> eof());
return parse(codes, rEnd).node;
}
std::string toString(ast_node* node);
std::string toString(input::iterator begin, input::iterator end);
input encode(std::string_view input);
std::string decode(const input& input);
protected:
ParseInfo parse(const std::string& codes, rule& r);
struct State {
State() {
indents.push(0);
stringOpen = -1;
}
std::string buffer;
size_t stringOpen;
std::stack<int> indents;
std::stack<bool> doStack;
};
template <class T>
inline rule& getRule() {
return getRule(identity<T>());
}
private:
Converter _converter;
template <class T>
inline rule& getRule(identity<T>) {
assert(false);
return Cut;
}
rule plain_space;
rule Break;
rule Any;
rule White;
rule Stop;
rule Comment;
rule multi_line_open;
rule multi_line_close;
rule multi_line_content;
rule MultiLineComment;
rule Indent;
rule EscapeNewLine;
rule Space;
rule SpaceBreak;
rule EmptyLine;
rule AlphaNum;
rule Cut;
rule check_indent;
rule CheckIndent;
rule advance;
rule Advance;
rule push_indent;
rule PushIndent;
rule PreventIndent;
rule PopIndent;
rule InBlock;
rule ImportName;
rule ImportNameList;
rule import_literal_chain;
rule WithExp;
rule PopDo;
rule DisableDo;
rule SwitchElse;
rule SwitchBlock;
rule IfElseIf;
rule IfElse;
rule for_args;
rule for_in;
rule CompClause;
rule Chain;
rule KeyValue;
rule single_string_inner;
rule interp;
rule double_string_plain;
rule lua_string_open;
rule lua_string_close;
rule FnArgsExpList;
rule FnArgs;
rule chain_call;
rule chain_item;
rule ChainItems;
rule chain_dot_chain;
rule ColonChain;
rule chain_with_colon;
rule ChainItem;
rule Index;
rule invoke_chain;
rule TableValue;
rule table_lit_lines;
rule TableLitLine;
rule TableValueList;
rule TableBlockInner;
rule ClassLine;
rule KeyValueLine;
rule KeyValueList;
rule ArgLine;
rule ArgBlock;
rule invoke_args_with_table;
rule minus_exp;
rule sharp_exp;
rule tilde_exp;
rule not_exp;
rule empty_line_stop;
rule Line;
rule Shebang;
AST_RULE(Num)
AST_RULE(Name)
AST_RULE(Variable)
AST_RULE(LuaKeyword)
AST_RULE(self)
AST_RULE(self_name)
AST_RULE(self_class)
AST_RULE(self_class_name)
AST_RULE(SelfName)
AST_RULE(KeyName)
AST_RULE(VarArg)
AST_RULE(local_flag)
AST_RULE(Seperator)
AST_RULE(NameList)
AST_RULE(Local)
AST_RULE(colon_import_name)
AST_RULE(import_literal_inner)
AST_RULE(ImportLiteral)
AST_RULE(ImportFrom)
AST_RULE(ImportAs)
AST_RULE(Import)
AST_RULE(Backcall)
AST_RULE(ExpListLow)
AST_RULE(ExpList)
AST_RULE(Return)
AST_RULE(With)
AST_RULE(SwitchCase)
AST_RULE(Switch)
AST_RULE(IfCond)
AST_RULE(If)
AST_RULE(Unless)
AST_RULE(While)
AST_RULE(for_step_value)
AST_RULE(For)
AST_RULE(ForEach)
AST_RULE(Do)
AST_RULE(Comprehension)
AST_RULE(comp_value)
AST_RULE(TblComprehension)
AST_RULE(star_exp)
AST_RULE(CompForEach)
AST_RULE(CompFor)
AST_RULE(CompInner)
AST_RULE(Assign)
AST_RULE(update_op)
AST_RULE(Update)
AST_RULE(BinaryOperator)
AST_RULE(BackcallOperator)
AST_RULE(Assignable)
AST_RULE(AssignableChain)
AST_RULE(exp_op_value)
AST_RULE(Exp)
AST_RULE(Callable)
AST_RULE(ChainValue)
AST_RULE(simple_table)
AST_RULE(SimpleValue)
AST_RULE(Value)
AST_RULE(LuaStringOpen);
AST_RULE(LuaStringContent);
AST_RULE(LuaStringClose);
AST_RULE(LuaString)
AST_RULE(SingleString)
AST_RULE(double_string_inner)
AST_RULE(double_string_content)
AST_RULE(DoubleString)
AST_RULE(String)
AST_RULE(Parens)
AST_RULE(DotChainItem)
AST_RULE(ColonChainItem)
AST_RULE(default_value)
AST_RULE(Slice)
AST_RULE(Invoke)
AST_RULE(existential_op)
AST_RULE(TableLit)
AST_RULE(TableBlock)
AST_RULE(class_member_list)
AST_RULE(ClassBlock)
AST_RULE(ClassDecl)
AST_RULE(export_values)
AST_RULE(export_op)
AST_RULE(Export)
AST_RULE(variable_pair)
AST_RULE(normal_pair)
AST_RULE(FnArgDef)
AST_RULE(FnArgDefList)
AST_RULE(outer_var_shadow)
AST_RULE(FnArgsDef)
AST_RULE(fn_arrow)
AST_RULE(FunLit)
AST_RULE(NameOrDestructure)
AST_RULE(AssignableNameList)
AST_RULE(InvokeArgs)
AST_RULE(const_value)
AST_RULE(unary_exp)
AST_RULE(ExpListAssign)
AST_RULE(if_else_line)
AST_RULE(unless_line)
AST_RULE(statement_appendix)
AST_RULE(BreakLoop)
AST_RULE(Statement)
AST_RULE(Body)
AST_RULE(Block)
AST_RULE(File)
};
namespace Utils {
void replace(std::string& str, std::string_view from, std::string_view to);
};
} // namespace MoonP
|