From b6c86d19d74ac90a2450cf979a92187e691ea5fa Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 9 Aug 2024 02:55:56 +0800 Subject: add yue.is_ast(). --- src/yuescript/yue_ast.h | 322 +++++++++++++++++++++-------------------- src/yuescript/yue_compiler.cpp | 48 +++--- src/yuescript/yue_parser.cpp | 27 +++- src/yuescript/yue_parser.h | 21 ++- src/yuescript/yuescript.cpp | 26 +++- 5 files changed, 256 insertions(+), 188 deletions(-) (limited to 'src') diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index b287202..7bf17fe 100644 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h @@ -12,13 +12,17 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI namespace parserlib { +template +std::string_view ast_name() { } + #define AST_LEAF(type) \ COUNTER_INC; \ namespace yue { \ class type##_t : public ast_node { \ public: \ virtual int get_id() const override { return COUNTER_READ; } \ - virtual std::string to_string(void*) const override; + virtual std::string to_string(void*) const override; \ + virtual const std::string_view get_name() const override { return #type ""sv; } #define AST_NODE(type) \ COUNTER_INC; \ @@ -26,20 +30,22 @@ namespace parserlib { class type##_t : public ast_container { \ public: \ virtual int get_id() const override { return COUNTER_READ; } \ - virtual std::string to_string(void*) const override; + virtual std::string to_string(void*) const override; \ + virtual const std::string_view get_name() const override { return #type ""sv; } #define AST_MEMBER(type, ...) \ type##_t() { \ add_members({__VA_ARGS__}); \ } -#define AST_END(type, name) \ - virtual const std::string_view get_name() const override { return name; } \ +#define AST_END(type) \ } \ ; \ } \ template <> \ - constexpr int id() { return COUNTER_READ; } + constexpr int id() { return COUNTER_READ; } \ + template <> \ + constexpr std::string_view ast_name() { return #type ""sv; } // clang-format off @@ -85,75 +91,75 @@ class Value_t; } // namespace yue AST_LEAF(Num) -AST_END(Num, "num"sv) +AST_END(Num) AST_LEAF(Name) -AST_END(Name, "name"sv) +AST_END(Name) AST_LEAF(UnicodeName) -AST_END(UnicodeName, "unicode_name"sv) +AST_END(UnicodeName) AST_NODE(Variable) ast_sel name; AST_MEMBER(Variable, &name) -AST_END(Variable, "variable"sv) +AST_END(Variable) AST_NODE(LabelName) ast_ptr name; AST_MEMBER(LabelName, &name) -AST_END(LabelName, "label_name"sv) +AST_END(LabelName) AST_NODE(LuaKeyword) ast_ptr name; AST_MEMBER(LuaKeyword, &name) -AST_END(LuaKeyword, "lua_keyword"sv) +AST_END(LuaKeyword) AST_LEAF(Self) -AST_END(Self, "self"sv) +AST_END(Self) AST_NODE(SelfName) ast_sel name; AST_MEMBER(SelfName, &name) -AST_END(SelfName, "self_name"sv) +AST_END(SelfName) AST_LEAF(SelfClass) -AST_END(SelfClass, "self_class"sv) +AST_END(SelfClass) AST_NODE(SelfClassName) ast_sel name; AST_MEMBER(SelfClassName, &name) -AST_END(SelfClassName, "self_class_name"sv) +AST_END(SelfClassName) AST_NODE(SelfItem) ast_sel name; AST_MEMBER(SelfItem, &name) -AST_END(SelfItem, "self_item"sv) +AST_END(SelfItem) AST_NODE(KeyName) ast_sel name; AST_MEMBER(KeyName, &name) -AST_END(KeyName, "key_name"sv) +AST_END(KeyName) AST_LEAF(VarArg) -AST_END(VarArg, "var_arg"sv) +AST_END(VarArg) AST_LEAF(LocalFlag) -AST_END(LocalFlag, "local_flag"sv) +AST_END(LocalFlag) AST_LEAF(Seperator) -AST_END(Seperator, "seperator"sv) +AST_END(Seperator) AST_NODE(NameList) ast_ptr sep; ast_list names; AST_MEMBER(NameList, &sep, &names) -AST_END(NameList, "name_list"sv) +AST_END(NameList) AST_NODE(LocalValues) ast_ptr nameList; ast_sel valueList; AST_MEMBER(LocalValues, &nameList, &valueList) -AST_END(LocalValues, "local_values"sv) +AST_END(LocalValues) AST_NODE(Local) ast_sel item; @@ -162,13 +168,13 @@ AST_NODE(Local) bool collected = false; bool defined = false; AST_MEMBER(Local, &item) -AST_END(Local, "local"sv) +AST_END(Local) AST_LEAF(ConstAttrib) -AST_END(ConstAttrib, "const"sv) +AST_END(ConstAttrib) AST_LEAF(CloseAttrib) -AST_END(CloseAttrib, "close"sv) +AST_END(CloseAttrib) AST_NODE(LocalAttrib) ast_sel attrib; @@ -176,104 +182,104 @@ AST_NODE(LocalAttrib) ast_sel_list leftList; ast_ptr assign; AST_MEMBER(LocalAttrib, &attrib, &sep, &leftList, &assign) -AST_END(LocalAttrib, "local_attrib"sv) +AST_END(LocalAttrib) AST_NODE(ColonImportName) ast_ptr name; AST_MEMBER(ColonImportName, &name) -AST_END(ColonImportName, "colon_import_name"sv) +AST_END(ColonImportName) AST_LEAF(ImportLiteralInner) -AST_END(ImportLiteralInner, "import_literal_inner"sv) +AST_END(ImportLiteralInner) AST_NODE(ImportLiteral) ast_ptr sep; ast_sel_list inners; AST_MEMBER(ImportLiteral, &sep, &inners) -AST_END(ImportLiteral, "import_literal"sv) +AST_END(ImportLiteral) AST_NODE(ImportFrom) ast_ptr sep; ast_sel_list names; ast_sel item; AST_MEMBER(ImportFrom, &sep, &names, &item) -AST_END(ImportFrom, "import_from"sv) +AST_END(ImportFrom) AST_NODE(FromImport) ast_sel item; ast_ptr sep; ast_sel_list names; AST_MEMBER(FromImport, &item, &sep, &names) -AST_END(FromImport, "from_import"sv) +AST_END(FromImport) AST_NODE(MacroNamePair) ast_ptr key; ast_ptr value; AST_MEMBER(MacroNamePair, &key, &value) -AST_END(MacroNamePair, "macro_name_pair"sv) +AST_END(MacroNamePair) AST_LEAF(ImportAllMacro) -AST_END(ImportAllMacro, "import_all_macro"sv) +AST_END(ImportAllMacro) AST_NODE(ImportTabLit) ast_ptr sep; ast_sel_list items; AST_MEMBER(ImportTabLit, &sep, &items) -AST_END(ImportTabLit, "import_tab_lit"sv) +AST_END(ImportTabLit) AST_NODE(ImportAs) ast_ptr literal; ast_sel target; AST_MEMBER(ImportAs, &literal, &target) -AST_END(ImportAs, "import_as"sv) +AST_END(ImportAs) AST_NODE(Import) ast_sel content; AST_MEMBER(Import, &content) -AST_END(Import, "import"sv) +AST_END(Import) AST_NODE(Label) ast_ptr label; AST_MEMBER(Label, &label) -AST_END(Label, "label"sv) +AST_END(Label) AST_NODE(Goto) ast_ptr label; AST_MEMBER(Goto, &label) -AST_END(Goto, "goto"sv) +AST_END(Goto) AST_NODE(ShortTabAppending) ast_ptr assign; AST_MEMBER(ShortTabAppending, &assign) -AST_END(ShortTabAppending, "short_table_appending"sv) +AST_END(ShortTabAppending) AST_LEAF(FnArrowBack) -AST_END(FnArrowBack, "fn_arrow_back"sv) +AST_END(FnArrowBack) AST_NODE(Backcall) ast_ptr argsDef; ast_ptr arrow; ast_ptr value; AST_MEMBER(Backcall, &argsDef, &arrow, &value) -AST_END(Backcall, "backcall"sv) +AST_END(Backcall) AST_NODE(ExpListLow) ast_ptr sep; ast_list exprs; AST_MEMBER(ExpListLow, &sep, &exprs) -AST_END(ExpListLow, "exp_list_low"sv) +AST_END(ExpListLow) AST_NODE(ExpList) ast_ptr sep; ast_list exprs; AST_MEMBER(ExpList, &sep, &exprs) -AST_END(ExpList, "exp_list"sv) +AST_END(ExpList) AST_NODE(Return) bool allowBlockMacroReturn = false; ast_sel valueList; AST_MEMBER(Return, &valueList) -AST_END(Return, "return"sv) +AST_END(Return) AST_NODE(With) ast_ptr eop; @@ -281,19 +287,19 @@ AST_NODE(With) ast_ptr assigns; ast_sel body; AST_MEMBER(With, &eop, &valueList, &assigns, &body) -AST_END(With, "with"sv) +AST_END(With) AST_NODE(SwitchList) ast_ptr sep; ast_list exprs; AST_MEMBER(SwitchList, &sep, &exprs) -AST_END(SwitchList, "switch_list"sv) +AST_END(SwitchList) AST_NODE(SwitchCase) ast_ptr condition; ast_sel body; AST_MEMBER(SwitchCase, &condition, &body) -AST_END(SwitchCase, "switch_case"sv) +AST_END(SwitchCase) AST_NODE(Switch) ast_ptr target; @@ -301,49 +307,49 @@ AST_NODE(Switch) ast_list branches; ast_sel lastBranch; AST_MEMBER(Switch, &target, &sep, &branches, &lastBranch) -AST_END(Switch, "switch"sv) +AST_END(Switch) AST_NODE(Assignment) ast_ptr expList; ast_ptr assign; AST_MEMBER(Assignment, &expList, &assign) -AST_END(Assignment, "assignment"sv) +AST_END(Assignment) AST_NODE(IfCond) ast_ptr condition; ast_ptr assignment; AST_MEMBER(IfCond, &condition, &assignment) -AST_END(IfCond, "if_cond"sv) +AST_END(IfCond) AST_LEAF(IfType) -AST_END(IfType, "if_type"sv) +AST_END(IfType) AST_NODE(If) ast_ptr type; ast_sel_list nodes; AST_MEMBER(If, &type, &nodes) -AST_END(If, "if"sv) +AST_END(If) AST_LEAF(WhileType) -AST_END(WhileType, "while_type"sv) +AST_END(WhileType) AST_NODE(While) ast_ptr type; ast_ptr condition; ast_sel body; AST_MEMBER(While, &type, &condition, &body) -AST_END(While, "while"sv) +AST_END(While) AST_NODE(Repeat) ast_ptr body; ast_ptr condition; AST_MEMBER(Repeat, &body, &condition) -AST_END(Repeat, "repeat"sv) +AST_END(Repeat) AST_NODE(ForStepValue) ast_ptr value; AST_MEMBER(ForStepValue, &value) -AST_END(ForStepValue, "for_step_value"sv) +AST_END(ForStepValue) AST_NODE(For) ast_ptr varName; @@ -352,61 +358,61 @@ AST_NODE(For) ast_ptr stepValue; ast_sel body; AST_MEMBER(For, &varName, &startValue, &stopValue, &stepValue, &body) -AST_END(For, "for"sv) +AST_END(For) AST_NODE(ForEach) ast_ptr nameList; ast_sel loopValue; ast_sel body; AST_MEMBER(ForEach, &nameList, &loopValue, &body) -AST_END(ForEach, "for_each"sv) +AST_END(ForEach) AST_NODE(Do) ast_ptr body; AST_MEMBER(Do, &body) -AST_END(Do, "do"sv) +AST_END(Do) AST_NODE(CatchBlock) ast_ptr err; ast_ptr block; AST_MEMBER(CatchBlock, &err, &block) -AST_END(CatchBlock, "catch_block"sv) +AST_END(CatchBlock) AST_NODE(Try) ast_sel func; ast_ptr catchBlock; AST_MEMBER(Try, &func, &catchBlock) -AST_END(Try, "try"sv) +AST_END(Try) AST_NODE(Comprehension) ast_ptr sep; ast_sel_list items; AST_MEMBER(Comprehension, &sep, &items) -AST_END(Comprehension, "comp"sv) +AST_END(Comprehension) AST_NODE(CompValue) ast_ptr value; AST_MEMBER(CompValue, &value) -AST_END(CompValue, "comp_value"sv) +AST_END(CompValue) AST_NODE(TblComprehension) ast_ptr key; ast_ptr value; ast_ptr forLoop; AST_MEMBER(TblComprehension, &key, &value, &forLoop) -AST_END(TblComprehension, "tbl_comp"sv) +AST_END(TblComprehension) AST_NODE(StarExp) ast_ptr value; AST_MEMBER(StarExp, &value) -AST_END(StarExp, "star_exp"sv) +AST_END(StarExp) AST_NODE(CompForEach) ast_ptr nameList; ast_sel loopValue; AST_MEMBER(CompForEach, &nameList, &loopValue) -AST_END(CompForEach, "comp_for_each"sv) +AST_END(CompForEach) AST_NODE(CompFor) ast_ptr varName; @@ -414,54 +420,54 @@ AST_NODE(CompFor) ast_ptr stopValue; ast_ptr stepValue; AST_MEMBER(CompFor, &varName, &startValue, &stopValue, &stepValue) -AST_END(CompFor, "comp_for"sv) +AST_END(CompFor) AST_NODE(CompInner) ast_ptr sep; ast_sel_list items; AST_MEMBER(CompInner, &sep, &items) -AST_END(CompInner, "comp_inner"sv) +AST_END(CompInner) AST_NODE(Assign) ast_ptr sep; ast_sel_list values; AST_MEMBER(Assign, &sep, &values) -AST_END(Assign, "assign"sv) +AST_END(Assign) AST_LEAF(UpdateOp) -AST_END(UpdateOp, "update_op"sv) +AST_END(UpdateOp) AST_NODE(Update) ast_ptr op; ast_ptr value; AST_MEMBER(Update, &op, &value) -AST_END(Update, "update"sv) +AST_END(Update) AST_LEAF(BinaryOperator) -AST_END(BinaryOperator, "binary_op"sv) +AST_END(BinaryOperator) AST_LEAF(UnaryOperator) -AST_END(UnaryOperator, "unary_op"sv) +AST_END(UnaryOperator) AST_LEAF(NotIn) -AST_END(NotIn, "not_in"sv) +AST_END(NotIn) AST_NODE(In) ast_ptr not_; ast_ptr value; AST_MEMBER(In, ¬_, &value) -AST_END(In, "in"sv) +AST_END(In) AST_NODE(Assignable) ast_sel item; AST_MEMBER(Assignable, &item) -AST_END(Assignable, "assignable"sv) +AST_END(Assignable) AST_NODE(ExpOpValue) ast_ptr op; ast_list pipeExprs; AST_MEMBER(ExpOpValue, &op, &pipeExprs) -AST_END(ExpOpValue, "exp_op_value"sv) +AST_END(ExpOpValue) AST_NODE(Exp) ast_ptr sep; @@ -469,71 +475,71 @@ AST_NODE(Exp) ast_list opValues; ast_ptr nilCoalesed; AST_MEMBER(Exp, &sep, &pipeExprs, &opValues, &nilCoalesed) -AST_END(Exp, "exp"sv) +AST_END(Exp) AST_NODE(Callable) ast_sel item; AST_MEMBER(Callable, &item) -AST_END(Callable, "callable"sv) +AST_END(Callable) AST_NODE(VariablePair) ast_ptr name; AST_MEMBER(VariablePair, &name) -AST_END(VariablePair, "variable_pair"sv) +AST_END(VariablePair) AST_NODE(VariablePairDef) ast_ptr pair; ast_ptr defVal; AST_MEMBER(VariablePairDef, &pair, &defVal) -AST_END(VariablePairDef, "variable_pair_def"sv) +AST_END(VariablePairDef) AST_NODE(NormalPair) ast_sel key; ast_sel value; AST_MEMBER(NormalPair, &key, &value) -AST_END(NormalPair, "normal_pair"sv) +AST_END(NormalPair) AST_NODE(NormalPairDef) ast_ptr pair; ast_ptr defVal; AST_MEMBER(NormalPairDef, &pair, &defVal) -AST_END(NormalPairDef, "normal_pair_def"sv) +AST_END(NormalPairDef) AST_NODE(NormalDef) ast_ptr item; ast_ptr sep; ast_ptr defVal; AST_MEMBER(NormalDef, &item, &sep, &defVal) -AST_END(NormalDef, "normal_def") +AST_END(NormalDef) AST_NODE(MetaVariablePair) ast_ptr name; AST_MEMBER(MetaVariablePair, &name) -AST_END(MetaVariablePair, "meta_variable_pair"sv) +AST_END(MetaVariablePair) AST_NODE(MetaVariablePairDef) ast_ptr pair; ast_ptr defVal; AST_MEMBER(MetaVariablePairDef, &pair, &defVal) -AST_END(MetaVariablePairDef, "meta_variable_pair_def"sv) +AST_END(MetaVariablePairDef) AST_NODE(MetaNormalPair) ast_sel key; ast_sel value; AST_MEMBER(MetaNormalPair, &key, &value) -AST_END(MetaNormalPair, "meta_normal_pair"sv) +AST_END(MetaNormalPair) AST_NODE(MetaNormalPairDef) ast_ptr pair; ast_ptr defVal; AST_MEMBER(MetaNormalPairDef, &pair, &defVal) -AST_END(MetaNormalPairDef, "meta_normal_pair_def"sv) +AST_END(MetaNormalPairDef) AST_NODE(SimpleTable) ast_ptr sep; ast_sel_list pairs; AST_MEMBER(SimpleTable, &sep, &pairs) -AST_END(SimpleTable, "simple_table"sv) +AST_END(SimpleTable) AST_NODE(SimpleValue) ast_sel value; AST_MEMBER(SimpleValue, &value) -AST_END(SimpleValue, "simple_value"sv) +AST_END(SimpleValue) AST_LEAF(LuaStringOpen) -AST_END(LuaStringOpen, "lua_string_open"sv) +AST_END(LuaStringOpen) AST_LEAF(LuaStringContent) -AST_END(LuaStringContent, "lua_string_content"sv) +AST_END(LuaStringContent) AST_LEAF(LuaStringClose) -AST_END(LuaStringClose, "lua_string_close"sv) +AST_END(LuaStringClose) AST_NODE(LuaString) ast_ptr open; ast_ptr content; ast_ptr close; AST_MEMBER(LuaString, &open, &content, &close) -AST_END(LuaString, "lua_string"sv) +AST_END(LuaString) AST_LEAF(SingleString) -AST_END(SingleString, "single_string"sv) +AST_END(SingleString) AST_LEAF(DoubleStringInner) -AST_END(DoubleStringInner, "double_string_inner"sv) +AST_END(DoubleStringInner) AST_NODE(DoubleStringContent) ast_sel content; AST_MEMBER(DoubleStringContent, &content) -AST_END(DoubleStringContent, "double_string_content"sv) +AST_END(DoubleStringContent) AST_NODE(DoubleString) ast_ptr sep; ast_list segments; AST_MEMBER(DoubleString, &sep, &segments) -AST_END(DoubleString, "double_string"sv) +AST_END(DoubleString) AST_NODE(String) ast_sel str; AST_MEMBER(String, &str) -AST_END(String, "string"sv) +AST_END(String) AST_LEAF(Metatable) -AST_END(Metatable, "metatable"sv) +AST_END(Metatable) AST_NODE(Metamethod) ast_sel item; AST_MEMBER(Metamethod, &item) -AST_END(Metamethod, "metamethod"sv) +AST_END(Metamethod) AST_NODE(DotChainItem) ast_sel name; AST_MEMBER(DotChainItem, &name) -AST_END(DotChainItem, "dot_chain_item"sv) +AST_END(DotChainItem) AST_NODE(ColonChainItem) ast_sel name; bool switchToDot = false; AST_MEMBER(ColonChainItem, &name) -AST_END(ColonChainItem, "colon_chain_item"sv) +AST_END(ColonChainItem) AST_NODE(Slice) ast_sel startValue; ast_sel stopValue; ast_sel stepValue; AST_MEMBER(Slice, &startValue, &stopValue, &stepValue) -AST_END(Slice, "slice"sv) +AST_END(Slice) AST_NODE(Parens) ast_ptr expr; AST_MEMBER(Parens, &expr) -AST_END(Parens, "parens"sv) +AST_END(Parens) AST_NODE(Invoke) ast_ptr sep; ast_sel_list args; AST_MEMBER(Invoke, &sep, &args) -AST_END(Invoke, "invoke"sv) +AST_END(Invoke) AST_LEAF(ExistentialOp) -AST_END(ExistentialOp, "existential_op"sv) +AST_END(ExistentialOp) AST_LEAF(TableAppendingOp) -AST_END(TableAppendingOp, "table_appending_op"sv) +AST_END(TableAppendingOp) AST_NODE(ChainValue) ast_ptr sep; ast_sel_list items; AST_MEMBER(ChainValue, &sep, &items) -AST_END(ChainValue, "chain_value"sv) +AST_END(ChainValue) AST_NODE(AssignableChain) ast_ptr sep; ast_sel_list items; AST_MEMBER(AssignableChain, &sep, &items) -AST_END(AssignableChain, "assignable_chain"sv) +AST_END(AssignableChain) AST_NODE(Value) ast_sel item; AST_MEMBER(Value, &item) -AST_END(Value, "value"sv) +AST_END(Value) AST_LEAF(DefaultValue) -AST_END(DefaultValue, "default_value"sv) +AST_END(DefaultValue) AST_NODE(SpreadExp) ast_ptr exp; AST_MEMBER(SpreadExp, &exp) -AST_END(SpreadExp, "spread_exp"sv) +AST_END(SpreadExp) AST_NODE(SpreadListExp) ast_ptr exp; AST_MEMBER(SpreadListExp, &exp) -AST_END(SpreadListExp, "spread_list_exp"sv) +AST_END(SpreadListExp) AST_NODE(TableLit) ast_ptr sep; @@ -666,7 +672,7 @@ AST_NODE(TableLit) MetaVariablePair_t, MetaNormalPair_t, /*non-syntax-rule*/ TableBlockIndent_t, SpreadListExp_t> values; AST_MEMBER(TableLit, &sep, &values) -AST_END(TableLit, "table_lit"sv) +AST_END(TableLit) AST_NODE(TableBlockIndent) ast_ptr sep; @@ -674,25 +680,25 @@ AST_NODE(TableBlockIndent) VariablePair_t, NormalPair_t, Exp_t, TableBlockIndent_t, MetaVariablePair_t, MetaNormalPair_t> values; AST_MEMBER(TableBlockIndent, &sep, &values) -AST_END(TableBlockIndent, "table_block_indent"sv) +AST_END(TableBlockIndent) AST_NODE(TableBlock) ast_ptr sep; ast_sel_list values; AST_MEMBER(TableBlock, &sep, &values) -AST_END(TableBlock, "table_block"sv) +AST_END(TableBlock) AST_NODE(ClassMemberList) ast_ptr sep; ast_sel_list values; AST_MEMBER(ClassMemberList, &sep, &values) -AST_END(ClassMemberList, "class_member_list"sv) +AST_END(ClassMemberList) AST_NODE(ClassBlock) ast_ptr sep; ast_sel_list contents; AST_MEMBER(ClassBlock, &sep, &contents) -AST_END(ClassBlock, "class_block"sv) +AST_END(ClassBlock) AST_NODE(ClassDecl) ast_ptr name; @@ -700,59 +706,59 @@ AST_NODE(ClassDecl) ast_ptr mixes; ast_ptr body; AST_MEMBER(ClassDecl, &name, &extend, &mixes, &body) -AST_END(ClassDecl, "class_decl"sv) +AST_END(ClassDecl) AST_NODE(GlobalValues) ast_ptr nameList; ast_sel valueList; AST_MEMBER(GlobalValues, &nameList, &valueList) -AST_END(GlobalValues, "global_values"sv) +AST_END(GlobalValues) AST_LEAF(GlobalOp) -AST_END(GlobalOp, "global_op"sv) +AST_END(GlobalOp) AST_NODE(Global) ast_sel item; AST_MEMBER(Global, &item) -AST_END(Global, "global"sv) +AST_END(Global) AST_LEAF(ExportDefault) -AST_END(ExportDefault, "export_default"sv) +AST_END(ExportDefault) AST_NODE(Export) ast_ptr def; ast_sel target; ast_ptr assign; AST_MEMBER(Export, &def, &target, &assign) -AST_END(Export, "export"sv) +AST_END(Export) AST_NODE(FnArgDef) ast_sel name; ast_ptr op; ast_ptr defaultValue; AST_MEMBER(FnArgDef, &name, &op, &defaultValue) -AST_END(FnArgDef, "fn_arg_def"sv) +AST_END(FnArgDef) AST_NODE(FnArgDefList) ast_ptr sep; ast_list definitions; ast_ptr varArg; AST_MEMBER(FnArgDefList, &sep, &definitions, &varArg) -AST_END(FnArgDefList, "fn_arg_def_list"sv) +AST_END(FnArgDefList) AST_NODE(OuterVarShadow) ast_ptr varList; AST_MEMBER(OuterVarShadow, &varList) -AST_END(OuterVarShadow, "outer_var_shadow"sv) +AST_END(OuterVarShadow) AST_NODE(FnArgsDef) ast_ptr defList; ast_ptr shadowOption; AST_MEMBER(FnArgsDef, &defList, &shadowOption) -AST_END(FnArgsDef, "fn_args_def"sv) +AST_END(FnArgsDef) AST_LEAF(FnArrow) -AST_END(FnArrow, "fn_arrow"sv) +AST_END(FnArrow) AST_NODE(FunLit) ast_ptr argsDef; @@ -761,121 +767,121 @@ AST_NODE(FunLit) ast_ptr body; bool noRecursion = false; AST_MEMBER(FunLit, &argsDef, &defaultReturn, &arrow, &body) -AST_END(FunLit, "fun_lit"sv) +AST_END(FunLit) AST_NODE(MacroName) ast_ptr name; AST_MEMBER(MacroName, &name) -AST_END(MacroName, "macro_name"sv) +AST_END(MacroName) AST_NODE(MacroLit) ast_ptr argsDef; ast_ptr body; AST_MEMBER(MacroLit, &argsDef, &body) -AST_END(MacroLit, "macro_lit"sv) +AST_END(MacroLit) AST_NODE(MacroFunc) ast_ptr name; ast_sel invoke; AST_MEMBER(MacroFunc, &name, &invoke) -AST_END(MacroFunc, "macro_func"sv) +AST_END(MacroFunc) AST_NODE(MacroInPlace) ast_ptr body; AST_MEMBER(MacroInPlace, &body) -AST_END(MacroInPlace, "macro_in_place"sv) +AST_END(MacroInPlace) AST_NODE(Macro) ast_ptr name; ast_sel decl; AST_MEMBER(Macro, &name, &decl) -AST_END(Macro, "macro"sv) +AST_END(Macro) AST_NODE(NameOrDestructure) ast_sel item; AST_MEMBER(NameOrDestructure, &item) -AST_END(NameOrDestructure, "name_or_des"sv) +AST_END(NameOrDestructure) AST_NODE(AssignableNameList) ast_ptr sep; ast_list items; AST_MEMBER(AssignableNameList, &sep, &items) -AST_END(AssignableNameList, "assignable_name_list"sv) +AST_END(AssignableNameList) AST_NODE(InvokeArgs) ast_ptr sep; ast_sel_list args; AST_MEMBER(InvokeArgs, &sep, &args) -AST_END(InvokeArgs, "invoke_args"sv) +AST_END(InvokeArgs) AST_LEAF(ConstValue) -AST_END(ConstValue, "const_value"sv) +AST_END(ConstValue) AST_NODE(UnaryValue) ast_list ops; ast_ptr value; AST_MEMBER(UnaryValue, &ops, &value) -AST_END(UnaryValue, "unary_value"sv) +AST_END(UnaryValue) AST_NODE(UnaryExp) ast_list ops; ast_list expos; ast_ptr inExp; AST_MEMBER(UnaryExp, &ops, &expos, &inExp) -AST_END(UnaryExp, "unary_exp"sv) +AST_END(UnaryExp) AST_NODE(ExpListAssign) ast_ptr expList; ast_sel action; AST_MEMBER(ExpListAssign, &expList, &action) -AST_END(ExpListAssign, "exp_list_assign"sv) +AST_END(ExpListAssign) AST_NODE(IfLine) ast_ptr type; ast_ptr condition; AST_MEMBER(IfLine, &type, &condition) -AST_END(IfLine, "if_line"sv) +AST_END(IfLine) AST_NODE(WhileLine) ast_ptr type; ast_ptr condition; AST_MEMBER(WhileLine, &type, &condition) -AST_END(WhileLine, "while_line"sv) +AST_END(WhileLine) AST_LEAF(BreakLoop) -AST_END(BreakLoop, "break_loop"sv) +AST_END(BreakLoop) AST_NODE(PipeBody) ast_ptr sep; ast_list values; AST_MEMBER(PipeBody, &sep, &values) -AST_END(PipeBody, "pipe_body"sv) +AST_END(PipeBody) AST_NODE(StatementAppendix) ast_sel item; AST_MEMBER(StatementAppendix, &item) -AST_END(StatementAppendix, "statement_appendix"sv) +AST_END(StatementAppendix) AST_LEAF(StatementSep) -AST_END(StatementSep, "statement_sep"sv) +AST_END(StatementSep) AST_LEAF(YueLineComment) -AST_END(YueLineComment, "comment"sv) +AST_END(YueLineComment) AST_LEAF(MultilineCommentInner) -AST_END(MultilineCommentInner, "comment"sv) +AST_END(MultilineCommentInner) AST_NODE(YueMultilineComment) ast_ptr inner; AST_MEMBER(YueMultilineComment, &inner) -AST_END(YueMultilineComment, "comment"sv) +AST_END(YueMultilineComment) AST_NODE(ChainAssign) ast_ptr sep; ast_list exprs; ast_ptr assign; AST_MEMBER(ChainAssign, &sep, &exprs, &assign) -AST_END(ChainAssign, "chain_assign") +AST_END(ChainAssign) AST_NODE(Statement) ast_ptr sep; @@ -888,28 +894,28 @@ AST_NODE(Statement) > content; ast_ptr appendix; AST_MEMBER(Statement, &sep, &comments, &content, &appendix) -AST_END(Statement, "statement"sv) +AST_END(Statement) AST_NODE(Body) ast_sel content; AST_MEMBER(Body, &content) -AST_END(Body, "body"sv) +AST_END(Body) AST_NODE(Block) ast_ptr sep; ast_list statements; AST_MEMBER(Block, &sep, &statements) -AST_END(Block, "block"sv) +AST_END(Block) AST_NODE(BlockEnd) ast_ptr block; AST_MEMBER(BlockEnd, &block) -AST_END(BlockEnd, "block_end"sv) +AST_END(BlockEnd) AST_NODE(File) ast_ptr block; AST_MEMBER(File, &block) -AST_END(File, "file"sv) +AST_END(File) // clang-format on diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 994ab57..a661356 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -29,7 +29,7 @@ extern "C" { } // extern "C" // name of table stored in lua registry -#define YUE_MODULE "__yue_modules__" +#define YUE_MODULES "__yue_modules__" #if LUA_VERSION_NUM > 501 #ifndef LUA_COMPAT_5_1 @@ -75,7 +75,7 @@ static std::unordered_set Metamethods = { "close"s // Lua 5.4 }; -const std::string_view version = "0.23.9"sv; +const std::string_view version = "0.24.0"sv; const std::string_view extension = "yue"sv; class CompileError : public std::logic_error { @@ -136,8 +136,8 @@ public: _sameModule = true; int top = lua_gettop(L); DEFER(lua_settop(L, top)); - lua_pushliteral(L, YUE_MODULE); // YUE_MODULE - lua_rawget(L, LUA_REGISTRYINDEX); // reg[YUE_MODULE], tb + lua_pushliteral(L, YUE_MODULES); // YUE_MODULES + lua_rawget(L, LUA_REGISTRYINDEX); // reg[YUE_MODULES], tb BREAK_IF(lua_istable(L, -1) == 0); int idx = static_cast(lua_objlen(L, -1)); // idx = #tb, tb BREAK_IF(idx == 0); @@ -351,8 +351,8 @@ public: if (!_sameModule) { int top = lua_gettop(L); DEFER(lua_settop(L, top)); - lua_pushliteral(L, YUE_MODULE); // YUE_MODULE - lua_rawget(L, LUA_REGISTRYINDEX); // reg[YUE_MODULE], tb + lua_pushliteral(L, YUE_MODULES); // YUE_MODULES + lua_rawget(L, LUA_REGISTRYINDEX); // reg[YUE_MODULES], tb int idx = static_cast(lua_objlen(L, -1)); lua_pushnil(L); // tb nil lua_rawseti(L, -2, idx); // tb[idx] = nil, tb @@ -370,7 +370,7 @@ private: std::function _luaOpen; #endif // YUE_NO_MACRO YueConfig _config; - YueParser _parser; + YueParser& _parser = YueParser::shared(); ParseInfo _info; int _indentOffset = 0; struct VarArgState { @@ -4960,8 +4960,8 @@ private: void pushCurrentModule() { if (_useModule) { - lua_pushliteral(L, YUE_MODULE); // YUE_MODULE - lua_rawget(L, LUA_REGISTRYINDEX); // reg[YUE_MODULE], tb + lua_pushliteral(L, YUE_MODULES); // YUE_MODULES + lua_rawget(L, LUA_REGISTRYINDEX); // reg[YUE_MODULES], tb int idx = static_cast(lua_objlen(L, -1)); // idx = #tb, tb lua_rawgeti(L, -1, idx); // tb[idx], tb cur lua_remove(L, -2); // cur @@ -4987,14 +4987,14 @@ private: } _stateOwner = true; } - lua_pushliteral(L, YUE_MODULE); // YUE_MODULE - lua_rawget(L, LUA_REGISTRYINDEX); // reg[YUE_MODULE], tb + lua_pushliteral(L, YUE_MODULES); // YUE_MODULES + lua_rawget(L, LUA_REGISTRYINDEX); // reg[YUE_MODULES], tb if (lua_isnil(L, -1) != 0) { // tb == nil lua_pop(L, 1); lua_newtable(L); // tb - lua_pushliteral(L, YUE_MODULE); // tb YUE_MODULE + lua_pushliteral(L, YUE_MODULES); // tb YUE_MODULES lua_pushvalue(L, -2); // tb YUE_MODULE tb - lua_rawset(L, LUA_REGISTRYINDEX); // reg[YUE_MODULE] = tb, tb + lua_rawset(L, LUA_REGISTRYINDEX); // reg[YUE_MODULES] = tb, tb } // tb int idx = static_cast(lua_objlen(L, -1)); // idx = #tb, tb lua_newtable(L); // tb cur @@ -5016,7 +5016,7 @@ private: bool isModuleLoaded(std::string_view name) { int top = lua_gettop(L); DEFER(lua_settop(L, top)); - lua_pushliteral(L, YUE_MODULE); // YUE_MODULE + lua_pushliteral(L, YUE_MODULES); // YUE_MODULES lua_rawget(L, LUA_REGISTRYINDEX); // modules lua_pushlstring(L, &name.front(), name.size()); lua_rawget(L, -2); // modules module @@ -5027,7 +5027,7 @@ private: } void pushModuleTable(std::string_view name) { - lua_pushliteral(L, YUE_MODULE); // YUE_MODULE + lua_pushliteral(L, YUE_MODULES); // YUE_MODULES lua_rawget(L, LUA_REGISTRYINDEX); // modules lua_pushlstring(L, &name.front(), name.size()); lua_rawget(L, -2); // modules module @@ -9869,9 +9869,17 @@ private: if (importAllMacro) { lua_pushnil(L); // cur mod startKey while (lua_next(L, -2) != 0) { // cur mod key value - lua_pushvalue(L, -2); // cur mod key value key - lua_insert(L, -2); // cur mod key key value - lua_rawset(L, -5); // cur[key] = value, cur mod key + const char* key = lua_tostring(L, -2); + auto it = std::find_if(macroPairs.begin(), macroPairs.end(), [&](const auto& item) { + return key == item.first; + }); + if (it == macroPairs.end()) { + lua_pushvalue(L, -2); // cur mod key value key + lua_insert(L, -2); // cur mod key key value + lua_rawset(L, -5); // cur[key] = value, cur mod key + } else { + lua_pop(L, 1); // cur mod key + } } } for (const auto& pair : macroPairs) { @@ -10696,9 +10704,9 @@ CompileInfo YueCompiler::compile(std::string_view codes, const YueConfig& config void YueCompiler::clear(void* luaState) { #ifndef YUE_NO_MACRO auto L = static_cast(luaState); - lua_pushliteral(L, YUE_MODULE); // YUE_MODULE + lua_pushliteral(L, YUE_MODULES); // YUE_MODULES lua_pushnil(L); - lua_rawset(L, LUA_REGISTRYINDEX); // reg[YUE_MODULE] = nil + lua_rawset(L, LUA_REGISTRYINDEX); // reg[YUE_MODULES] = nil #else (void)(luaState); #endif // YUE_NO_MACRO diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index 986f67a..83aba40 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp @@ -309,7 +309,7 @@ YueParser::YueParser() { ); MacroNamePair = MacroName >> ':' >> space >> MacroName; - ImportAllMacro = '$'; + ImportAllMacro = '$' >> not_(UnicodeName); import_tab_item = VariablePair | NormalPair | @@ -324,6 +324,7 @@ YueParser::YueParser() { push_indent_match >> (space >> import_tab_list >> pop_indent | pop_indent) ) | space; import_tab_lines = space_break >> import_tab_line >> *(-(space >> ',') >> space_break >> import_tab_line) >> -(space >> ','); + import_tab_key_value = key_value | ':' >> MacroName | MacroNamePair | ImportAllMacro; ImportTabLit = ( '{' >> Seperator >> -(space >> import_tab_list) >> @@ -332,7 +333,7 @@ YueParser::YueParser() { white >> '}' ) | ( - Seperator >> key_value >> *(space >> ',' >> space >> key_value) + Seperator >> import_tab_key_value >> *(space >> ',' >> space >> import_tab_key_value) ); ImportAs = ImportLiteral >> -(space >> key("as") >> space >> (ImportTabLit | Variable | ImportAllMacro)); @@ -1083,6 +1084,23 @@ ParseInfo YueParser::parse(std::string_view codes, rule& r) { return res; } +ParseInfo YueParser::parse(std::string_view astName, std::string_view codes) { + auto it = _rules.find(astName); + if (it != _rules.end()) { + return parse(codes, *it->second); + } + return {}; +} + +bool YueParser::match(std::string_view astName, std::string_view codes) { + auto it = _rules.find(astName); + if (it != _rules.end()) { + auto rEnd = rule(*it->second >> eof()); + return parse(codes, rEnd).node; + } + return false; +} + std::string YueParser::toString(ast_node* node) { return _converter.to_bytes(std::wstring(node->m_begin.m_it, node->m_end.m_it)); } @@ -1091,6 +1109,11 @@ std::string YueParser::toString(input::iterator begin, input::iterator end) { return _converter.to_bytes(std::wstring(begin, end)); } +YueParser& YueParser::shared() { + thread_local static YueParser parser; + return parser; +} + namespace Utils { void replace(std::string& str, std::string_view from, std::string_view to) { size_t start_pos = 0; diff --git a/src/yuescript/yue_parser.h b/src/yuescript/yue_parser.h index 6623653..3c50602 100644 --- a/src/yuescript/yue_parser.h +++ b/src/yuescript/yue_parser.h @@ -16,6 +16,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI #include #include #include +#include #include #include "yuescript/ast.hpp" @@ -55,7 +56,7 @@ struct identity { #define AST_RULE(type) \ rule type; \ - ast type##_impl = type; \ + ast type##_impl{collect(ast_name(), type)}; \ inline rule& getRule(identity) { return type; } #else // NDEBUG #define NONE_AST_RULE(type) \ @@ -63,7 +64,7 @@ struct identity { #define AST_RULE(type) \ rule type{#type, rule::initTag{}}; \ - ast type##_impl = type; \ + ast type##_impl{collect(ast_name(), type)}; \ inline rule& getRule(identity) { return type; } #endif // NDEBUG @@ -72,19 +73,21 @@ extern std::unordered_set Keywords; class YueParser { public: - YueParser(); - template ParseInfo parse(std::string_view codes) { return parse(codes, getRule()); } + ParseInfo parse(std::string_view astName, std::string_view codes); + template bool match(std::string_view codes) { auto rEnd = rule(getRule() >> eof()); return parse(codes, rEnd).node; } + bool match(std::string_view astName, std::string_view codes); + template bool startWith(std::string_view codes) { return startWith(codes, getRule()); @@ -93,7 +96,10 @@ public: std::string toString(ast_node* node); std::string toString(input::iterator begin, input::iterator end); + static YueParser& shared(); + protected: + YueParser(); ParseInfo parse(std::string_view codes, rule& r); bool startWith(std::string_view codes, rule& r); @@ -125,6 +131,7 @@ protected: private: Converter _converter; + std::unordered_map _rules; template inline rule& getRule(identity) { @@ -132,6 +139,11 @@ private: return cut; } + inline rule& collect(std::string_view name, rule& rule) { + _rules[name] = rule.this_ptr(); + return rule; + } + NONE_AST_RULE(empty_block_error); NONE_AST_RULE(leading_spaces_error); NONE_AST_RULE(indentation_error); @@ -188,6 +200,7 @@ private: NONE_AST_RULE(import_tab_list); NONE_AST_RULE(import_tab_line); NONE_AST_RULE(import_tab_lines); + NONE_AST_RULE(import_tab_key_value); NONE_AST_RULE(with_exp); NONE_AST_RULE(disable_do); NONE_AST_RULE(enable_do); diff --git a/src/yuescript/yuescript.cpp b/src/yuescript/yuescript.cpp index 2de5571..84a9554 100644 --- a/src/yuescript/yuescript.cpp +++ b/src/yuescript/yuescript.cpp @@ -180,7 +180,7 @@ static int yueformat(lua_State* L) { tabSize = static_cast(luaL_checkinteger(L, 2)); } std::string_view codes(input, len); - auto info = yue::YueParser{}.parse(codes); + auto info = yue::YueParser::shared().parse(codes); if (info.error) { const auto& error = info.error.value(); if (!info.codes) { @@ -271,12 +271,19 @@ static int yuetoast(lua_State* L) { size_t size = 0; const char* input = luaL_checklstring(L, 1, &size); int flattenLevel = 0; - if (lua_isnoneornil(L, 2) == 0) { + if (!lua_isnoneornil(L, 2)) { flattenLevel = static_cast(luaL_checkinteger(L, 2)); flattenLevel = std::max(std::min(2, flattenLevel), 0); } - yue::YueParser parser; - auto info = parser.parse({input, size}); + std::string_view ruleName; + if (!lua_isnoneornil(L, 3)) { + size_t nameSize = 0; + if (auto name = luaL_checklstring(L, 3, &nameSize)) { + ruleName = {name, nameSize}; + } + } + auto& yueParser = yue::YueParser::shared(); + auto info = ruleName.empty() ? yueParser.parse({input, size}) : yueParser.parse(ruleName, {input, size}); if (!info.error) { lua_createtable(L, 0, 0); int tableIndex = lua_gettop(L); @@ -413,9 +420,20 @@ static int yuetoast(lua_State* L) { } } +static int yueisast(lua_State* L) { + size_t nameLen = 0; + auto name = luaL_tolstring(L, 1, &nameLen); + size_t codeLen = 0; + auto code = luaL_tolstring(L, 2, &codeLen); + bool result = yue::YueParser::shared().match({name, nameLen}, {code, codeLen}); + lua_pushboolean(L, result ? 1 : 0); + return 1; +} + static const luaL_Reg yuelib[] = { {"to_lua", yuetolua}, {"to_ast", yuetoast}, + {"is_ast", yueisast}, {"check", yuecheck}, {"format", yueformat}, {"version", nullptr}, -- cgit v1.2.3-55-g6feb