/* 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 "MoonP/ast.hpp" namespace parserlib { #define AST_LEAF(type) \ COUNTER_INC; \ class type##_t : public ast_node \ { \ public: \ virtual int getId() const override { return COUNTER_READ; } #define AST_NODE(type) \ COUNTER_INC; \ class type##_t : public ast_container \ { \ public: \ virtual int getId() const override { return COUNTER_READ; } #define AST_MEMBER(type, ...) \ type##_t() { \ add_members({__VA_ARGS__}); \ } #define AST_END(type) \ }; \ template<> constexpr int id() { return COUNTER_READ; } AST_LEAF(Num) AST_END(Num) AST_LEAF(Name) AST_END(Name) AST_NODE(Variable) ast_ptr name; AST_MEMBER(Variable, &name) AST_END(Variable) AST_NODE(LabelName) ast_ptr name; AST_MEMBER(LabelName, &name) AST_END(LabelName) AST_NODE(LuaKeyword) ast_ptr name; AST_MEMBER(LuaKeyword, &name) AST_END(LuaKeyword) AST_LEAF(self) AST_END(self) AST_NODE(self_name) ast_ptr name; AST_MEMBER(self_name, &name) AST_END(self_name) AST_LEAF(self_class) AST_END(self_class) AST_NODE(self_class_name) ast_ptr name; AST_MEMBER(self_class_name, &name) AST_END(self_class_name) AST_NODE(SelfName) ast_sel name; AST_MEMBER(SelfName, &name) AST_END(SelfName) AST_NODE(KeyName) ast_sel name; AST_MEMBER(KeyName, &name) AST_END(KeyName) AST_LEAF(VarArg) AST_END(VarArg) AST_LEAF(local_flag) AST_END(local_flag) AST_LEAF(Seperator) AST_END(Seperator) AST_NODE(NameList) ast_ptr sep; ast_list names; AST_MEMBER(NameList, &sep, &names) AST_END(NameList) class ExpListLow_t; AST_NODE(local_values) ast_ptr nameList; ast_ptr valueList; AST_MEMBER(local_values, &nameList, &valueList) AST_END(local_values) AST_NODE(Local) ast_sel item; std::list forceDecls; std::list decls; AST_MEMBER(Local, &item) AST_END(Local) AST_NODE(colon_import_name) ast_ptr name; AST_MEMBER(colon_import_name, &name) AST_END(colon_import_name) class Exp_t; class TableLit_t; AST_LEAF(import_literal_inner) AST_END(import_literal_inner) AST_NODE(ImportLiteral) ast_ptr sep; ast_sel_list inners; AST_MEMBER(ImportLiteral, &sep, &inners) AST_END(ImportLiteral) AST_NODE(ImportFrom) ast_ptr sep; ast_sel_list names; ast_ptr exp; AST_MEMBER(ImportFrom, &sep, &names, &exp) AST_END(ImportFrom) AST_NODE(ImportAs) ast_ptr literal; ast_sel target; AST_MEMBER(ImportAs, &literal, &target) AST_END(ImportAs) AST_NODE(Import) ast_sel content; AST_MEMBER(Import, &content) AST_END(Import) AST_NODE(Label) ast_ptr label; AST_MEMBER(Label, &label) AST_END(Label) AST_NODE(Goto) ast_ptr label; AST_MEMBER(Goto, &label) AST_END(Goto) class FnArgsDef_t; AST_LEAF(fn_arrow_back) AST_END(fn_arrow_back) class ChainValue_t; AST_NODE(Backcall) ast_ptr argsDef; ast_ptr arrow; ast_ptr value; AST_MEMBER(Backcall, &argsDef, &arrow, &value) AST_END(Backcall) AST_NODE(ExpListLow) ast_ptr sep; ast_list exprs; AST_MEMBER(ExpListLow, &sep, &exprs) AST_END(ExpListLow) AST_NODE(ExpList) ast_ptr sep; ast_list exprs; AST_MEMBER(ExpList, &sep, &exprs) AST_END(ExpList) AST_NODE(Return) ast_ptr valueList; AST_MEMBER(Return, &valueList) AST_END(Return) class Assign_t; class Body_t; AST_NODE(With) ast_ptr valueList; ast_ptr assigns; ast_ptr body; AST_MEMBER(With, &valueList, &assigns, &body) AST_END(With) AST_NODE(SwitchCase) ast_ptr valueList; ast_ptr body; AST_MEMBER(SwitchCase, &valueList, &body) AST_END(SwitchCase) AST_NODE(Switch) ast_ptr target; ast_ptr sep; ast_list branches; ast_ptr lastBranch; AST_MEMBER(Switch, &target, &sep, &branches, &lastBranch) AST_END(Switch) AST_NODE(IfCond) ast_ptr condition; ast_ptr assign; AST_MEMBER(IfCond, &condition, &assign) AST_END(IfCond) AST_NODE(If) ast_ptr sep; ast_sel_list nodes; AST_MEMBER(If, &sep, &nodes) AST_END(If) AST_NODE(Unless) ast_ptr sep; ast_sel_list nodes; AST_MEMBER(Unless, &sep, &nodes) AST_END(Unless) AST_NODE(While) ast_ptr condition; ast_ptr body; AST_MEMBER(While, &condition, &body) AST_END(While) AST_NODE(for_step_value) ast_ptr value; AST_MEMBER(for_step_value, &value) AST_END(for_step_value) AST_NODE(For) ast_ptr varName; ast_ptr startValue; ast_ptr stopValue; ast_ptr stepValue; ast_ptr body; AST_MEMBER(For, &varName, &startValue, &stopValue, &stepValue, &body) AST_END(For) class AssignableNameList_t; class star_exp_t; AST_NODE(ForEach) ast_ptr nameList; ast_sel loopValue; ast_ptr body; AST_MEMBER(ForEach, &nameList, &loopValue, &body) AST_END(ForEach) AST_NODE(Do) ast_ptr body; AST_MEMBER(Do, &body) AST_END(Do) class CompInner_t; class Statement_t; AST_NODE(Comprehension) ast_sel value; ast_ptr forLoop; AST_MEMBER(Comprehension, &value, &forLoop) AST_END(Comprehension) AST_NODE(comp_value) ast_ptr value; AST_MEMBER(comp_value, &value) AST_END(comp_value) AST_NODE(TblComprehension) ast_ptr key; ast_ptr value; ast_ptr forLoop; AST_MEMBER(TblComprehension, &key, &value, &forLoop) AST_END(TblComprehension) AST_NODE(star_exp) ast_ptr value; AST_MEMBER(star_exp, &value) AST_END(star_exp) AST_NODE(CompForEach) ast_ptr nameList; ast_sel loopValue; AST_MEMBER(CompForEach, &nameList, &loopValue) AST_END(CompForEach) AST_NODE(CompFor) ast_ptr varName; ast_ptr startValue; ast_ptr stopValue; ast_ptr stepValue; AST_MEMBER(CompFor, &varName, &startValue, &stopValue, &stepValue) AST_END(CompFor) AST_NODE(CompInner) ast_ptr sep; ast_sel_list items; AST_MEMBER(CompInner, &sep, &items) AST_END(CompInner) class TableBlock_t; AST_NODE(Assign) ast_ptr sep; ast_sel_list values; AST_MEMBER(Assign, &sep, &values) AST_END(Assign) AST_LEAF(update_op) AST_END(update_op) AST_NODE(Update) ast_ptr op; ast_ptr value; AST_MEMBER(Update, &op, &value) AST_END(Update) AST_LEAF(BinaryOperator) AST_END(BinaryOperator) AST_LEAF(BackcallOperator) AST_END(BackcallOperator) class AssignableChain_t; AST_NODE(Assignable) ast_sel item; AST_MEMBER(Assignable, &item) AST_END(Assignable) class Value_t; AST_NODE(exp_op_value) ast_sel op; ast_ptr value; AST_MEMBER(exp_op_value, &op, &value) AST_END(exp_op_value) AST_NODE(Exp) ast_ptr value; ast_list opValues; AST_MEMBER(Exp, &value, &opValues) AST_END(Exp) class Parens_t; class MacroName_t; AST_NODE(Callable) ast_sel item; AST_MEMBER(Callable, &item) AST_END(Callable) AST_NODE(variable_pair) ast_ptr name; AST_MEMBER(variable_pair, &name) AST_END(variable_pair) class DoubleString_t; class SingleString_t; AST_NODE(normal_pair) ast_sel key; ast_sel value; AST_MEMBER(normal_pair, &key, &value) AST_END(normal_pair) AST_NODE(simple_table) ast_ptr sep; ast_sel_list pairs; AST_MEMBER(simple_table, &sep, &pairs) AST_END(simple_table) class String_t; class const_value_t; class ClassDecl_t; class unary_exp_t; class TableLit_t; class FunLit_t; AST_NODE(SimpleValue) ast_sel value; AST_MEMBER(SimpleValue, &value) AST_END(SimpleValue) AST_LEAF(LuaStringOpen) AST_END(LuaStringOpen) AST_LEAF(LuaStringContent) AST_END(LuaStringContent) AST_LEAF(LuaStringClose) AST_END(LuaStringClose) AST_NODE(LuaString) ast_ptr open; ast_ptr content; ast_ptr close; AST_MEMBER(LuaString, &open, &content, &close) AST_END(LuaString) AST_LEAF(SingleString) AST_END(SingleString) AST_LEAF(double_string_inner) AST_END(double_string_inner) AST_NODE(double_string_content) ast_sel content; AST_MEMBER(double_string_content, &content) AST_END(double_string_content) AST_NODE(DoubleString) ast_ptr sep; ast_list segments; AST_MEMBER(DoubleString, &sep, &segments) AST_END(DoubleString) AST_NODE(String) ast_sel str; AST_MEMBER(String, &str) AST_END(String) AST_NODE(DotChainItem) ast_ptr name; AST_MEMBER(DotChainItem, &name) AST_END(DotChainItem) AST_NODE(ColonChainItem) ast_sel name; bool switchToDot = false; AST_MEMBER(ColonChainItem, &name) AST_END(ColonChainItem) class default_value_t; AST_NODE(Slice) ast_sel startValue; ast_sel stopValue; ast_sel stepValue; AST_MEMBER(Slice, &startValue, &stopValue, &stepValue) AST_END(Slice) AST_NODE(Parens) ast_ptr expr; AST_MEMBER(Parens, &expr) AST_END(Parens) AST_NODE(Invoke) ast_ptr sep; ast_sel_list args; AST_MEMBER(Invoke, &sep, &args) AST_END(Invoke) AST_LEAF(existential_op) AST_END(existential_op) class InvokeArgs_t; AST_NODE(ChainValue) ast_ptr sep; ast_sel_list items; AST_MEMBER(ChainValue, &sep, &items) AST_END(ChainValue) AST_NODE(AssignableChain) ast_ptr sep; ast_sel_list items; AST_MEMBER(AssignableChain, &sep, &items) AST_END(AssignableChain) AST_NODE(Value) ast_sel item; AST_MEMBER(Value, &item) AST_END(Value) AST_LEAF(default_value) AST_END(default_value) AST_NODE(macro_name_pair) ast_ptr key; ast_ptr value; AST_MEMBER(macro_name_pair, &key, &value) AST_END(macro_name_pair) AST_NODE(TableLit) ast_ptr sep; ast_sel_list values; AST_MEMBER(TableLit, &sep, &values) AST_END(TableLit) AST_NODE(TableBlock) ast_ptr sep; ast_sel_list values; AST_MEMBER(TableBlock, &sep, &values) AST_END(TableBlock) AST_NODE(class_member_list) ast_ptr sep; ast_sel_list values; AST_MEMBER(class_member_list, &sep, &values) AST_END(class_member_list) AST_NODE(ClassBlock) ast_ptr sep; ast_sel_list contents; AST_MEMBER(ClassBlock, &sep, &contents) AST_END(ClassBlock) AST_NODE(ClassDecl) ast_ptr name; ast_ptr extend; ast_ptr body; AST_MEMBER(ClassDecl, &name, &extend, &body) AST_END(ClassDecl) AST_NODE(global_values) ast_ptr nameList; ast_ptr valueList; AST_MEMBER(global_values, &nameList, &valueList) AST_END(global_values) AST_LEAF(global_op) AST_END(global_op) AST_NODE(Global) ast_sel item; AST_MEMBER(Global, &item) AST_END(Global) AST_LEAF(export_default) AST_END(export_default) class Macro_t; AST_NODE(Export) ast_ptr def; ast_sel target; ast_ptr assign; AST_MEMBER(Export, &def, &target, &assign) AST_END(Export) AST_NODE(FnArgDef) ast_sel name; ast_ptr defaultValue; AST_MEMBER(FnArgDef, &name, &defaultValue) AST_END(FnArgDef) AST_NODE(FnArgDefList) ast_ptr sep; ast_list definitions; ast_ptr varArg; AST_MEMBER(FnArgDefList, &sep, &definitions, &varArg) AST_END(FnArgDefList) AST_NODE(outer_var_shadow) ast_ptr varList; AST_MEMBER(outer_var_shadow, &varList) AST_END(outer_var_shadow) AST_NODE(FnArgsDef) ast_ptr defList; ast_ptr shadowOption; AST_MEMBER(FnArgsDef, &defList, &shadowOption) AST_END(FnArgsDef) AST_LEAF(fn_arrow) AST_END(fn_arrow) AST_NODE(FunLit) ast_ptr argsDef; ast_ptr arrow; ast_ptr body; AST_MEMBER(FunLit, &argsDef, &arrow, &body) AST_END(FunLit) AST_LEAF(macro_type) AST_END(macro_type) AST_NODE(MacroName) ast_ptr name; AST_MEMBER(MacroName, &name) AST_END(MacroName) AST_NODE(MacroLit) ast_ptr argsDef; ast_ptr body; AST_MEMBER(MacroLit, &argsDef, &body) AST_END(MacroLit) AST_NODE(Macro) ast_ptr type; ast_ptr name; ast_ptr macroLit; AST_MEMBER(Macro, &type, &name, ¯oLit) AST_END(Macro) AST_NODE(NameOrDestructure) ast_sel item; AST_MEMBER(NameOrDestructure, &item) AST_END(NameOrDestructure) AST_NODE(AssignableNameList) ast_ptr sep; ast_list items; AST_MEMBER(AssignableNameList, &sep, &items) AST_END(AssignableNameList) AST_NODE(InvokeArgs) ast_ptr sep; ast_sel_list args; AST_MEMBER(InvokeArgs, &sep, &args) AST_END(InvokeArgs) AST_LEAF(const_value) AST_END(const_value) AST_NODE(unary_exp) ast_ptr item; AST_MEMBER(unary_exp, &item) AST_END(unary_exp) AST_NODE(ExpListAssign) ast_ptr expList; ast_sel action; AST_MEMBER(ExpListAssign, &expList, &action) AST_END(ExpListAssign) AST_NODE(if_else_line) ast_ptr condition; ast_ptr assign; ast_sel elseExpr; AST_MEMBER(if_else_line, &condition, &assign, &elseExpr) AST_END(if_else_line) AST_NODE(unless_line) ast_ptr condition; AST_MEMBER(unless_line, &condition) AST_END(unless_line) AST_NODE(statement_appendix) ast_sel item; AST_MEMBER(statement_appendix, &item) AST_END(statement_appendix) AST_LEAF(BreakLoop) AST_END(BreakLoop) AST_NODE(Statement) ast_sel content; ast_ptr appendix; AST_MEMBER(Statement, &content, &appendix) AST_END(Statement) class Block_t; AST_NODE(Body) ast_sel content; AST_MEMBER(Body, &content) AST_END(Body) AST_NODE(Block) ast_ptr sep; ast_list statements; AST_MEMBER(Block, &sep, &statements) AST_END(Block) AST_NODE(File) ast_ptr block; AST_MEMBER(File, &block) AST_END(File) } // namespace parserlib