diff options
| author | Li Jin <dragon-fly@qq.com> | 2020-01-30 16:06:32 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2020-01-30 16:06:32 +0800 |
| commit | c62d9eb35a310e7663234526ce4b9fe3519ca7cf (patch) | |
| tree | 56b4b418aadab90989e3e29e29e8ab7e04806e8d /src/MoonP/moon_parser.h | |
| parent | 240b1ec4a49128c00a787e6d0928865995b1d02e (diff) | |
| download | yuescript-c62d9eb35a310e7663234526ce4b9fe3519ca7cf.tar.gz yuescript-c62d9eb35a310e7663234526ce4b9fe3519ca7cf.tar.bz2 yuescript-c62d9eb35a310e7663234526ce4b9fe3519ca7cf.zip | |
Separate MoonParser from MoonCompiler, make moonc compile multiple file in threads.
Diffstat (limited to 'src/MoonP/moon_parser.h')
| -rw-r--r-- | src/MoonP/moon_parser.h | 263 |
1 files changed, 251 insertions, 12 deletions
diff --git a/src/MoonP/moon_parser.h b/src/MoonP/moon_parser.h index fc4ee55..933aa7a 100644 --- a/src/MoonP/moon_parser.h +++ b/src/MoonP/moon_parser.h | |||
| @@ -9,27 +9,266 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI | |||
| 9 | #pragma once | 9 | #pragma once |
| 10 | 10 | ||
| 11 | #include <string> | 11 | #include <string> |
| 12 | #include <codecvt> | ||
| 13 | #include <unordered_set> | 12 | #include <unordered_set> |
| 14 | #include <stack> | 13 | #include <stack> |
| 15 | #include <algorithm> | 14 | #include <algorithm> |
| 16 | #include <vector> | 15 | #include <vector> |
| 16 | #include <sstream> | ||
| 17 | #include <string_view> | ||
| 18 | using namespace std::string_view_literals; | ||
| 17 | #include "MoonP/ast.hpp" | 19 | #include "MoonP/ast.hpp" |
| 18 | using namespace parserlib; | 20 | #include "MoonP/moon_ast.h" |
| 19 | 21 | ||
| 20 | namespace MoonP { | 22 | namespace MoonP { |
| 21 | 23 | ||
| 22 | struct State { | 24 | struct ParseInfo { |
| 23 | State() { | 25 | ast_ptr<false,ast_node> node; |
| 24 | indents.push(0); | 26 | std::string error; |
| 25 | stringOpen = -1; | 27 | std::unique_ptr<input> input; |
| 28 | std::string errorMessage(std::string_view msg, const input_range* loc) const; | ||
| 29 | }; | ||
| 30 | |||
| 31 | #define AST_RULE(type) \ | ||
| 32 | rule type; \ | ||
| 33 | ast<type##_t> type##_impl = type; \ | ||
| 34 | template<> inline rule& getRule<type##_t>() { return type; } | ||
| 35 | |||
| 36 | extern std::unordered_set<std::string> LuaKeywords; | ||
| 37 | extern std::unordered_set<std::string> Keywords; | ||
| 38 | |||
| 39 | class MoonParser { | ||
| 40 | public: | ||
| 41 | MoonParser(); | ||
| 42 | |||
| 43 | template<class AST> | ||
| 44 | ParseInfo parse(const std::string& codes) { | ||
| 45 | error_list errors; | ||
| 46 | auto res = parse(codes, getRule<AST>()); | ||
| 47 | if (res.node.template is<AST>()) { | ||
| 48 | return res; | ||
| 49 | } | ||
| 50 | return res; | ||
| 51 | } | ||
| 52 | |||
| 53 | template <class AST> | ||
| 54 | bool match(const std::string& codes) { | ||
| 55 | auto rEnd = rule(getRule<AST>() >> eof()); | ||
| 56 | return parse(codes, rEnd).node; | ||
| 57 | } | ||
| 58 | |||
| 59 | std::string toString(ast_node* node); | ||
| 60 | std::string toString(input::iterator begin, input::iterator end); | ||
| 61 | |||
| 62 | input encode(std::string_view input); | ||
| 63 | std::string decode(const input& input); | ||
| 64 | |||
| 65 | protected: | ||
| 66 | ParseInfo parse(const std::string& codes, rule& r); | ||
| 67 | |||
| 68 | struct State { | ||
| 69 | State() { | ||
| 70 | indents.push(0); | ||
| 71 | stringOpen = -1; | ||
| 72 | } | ||
| 73 | std::string buffer; | ||
| 74 | size_t stringOpen; | ||
| 75 | std::stack<int> indents; | ||
| 76 | std::stack<bool> doStack; | ||
| 77 | }; | ||
| 78 | |||
| 79 | private: | ||
| 80 | Converter _converter; | ||
| 81 | |||
| 82 | template <class T> | ||
| 83 | inline rule& getRule() { | ||
| 84 | assert(false); | ||
| 85 | return Cut; | ||
| 26 | } | 86 | } |
| 27 | std::string buffer; | 87 | |
| 28 | size_t stringOpen; | 88 | rule plain_space; |
| 29 | std::stack<int> indents; | 89 | rule Break; |
| 30 | std::stack<bool> doStack; | 90 | rule Any; |
| 31 | static std::unordered_set<std::string> luaKeywords; | 91 | rule White; |
| 32 | static std::unordered_set<std::string> keywords; | 92 | rule Stop; |
| 93 | rule Comment; | ||
| 94 | rule multi_line_open; | ||
| 95 | rule multi_line_close; | ||
| 96 | rule multi_line_content; | ||
| 97 | rule MultiLineComment; | ||
| 98 | rule Indent; | ||
| 99 | rule EscapeNewLine; | ||
| 100 | rule Space; | ||
| 101 | rule SomeSpace; | ||
| 102 | rule SpaceBreak; | ||
| 103 | rule EmptyLine; | ||
| 104 | rule AlphaNum; | ||
| 105 | rule Cut; | ||
| 106 | rule check_indent; | ||
| 107 | rule CheckIndent; | ||
| 108 | rule advance; | ||
| 109 | rule Advance; | ||
| 110 | rule push_indent; | ||
| 111 | rule PushIndent; | ||
| 112 | rule PreventIndent; | ||
| 113 | rule PopIndent; | ||
| 114 | rule InBlock; | ||
| 115 | rule ImportName; | ||
| 116 | rule ImportNameList; | ||
| 117 | rule import_literal_chain; | ||
| 118 | rule WithExp; | ||
| 119 | rule PopDo; | ||
| 120 | rule DisableDo; | ||
| 121 | rule SwitchElse; | ||
| 122 | rule SwitchBlock; | ||
| 123 | rule IfElseIf; | ||
| 124 | rule IfElse; | ||
| 125 | rule for_args; | ||
| 126 | rule for_in; | ||
| 127 | rule CompClause; | ||
| 128 | rule Chain; | ||
| 129 | rule KeyValue; | ||
| 130 | rule single_string_inner; | ||
| 131 | rule interp; | ||
| 132 | rule double_string_plain; | ||
| 133 | rule lua_string_open; | ||
| 134 | rule lua_string_close; | ||
| 135 | rule FnArgsExpList; | ||
| 136 | rule FnArgs; | ||
| 137 | rule chain_call; | ||
| 138 | rule chain_item; | ||
| 139 | rule ChainItems; | ||
| 140 | rule chain_dot_chain; | ||
| 141 | rule ColonChain; | ||
| 142 | rule chain_with_colon; | ||
| 143 | rule ChainItem; | ||
| 144 | rule Index; | ||
| 145 | rule invoke_chain; | ||
| 146 | rule TableValue; | ||
| 147 | rule table_lit_lines; | ||
| 148 | rule TableLitLine; | ||
| 149 | rule TableValueList; | ||
| 150 | rule TableBlockInner; | ||
| 151 | rule ClassLine; | ||
| 152 | rule KeyValueLine; | ||
| 153 | rule KeyValueList; | ||
| 154 | rule ArgLine; | ||
| 155 | rule ArgBlock; | ||
| 156 | rule invoke_args_with_table; | ||
| 157 | rule minus_exp; | ||
| 158 | rule sharp_exp; | ||
| 159 | rule tilde_exp; | ||
| 160 | rule not_exp; | ||
| 161 | rule empty_line_stop; | ||
| 162 | rule Line; | ||
| 163 | rule Shebang; | ||
| 164 | |||
| 165 | AST_RULE(Num) | ||
| 166 | AST_RULE(Name) | ||
| 167 | AST_RULE(Variable) | ||
| 168 | AST_RULE(LuaKeyword) | ||
| 169 | AST_RULE(self) | ||
| 170 | AST_RULE(self_name) | ||
| 171 | AST_RULE(self_class) | ||
| 172 | AST_RULE(self_class_name) | ||
| 173 | AST_RULE(SelfName) | ||
| 174 | AST_RULE(KeyName) | ||
| 175 | AST_RULE(VarArg) | ||
| 176 | AST_RULE(local_flag) | ||
| 177 | AST_RULE(Seperator) | ||
| 178 | AST_RULE(NameList) | ||
| 179 | AST_RULE(Local) | ||
| 180 | AST_RULE(colon_import_name) | ||
| 181 | AST_RULE(import_literal_inner) | ||
| 182 | AST_RULE(ImportLiteral) | ||
| 183 | AST_RULE(ImportFrom) | ||
| 184 | AST_RULE(ImportAs) | ||
| 185 | AST_RULE(Import) | ||
| 186 | AST_RULE(Backcall) | ||
| 187 | AST_RULE(ExpListLow) | ||
| 188 | AST_RULE(ExpList) | ||
| 189 | AST_RULE(Return) | ||
| 190 | AST_RULE(With) | ||
| 191 | AST_RULE(SwitchCase) | ||
| 192 | AST_RULE(Switch) | ||
| 193 | AST_RULE(IfCond) | ||
| 194 | AST_RULE(If) | ||
| 195 | AST_RULE(Unless) | ||
| 196 | AST_RULE(While) | ||
| 197 | AST_RULE(for_step_value) | ||
| 198 | AST_RULE(For) | ||
| 199 | AST_RULE(ForEach) | ||
| 200 | AST_RULE(Do) | ||
| 201 | AST_RULE(Comprehension) | ||
| 202 | AST_RULE(comp_value) | ||
| 203 | AST_RULE(TblComprehension) | ||
| 204 | AST_RULE(star_exp) | ||
| 205 | AST_RULE(CompForEach) | ||
| 206 | AST_RULE(CompFor) | ||
| 207 | AST_RULE(CompInner) | ||
| 208 | AST_RULE(Assign) | ||
| 209 | AST_RULE(update_op) | ||
| 210 | AST_RULE(Update) | ||
| 211 | AST_RULE(BinaryOperator) | ||
| 212 | AST_RULE(BackcallOperator) | ||
| 213 | AST_RULE(Assignable) | ||
| 214 | AST_RULE(AssignableChain) | ||
| 215 | AST_RULE(exp_op_value) | ||
| 216 | AST_RULE(Exp) | ||
| 217 | AST_RULE(Callable) | ||
| 218 | AST_RULE(ChainValue) | ||
| 219 | AST_RULE(simple_table) | ||
| 220 | AST_RULE(SimpleValue) | ||
| 221 | AST_RULE(Value) | ||
| 222 | AST_RULE(LuaStringOpen); | ||
| 223 | AST_RULE(LuaStringContent); | ||
| 224 | AST_RULE(LuaStringClose); | ||
| 225 | AST_RULE(LuaString) | ||
| 226 | AST_RULE(SingleString) | ||
| 227 | AST_RULE(double_string_inner) | ||
| 228 | AST_RULE(double_string_content) | ||
| 229 | AST_RULE(DoubleString) | ||
| 230 | AST_RULE(String) | ||
| 231 | AST_RULE(Parens) | ||
| 232 | AST_RULE(DotChainItem) | ||
| 233 | AST_RULE(ColonChainItem) | ||
| 234 | AST_RULE(default_value) | ||
| 235 | AST_RULE(Slice) | ||
| 236 | AST_RULE(Invoke) | ||
| 237 | AST_RULE(existential_op) | ||
| 238 | AST_RULE(TableLit) | ||
| 239 | AST_RULE(TableBlock) | ||
| 240 | AST_RULE(class_member_list) | ||
| 241 | AST_RULE(ClassBlock) | ||
| 242 | AST_RULE(ClassDecl) | ||
| 243 | AST_RULE(export_values) | ||
| 244 | AST_RULE(export_op) | ||
| 245 | AST_RULE(Export) | ||
| 246 | AST_RULE(variable_pair) | ||
| 247 | AST_RULE(normal_pair) | ||
| 248 | AST_RULE(FnArgDef) | ||
| 249 | AST_RULE(FnArgDefList) | ||
| 250 | AST_RULE(outer_var_shadow) | ||
| 251 | AST_RULE(FnArgsDef) | ||
| 252 | AST_RULE(fn_arrow) | ||
| 253 | AST_RULE(FunLit) | ||
| 254 | AST_RULE(NameOrDestructure) | ||
| 255 | AST_RULE(AssignableNameList) | ||
| 256 | AST_RULE(InvokeArgs) | ||
| 257 | AST_RULE(const_value) | ||
| 258 | AST_RULE(unary_exp) | ||
| 259 | AST_RULE(ExpListAssign) | ||
| 260 | AST_RULE(if_else_line) | ||
| 261 | AST_RULE(unless_line) | ||
| 262 | AST_RULE(statement_appendix) | ||
| 263 | AST_RULE(BreakLoop) | ||
| 264 | AST_RULE(Statement) | ||
| 265 | AST_RULE(Body) | ||
| 266 | AST_RULE(Block) | ||
| 267 | AST_RULE(File) | ||
| 268 | }; | ||
| 269 | |||
| 270 | namespace Utils { | ||
| 271 | void replace(std::string& str, std::string_view from, std::string_view to); | ||
| 33 | }; | 272 | }; |
| 34 | 273 | ||
| 35 | } // namespace MoonP | 274 | } // namespace MoonP |
