diff options
Diffstat (limited to 'src/yuescript/yue_parser.cpp')
-rw-r--r-- | src/yuescript/yue_parser.cpp | 684 |
1 files changed, 361 insertions, 323 deletions
diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index 48308a6..e78b331 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp | |||
@@ -33,23 +33,23 @@ std::unordered_set<std::string> Keywords = { | |||
33 | // clang-format off | 33 | // clang-format off |
34 | YueParser::YueParser() { | 34 | YueParser::YueParser() { |
35 | plain_space = *set(" \t"); | 35 | plain_space = *set(" \t"); |
36 | Break = nl(-expr('\r') >> '\n'); | 36 | line_break = nl(-expr('\r') >> '\n'); |
37 | Any = Break | any(); | 37 | any_char = line_break | any(); |
38 | Stop = Break | eof(); | 38 | stop = line_break | eof(); |
39 | Indent = plain_space; | 39 | indent = plain_space; |
40 | Comment = "--" >> *(not_(set("\r\n")) >> Any) >> and_(Stop); | 40 | comment = "--" >> *(not_(set("\r\n")) >> any_char) >> and_(stop); |
41 | multi_line_open = "--[["; | 41 | multi_line_open = "--[["; |
42 | multi_line_close = "]]"; | 42 | multi_line_close = "]]"; |
43 | multi_line_content = *(not_(multi_line_close) >> Any); | 43 | multi_line_content = *(not_(multi_line_close) >> any_char); |
44 | MultiLineComment = multi_line_open >> multi_line_content >> multi_line_close; | 44 | multi_line_comment = multi_line_open >> multi_line_content >> multi_line_close; |
45 | EscapeNewLine = '\\' >> *(set(" \t") | MultiLineComment) >> -Comment >> Break; | 45 | escape_new_line = '\\' >> *(set(" \t") | multi_line_comment) >> -comment >> line_break; |
46 | space_one = set(" \t") | and_(set("-\\")) >> (MultiLineComment | EscapeNewLine); | 46 | space_one = set(" \t") | and_(set("-\\")) >> (multi_line_comment | escape_new_line); |
47 | Space = -(and_(set(" \t-\\")) >> *space_one >> -Comment); | 47 | space = -(and_(set(" \t-\\")) >> *space_one >> -comment); |
48 | SpaceBreak = Space >> Break; | 48 | space_break = space >> line_break; |
49 | White = Space >> *(Break >> Space); | 49 | white = space >> *(line_break >> space); |
50 | EmptyLine = SpaceBreak; | 50 | empty_line = space_break; |
51 | AlphaNum = sel({range('a', 'z'), range('A', 'Z'), range('0', '9'), '_'}); | 51 | alpha_num = sel({range('a', 'z'), range('A', 'Z'), range('0', '9'), '_'}); |
52 | Name = sel({range('a', 'z'), range('A', 'Z'), '_'}) >> *AlphaNum; | 52 | Name = sel({range('a', 'z'), range('A', 'Z'), '_'}) >> *alpha_num; |
53 | num_expo = set("eE") >> -set("+-") >> num_char; | 53 | num_expo = set("eE") >> -set("+-") >> num_char; |
54 | num_expo_hex = set("pP") >> -set("+-") >> num_char; | 54 | num_expo_hex = set("pP") >> -set("+-") >> num_char; |
55 | lj_num = -set("uU") >> set("lL") >> set("lL"); | 55 | lj_num = -set("uU") >> set("lL") >> set("lL"); |
@@ -76,7 +76,7 @@ YueParser::YueParser() { | |||
76 | seq({'.', +num_char, -num_expo}) | 76 | seq({'.', +num_char, -num_expo}) |
77 | }); | 77 | }); |
78 | 78 | ||
79 | Cut = false_(); | 79 | cut = false_(); |
80 | Seperator = true_(); | 80 | Seperator = true_(); |
81 | 81 | ||
82 | empty_block_error = pl::user(true_(), [](const item_t& item) { | 82 | empty_block_error = pl::user(true_(), [](const item_t& item) { |
@@ -84,15 +84,52 @@ YueParser::YueParser() { | |||
84 | return false; | 84 | return false; |
85 | }); | 85 | }); |
86 | 86 | ||
87 | #define ensure(patt, finally) ((patt) >> (finally) | (finally) >> Cut) | 87 | #define ensure(patt, finally) ((patt) >> (finally) | (finally) >> cut) |
88 | #define key(str) (str >> not_(AlphaNum)) | 88 | |
89 | #define disable_do(patt) (DisableDo >> ((patt) >> EnableDo | EnableDo >> Cut)) | 89 | #define key(str) (str >> not_(alpha_num)) |
90 | #define disable_chain(patt) (DisableChain >> ((patt) >> EnableChain | EnableChain >> Cut)) | 90 | |
91 | #define disable_do_chain_arg_table_block(patt) (DisableDoChainArgTableBlock >> ((patt) >> EnableDoChainArgTableBlock | EnableDoChainArgTableBlock >> Cut)) | 91 | #define disable_do_rule(patt) ( \ |
92 | #define disable_arg_table_block(patt) (DisableArgTableBlock >> ((patt) >> EnableArgTableBlock | EnableArgTableBlock >> Cut)) | 92 | disable_do >> ( \ |
93 | #define body_with(str) (sel({key(str) >> Space >> (InBlock | Statement), InBlock, empty_block_error})) | 93 | (patt) >> enable_do | \ |
94 | #define opt_body_with(str) (key(str) >> Space >> (InBlock | Statement) | InBlock) | 94 | enable_do >> cut \ |
95 | #define body (sel({InBlock, Statement, empty_block_error})) | 95 | ) \ |
96 | ) | ||
97 | |||
98 | #define disable_chain_rule(patt) ( \ | ||
99 | disable_chain >> ( \ | ||
100 | (patt) >> enable_chain | \ | ||
101 | enable_chain >> cut \ | ||
102 | ) \ | ||
103 | ) | ||
104 | |||
105 | #define disable_do_chain_arg_table_block_rule(patt) ( \ | ||
106 | disable_do_chain_arg_table_block >> ( \ | ||
107 | (patt) >> enable_do_chain_arg_table_block | \ | ||
108 | enable_do_chain_arg_table_block >> cut \ | ||
109 | ) \ | ||
110 | ) | ||
111 | |||
112 | #define disable_arg_table_block_rule(patt) ( \ | ||
113 | disable_arg_table_block >> ( \ | ||
114 | (patt) >> enable_arg_table_block | \ | ||
115 | enable_arg_table_block >> cut \ | ||
116 | ) \ | ||
117 | ) | ||
118 | |||
119 | #define body_with(str) ( \ | ||
120 | sel({ \ | ||
121 | key(str) >> space >> (in_block | Statement), \ | ||
122 | in_block, \ | ||
123 | empty_block_error \ | ||
124 | }) \ | ||
125 | ) | ||
126 | |||
127 | #define opt_body_with(str) ( \ | ||
128 | key(str) >> space >> (in_block | Statement) | \ | ||
129 | in_block \ | ||
130 | ) | ||
131 | |||
132 | #define body (sel({in_block, Statement, empty_block_error})) | ||
96 | 133 | ||
97 | Variable = pl::user(Name, [](const item_t& item) { | 134 | Variable = pl::user(Name, [](const item_t& item) { |
98 | State* st = reinterpret_cast<State*>(item.user_data); | 135 | State* st = reinterpret_cast<State*>(item.user_data); |
@@ -124,16 +161,16 @@ YueParser::YueParser() { | |||
124 | return it != LuaKeywords.end(); | 161 | return it != LuaKeywords.end(); |
125 | }); | 162 | }); |
126 | 163 | ||
127 | self = '@'; | 164 | Self = '@'; |
128 | self_name = '@' >> Name; | 165 | SelfName = '@' >> Name; |
129 | self_class = "@@"; | 166 | SelfClass = "@@"; |
130 | self_class_name = "@@" >> Name; | 167 | SelfClassName = "@@" >> Name; |
131 | 168 | ||
132 | SelfName = sel({self_class_name, self_class, self_name, self}); | 169 | SelfItem = sel({SelfClassName, SelfClass, SelfName, Self}); |
133 | KeyName = SelfName | Name; | 170 | KeyName = SelfItem | Name; |
134 | VarArg = "..."; | 171 | VarArg = "..."; |
135 | 172 | ||
136 | check_indent = pl::user(Indent, [](const item_t& item) { | 173 | check_indent = pl::user(indent, [](const item_t& item) { |
137 | int indent = 0; | 174 | int indent = 0; |
138 | for (input_it i = item.begin->m_it; i != item.end->m_it; ++i) { | 175 | for (input_it i = item.begin->m_it; i != item.end->m_it; ++i) { |
139 | switch (*i) { | 176 | switch (*i) { |
@@ -144,9 +181,9 @@ YueParser::YueParser() { | |||
144 | State* st = reinterpret_cast<State*>(item.user_data); | 181 | State* st = reinterpret_cast<State*>(item.user_data); |
145 | return st->indents.top() == indent; | 182 | return st->indents.top() == indent; |
146 | }); | 183 | }); |
147 | CheckIndent = and_(check_indent); | 184 | check_indent_match = and_(check_indent); |
148 | 185 | ||
149 | advance = pl::user(Indent, [](const item_t& item) { | 186 | advance = pl::user(indent, [](const item_t& item) { |
150 | int indent = 0; | 187 | int indent = 0; |
151 | for (input_it i = item.begin->m_it; i != item.end->m_it; ++i) { | 188 | for (input_it i = item.begin->m_it; i != item.end->m_it; ++i) { |
152 | switch (*i) { | 189 | switch (*i) { |
@@ -162,9 +199,9 @@ YueParser::YueParser() { | |||
162 | } | 199 | } |
163 | return false; | 200 | return false; |
164 | }); | 201 | }); |
165 | Advance = and_(advance); | 202 | advance_match = and_(advance); |
166 | 203 | ||
167 | push_indent = pl::user(Indent, [](const item_t& item) { | 204 | push_indent = pl::user(indent, [](const item_t& item) { |
168 | int indent = 0; | 205 | int indent = 0; |
169 | for (input_it i = item.begin->m_it; i != item.end->m_it; ++i) { | 206 | for (input_it i = item.begin->m_it; i != item.end->m_it; ++i) { |
170 | switch (*i) { | 207 | switch (*i) { |
@@ -176,152 +213,152 @@ YueParser::YueParser() { | |||
176 | st->indents.push(indent); | 213 | st->indents.push(indent); |
177 | return true; | 214 | return true; |
178 | }); | 215 | }); |
179 | PushIndent = and_(push_indent); | 216 | push_indent_match = and_(push_indent); |
180 | 217 | ||
181 | PreventIndent = pl::user(true_(), [](const item_t& item) { | 218 | prevent_indent = pl::user(true_(), [](const item_t& item) { |
182 | State* st = reinterpret_cast<State*>(item.user_data); | 219 | State* st = reinterpret_cast<State*>(item.user_data); |
183 | st->indents.push(-1); | 220 | st->indents.push(-1); |
184 | return true; | 221 | return true; |
185 | }); | 222 | }); |
186 | 223 | ||
187 | PopIndent = pl::user(true_(), [](const item_t& item) { | 224 | pop_indent = pl::user(true_(), [](const item_t& item) { |
188 | State* st = reinterpret_cast<State*>(item.user_data); | 225 | State* st = reinterpret_cast<State*>(item.user_data); |
189 | st->indents.pop(); | 226 | st->indents.pop(); |
190 | return true; | 227 | return true; |
191 | }); | 228 | }); |
192 | 229 | ||
193 | InBlock = +SpaceBreak >> Advance >> ensure(Block, PopIndent); | 230 | in_block = +space_break >> advance_match >> ensure(Block, pop_indent); |
194 | 231 | ||
195 | local_flag = sel({'*', '^'}); | 232 | LocalFlag = sel({'*', '^'}); |
196 | local_values = NameList >> -(Space >> '=' >> Space >> (TableBlock | ExpListLow)); | 233 | LocalValues = NameList >> -(space >> '=' >> space >> (TableBlock | ExpListLow)); |
197 | Local = key("local") >> Space >> (local_flag | local_values); | 234 | Local = key("local") >> space >> (LocalFlag | LocalValues); |
198 | 235 | ||
199 | const_attrib = key("const"); | 236 | ConstAttrib = key("const"); |
200 | close_attrib = key("close"); | 237 | CloseAttrib = key("close"); |
201 | local_const_item = sel({Variable, simple_table, TableLit}); | 238 | local_const_item = sel({Variable, SimpleTable, TableLit}); |
202 | LocalAttrib = ( | 239 | LocalAttrib = ( |
203 | const_attrib >> Seperator >> Space >> local_const_item >> *(Space >> ',' >> Space >> local_const_item) | | 240 | ConstAttrib >> Seperator >> space >> local_const_item >> *(space >> ',' >> space >> local_const_item) | |
204 | close_attrib >> Seperator >> Space >> Variable >> *(Space >> ',' >> Space >> Variable) | 241 | CloseAttrib >> Seperator >> space >> Variable >> *(space >> ',' >> space >> Variable) |
205 | ) >> Space >> Assign; | 242 | ) >> space >> Assign; |
206 | 243 | ||
207 | colon_import_name = '\\' >> Space >> Variable; | 244 | ColonImportName = '\\' >> space >> Variable; |
208 | ImportName = colon_import_name | Variable; | 245 | import_name = ColonImportName | Variable; |
209 | ImportNameList = Seperator >> *SpaceBreak >> Space >> ImportName >> *((+SpaceBreak | Space >> ',' >> *SpaceBreak) >> Space >> ImportName); | 246 | import_name_list = Seperator >> *space_break >> space >> import_name >> *((+space_break | space >> ',' >> *space_break) >> space >> import_name); |
210 | ImportFrom = ImportNameList >> *SpaceBreak >> Space >> key("from") >> Space >> Exp; | 247 | ImportFrom = import_name_list >> *space_break >> space >> key("from") >> space >> Exp; |
211 | 248 | ||
212 | import_literal_inner = sel({range('a', 'z'), range('A', 'Z'), set("_-")}) >> *(AlphaNum | '-'); | 249 | ImportLiteralInner = sel({range('a', 'z'), range('A', 'Z'), set("_-")}) >> *(alpha_num | '-'); |
213 | import_literal_chain = Seperator >> import_literal_inner >> *('.' >> import_literal_inner); | 250 | import_literal_chain = Seperator >> ImportLiteralInner >> *('.' >> ImportLiteralInner); |
214 | ImportLiteral = sel({ | 251 | ImportLiteral = sel({ |
215 | '\'' >> import_literal_chain >> '\'', | 252 | '\'' >> import_literal_chain >> '\'', |
216 | '"' >> import_literal_chain >> '"' | 253 | '"' >> import_literal_chain >> '"' |
217 | }); | 254 | }); |
218 | 255 | ||
219 | macro_name_pair = MacroName >> ':' >> Space >> MacroName; | 256 | MacroNamePair = MacroName >> ':' >> space >> MacroName; |
220 | import_all_macro = '$'; | 257 | ImportAllMacro = '$'; |
221 | ImportTabItem = sel({ | 258 | import_tab_item = sel({ |
222 | variable_pair, | 259 | VariablePair, |
223 | normal_pair, | 260 | NormalPair, |
224 | ':' >> MacroName, | 261 | ':' >> MacroName, |
225 | macro_name_pair, | 262 | MacroNamePair, |
226 | import_all_macro, | 263 | ImportAllMacro, |
227 | meta_variable_pair, | 264 | MetaVariablePair, |
228 | meta_normal_pair, | 265 | MetaNormalPair, |
229 | Exp | 266 | Exp |
230 | }); | 267 | }); |
231 | ImportTabList = ImportTabItem >> *(Space >> ',' >> Space >> ImportTabItem); | 268 | import_tab_list = import_tab_item >> *(space >> ',' >> space >> import_tab_item); |
232 | ImportTabLine = ( | 269 | import_tab_line = ( |
233 | PushIndent >> (Space >> ImportTabList >> PopIndent | PopIndent) | 270 | push_indent_match >> (space >> import_tab_list >> pop_indent | pop_indent) |
234 | ) | Space; | 271 | ) | space; |
235 | import_tab_lines = SpaceBreak >> ImportTabLine >> *(-(Space >> ',') >> SpaceBreak >> ImportTabLine) >> -(Space >> ','); | 272 | import_tab_lines = space_break >> import_tab_line >> *(-(space >> ',') >> space_break >> import_tab_line) >> -(space >> ','); |
236 | ImportTabLit = seq({ | 273 | ImportTabLit = seq({ |
237 | '{', Seperator, | 274 | '{', Seperator, |
238 | -(Space >> ImportTabList), | 275 | -(space >> import_tab_list), |
239 | -(Space >> ','), | 276 | -(space >> ','), |
240 | -import_tab_lines, | 277 | -import_tab_lines, |
241 | White, | 278 | white, |
242 | '}' | 279 | '}' |
243 | }) | seq({ | 280 | }) | seq({ |
244 | Seperator, KeyValue, *(Space >> ',' >> Space >> KeyValue) | 281 | Seperator, key_value, *(space >> ',' >> space >> key_value) |
245 | }); | 282 | }); |
246 | 283 | ||
247 | ImportAs = ImportLiteral >> -(Space >> key("as") >> Space >> sel({ImportTabLit, Variable, import_all_macro})); | 284 | ImportAs = ImportLiteral >> -(space >> key("as") >> space >> sel({ImportTabLit, Variable, ImportAllMacro})); |
248 | 285 | ||
249 | Import = key("import") >> Space >> (ImportAs | ImportFrom); | 286 | Import = key("import") >> space >> (ImportAs | ImportFrom); |
250 | 287 | ||
251 | Label = "::" >> LabelName >> "::"; | 288 | Label = "::" >> LabelName >> "::"; |
252 | 289 | ||
253 | Goto = key("goto") >> Space >> LabelName; | 290 | Goto = key("goto") >> space >> LabelName; |
254 | 291 | ||
255 | ShortTabAppending = "[]" >> Space >> Assign; | 292 | ShortTabAppending = "[]" >> space >> Assign; |
256 | 293 | ||
257 | BreakLoop = sel({"break", "continue"}) >> not_(AlphaNum); | 294 | BreakLoop = sel({"break", "continue"}) >> not_(alpha_num); |
258 | 295 | ||
259 | Return = key("return") >> -(Space >> (TableBlock | ExpListLow)); | 296 | Return = key("return") >> -(space >> (TableBlock | ExpListLow)); |
260 | 297 | ||
261 | WithExp = ExpList >> -(Space >> Assign); | 298 | with_exp = ExpList >> -(space >> Assign); |
262 | 299 | ||
263 | With = key("with") >> -existential_op >> Space >> disable_do_chain_arg_table_block(WithExp) >> Space >> body_with("do"); | 300 | With = key("with") >> -ExistentialOp >> space >> disable_do_chain_arg_table_block_rule(with_exp) >> space >> body_with("do"); |
264 | SwitchCase = key("when") >> disable_chain(disable_arg_table_block(SwitchList)) >> Space >> body_with("then"); | 301 | SwitchCase = key("when") >> disable_chain_rule(disable_arg_table_block_rule(SwitchList)) >> space >> body_with("then"); |
265 | SwitchElse = key("else") >> Space >> body; | 302 | switch_else = key("else") >> space >> body; |
266 | 303 | ||
267 | SwitchBlock = | 304 | switch_block = |
268 | *(Break >> *EmptyLine >> CheckIndent >> Space >> SwitchCase) >> | 305 | *(line_break >> *empty_line >> check_indent_match >> space >> SwitchCase) >> |
269 | -(Break >> *EmptyLine >> CheckIndent >> Space >> SwitchElse); | 306 | -(line_break >> *empty_line >> check_indent_match >> space >> switch_else); |
270 | 307 | ||
271 | exp_not_tab = not_(simple_table | TableLit) >> Space >> Exp; | 308 | exp_not_tab = not_(SimpleTable | TableLit) >> space >> Exp; |
272 | 309 | ||
273 | SwitchList = Seperator >> ( | 310 | SwitchList = Seperator >> ( |
274 | and_(simple_table | TableLit) >> Space >> Exp | | 311 | and_(SimpleTable | TableLit) >> space >> Exp | |
275 | exp_not_tab >> *(Space >> ',' >> exp_not_tab) | 312 | exp_not_tab >> *(space >> ',' >> exp_not_tab) |
276 | ); | 313 | ); |
277 | Switch = key("switch") >> Space >> Exp >> | 314 | Switch = key("switch") >> space >> Exp >> |
278 | Space >> Seperator >> ( | 315 | space >> Seperator >> ( |
279 | SwitchCase >> Space >> ( | 316 | SwitchCase >> space >> ( |
280 | Break >> *EmptyLine >> CheckIndent >> Space >> SwitchCase >> SwitchBlock | | 317 | line_break >> *empty_line >> check_indent_match >> space >> SwitchCase >> switch_block | |
281 | *(Space >> SwitchCase) >> -(Space >> SwitchElse) | 318 | *(space >> SwitchCase) >> -(space >> switch_else) |
282 | ) | | 319 | ) | |
283 | SpaceBreak >> *EmptyLine >> Advance >> Space >> SwitchCase >> SwitchBlock >> PopIndent | 320 | space_break >> *empty_line >> advance_match >> space >> SwitchCase >> switch_block >> pop_indent |
284 | ) >> SwitchBlock; | 321 | ) >> switch_block; |
285 | 322 | ||
286 | assignment = ExpList >> Space >> Assign; | 323 | Assignment = ExpList >> space >> Assign; |
287 | IfCond = disable_chain(disable_arg_table_block(assignment | Exp)); | 324 | IfCond = disable_chain_rule(disable_arg_table_block_rule(Assignment | Exp)); |
288 | IfElseIf = -(Break >> *EmptyLine >> CheckIndent) >> Space >> key("elseif") >> Space >> IfCond >> Space >> body_with("then"); | 325 | if_else_if = -(line_break >> *empty_line >> check_indent_match) >> space >> key("elseif") >> space >> IfCond >> space >> body_with("then"); |
289 | IfElse = -(Break >> *EmptyLine >> CheckIndent) >> Space >> key("else") >> Space >> body; | 326 | if_else = -(line_break >> *empty_line >> check_indent_match) >> space >> key("else") >> space >> body; |
290 | IfType = sel({"if", "unless"}) >> not_(AlphaNum); | 327 | IfType = sel({"if", "unless"}) >> not_(alpha_num); |
291 | If = seq({IfType, Space, IfCond, Space, opt_body_with("then"), *IfElseIf, -IfElse}); | 328 | If = seq({IfType, space, IfCond, space, opt_body_with("then"), *if_else_if, -if_else}); |
292 | 329 | ||
293 | WhileType = sel({"while", "until"}) >> not_(AlphaNum); | 330 | WhileType = sel({"while", "until"}) >> not_(alpha_num); |
294 | While = WhileType >> Space >> disable_do_chain_arg_table_block(Exp) >> Space >> opt_body_with("do"); | 331 | While = WhileType >> space >> disable_do_chain_arg_table_block_rule(Exp) >> space >> opt_body_with("do"); |
295 | Repeat = seq({key("repeat"), Space, Body, Break, *EmptyLine, CheckIndent, Space, key("until"), Space, Exp}); | 332 | Repeat = seq({key("repeat"), space, Body, line_break, *empty_line, check_indent_match, space, key("until"), space, Exp}); |
296 | 333 | ||
297 | for_step_value = ',' >> Space >> Exp; | 334 | ForStepValue = ',' >> space >> Exp; |
298 | for_args = Variable >> Space >> '=' >> Space >> Exp >> Space >> ',' >> Space >> Exp >> Space >> -for_step_value; | 335 | for_args = Variable >> space >> '=' >> space >> Exp >> space >> ',' >> space >> Exp >> space >> -ForStepValue; |
299 | 336 | ||
300 | For = key("for") >> Space >> disable_do_chain_arg_table_block(for_args) >> Space >> opt_body_with("do"); | 337 | For = key("for") >> space >> disable_do_chain_arg_table_block_rule(for_args) >> space >> opt_body_with("do"); |
301 | 338 | ||
302 | for_in = star_exp | ExpList; | 339 | for_in = StarExp | ExpList; |
303 | 340 | ||
304 | ForEach = key("for") >> Space >> AssignableNameList >> Space >> key("in") >> Space >> | 341 | ForEach = key("for") >> space >> AssignableNameList >> space >> key("in") >> space >> |
305 | disable_do_chain_arg_table_block(for_in) >> Space >> opt_body_with("do"); | 342 | disable_do_chain_arg_table_block_rule(for_in) >> space >> opt_body_with("do"); |
306 | 343 | ||
307 | Do = pl::user(key("do"), [](const item_t& item) { | 344 | Do = pl::user(key("do"), [](const item_t& item) { |
308 | State* st = reinterpret_cast<State*>(item.user_data); | 345 | State* st = reinterpret_cast<State*>(item.user_data); |
309 | return st->noDoStack.empty() || !st->noDoStack.top(); | 346 | return st->noDoStack.empty() || !st->noDoStack.top(); |
310 | }) >> Space >> Body; | 347 | }) >> space >> Body; |
311 | 348 | ||
312 | DisableDo = pl::user(true_(), [](const item_t& item) { | 349 | disable_do = pl::user(true_(), [](const item_t& item) { |
313 | State* st = reinterpret_cast<State*>(item.user_data); | 350 | State* st = reinterpret_cast<State*>(item.user_data); |
314 | st->noDoStack.push(true); | 351 | st->noDoStack.push(true); |
315 | return true; | 352 | return true; |
316 | }); | 353 | }); |
317 | 354 | ||
318 | EnableDo = pl::user(true_(), [](const item_t& item) { | 355 | enable_do = pl::user(true_(), [](const item_t& item) { |
319 | State* st = reinterpret_cast<State*>(item.user_data); | 356 | State* st = reinterpret_cast<State*>(item.user_data); |
320 | st->noDoStack.pop(); | 357 | st->noDoStack.pop(); |
321 | return true; | 358 | return true; |
322 | }); | 359 | }); |
323 | 360 | ||
324 | DisableDoChainArgTableBlock = pl::user(true_(), [](const item_t& item) { | 361 | disable_do_chain_arg_table_block = pl::user(true_(), [](const item_t& item) { |
325 | State* st = reinterpret_cast<State*>(item.user_data); | 362 | State* st = reinterpret_cast<State*>(item.user_data); |
326 | st->noDoStack.push(true); | 363 | st->noDoStack.push(true); |
327 | st->noChainBlockStack.push(true); | 364 | st->noChainBlockStack.push(true); |
@@ -329,7 +366,7 @@ YueParser::YueParser() { | |||
329 | return true; | 366 | return true; |
330 | }); | 367 | }); |
331 | 368 | ||
332 | EnableDoChainArgTableBlock = pl::user(true_(), [](const item_t& item) { | 369 | enable_do_chain_arg_table_block = pl::user(true_(), [](const item_t& item) { |
333 | State* st = reinterpret_cast<State*>(item.user_data); | 370 | State* st = reinterpret_cast<State*>(item.user_data); |
334 | st->noDoStack.pop(); | 371 | st->noDoStack.pop(); |
335 | st->noChainBlockStack.pop(); | 372 | st->noChainBlockStack.pop(); |
@@ -337,110 +374,111 @@ YueParser::YueParser() { | |||
337 | return true; | 374 | return true; |
338 | }); | 375 | }); |
339 | 376 | ||
340 | DisableArgTableBlock = pl::user(true_(), [](const item_t& item) { | 377 | disable_arg_table_block = pl::user(true_(), [](const item_t& item) { |
341 | State* st = reinterpret_cast<State*>(item.user_data); | 378 | State* st = reinterpret_cast<State*>(item.user_data); |
342 | st->noTableBlockStack.push(true); | 379 | st->noTableBlockStack.push(true); |
343 | return true; | 380 | return true; |
344 | }); | 381 | }); |
345 | 382 | ||
346 | EnableArgTableBlock = pl::user(true_(), [](const item_t& item) { | 383 | enable_arg_table_block = pl::user(true_(), [](const item_t& item) { |
347 | State* st = reinterpret_cast<State*>(item.user_data); | 384 | State* st = reinterpret_cast<State*>(item.user_data); |
348 | st->noTableBlockStack.pop(); | 385 | st->noTableBlockStack.pop(); |
349 | return true; | 386 | return true; |
350 | }); | 387 | }); |
351 | 388 | ||
352 | catch_block = Break >> *EmptyLine >> CheckIndent >> Space >> key("catch") >> Space >> Variable >> Space >> InBlock; | 389 | CatchBlock = line_break >> *empty_line >> check_indent_match >> space >> key("catch") >> space >> Variable >> space >> in_block; |
353 | Try = key("try") >> Space >> (InBlock | Exp) >> -catch_block; | 390 | Try = key("try") >> space >> (in_block | Exp) >> -CatchBlock; |
354 | 391 | ||
355 | Comprehension = '[' >> not_('[') >> Space >> Exp >> Space >> CompInner >> Space >> ']'; | 392 | Comprehension = '[' >> not_('[') >> space >> Exp >> space >> CompInner >> space >> ']'; |
356 | comp_value = ',' >> Space >> Exp; | 393 | CompValue = ',' >> space >> Exp; |
357 | TblComprehension = '{' >> Space >> Exp >> Space >> -(comp_value >> Space) >> CompInner >> Space >> '}'; | 394 | TblComprehension = '{' >> space >> Exp >> space >> -(CompValue >> space) >> CompInner >> space >> '}'; |
358 | 395 | ||
359 | CompInner = Seperator >> (CompForEach | CompFor) >> *(Space >> CompClause); | 396 | CompInner = Seperator >> (CompForEach | CompFor) >> *(space >> comp_clause); |
360 | star_exp = '*' >> Space >> Exp; | 397 | StarExp = '*' >> space >> Exp; |
361 | CompForEach = key("for") >> Space >> AssignableNameList >> Space >> key("in") >> Space >> (star_exp | Exp); | 398 | CompForEach = key("for") >> space >> AssignableNameList >> space >> key("in") >> space >> (StarExp | Exp); |
362 | CompFor = key("for") >> Space >> Variable >> Space >> '=' >> Space >> Exp >> Space >> ',' >> Space >> Exp >> -for_step_value; | 399 | CompFor = key("for") >> space >> Variable >> space >> '=' >> space >> Exp >> space >> ',' >> space >> Exp >> -ForStepValue; |
363 | CompClause = sel({CompFor, CompForEach, key("when") >> Space >> Exp}); | 400 | comp_clause = sel({CompFor, CompForEach, key("when") >> space >> Exp}); |
364 | 401 | ||
365 | Assign = '=' >> Space >> Seperator >> sel({ | 402 | Assign = '=' >> space >> Seperator >> sel({ |
366 | With, If, Switch, TableBlock, | 403 | With, If, Switch, TableBlock, |
367 | Exp >> *(Space >> set(",;") >> Space >> Exp) | 404 | Exp >> *(space >> set(",;") >> space >> Exp) |
368 | }); | 405 | }); |
369 | 406 | ||
370 | update_op = sel({ | 407 | UpdateOp = sel({ |
371 | "..", "//", "or", "and", | 408 | "..", "//", "or", "and", |
372 | ">>", "<<", "??", | 409 | ">>", "<<", "??", |
373 | set("+-*/%&|") | 410 | set("+-*/%&|") |
374 | }); | 411 | }); |
375 | 412 | ||
376 | Update = update_op >> '=' >> Space >> Exp; | 413 | Update = UpdateOp >> '=' >> space >> Exp; |
377 | 414 | ||
378 | Assignable = sel({AssignableChain, Variable, SelfName}); | 415 | Assignable = sel({AssignableChain, Variable, SelfItem}); |
379 | 416 | ||
380 | unary_value = +(unary_operator >> Space) >> Value; | 417 | UnaryValue = +(UnaryOperator >> space) >> Value; |
381 | 418 | ||
382 | ExponentialOperator = '^'; | 419 | exponential_operator = '^'; |
383 | expo_value = seq({ExponentialOperator, *SpaceBreak, Space, Value}); | 420 | expo_value = seq({exponential_operator, *space_break, space, Value}); |
384 | expo_exp = Value >> *(Space >> expo_value); | 421 | expo_exp = Value >> *(space >> expo_value); |
385 | 422 | ||
386 | unary_operator = sel({ | 423 | UnaryOperator = sel({ |
387 | '-' >> not_(set(">=") | space_one), | 424 | '-' >> not_(set(">=") | space_one), |
388 | '#', | 425 | '#', |
389 | '~' >> not_('=' | space_one), | 426 | '~' >> not_('=' | space_one), |
390 | "not" >> not_(AlphaNum) | 427 | "not" >> not_(alpha_num) |
391 | }); | 428 | }); |
392 | unary_exp = *(unary_operator >> Space) >> expo_exp; | 429 | UnaryExp = *(UnaryOperator >> space) >> expo_exp; |
393 | 430 | ||
394 | PipeOperator = "|>"; | 431 | pipe_operator = "|>"; |
395 | pipe_value = seq({PipeOperator, *SpaceBreak, Space, unary_exp}); | 432 | pipe_value = seq({pipe_operator, *space_break, space, UnaryExp}); |
396 | pipe_exp = unary_exp >> *(Space >> pipe_value); | 433 | pipe_exp = UnaryExp >> *(space >> pipe_value); |
397 | 434 | ||
398 | BinaryOperator = sel({ | 435 | BinaryOperator = sel({ |
399 | "or" >> not_(AlphaNum), | 436 | "or" >> not_(alpha_num), |
400 | "and" >> not_(AlphaNum), | 437 | "and" >> not_(alpha_num), |
401 | "<=", ">=", "~=", "!=", "==", | 438 | "<=", ">=", "~=", "!=", "==", |
402 | "..", "<<", ">>", "//", | 439 | "..", "<<", ">>", "//", |
403 | set("+-*/%><|&~") | 440 | set("+-*/%><|&~") |
404 | }); | 441 | }); |
405 | exp_op_value = seq({BinaryOperator, *SpaceBreak, Space, pipe_exp}); | 442 | ExpOpValue = seq({BinaryOperator, *space_break, space, pipe_exp}); |
406 | Exp = seq({Seperator, pipe_exp, *(Space >> exp_op_value), -(Space >> "??" >> Space >> Exp)}); | 443 | Exp = seq({Seperator, pipe_exp, *(space >> ExpOpValue), -(space >> "??" >> space >> Exp)}); |
407 | 444 | ||
408 | DisableChain = pl::user(true_(), [](const item_t& item) { | 445 | disable_chain = pl::user(true_(), [](const item_t& item) { |
409 | State* st = reinterpret_cast<State*>(item.user_data); | 446 | State* st = reinterpret_cast<State*>(item.user_data); |
410 | st->noChainBlockStack.push(true); | 447 | st->noChainBlockStack.push(true); |
411 | return true; | 448 | return true; |
412 | }); | 449 | }); |
413 | 450 | ||
414 | EnableChain = pl::user(true_(), [](const item_t& item) { | 451 | enable_chain = pl::user(true_(), [](const item_t& item) { |
415 | State* st = reinterpret_cast<State*>(item.user_data); | 452 | State* st = reinterpret_cast<State*>(item.user_data); |
416 | st->noChainBlockStack.pop(); | 453 | st->noChainBlockStack.pop(); |
417 | return true; | 454 | return true; |
418 | }); | 455 | }); |
419 | 456 | ||
420 | chain_line = seq({CheckIndent, Space, chain_dot_chain | ColonChain, -InvokeArgs}); | 457 | chain_line = seq({check_indent_match, space, chain_dot_chain | colon_chain, -InvokeArgs}); |
421 | chain_block = pl::user(true_(), [](const item_t& item) { | 458 | chain_block = pl::user(true_(), [](const item_t& item) { |
422 | State* st = reinterpret_cast<State*>(item.user_data); | 459 | State* st = reinterpret_cast<State*>(item.user_data); |
423 | return st->noChainBlockStack.empty() || !st->noChainBlockStack.top(); | 460 | return st->noChainBlockStack.empty() || !st->noChainBlockStack.top(); |
424 | }) >> +SpaceBreak >> Advance >> ensure( | 461 | }) >> +space_break >> advance_match >> ensure( |
425 | chain_line >> *(+SpaceBreak >> chain_line), PopIndent); | 462 | chain_line >> *(+space_break >> chain_line), pop_indent); |
426 | ChainValue = seq({ | 463 | ChainValue = seq({ |
427 | Seperator, | 464 | Seperator, |
428 | Chain, | 465 | chain, |
429 | -existential_op, | 466 | -ExistentialOp, |
430 | -(InvokeArgs | chain_block), | 467 | -(InvokeArgs | chain_block), |
431 | -table_appending_op | 468 | -TableAppendingOp |
432 | }); | 469 | }); |
433 | 470 | ||
434 | simple_table = seq({Seperator, KeyValue, *(Space >> ',' >> Space >> KeyValue)}); | 471 | SimpleTable = seq({Seperator, key_value, *(space >> ',' >> space >> key_value)}); |
435 | Value = sel({SimpleValue, simple_table, ChainValue, String}); | 472 | Value = sel({SimpleValue, SimpleTable, ChainValue, String}); |
436 | 473 | ||
437 | single_string_inner = '\\' >> set("'\\") | not_('\'') >> Any; | 474 | single_string_inner = '\\' >> set("'\\") | not_('\'') >> any_char; |
438 | SingleString = '\'' >> *single_string_inner >> '\''; | 475 | SingleString = '\'' >> *single_string_inner >> '\''; |
439 | interp = "#{" >> Space >> Exp >> Space >> '}'; | 476 | |
440 | double_string_plain = '\\' >> set("\"\\") | not_('"') >> Any; | 477 | interp = "#{" >> space >> Exp >> space >> '}'; |
441 | double_string_inner = +(not_(interp) >> double_string_plain); | 478 | double_string_plain = '\\' >> set("\"\\") | not_('"') >> any_char; |
442 | double_string_content = double_string_inner | interp; | 479 | DoubleStringInner = +(not_(interp) >> double_string_plain); |
443 | DoubleString = '"' >> Seperator >> *double_string_content >> '"'; | 480 | DoubleStringContent = DoubleStringInner | interp; |
481 | DoubleString = '"' >> Seperator >> *DoubleStringContent >> '"'; | ||
444 | String = sel({DoubleString, SingleString, LuaString}); | 482 | String = sel({DoubleString, SingleString, LuaString}); |
445 | 483 | ||
446 | lua_string_open = '[' >> *expr('=') >> '['; | 484 | lua_string_open = '[' >> *expr('=') >> '['; |
@@ -459,143 +497,143 @@ YueParser::YueParser() { | |||
459 | return st->stringOpen == count; | 497 | return st->stringOpen == count; |
460 | }); | 498 | }); |
461 | 499 | ||
462 | LuaStringContent = *(not_(LuaStringClose) >> Any); | 500 | LuaStringContent = *(not_(LuaStringClose) >> any_char); |
463 | 501 | ||
464 | LuaString = LuaStringOpen >> -Break >> LuaStringContent >> LuaStringClose; | 502 | LuaString = LuaStringOpen >> -line_break >> LuaStringContent >> LuaStringClose; |
465 | 503 | ||
466 | Parens = seq({'(', *SpaceBreak, Space, Exp, *SpaceBreak, Space, ')'}); | 504 | Parens = seq({'(', *space_break, space, Exp, *space_break, space, ')'}); |
467 | Callable = sel({Variable, SelfName, MacroName, VarArg, Parens}); | 505 | Callable = sel({Variable, SelfItem, MacroName, VarArg, Parens}); |
468 | FnArgsExpList = Space >> Exp >> Space >> *seq({Break | ',', White, Exp}); | 506 | fn_args_exp_list = space >> Exp >> space >> *seq({line_break | ',', white, Exp}); |
469 | 507 | ||
470 | FnArgs = sel({ | 508 | fn_args = sel({ |
471 | seq({'(', *SpaceBreak, -FnArgsExpList, *SpaceBreak, Space, ')'}), | 509 | seq({'(', *space_break, -fn_args_exp_list, *space_break, space, ')'}), |
472 | seq({Space, '!', not_('=')}) | 510 | seq({space, '!', not_('=')}) |
473 | }); | 511 | }); |
474 | 512 | ||
475 | meta_index = sel({Name, Index, String}); | 513 | meta_index = sel({Name, index, String}); |
476 | Metatable = '<' >> Space >> '>'; | 514 | Metatable = '<' >> space >> '>'; |
477 | Metamethod = '<' >> Space >> meta_index >> Space >> '>'; | 515 | Metamethod = '<' >> space >> meta_index >> space >> '>'; |
478 | 516 | ||
479 | existential_op = '?' >> not_('?'); | 517 | ExistentialOp = '?' >> not_('?'); |
480 | table_appending_op = "[]"; | 518 | TableAppendingOp = "[]"; |
481 | chain_call = seq({ | 519 | chain_call = seq({ |
482 | Callable, | 520 | Callable, |
483 | -existential_op, | 521 | -ExistentialOp, |
484 | -ChainItems | 522 | -chain_items |
485 | }) | seq({ | 523 | }) | seq({ |
486 | String, | 524 | String, |
487 | ChainItems | 525 | chain_items |
488 | }); | 526 | }); |
489 | chain_index_chain = seq({Index, -existential_op, -ChainItems}); | 527 | chain_index_chain = seq({index, -ExistentialOp, -chain_items}); |
490 | chain_dot_chain = seq({DotChainItem, -existential_op, -ChainItems}); | 528 | chain_dot_chain = seq({DotChainItem, -ExistentialOp, -chain_items}); |
491 | 529 | ||
492 | Chain = sel({chain_call, chain_dot_chain, ColonChain, chain_index_chain}); | 530 | chain = sel({chain_call, chain_dot_chain, colon_chain, chain_index_chain}); |
493 | 531 | ||
494 | chain_call_list = seq({ | 532 | chain_call_list = seq({ |
495 | Callable, | 533 | Callable, |
496 | -existential_op, | 534 | -ExistentialOp, |
497 | ChainItems | 535 | chain_items |
498 | }) | seq({ | 536 | }) | seq({ |
499 | String, | 537 | String, |
500 | ChainItems | 538 | chain_items |
501 | }); | 539 | }); |
502 | ChainList = sel({chain_call_list, chain_dot_chain, ColonChain, chain_index_chain}); | 540 | chain_list = sel({chain_call_list, chain_dot_chain, colon_chain, chain_index_chain}); |
503 | 541 | ||
504 | AssignableChain = Seperator >> ChainList; | 542 | AssignableChain = Seperator >> chain_list; |
505 | 543 | ||
506 | chain_with_colon = +ChainItem >> -ColonChain; | 544 | chain_with_colon = +chain_item >> -colon_chain; |
507 | ChainItems = chain_with_colon | ColonChain; | 545 | chain_items = chain_with_colon | colon_chain; |
508 | 546 | ||
509 | Index = seq({'[', not_('['), Space, Exp, Space, ']'}); | 547 | index = seq({'[', not_('['), space, Exp, space, ']'}); |
510 | ChainItem = sel({ | 548 | chain_item = sel({ |
511 | Invoke >> -existential_op, | 549 | Invoke >> -ExistentialOp, |
512 | DotChainItem >> -existential_op, | 550 | DotChainItem >> -ExistentialOp, |
513 | Slice, | 551 | Slice, |
514 | Index >> -existential_op | 552 | index >> -ExistentialOp |
515 | }); | 553 | }); |
516 | DotChainItem = '.' >> sel({Name, Metatable, Metamethod}); | 554 | DotChainItem = '.' >> sel({Name, Metatable, Metamethod}); |
517 | ColonChainItem = sel({'\\', "::"}) >> sel({LuaKeyword, Name, Metamethod}); | 555 | ColonChainItem = sel({'\\', "::"}) >> sel({LuaKeyword, Name, Metamethod}); |
518 | invoke_chain = Invoke >> -existential_op >> -ChainItems; | 556 | invoke_chain = Invoke >> -ExistentialOp >> -chain_items; |
519 | ColonChain = ColonChainItem >> -existential_op >> -invoke_chain; | 557 | colon_chain = ColonChainItem >> -ExistentialOp >> -invoke_chain; |
520 | 558 | ||
521 | default_value = true_(); | 559 | DefaultValue = true_(); |
522 | Slice = seq({ | 560 | Slice = seq({ |
523 | '[', not_('['), | 561 | '[', not_('['), |
524 | Space, Exp | default_value, | 562 | space, Exp | DefaultValue, |
525 | Space, ',', | 563 | space, ',', |
526 | Space, Exp | default_value, | 564 | space, Exp | DefaultValue, |
527 | Space, ',' >> Space >> Exp | default_value, | 565 | space, ',' >> space >> Exp | DefaultValue, |
528 | Space, ']' | 566 | space, ']' |
529 | }); | 567 | }); |
530 | 568 | ||
531 | Invoke = Seperator >> sel({ | 569 | Invoke = Seperator >> sel({ |
532 | FnArgs, | 570 | fn_args, |
533 | SingleString, | 571 | SingleString, |
534 | DoubleString, | 572 | DoubleString, |
535 | and_('[') >> LuaString, | 573 | and_('[') >> LuaString, |
536 | and_('{') >> TableLit | 574 | and_('{') >> TableLit |
537 | }); | 575 | }); |
538 | 576 | ||
539 | SpreadExp = "..." >> Space >> Exp; | 577 | SpreadExp = "..." >> space >> Exp; |
540 | 578 | ||
541 | TableValue = sel({ | 579 | table_value = sel({ |
542 | variable_pair_def, | 580 | VariablePairDef, |
543 | normal_pair_def, | 581 | NormalPairDef, |
544 | meta_variable_pair_def, | 582 | MetaVariablePairDef, |
545 | meta_normal_pair_def, | 583 | MetaNormalPairDef, |
546 | SpreadExp, | 584 | SpreadExp, |
547 | normal_def | 585 | NormalDef |
548 | }); | 586 | }); |
549 | 587 | ||
550 | table_lit_lines = SpaceBreak >> TableLitLine >> *(-(Space >> ',') >> SpaceBreak >> TableLitLine) >> -(Space >> ','); | 588 | table_lit_lines = space_break >> table_lit_line >> *(-(space >> ',') >> space_break >> table_lit_line) >> -(space >> ','); |
551 | 589 | ||
552 | TableLit = seq({ | 590 | TableLit = seq({ |
553 | Space, '{', Seperator, | 591 | space, '{', Seperator, |
554 | -(Space >> TableValueList), | 592 | -(space >> table_value_list), |
555 | -(Space >> ','), | 593 | -(space >> ','), |
556 | -table_lit_lines, | 594 | -table_lit_lines, |
557 | White, '}' | 595 | white, '}' |
558 | }); | 596 | }); |
559 | 597 | ||
560 | TableValueList = TableValue >> *(Space >> ',' >> Space >> TableValue); | 598 | table_value_list = table_value >> *(space >> ',' >> space >> table_value); |
561 | 599 | ||
562 | TableLitLine = ( | 600 | table_lit_line = ( |
563 | PushIndent >> (Space >> TableValueList >> PopIndent | PopIndent) | 601 | push_indent_match >> (space >> table_value_list >> pop_indent | pop_indent) |
564 | ) | ( | 602 | ) | ( |
565 | Space | 603 | space |
566 | ); | 604 | ); |
567 | 605 | ||
568 | TableBlockInner = Seperator >> KeyValueLine >> *(+SpaceBreak >> KeyValueLine); | 606 | table_block_inner = Seperator >> key_value_line >> *(+space_break >> key_value_line); |
569 | TableBlock = +SpaceBreak >> Advance >> ensure(TableBlockInner, PopIndent); | 607 | TableBlock = +space_break >> advance_match >> ensure(table_block_inner, pop_indent); |
570 | TableBlockIndent = '*' >> Seperator >> disable_arg_table_block( | 608 | TableBlockIndent = '*' >> Seperator >> disable_arg_table_block_rule( |
571 | Space >> KeyValueList >> -(Space >> ',') >> | 609 | space >> key_value_list >> -(space >> ',') >> |
572 | -(+SpaceBreak >> Advance >> Space >> ensure(KeyValueList >> -(Space >> ',') >> *(+SpaceBreak >> KeyValueLine), PopIndent))); | 610 | -(+space_break >> advance_match >> space >> ensure(key_value_list >> -(space >> ',') >> *(+space_break >> key_value_line), pop_indent))); |
573 | 611 | ||
574 | class_member_list = Seperator >> KeyValue >> *(Space >> ',' >> Space >> KeyValue); | 612 | ClassMemberList = Seperator >> key_value >> *(space >> ',' >> space >> key_value); |
575 | ClassLine = CheckIndent >> Space >> (class_member_list | Statement) >> -(Space >> ','); | 613 | class_line = check_indent_match >> space >> (ClassMemberList | Statement) >> -(space >> ','); |
576 | ClassBlock = seq({+SpaceBreak, Advance, Seperator, ClassLine, *(+SpaceBreak >> ClassLine), PopIndent}); | 614 | ClassBlock = seq({+space_break, advance_match, Seperator, class_line, *(+space_break >> class_line), pop_indent}); |
577 | 615 | ||
578 | ClassDecl = seq({ | 616 | ClassDecl = seq({ |
579 | key("class"), not_(':'), | 617 | key("class"), not_(':'), |
580 | disable_arg_table_block(seq({ | 618 | disable_arg_table_block_rule(seq({ |
581 | -(Space >> Assignable), | 619 | -(space >> Assignable), |
582 | -seq({Space, key("extends"), PreventIndent, Space, ensure(Exp, PopIndent)}), | 620 | -seq({space, key("extends"), prevent_indent, space, ensure(Exp, pop_indent)}), |
583 | -seq({Space, key("using"), PreventIndent, Space, ensure(ExpList, PopIndent)}) | 621 | -seq({space, key("using"), prevent_indent, space, ensure(ExpList, pop_indent)}) |
584 | })), | 622 | })), |
585 | -ClassBlock | 623 | -ClassBlock |
586 | }); | 624 | }); |
587 | 625 | ||
588 | global_values = NameList >> -(Space >> '=' >> Space >> (TableBlock | ExpListLow)); | 626 | GlobalValues = NameList >> -(space >> '=' >> space >> (TableBlock | ExpListLow)); |
589 | global_op = sel({'*', '^'}); | 627 | GlobalOp = sel({'*', '^'}); |
590 | Global = key("global") >> Space >> sel({ClassDecl, global_op, global_values}); | 628 | Global = key("global") >> space >> sel({ClassDecl, GlobalOp, GlobalValues}); |
591 | 629 | ||
592 | export_default = key("default"); | 630 | ExportDefault = key("default"); |
593 | 631 | ||
594 | Export = pl::user(key("export"), [](const item_t& item) { | 632 | Export = pl::user(key("export"), [](const item_t& item) { |
595 | State* st = reinterpret_cast<State*>(item.user_data); | 633 | State* st = reinterpret_cast<State*>(item.user_data); |
596 | st->exportCount++; | 634 | st->exportCount++; |
597 | return true; | 635 | return true; |
598 | }) >> (pl::user(Space >> export_default >> Space >> Exp, [](const item_t& item) { | 636 | }) >> (pl::user(space >> ExportDefault >> space >> Exp, [](const item_t& item) { |
599 | State* st = reinterpret_cast<State*>(item.user_data); | 637 | State* st = reinterpret_cast<State*>(item.user_data); |
600 | if (st->exportDefault) { | 638 | if (st->exportDefault) { |
601 | throw ParserError("export default has already been declared", *item.begin, *item.end); | 639 | throw ParserError("export default has already been declared", *item.begin, *item.end); |
@@ -606,99 +644,99 @@ YueParser::YueParser() { | |||
606 | st->exportDefault = true; | 644 | st->exportDefault = true; |
607 | return true; | 645 | return true; |
608 | }) | 646 | }) |
609 | | (not_(Space >> export_default) >> pl::user(true_(), [](const item_t& item) { | 647 | | (not_(space >> ExportDefault) >> pl::user(true_(), [](const item_t& item) { |
610 | State* st = reinterpret_cast<State*>(item.user_data); | 648 | State* st = reinterpret_cast<State*>(item.user_data); |
611 | if (st->exportDefault && st->exportCount > 1) { | 649 | if (st->exportDefault && st->exportCount > 1) { |
612 | throw ParserError("can not export any more items when 'export default' is declared", *item.begin, *item.end); | 650 | throw ParserError("can not export any more items when 'export default' is declared", *item.begin, *item.end); |
613 | } | 651 | } |
614 | return true; | 652 | return true; |
615 | }) >> Space >> ExpList >> -(Space >> Assign)) | 653 | }) >> space >> ExpList >> -(space >> Assign)) |
616 | | Space >> pl::user(Macro, [](const item_t& item) { | 654 | | space >> pl::user(Macro, [](const item_t& item) { |
617 | State* st = reinterpret_cast<State*>(item.user_data); | 655 | State* st = reinterpret_cast<State*>(item.user_data); |
618 | st->exportMacro = true; | 656 | st->exportMacro = true; |
619 | return true; | 657 | return true; |
620 | })) >> not_(Space >> statement_appendix); | 658 | })) >> not_(space >> StatementAppendix); |
621 | 659 | ||
622 | variable_pair = ':' >> Variable; | 660 | VariablePair = ':' >> Variable; |
623 | 661 | ||
624 | normal_pair = seq({ | 662 | NormalPair = seq({ |
625 | sel({ | 663 | sel({ |
626 | KeyName, | 664 | KeyName, |
627 | seq({'[', not_('['), Space, Exp, Space, ']'}), | 665 | seq({'[', not_('['), space, Exp, space, ']'}), |
628 | String | 666 | String |
629 | }), | 667 | }), |
630 | ':', not_(':'), Space, | 668 | ':', not_(':'), space, |
631 | sel({Exp, TableBlock, +SpaceBreak >> Space >> Exp}) | 669 | sel({Exp, TableBlock, +space_break >> space >> Exp}) |
632 | }); | 670 | }); |
633 | 671 | ||
634 | meta_variable_pair = ":<" >> Space >> Variable >> Space >> '>'; | 672 | MetaVariablePair = ":<" >> space >> Variable >> space >> '>'; |
635 | 673 | ||
636 | meta_normal_pair = '<' >> Space >> -meta_index >> Space >> ">:" >> Space >> | 674 | MetaNormalPair = '<' >> space >> -meta_index >> space >> ">:" >> space >> |
637 | sel({Exp, TableBlock, +(SpaceBreak) >> Space >> Exp}); | 675 | sel({Exp, TableBlock, +space_break >> space >> Exp}); |
638 | 676 | ||
639 | destruct_def = -seq({Space, '=', Space, Exp}); | 677 | destruct_def = -seq({space, '=', space, Exp}); |
640 | variable_pair_def = variable_pair >> destruct_def; | 678 | VariablePairDef = VariablePair >> destruct_def; |
641 | normal_pair_def = normal_pair >> destruct_def; | 679 | NormalPairDef = NormalPair >> destruct_def; |
642 | meta_variable_pair_def = meta_variable_pair >> destruct_def; | 680 | MetaVariablePairDef = MetaVariablePair >> destruct_def; |
643 | meta_normal_pair_def = meta_normal_pair >> destruct_def; | 681 | MetaNormalPairDef = MetaNormalPair >> destruct_def; |
644 | normal_def = Exp >> Seperator >> destruct_def; | 682 | NormalDef = Exp >> Seperator >> destruct_def; |
645 | 683 | ||
646 | KeyValue = sel({ | 684 | key_value = sel({ |
647 | variable_pair, | 685 | VariablePair, |
648 | normal_pair, | 686 | NormalPair, |
649 | meta_variable_pair, | 687 | MetaVariablePair, |
650 | meta_normal_pair | 688 | MetaNormalPair |
651 | }); | 689 | }); |
652 | KeyValueList = KeyValue >> *(Space >> ',' >> Space >> KeyValue); | 690 | key_value_list = key_value >> *(space >> ',' >> space >> key_value); |
653 | KeyValueLine = CheckIndent >> Space >> sel({ | 691 | key_value_line = check_indent_match >> space >> sel({ |
654 | KeyValueList >> -(Space >> ','), | 692 | key_value_list >> -(space >> ','), |
655 | TableBlockIndent, | 693 | TableBlockIndent, |
656 | '*' >> Space >> sel({SpreadExp, Exp, TableBlock}) | 694 | '*' >> space >> sel({SpreadExp, Exp, TableBlock}) |
657 | }); | 695 | }); |
658 | 696 | ||
659 | FnArgDef = (Variable | SelfName >> -existential_op) >> -(Space >> '=' >> Space >> Exp); | 697 | FnArgDef = (Variable | SelfItem >> -ExistentialOp) >> -(space >> '=' >> space >> Exp); |
660 | 698 | ||
661 | FnArgDefList = Seperator >> ( | 699 | FnArgDefList = Seperator >> ( |
662 | seq({ | 700 | seq({ |
663 | FnArgDef, | 701 | FnArgDef, |
664 | *seq({Space, ',' | Break, White, FnArgDef}), | 702 | *seq({space, ',' | line_break, white, FnArgDef}), |
665 | -seq({Space, ',' | Break, White, VarArg}) | 703 | -seq({space, ',' | line_break, white, VarArg}) |
666 | }) | | 704 | }) | |
667 | VarArg | 705 | VarArg |
668 | ); | 706 | ); |
669 | 707 | ||
670 | outer_var_shadow = key("using") >> Space >> (NameList | key("nil")); | 708 | OuterVarShadow = key("using") >> space >> (NameList | key("nil")); |
671 | 709 | ||
672 | FnArgsDef = seq({'(', White, -FnArgDefList, -(Space >> outer_var_shadow), White, ')'}); | 710 | FnArgsDef = seq({'(', white, -FnArgDefList, -(space >> OuterVarShadow), white, ')'}); |
673 | fn_arrow = sel({"->", "=>"}); | 711 | FnArrow = sel({"->", "=>"}); |
674 | FunLit = seq({-FnArgsDef, Space, fn_arrow, -(Space >> Body)}); | 712 | FunLit = seq({-FnArgsDef, space, FnArrow, -(space >> Body)}); |
675 | 713 | ||
676 | MacroName = '$' >> Name; | 714 | MacroName = '$' >> Name; |
677 | macro_args_def = '(' >> White >> -FnArgDefList >> White >> ')'; | 715 | macro_args_def = '(' >> white >> -FnArgDefList >> white >> ')'; |
678 | MacroLit = -(macro_args_def >> Space) >> "->" >> Space >> Body; | 716 | MacroLit = -(macro_args_def >> space) >> "->" >> space >> Body; |
679 | Macro = key("macro") >> Space >> Name >> Space >> '=' >> Space >> MacroLit; | 717 | Macro = key("macro") >> space >> Name >> space >> '=' >> space >> MacroLit; |
680 | MacroInPlace = '$' >> Space >> "->" >> Space >> Body; | 718 | MacroInPlace = '$' >> space >> "->" >> space >> Body; |
681 | 719 | ||
682 | NameList = Seperator >> Variable >> *(Space >> ',' >> Space >> Variable); | 720 | NameList = Seperator >> Variable >> *(space >> ',' >> space >> Variable); |
683 | NameOrDestructure = Variable | TableLit; | 721 | NameOrDestructure = Variable | TableLit; |
684 | AssignableNameList = Seperator >> NameOrDestructure >> *(Space >> ',' >> Space >> NameOrDestructure); | 722 | AssignableNameList = Seperator >> NameOrDestructure >> *(space >> ',' >> space >> NameOrDestructure); |
685 | 723 | ||
686 | fn_arrow_back = '<' >> set("-="); | 724 | FnArrowBack = '<' >> set("-="); |
687 | Backcall = seq({-(FnArgsDef >> Space), fn_arrow_back, Space, ChainValue}); | 725 | Backcall = seq({-(FnArgsDef >> space), FnArrowBack, space, ChainValue}); |
688 | 726 | ||
689 | PipeBody = seq({ | 727 | PipeBody = seq({ |
690 | Seperator, | 728 | Seperator, |
691 | PipeOperator, | 729 | pipe_operator, |
692 | Space, | 730 | space, |
693 | unary_exp, | 731 | UnaryExp, |
694 | *seq({+SpaceBreak, CheckIndent, Space, PipeOperator, Space, unary_exp}) | 732 | *seq({+space_break, check_indent_match, space, pipe_operator, space, UnaryExp}) |
695 | }); | 733 | }); |
696 | 734 | ||
697 | ExpList = Seperator >> Exp >> *(Space >> ',' >> Space >> Exp); | 735 | ExpList = Seperator >> Exp >> *(space >> ',' >> space >> Exp); |
698 | ExpListLow = Seperator >> Exp >> *(Space >> set(",;") >> Space >> Exp); | 736 | ExpListLow = Seperator >> Exp >> *(space >> set(",;") >> space >> Exp); |
699 | 737 | ||
700 | ArgLine = CheckIndent >> Space >> Exp >> *(Space >> ',' >> Space >> Exp); | 738 | arg_line = check_indent_match >> space >> Exp >> *(space >> ',' >> space >> Exp); |
701 | ArgBlock = ArgLine >> *(Space >> ',' >> SpaceBreak >> ArgLine) >> PopIndent; | 739 | arg_block = arg_line >> *(space >> ',' >> space_break >> arg_line) >> pop_indent; |
702 | 740 | ||
703 | arg_table_block = pl::user(true_(), [](const item_t& item) { | 741 | arg_table_block = pl::user(true_(), [](const item_t& item) { |
704 | State* st = reinterpret_cast<State*>(item.user_data); | 742 | State* st = reinterpret_cast<State*>(item.user_data); |
@@ -708,38 +746,38 @@ YueParser::YueParser() { | |||
708 | invoke_args_with_table = | 746 | invoke_args_with_table = |
709 | ',' >> ( | 747 | ',' >> ( |
710 | TableBlock | | 748 | TableBlock | |
711 | SpaceBreak >> Advance >> ArgBlock >> -arg_table_block | 749 | space_break >> advance_match >> arg_block >> -arg_table_block |
712 | ) | arg_table_block; | 750 | ) | arg_table_block; |
713 | 751 | ||
714 | leading_spaces_error = pl::user(+space_one >> '(' >> Space >> Exp >> +(Space >> ',' >> Space >> Exp) >> Space >> ')', [](const item_t& item) { | 752 | leading_spaces_error = pl::user(+space_one >> '(' >> space >> Exp >> +(space >> ',' >> space >> Exp) >> space >> ')', [](const item_t& item) { |
715 | throw ParserError("write invoke arguments in parentheses without leading spaces or just leading spaces without parentheses", *item.begin, *item.end); | 753 | throw ParserError("write invoke arguments in parentheses without leading spaces or just leading spaces without parentheses", *item.begin, *item.end); |
716 | return false; | 754 | return false; |
717 | }); | 755 | }); |
718 | 756 | ||
719 | InvokeArgs = | 757 | InvokeArgs = |
720 | not_(set("-~")) >> Space >> Seperator >> | 758 | not_(set("-~")) >> space >> Seperator >> |
721 | sel({ | 759 | sel({ |
722 | Exp >> *(Space >> ',' >> Space >> Exp) >> -(Space >> invoke_args_with_table), | 760 | Exp >> *(space >> ',' >> space >> Exp) >> -(space >> invoke_args_with_table), |
723 | arg_table_block, | 761 | arg_table_block, |
724 | leading_spaces_error | 762 | leading_spaces_error |
725 | }); | 763 | }); |
726 | 764 | ||
727 | const_value = sel({"nil", "true", "false"}) >> not_(AlphaNum); | 765 | ConstValue = sel({"nil", "true", "false"}) >> not_(alpha_num); |
728 | 766 | ||
729 | SimpleValue = sel({ | 767 | SimpleValue = sel({ |
730 | TableLit, const_value, If, Switch, Try, With, | 768 | TableLit, ConstValue, If, Switch, Try, With, |
731 | ClassDecl, ForEach, For, While, Do, | 769 | ClassDecl, ForEach, For, While, Do, |
732 | unary_value, TblComprehension, Comprehension, | 770 | UnaryValue, TblComprehension, Comprehension, |
733 | FunLit, Num | 771 | FunLit, Num |
734 | }); | 772 | }); |
735 | 773 | ||
736 | ExpListAssign = ExpList >> -(Space >> (Update | Assign)) >> not_(Space >> '='); | 774 | ExpListAssign = ExpList >> -(space >> (Update | Assign)) >> not_(space >> '='); |
737 | 775 | ||
738 | if_line = IfType >> Space >> IfCond; | 776 | IfLine = IfType >> space >> IfCond; |
739 | while_line = WhileType >> Space >> Exp; | 777 | WhileLine = WhileType >> space >> Exp; |
740 | 778 | ||
741 | YueLineComment = *(not_(set("\r\n")) >> Any); | 779 | YueLineComment = *(not_(set("\r\n")) >> any_char); |
742 | yue_line_comment = "--" >> YueLineComment >> and_(Stop); | 780 | yue_line_comment = "--" >> YueLineComment >> and_(stop); |
743 | YueMultilineComment = multi_line_content; | 781 | YueMultilineComment = multi_line_content; |
744 | yue_multiline_comment = multi_line_open >> YueMultilineComment >> multi_line_close; | 782 | yue_multiline_comment = multi_line_open >> YueMultilineComment >> multi_line_close; |
745 | yue_comment = check_indent >> sel({ | 783 | yue_comment = check_indent >> sel({ |
@@ -749,13 +787,13 @@ YueParser::YueParser() { | |||
749 | -yue_line_comment | 787 | -yue_line_comment |
750 | }), | 788 | }), |
751 | yue_line_comment | 789 | yue_line_comment |
752 | }) >> and_(Break); | 790 | }) >> and_(line_break); |
753 | 791 | ||
754 | ChainAssign = Seperator >> Exp >> +(Space >> '=' >> Space >> Exp >> Space >> and_('=')) >> Space >> Assign; | 792 | ChainAssign = Seperator >> Exp >> +(space >> '=' >> space >> Exp >> space >> and_('=')) >> space >> Assign; |
755 | 793 | ||
756 | statement_appendix = sel({if_line, while_line, CompInner}) >> Space; | 794 | StatementAppendix = sel({IfLine, WhileLine, CompInner}) >> space; |
757 | statement_sep = and_(seq({ | 795 | StatementSep = and_(seq({ |
758 | *SpaceBreak, CheckIndent, Space, | 796 | *space_break, check_indent_match, space, |
759 | sel({ | 797 | sel({ |
760 | set("($'\""), | 798 | set("($'\""), |
761 | "[[", | 799 | "[[", |
@@ -766,46 +804,46 @@ YueParser::YueParser() { | |||
766 | Seperator, | 804 | Seperator, |
767 | -seq({ | 805 | -seq({ |
768 | yue_comment, | 806 | yue_comment, |
769 | *(Break >> yue_comment), | 807 | *(line_break >> yue_comment), |
770 | Break, | 808 | line_break, |
771 | CheckIndent | 809 | check_indent_match |
772 | }), | 810 | }), |
773 | Space, | 811 | space, |
774 | sel({ | 812 | sel({ |
775 | Import, While, Repeat, For, ForEach, | 813 | Import, While, Repeat, For, ForEach, |
776 | Return, Local, Global, Export, Macro, | 814 | Return, Local, Global, Export, Macro, |
777 | MacroInPlace, BreakLoop, Label, Goto, ShortTabAppending, | 815 | MacroInPlace, BreakLoop, Label, Goto, ShortTabAppending, |
778 | LocalAttrib, Backcall, PipeBody, ExpListAssign, ChainAssign, | 816 | LocalAttrib, Backcall, PipeBody, ExpListAssign, ChainAssign, |
779 | statement_appendix >> empty_block_error | 817 | StatementAppendix >> empty_block_error |
780 | }), | 818 | }), |
781 | Space, | 819 | space, |
782 | -statement_appendix, | 820 | -StatementAppendix, |
783 | -statement_sep | 821 | -StatementSep |
784 | }); | 822 | }); |
785 | 823 | ||
786 | Body = InBlock | Statement; | 824 | Body = in_block | Statement; |
787 | 825 | ||
788 | empty_line_break = sel({ | 826 | empty_line_break = sel({ |
789 | check_indent >> (MultiLineComment >> Space | Comment), | 827 | check_indent >> (multi_line_comment >> space | comment), |
790 | advance >> ensure(MultiLineComment >> Space | Comment, PopIndent), | 828 | advance >> ensure(multi_line_comment >> space | comment, pop_indent), |
791 | plain_space | 829 | plain_space |
792 | }) >> and_(Break); | 830 | }) >> and_(line_break); |
793 | 831 | ||
794 | indentation_error = pl::user(not_(PipeOperator | eof()), [](const item_t& item) { | 832 | indentation_error = pl::user(not_(pipe_operator | eof()), [](const item_t& item) { |
795 | throw ParserError("unexpected indent", *item.begin, *item.end); | 833 | throw ParserError("unexpected indent", *item.begin, *item.end); |
796 | return false; | 834 | return false; |
797 | }); | 835 | }); |
798 | 836 | ||
799 | Line = sel({ | 837 | line = sel({ |
800 | CheckIndent >> Statement, | 838 | check_indent_match >> Statement, |
801 | empty_line_break, | 839 | empty_line_break, |
802 | Advance >> ensure(Space >> (indentation_error | Statement), PopIndent) | 840 | advance_match >> ensure(space >> (indentation_error | Statement), pop_indent) |
803 | }); | 841 | }); |
804 | Block = seq({Seperator, Line, *(+Break >> Line)}); | 842 | Block = seq({Seperator, line, *(+line_break >> line)}); |
805 | 843 | ||
806 | Shebang = "#!" >> *(not_(Stop) >> Any); | 844 | shebang = "#!" >> *(not_(stop) >> any_char); |
807 | BlockEnd = seq({Block, White, Stop}); | 845 | BlockEnd = seq({Block, white, stop}); |
808 | File = seq({-Shebang, -Block, White, Stop}); | 846 | File = seq({-shebang, -Block, white, stop}); |
809 | } | 847 | } |
810 | // clang-format on | 848 | // clang-format on |
811 | 849 | ||