aboutsummaryrefslogtreecommitdiff
path: root/src/MoonP/moon_parser.h
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/moon_parser.h
parent90cd12ad9ef465f3e435e1bd034dcfbe4e19d016 (diff)
downloadyuescript-7066392d1c974065181d95d93274136dcd625d43.tar.gz
yuescript-7066392d1c974065181d95d93274136dcd625d43.tar.bz2
yuescript-7066392d1c974065181d95d93274136dcd625d43.zip
stop reusing variables, rename project.
Diffstat (limited to 'src/MoonP/moon_parser.h')
-rw-r--r--src/MoonP/moon_parser.h317
1 files changed, 0 insertions, 317 deletions
diff --git a/src/MoonP/moon_parser.h b/src/MoonP/moon_parser.h
deleted file mode 100644
index b165070..0000000
--- a/src/MoonP/moon_parser.h
+++ /dev/null
@@ -1,317 +0,0 @@
1/* Copyright (c) 2021 Jin Li, http://www.luvfight.me
2
3Permission 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:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE 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. */
8
9#pragma once
10
11#include <string>
12#include <unordered_set>
13#include <stack>
14#include <algorithm>
15#include <list>
16#include <sstream>
17#include <string_view>
18
19#include "MoonP/ast.hpp"
20#include "MoonP/moon_ast.h"
21
22namespace MoonP {
23using namespace parserlib;
24
25struct ParseInfo {
26 ast_ptr<false, ast_node> node;
27 std::string error;
28 std::unique_ptr<input> codes;
29 bool exportDefault = false;
30 std::string moduleName;
31 std::string errorMessage(std::string_view msg, const input_range* loc) const;
32};
33
34template<typename T>
35struct identity { typedef T type; };
36
37#define AST_RULE(type) \
38 rule type; \
39 ast<type##_t> type##_impl = type; \
40 inline rule& getRule(identity<type##_t>) { return type; }
41
42extern std::unordered_set<std::string> LuaKeywords;
43extern std::unordered_set<std::string> Keywords;
44
45class MoonParser {
46public:
47 MoonParser();
48
49 template<class AST>
50 ParseInfo parse(std::string_view codes) {
51 return parse(codes, getRule<AST>());
52 }
53
54 template <class AST>
55 bool match(std::string_view codes) {
56 auto rEnd = rule(getRule<AST>() >> eof());
57 return parse(codes, rEnd).node;
58 }
59
60 std::string toString(ast_node* node);
61 std::string toString(input::iterator begin, input::iterator end);
62
63 input encode(std::string_view input);
64 std::string decode(const input& input);
65
66protected:
67 ParseInfo parse(std::string_view codes, rule& r);
68
69 struct State {
70 State() {
71 indents.push(0);
72 }
73 bool exportDefault = false;
74 int exportCount = 0;
75 int moduleFix = 0;
76 size_t stringOpen = 0;
77 std::string moduleName = "_module_0";
78 std::string buffer;
79 std::stack<int> indents;
80 std::stack<bool> doStack;
81 std::stack<bool> chainBlockStack;
82 };
83
84 template <class T>
85 inline rule& getRule() {
86 return getRule(identity<T>());
87 }
88
89private:
90 Converter _converter;
91
92 template <class T>
93 inline rule& getRule(identity<T>) {
94 assert(false);
95 return Cut;
96 }
97
98 rule plain_space;
99 rule Break;
100 rule Any;
101 rule White;
102 rule Stop;
103 rule Comment;
104 rule multi_line_open;
105 rule multi_line_close;
106 rule multi_line_content;
107 rule MultiLineComment;
108 rule Indent;
109 rule EscapeNewLine;
110 rule space_one;
111 rule Space;
112 rule SpaceBreak;
113 rule EmptyLine;
114 rule AlphaNum;
115 rule Cut;
116 rule check_indent;
117 rule CheckIndent;
118 rule advance;
119 rule Advance;
120 rule push_indent;
121 rule PushIndent;
122 rule PreventIndent;
123 rule PopIndent;
124 rule InBlock;
125 rule ImportName;
126 rule ImportNameList;
127 rule import_literal_chain;
128 rule ImportTabItem;
129 rule ImportTabList;
130 rule ImportTabLine;
131 rule import_tab_lines;
132 rule WithExp;
133 rule DisableDo;
134 rule EnableDo;
135 rule DisableChain;
136 rule EnableChain;
137 rule DisableDoChain;
138 rule EnableDoChain;
139 rule SwitchElse;
140 rule SwitchBlock;
141 rule IfElseIf;
142 rule IfElse;
143 rule for_args;
144 rule for_in;
145 rule CompClause;
146 rule Chain;
147 rule KeyValue;
148 rule single_string_inner;
149 rule interp;
150 rule double_string_plain;
151 rule lua_string_open;
152 rule lua_string_close;
153 rule FnArgsExpList;
154 rule FnArgs;
155 rule macro_args_def;
156 rule chain_call;
157 rule chain_item;
158 rule ChainItems;
159 rule chain_dot_chain;
160 rule ColonChain;
161 rule chain_with_colon;
162 rule ChainItem;
163 rule chain_line;
164 rule chain_block;
165 rule Index;
166 rule invoke_chain;
167 rule TableValue;
168 rule table_lit_lines;
169 rule TableLitLine;
170 rule TableValueList;
171 rule TableBlockInner;
172 rule ClassLine;
173 rule KeyValueLine;
174 rule KeyValueList;
175 rule ArgLine;
176 rule ArgBlock;
177 rule invoke_args_with_table;
178 rule BackcallOperator;
179 rule ExponentialOperator;
180 rule backcall_value;
181 rule backcall_exp;
182 rule expo_value;
183 rule expo_exp;
184 rule empty_line_stop;
185 rule Line;
186 rule Shebang;
187
188 AST_RULE(Num)
189 AST_RULE(Name)
190 AST_RULE(Variable)
191 AST_RULE(LabelName)
192 AST_RULE(LuaKeyword)
193 AST_RULE(self)
194 AST_RULE(self_name)
195 AST_RULE(self_class)
196 AST_RULE(self_class_name)
197 AST_RULE(SelfName)
198 AST_RULE(KeyName)
199 AST_RULE(VarArg)
200 AST_RULE(Seperator)
201 AST_RULE(NameList)
202 AST_RULE(local_flag)
203 AST_RULE(local_values)
204 AST_RULE(Local)
205 AST_RULE(LocalAttrib);
206 AST_RULE(colon_import_name)
207 AST_RULE(import_literal_inner)
208 AST_RULE(ImportLiteral)
209 AST_RULE(ImportFrom)
210 AST_RULE(macro_name_pair)
211 AST_RULE(import_all_macro)
212 AST_RULE(ImportTabLit)
213 AST_RULE(ImportAs)
214 AST_RULE(Import)
215 AST_RULE(Label)
216 AST_RULE(Goto)
217 AST_RULE(fn_arrow_back)
218 AST_RULE(Backcall)
219 AST_RULE(BackcallBody)
220 AST_RULE(ExpListLow)
221 AST_RULE(ExpList)
222 AST_RULE(Return)
223 AST_RULE(With)
224 AST_RULE(SwitchCase)
225 AST_RULE(Switch)
226 AST_RULE(IfCond)
227 AST_RULE(If)
228 AST_RULE(Unless)
229 AST_RULE(While)
230 AST_RULE(Repeat)
231 AST_RULE(for_step_value)
232 AST_RULE(For)
233 AST_RULE(ForEach)
234 AST_RULE(Do)
235 AST_RULE(Comprehension)
236 AST_RULE(comp_value)
237 AST_RULE(TblComprehension)
238 AST_RULE(star_exp)
239 AST_RULE(CompForEach)
240 AST_RULE(CompFor)
241 AST_RULE(CompInner)
242 AST_RULE(Assign)
243 AST_RULE(update_op)
244 AST_RULE(Update)
245 AST_RULE(BinaryOperator)
246 AST_RULE(unary_operator)
247 AST_RULE(Assignable)
248 AST_RULE(AssignableChain)
249 AST_RULE(exp_op_value)
250 AST_RULE(Exp)
251 AST_RULE(Callable)
252 AST_RULE(ChainValue)
253 AST_RULE(simple_table)
254 AST_RULE(SimpleValue)
255 AST_RULE(Value)
256 AST_RULE(LuaStringOpen);
257 AST_RULE(LuaStringContent);
258 AST_RULE(LuaStringClose);
259 AST_RULE(LuaString)
260 AST_RULE(SingleString)
261 AST_RULE(double_string_inner)
262 AST_RULE(double_string_content)
263 AST_RULE(DoubleString)
264 AST_RULE(String)
265 AST_RULE(Parens)
266 AST_RULE(DotChainItem)
267 AST_RULE(ColonChainItem)
268 AST_RULE(default_value)
269 AST_RULE(Slice)
270 AST_RULE(Invoke)
271 AST_RULE(existential_op)
272 AST_RULE(TableLit)
273 AST_RULE(TableBlock)
274 AST_RULE(TableBlockIndent)
275 AST_RULE(class_member_list)
276 AST_RULE(ClassBlock)
277 AST_RULE(ClassDecl)
278 AST_RULE(global_values)
279 AST_RULE(global_op)
280 AST_RULE(Global)
281 AST_RULE(export_default)
282 AST_RULE(Export)
283 AST_RULE(variable_pair)
284 AST_RULE(normal_pair)
285 AST_RULE(FnArgDef)
286 AST_RULE(FnArgDefList)
287 AST_RULE(outer_var_shadow)
288 AST_RULE(FnArgsDef)
289 AST_RULE(fn_arrow)
290 AST_RULE(FunLit)
291 AST_RULE(MacroName)
292 AST_RULE(MacroLit)
293 AST_RULE(Macro)
294 AST_RULE(NameOrDestructure)
295 AST_RULE(AssignableNameList)
296 AST_RULE(InvokeArgs)
297 AST_RULE(const_value)
298 AST_RULE(unary_value)
299 AST_RULE(unary_exp)
300 AST_RULE(ExpListAssign)
301 AST_RULE(if_line)
302 AST_RULE(unless_line)
303 AST_RULE(BreakLoop)
304 AST_RULE(statement_appendix)
305 AST_RULE(statement_sep)
306 AST_RULE(Statement)
307 AST_RULE(Body)
308 AST_RULE(Block)
309 AST_RULE(File)
310};
311
312namespace Utils {
313 void replace(std::string& str, std::string_view from, std::string_view to);
314 void trim(std::string& str);
315};
316
317} // namespace MoonP