From d20be11da9fef9309faf7f83078ed963a3b1e627 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 6 May 2022 14:29:10 +0800 Subject: add to_ast function. --- src/yuescript/ast.hpp | 3 + src/yuescript/yue_ast.h | 276 +++++++++++++++++++++-------------------- src/yuescript/yue_compiler.cpp | 2 +- src/yuescript/yuescript.cpp | 81 ++++++++++++ 4 files changed, 224 insertions(+), 138 deletions(-) diff --git a/src/yuescript/ast.hpp b/src/yuescript/ast.hpp index 6f89b52..4860fc9 100644 --- a/src/yuescript/ast.hpp +++ b/src/yuescript/ast.hpp @@ -16,6 +16,7 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND #include #include #include +#include #include "yuescript/parser.hpp" @@ -96,6 +97,8 @@ public: virtual int getId() const = 0; + virtual const std::string_view getName() const = 0; + template inline ast_ptr new_ptr() const { auto item = new T; diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index 06896b8..d255236 100755 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h @@ -11,6 +11,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI #include "yuescript/ast.hpp" namespace parserlib { +using namespace std::string_view_literals; #define AST_LEAF(type) \ COUNTER_INC; \ @@ -31,71 +32,72 @@ public: \ add_members({__VA_ARGS__}); \ } -#define AST_END(type) \ +#define AST_END(type, name) \ + virtual const std::string_view getName() const override { return name; } \ }; \ template<> constexpr int id() { return COUNTER_READ; } AST_LEAF(Num) -AST_END(Num) +AST_END(Num, "num"sv) AST_LEAF(Name) -AST_END(Name) +AST_END(Name, "name"sv) AST_NODE(Variable) ast_ptr name; AST_MEMBER(Variable, &name) -AST_END(Variable) +AST_END(Variable, "variable"sv) AST_NODE(LabelName) ast_ptr name; AST_MEMBER(LabelName, &name) -AST_END(LabelName) +AST_END(LabelName, "label_name"sv) AST_NODE(LuaKeyword) ast_ptr name; AST_MEMBER(LuaKeyword, &name) -AST_END(LuaKeyword) +AST_END(LuaKeyword, "lua_keyword"sv) AST_LEAF(self) -AST_END(self) +AST_END(self, "self"sv) AST_NODE(self_name) ast_ptr name; AST_MEMBER(self_name, &name) -AST_END(self_name) +AST_END(self_name, "self_name"sv) AST_LEAF(self_class) -AST_END(self_class) +AST_END(self_class, "self_name"sv) AST_NODE(self_class_name) ast_ptr name; AST_MEMBER(self_class_name, &name) -AST_END(self_class_name) +AST_END(self_class_name, "self_class_name"sv) AST_NODE(SelfName) ast_sel name; AST_MEMBER(SelfName, &name) -AST_END(SelfName) +AST_END(SelfName, "self_item"sv) AST_NODE(KeyName) ast_sel name; AST_MEMBER(KeyName, &name) -AST_END(KeyName) +AST_END(KeyName, "key_name"sv) AST_LEAF(VarArg) -AST_END(VarArg) +AST_END(VarArg, "var_arg"sv) AST_LEAF(local_flag) -AST_END(local_flag) +AST_END(local_flag, "local_flag"sv) AST_LEAF(Seperator) -AST_END(Seperator) +AST_END(Seperator, "seperator"sv) AST_NODE(NameList) ast_ptr sep; ast_list names; AST_MEMBER(NameList, &sep, &names) -AST_END(NameList) +AST_END(NameList, "name_list"sv) class ExpListLow_t; class TableBlock_t; @@ -105,7 +107,7 @@ AST_NODE(local_values) ast_ptr nameList; ast_sel valueList; AST_MEMBER(local_values, &nameList, &valueList) -AST_END(local_values) +AST_END(local_values, "local_values"sv) AST_NODE(Local) ast_sel item; @@ -114,43 +116,43 @@ AST_NODE(Local) bool collected = false; bool defined = false; AST_MEMBER(Local, &item) -AST_END(Local) +AST_END(Local, "local"sv) class Assign_t; AST_LEAF(Attrib) -AST_END(Attrib) +AST_END(Attrib, "attrib"sv) AST_NODE(LocalAttrib) ast_ptr attrib; ast_ptr nameList; ast_ptr assign; AST_MEMBER(LocalAttrib, &attrib, &nameList, &assign) -AST_END(LocalAttrib) +AST_END(LocalAttrib, "local_attrib"sv) AST_NODE(colon_import_name) ast_ptr name; AST_MEMBER(colon_import_name, &name) -AST_END(colon_import_name) +AST_END(colon_import_name, "colon_import_name"sv) class Exp_t; class TableLit_t; AST_LEAF(import_literal_inner) -AST_END(import_literal_inner) +AST_END(import_literal_inner, "import_literal_inner"sv) AST_NODE(ImportLiteral) ast_ptr sep; ast_sel_list inners; AST_MEMBER(ImportLiteral, &sep, &inners) -AST_END(ImportLiteral) +AST_END(ImportLiteral, "import_literal"sv) AST_NODE(ImportFrom) ast_ptr sep; ast_sel_list names; ast_ptr exp; AST_MEMBER(ImportFrom, &sep, &names, &exp) -AST_END(ImportFrom) +AST_END(ImportFrom, "import_from"sv) class MacroName_t; @@ -158,10 +160,10 @@ AST_NODE(macro_name_pair) ast_ptr key; ast_ptr value; AST_MEMBER(macro_name_pair, &key, &value) -AST_END(macro_name_pair) +AST_END(macro_name_pair, "macro_name_pair"sv) AST_LEAF(import_all_macro) -AST_END(import_all_macro) +AST_END(import_all_macro, "import_all_macro"sv) class variable_pair_t; class normal_pair_t; @@ -172,33 +174,33 @@ AST_NODE(ImportTabLit) ast_ptr sep; ast_sel_list items; AST_MEMBER(ImportTabLit, &sep, &items) -AST_END(ImportTabLit) +AST_END(ImportTabLit, "import_tab_lit"sv) AST_NODE(ImportAs) ast_ptr literal; ast_sel target; AST_MEMBER(ImportAs, &literal, &target) -AST_END(ImportAs) +AST_END(ImportAs, "import_as"sv) AST_NODE(Import) ast_sel content; AST_MEMBER(Import, &content) -AST_END(Import) +AST_END(Import, "import"sv) AST_NODE(Label) ast_ptr label; AST_MEMBER(Label, &label) -AST_END(Label) +AST_END(Label, "label"sv) AST_NODE(Goto) ast_ptr label; AST_MEMBER(Goto, &label) -AST_END(Goto) +AST_END(Goto, "goto"sv) class FnArgsDef_t; AST_LEAF(fn_arrow_back) -AST_END(fn_arrow_back) +AST_END(fn_arrow_back, "fn_arrow_back"sv) class ChainValue_t; @@ -207,19 +209,19 @@ AST_NODE(Backcall) ast_ptr arrow; ast_ptr value; AST_MEMBER(Backcall, &argsDef, &arrow, &value) -AST_END(Backcall) +AST_END(Backcall, "backcall"sv) AST_NODE(ExpListLow) ast_ptr sep; ast_list exprs; AST_MEMBER(ExpListLow, &sep, &exprs) -AST_END(ExpListLow) +AST_END(ExpListLow, "exp_list_low"sv) AST_NODE(ExpList) ast_ptr sep; ast_list exprs; AST_MEMBER(ExpList, &sep, &exprs) -AST_END(ExpList) +AST_END(ExpList, "exp_list"sv) class TableBlock_t; @@ -227,7 +229,7 @@ AST_NODE(Return) bool allowBlockMacroReturn = false; ast_sel valueList; AST_MEMBER(Return, &valueList) -AST_END(Return) +AST_END(Return, "return"sv) class existential_op_t; class Assign_t; @@ -240,13 +242,13 @@ AST_NODE(With) ast_ptr assigns; ast_sel body; AST_MEMBER(With, &eop, &valueList, &assigns, &body) -AST_END(With) +AST_END(With, "with"sv) AST_NODE(SwitchCase) ast_ptr valueList; ast_sel body; AST_MEMBER(SwitchCase, &valueList, &body) -AST_END(SwitchCase) +AST_END(SwitchCase, "switch_case"sv) AST_NODE(Switch) ast_ptr target; @@ -254,37 +256,37 @@ AST_NODE(Switch) ast_list branches; ast_sel lastBranch; AST_MEMBER(Switch, &target, &sep, &branches, &lastBranch) -AST_END(Switch) +AST_END(Switch, "switch"sv) AST_NODE(assignment) ast_ptr expList; ast_ptr assign; AST_MEMBER(assignment, &expList, &assign) -AST_END(assignment) +AST_END(assignment, "assignment"sv) AST_NODE(IfCond) ast_sel condition; AST_MEMBER(IfCond, &condition) -AST_END(IfCond) +AST_END(IfCond, "if_cond"sv) AST_LEAF(IfType) -AST_END(IfType) +AST_END(IfType, "if_type"sv) AST_NODE(If) ast_ptr type; ast_sel_list nodes; AST_MEMBER(If, &type, &nodes) -AST_END(If) +AST_END(If, "if"sv) AST_LEAF(WhileType) -AST_END(WhileType) +AST_END(WhileType, "while_type"sv) AST_NODE(While) ast_ptr type; ast_ptr condition; ast_sel body; AST_MEMBER(While, &type, &condition, &body) -AST_END(While) +AST_END(While, "while"sv) class Body_t; @@ -292,12 +294,12 @@ AST_NODE(Repeat) ast_ptr body; ast_ptr condition; AST_MEMBER(Repeat, &body, &condition) -AST_END(Repeat) +AST_END(Repeat, "repeat"sv) AST_NODE(for_step_value) ast_ptr value; AST_MEMBER(for_step_value, &value) -AST_END(for_step_value) +AST_END(for_step_value, "for_step_value"sv) AST_NODE(For) ast_ptr varName; @@ -306,7 +308,7 @@ AST_NODE(For) ast_ptr stepValue; ast_sel body; AST_MEMBER(For, &varName, &startValue, &stopValue, &stepValue, &body) -AST_END(For) +AST_END(For, "for"sv) class AssignableNameList_t; class star_exp_t; @@ -316,24 +318,24 @@ AST_NODE(ForEach) ast_sel loopValue; ast_sel body; AST_MEMBER(ForEach, &nameList, &loopValue, &body) -AST_END(ForEach) +AST_END(ForEach, "for_each"sv) AST_NODE(Do) ast_ptr body; AST_MEMBER(Do, &body) -AST_END(Do) +AST_END(Do, "do"sv) AST_NODE(catch_block) ast_ptr err; ast_ptr body; AST_MEMBER(catch_block, &err, &body) -AST_END(catch_block) +AST_END(catch_block, "catch_block"sv) AST_NODE(Try) ast_sel func; ast_ptr catchBlock; AST_MEMBER(Try, &func, &catchBlock) -AST_END(Try) +AST_END(Try, "try"sv) class CompInner_t; @@ -341,30 +343,30 @@ AST_NODE(Comprehension) ast_sel value; ast_ptr forLoop; AST_MEMBER(Comprehension, &value, &forLoop) -AST_END(Comprehension) +AST_END(Comprehension, "comp"sv) AST_NODE(comp_value) ast_ptr value; AST_MEMBER(comp_value, &value) -AST_END(comp_value) +AST_END(comp_value, "comp_value"sv) AST_NODE(TblComprehension) ast_ptr key; ast_ptr value; ast_ptr forLoop; AST_MEMBER(TblComprehension, &key, &value, &forLoop) -AST_END(TblComprehension) +AST_END(TblComprehension, "tbl_comp"sv) AST_NODE(star_exp) ast_ptr value; AST_MEMBER(star_exp, &value) -AST_END(star_exp) +AST_END(star_exp, "star_exp"sv) AST_NODE(CompForEach) ast_ptr nameList; ast_sel loopValue; AST_MEMBER(CompForEach, &nameList, &loopValue) -AST_END(CompForEach) +AST_END(CompForEach, "comp_for_each"sv) AST_NODE(CompFor) ast_ptr varName; @@ -372,13 +374,13 @@ AST_NODE(CompFor) ast_ptr stopValue; ast_ptr stepValue; AST_MEMBER(CompFor, &varName, &startValue, &stopValue, &stepValue) -AST_END(CompFor) +AST_END(CompFor, "comp_for"sv) AST_NODE(CompInner) ast_ptr sep; ast_sel_list items; AST_MEMBER(CompInner, &sep, &items) -AST_END(CompInner) +AST_END(CompInner, "comp_inner"sv) class TableBlock_t; @@ -386,29 +388,29 @@ AST_NODE(Assign) ast_ptr sep; ast_sel_list values; AST_MEMBER(Assign, &sep, &values) -AST_END(Assign) +AST_END(Assign, "assign"sv) AST_LEAF(update_op) -AST_END(update_op) +AST_END(update_op, "update_op"sv) AST_NODE(Update) ast_ptr op; ast_ptr value; AST_MEMBER(Update, &op, &value) -AST_END(Update) +AST_END(Update, "update"sv) AST_LEAF(BinaryOperator) -AST_END(BinaryOperator) +AST_END(BinaryOperator, "binary_op"sv) AST_LEAF(unary_operator) -AST_END(unary_operator) +AST_END(unary_operator, "unary_op"sv) class AssignableChain_t; AST_NODE(Assignable) ast_sel item; AST_MEMBER(Assignable, &item) -AST_END(Assignable) +AST_END(Assignable, "assignable"sv) class unary_exp_t; @@ -416,7 +418,7 @@ AST_NODE(exp_op_value) ast_ptr op; ast_list pipeExprs; AST_MEMBER(exp_op_value, &op, &pipeExprs) -AST_END(exp_op_value) +AST_END(exp_op_value, "exp_op_value"sv) AST_NODE(Exp) ast_ptr sep; @@ -424,7 +426,7 @@ AST_NODE(Exp) ast_list opValues; ast_ptr nilCoalesed; AST_MEMBER(Exp, &sep, &pipeExprs, &opValues, &nilCoalesed) -AST_END(Exp) +AST_END(Exp, "exp"sv) class Parens_t; class MacroName_t; @@ -432,12 +434,12 @@ class MacroName_t; AST_NODE(Callable) ast_sel item; AST_MEMBER(Callable, &item) -AST_END(Callable) +AST_END(Callable, "callable"sv) AST_NODE(variable_pair) ast_ptr name; AST_MEMBER(variable_pair, &name) -AST_END(variable_pair) +AST_END(variable_pair, "variable_pair"sv) class DoubleString_t; class SingleString_t; @@ -447,7 +449,7 @@ AST_NODE(normal_pair) ast_sel key; ast_sel value; AST_MEMBER(normal_pair, &key, &value) -AST_END(normal_pair) +AST_END(normal_pair, "normal_pair"sv) AST_NODE(default_pair) ast_sel key; @@ -455,18 +457,18 @@ AST_NODE(default_pair) ast_ptr value; ast_ptr defVal; AST_MEMBER(default_pair, &key, &sep, &value, &defVal) -AST_END(default_pair) +AST_END(default_pair, "default_pair"sv) AST_NODE(meta_variable_pair) ast_ptr name; AST_MEMBER(meta_variable_pair, &name) -AST_END(meta_variable_pair) +AST_END(meta_variable_pair, "meta_variable_pair"sv) AST_NODE(meta_normal_pair) ast_sel key; ast_sel value; AST_MEMBER(meta_normal_pair, &key, &value) -AST_END(meta_normal_pair) +AST_END(meta_normal_pair, "meta_normal_pair"sv) AST_NODE(meta_default_pair) ast_sel key; @@ -474,13 +476,13 @@ AST_NODE(meta_default_pair) ast_ptr value; ast_ptr defVal; AST_MEMBER(meta_default_pair, &key, &sep, &value, &defVal) -AST_END(meta_default_pair) +AST_END(meta_default_pair, "meta_default_pair"sv) AST_NODE(simple_table) ast_ptr sep; ast_sel_list pairs; AST_MEMBER(simple_table, &sep, &pairs) -AST_END(simple_table) +AST_END(simple_table, "simple_table"sv) class String_t; class const_value_t; @@ -497,64 +499,64 @@ AST_NODE(SimpleValue) TblComprehension_t, TableLit_t, Comprehension_t, FunLit_t, Num_t> value; AST_MEMBER(SimpleValue, &value) -AST_END(SimpleValue) +AST_END(SimpleValue, "simple_value"sv) AST_LEAF(LuaStringOpen) -AST_END(LuaStringOpen) +AST_END(LuaStringOpen, "lua_string_open"sv) AST_LEAF(LuaStringContent) -AST_END(LuaStringContent) +AST_END(LuaStringContent, "lua_string_content"sv) AST_LEAF(LuaStringClose) -AST_END(LuaStringClose) +AST_END(LuaStringClose, "lua_string_close"sv) AST_NODE(LuaString) ast_ptr open; ast_ptr content; ast_ptr close; AST_MEMBER(LuaString, &open, &content, &close) -AST_END(LuaString) +AST_END(LuaString, "lua_string"sv) AST_LEAF(SingleString) -AST_END(SingleString) +AST_END(SingleString, "single_string"sv) AST_LEAF(double_string_inner) -AST_END(double_string_inner) +AST_END(double_string_inner, "double_string_inner"sv) AST_NODE(double_string_content) ast_sel content; AST_MEMBER(double_string_content, &content) -AST_END(double_string_content) +AST_END(double_string_content, "double_string_content"sv) AST_NODE(DoubleString) ast_ptr sep; ast_list segments; AST_MEMBER(DoubleString, &sep, &segments) -AST_END(DoubleString) +AST_END(DoubleString, "double_string"sv) AST_NODE(String) ast_sel str; AST_MEMBER(String, &str) -AST_END(String) +AST_END(String, "string"sv) AST_LEAF(Metatable) -AST_END(Metatable) +AST_END(Metatable, "metatable"sv) AST_NODE(Metamethod) ast_ptr name; AST_MEMBER(Metamethod, &name) -AST_END(Metamethod) +AST_END(Metamethod, "metamethod"sv) AST_NODE(DotChainItem) ast_sel name; AST_MEMBER(DotChainItem, &name) -AST_END(DotChainItem) +AST_END(DotChainItem, "dot_chain_item"sv) AST_NODE(ColonChainItem) ast_sel name; bool switchToDot = false; AST_MEMBER(ColonChainItem, &name) -AST_END(ColonChainItem) +AST_END(ColonChainItem, "colon_chain_item"sv) class default_value_t; @@ -563,24 +565,24 @@ AST_NODE(Slice) ast_sel stopValue; ast_sel stepValue; AST_MEMBER(Slice, &startValue, &stopValue, &stepValue) -AST_END(Slice) +AST_END(Slice, "slice"sv) AST_NODE(Parens) ast_ptr expr; AST_MEMBER(Parens, &expr) -AST_END(Parens) +AST_END(Parens, "parens"sv) AST_NODE(Invoke) ast_ptr sep; ast_sel_list args; AST_MEMBER(Invoke, &sep, &args) -AST_END(Invoke) +AST_END(Invoke, "invoke"sv) AST_LEAF(existential_op) -AST_END(existential_op) +AST_END(existential_op, "existential_op"sv) AST_LEAF(table_appending_op) -AST_END(table_appending_op) +AST_END(table_appending_op, "table_appending_op"sv) class InvokeArgs_t; @@ -588,21 +590,21 @@ AST_NODE(ChainValue) ast_ptr sep; ast_sel_list items; AST_MEMBER(ChainValue, &sep, &items) -AST_END(ChainValue) +AST_END(ChainValue, "chain_value"sv) AST_NODE(AssignableChain) ast_ptr sep; ast_sel_list items; AST_MEMBER(AssignableChain, &sep, &items) -AST_END(AssignableChain) +AST_END(AssignableChain, "assignable_chain"sv) AST_NODE(Value) ast_sel item; AST_MEMBER(Value, &item) -AST_END(Value) +AST_END(Value, "value"sv) AST_LEAF(default_value) -AST_END(default_value) +AST_END(default_value, "default_value"sv) class default_pair_t; class meta_default_pair_t; @@ -610,7 +612,7 @@ class meta_default_pair_t; AST_NODE(SpreadExp) ast_ptr exp; AST_MEMBER(SpreadExp, &exp) -AST_END(SpreadExp) +AST_END(SpreadExp, "spread_exp"sv) AST_NODE(TableLit) ast_ptr sep; @@ -618,7 +620,7 @@ AST_NODE(TableLit) variable_pair_t, normal_pair_t, SpreadExp_t, Exp_t, default_pair_t, meta_variable_pair_t, meta_normal_pair_t, meta_default_pair_t> values; AST_MEMBER(TableLit, &sep, &values) -AST_END(TableLit) +AST_END(TableLit, "table_lit"sv) AST_NODE(TableBlockIndent) ast_ptr sep; @@ -626,26 +628,26 @@ AST_NODE(TableBlockIndent) variable_pair_t, normal_pair_t, TableBlockIndent_t, default_pair_t, meta_variable_pair_t, meta_normal_pair_t, meta_default_pair_t> values; AST_MEMBER(TableBlockIndent, &sep, &values) -AST_END(TableBlockIndent) +AST_END(TableBlockIndent, "table_block_indent"sv) AST_NODE(TableBlock) ast_ptr sep; ast_sel_list values; AST_MEMBER(TableBlock, &sep, &values) -AST_END(TableBlock) +AST_END(TableBlock, "table_block"sv) 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_END(class_member_list, "class_member_list"sv) AST_NODE(ClassBlock) ast_ptr sep; ast_sel_list contents; AST_MEMBER(ClassBlock, &sep, &contents) -AST_END(ClassBlock) +AST_END(ClassBlock, "class_block"sv) AST_NODE(ClassDecl) ast_ptr name; @@ -653,24 +655,24 @@ AST_NODE(ClassDecl) ast_ptr mixes; ast_ptr body; AST_MEMBER(ClassDecl, &name, &extend, &mixes, &body) -AST_END(ClassDecl) +AST_END(ClassDecl, "class_decl"sv) AST_NODE(global_values) ast_ptr nameList; ast_sel valueList; AST_MEMBER(global_values, &nameList, &valueList) -AST_END(global_values) +AST_END(global_values, "global_values"sv) AST_LEAF(global_op) -AST_END(global_op) +AST_END(global_op, "global_op"sv) AST_NODE(Global) ast_sel item; AST_MEMBER(Global, &item) -AST_END(Global) +AST_END(Global, "global"sv) AST_LEAF(export_default) -AST_END(export_default) +AST_END(export_default, "export_default"sv) class Macro_t; @@ -679,125 +681,125 @@ AST_NODE(Export) ast_sel target; ast_ptr assign; AST_MEMBER(Export, &def, &target, &assign) -AST_END(Export) +AST_END(Export, "export"sv) AST_NODE(FnArgDef) ast_sel name; ast_ptr op; ast_ptr defaultValue; AST_MEMBER(FnArgDef, &name, &op, &defaultValue) -AST_END(FnArgDef) +AST_END(FnArgDef, "fn_arg_def"sv) AST_NODE(FnArgDefList) ast_ptr sep; ast_list definitions; ast_ptr varArg; AST_MEMBER(FnArgDefList, &sep, &definitions, &varArg) -AST_END(FnArgDefList) +AST_END(FnArgDefList, "fn_arg_def_list"sv) AST_NODE(outer_var_shadow) ast_ptr varList; AST_MEMBER(outer_var_shadow, &varList) -AST_END(outer_var_shadow) +AST_END(outer_var_shadow, "outer_var_shadow"sv) AST_NODE(FnArgsDef) ast_ptr defList; ast_ptr shadowOption; AST_MEMBER(FnArgsDef, &defList, &shadowOption) -AST_END(FnArgsDef) +AST_END(FnArgsDef, "fn_args_def"sv) AST_LEAF(fn_arrow) -AST_END(fn_arrow) +AST_END(fn_arrow, "fn_arrow"sv) AST_NODE(FunLit) ast_ptr argsDef; ast_ptr arrow; ast_ptr body; AST_MEMBER(FunLit, &argsDef, &arrow, &body) -AST_END(FunLit) +AST_END(FunLit, "fun_lit"sv) AST_NODE(MacroName) ast_ptr name; AST_MEMBER(MacroName, &name) -AST_END(MacroName) +AST_END(MacroName, "macro_name"sv) AST_NODE(MacroLit) ast_ptr argsDef; ast_ptr body; AST_MEMBER(MacroLit, &argsDef, &body) -AST_END(MacroLit) +AST_END(MacroLit, "macro_lit"sv) AST_NODE(MacroInPlace) ast_ptr body; AST_MEMBER(MacroInPlace, &body) -AST_END(MacroInPlace) +AST_END(MacroInPlace, "macro_in_place"sv) AST_NODE(Macro) ast_ptr name; ast_ptr macroLit; AST_MEMBER(Macro, &name, ¯oLit) -AST_END(Macro) +AST_END(Macro, "macro"sv) AST_NODE(NameOrDestructure) ast_sel item; AST_MEMBER(NameOrDestructure, &item) -AST_END(NameOrDestructure) +AST_END(NameOrDestructure, "name_or_des"sv) AST_NODE(AssignableNameList) ast_ptr sep; ast_list items; AST_MEMBER(AssignableNameList, &sep, &items) -AST_END(AssignableNameList) +AST_END(AssignableNameList, "assignable_name_list"sv) AST_NODE(InvokeArgs) ast_ptr sep; ast_sel_list args; AST_MEMBER(InvokeArgs, &sep, &args) -AST_END(InvokeArgs) +AST_END(InvokeArgs, "invoke_args"sv) AST_LEAF(const_value) -AST_END(const_value) +AST_END(const_value, "const_value"sv) AST_NODE(unary_value) ast_list ops; ast_ptr value; AST_MEMBER(unary_value, &ops, &value) -AST_END(unary_value) +AST_END(unary_value, "unary_value"sv) AST_NODE(unary_exp) ast_list ops; ast_list expos; AST_MEMBER(unary_exp, &ops, &expos) -AST_END(unary_exp) +AST_END(unary_exp, "unary_exp"sv) AST_NODE(ExpListAssign) ast_ptr expList; ast_sel action; AST_MEMBER(ExpListAssign, &expList, &action) -AST_END(ExpListAssign) +AST_END(ExpListAssign, "exp_list_assign"sv) AST_NODE(if_line) ast_ptr type; ast_ptr condition; AST_MEMBER(if_line, &type, &condition) -AST_END(if_line) +AST_END(if_line, "if_line"sv) AST_LEAF(BreakLoop) -AST_END(BreakLoop) +AST_END(BreakLoop, "break_loop"sv) AST_NODE(PipeBody) ast_ptr sep; ast_list values; AST_MEMBER(PipeBody, &sep, &values) -AST_END(PipeBody) +AST_END(PipeBody, "pipe_body"sv) AST_NODE(statement_appendix) ast_sel item; AST_MEMBER(statement_appendix, &item) -AST_END(statement_appendix) +AST_END(statement_appendix, "statement_appendix"sv) AST_LEAF(statement_sep) -AST_END(statement_sep) +AST_END(statement_sep, "statement_sep"sv) AST_NODE(Statement) ast_sel appendix; ast_ptr needSep; AST_MEMBER(Statement, &content, &appendix, &needSep) -AST_END(Statement) +AST_END(Statement, "statement"sv) class Block_t; AST_NODE(Body) ast_sel content; AST_MEMBER(Body, &content) -AST_END(Body) +AST_END(Body, "body"sv) AST_NODE(Block) ast_ptr sep; ast_list statements; AST_MEMBER(Block, &sep, &statements) -AST_END(Block) +AST_END(Block, "block"sv) AST_NODE(BlockEnd) ast_ptr block; AST_MEMBER(BlockEnd, &block) -AST_END(BlockEnd) +AST_END(BlockEnd, "block_end"sv) AST_NODE(File) ast_ptr block; AST_MEMBER(File, &block) -AST_END(File) +AST_END(File, "file"sv) } // namespace parserlib diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 1546b85..7d7bf25 100755 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -60,7 +60,7 @@ using namespace parserlib; typedef std::list str_list; -const std::string_view version = "0.10.16"sv; +const std::string_view version = "0.10.17"sv; const std::string_view extension = "yue"sv; class YueCompilerImpl { diff --git a/src/yuescript/yuescript.cpp b/src/yuescript/yuescript.cpp index 5e21fc5..2e96f16 100644 --- a/src/yuescript/yuescript.cpp +++ b/src/yuescript/yuescript.cpp @@ -5,7 +5,9 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of 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. */ + #include "yuescript/yue_compiler.h" +#include "yuescript/yue_parser.h" #if defined(YUE_BUILD_AS_DLL) #define YUE_API __declspec(dllexport) @@ -130,8 +132,87 @@ static int yuetolua(lua_State* L) { return 3; } +static int yuetoast(lua_State* L) { + size_t size = 0; + const char* input = luaL_checklstring(L, 1, &size); + int flattenLevel = 2; + if (lua_isnoneornil(L, 2) == 0) { + flattenLevel = static_cast(luaL_checkinteger(L, 2)); + flattenLevel = std::max(std::min(2, flattenLevel), 0); + } + yue::YueParser parser; + auto info = parser.parse({input, size}); + if (info.node) { + lua_createtable(L, 0, 0); + int cacheIndex = lua_gettop(L); + auto getName = [&](yue::ast_node* node) { + int id = node->getId(); + lua_rawgeti(L, cacheIndex, id); + if (lua_isnil(L, -1) != 0) { + lua_pop(L, 1); + auto name = node->getName(); + lua_pushlstring(L, &name.front(), name.length()); + lua_pushvalue(L, -1); + lua_rawseti(L, cacheIndex, id); + } + }; + std::function visit; + visit = [&](yue::ast_node* node) { + int count = 0; + bool hasSep = false; + node->visitChild([&](yue::ast_node* child) { + if (yue::ast_is(child)) { + hasSep = true; + return false; + } + count++; + visit(child); + return false; + }); + switch (count) { + case 0: { + lua_createtable(L, 2, 0); + getName(node); + lua_rawseti(L, -2, 1); + auto str = parser.toString(node); + yue::Utils::trim(str); + lua_pushlstring(L, str.c_str(), str.length()); + lua_rawseti(L, -2, 2); + break; + } + case 1: { + if (flattenLevel > 1 || (flattenLevel == 1 && !hasSep)) { + getName(node); + lua_rawseti(L, -2, 1); + break; + } + } + default: { + lua_createtable(L, count + 1, 0); + getName(node); + lua_rawseti(L, -2, 1); + for (int i = count, j = 2; i >= 1; i--, j++) { + lua_pushvalue(L, -1 - i); + lua_rawseti(L, -2, j); + } + lua_insert(L, -1 - count); + lua_pop(L, count); + break; + } + } + }; + visit(info.node); + return 1; + } else { + lua_pushnil(L); + lua_pushlstring(L, info.error.c_str(), info.error.length()); + return 2; + } +} + static const luaL_Reg yuelib[] = { {"to_lua", yuetolua}, + {"to_ast", yuetoast}, {"version", nullptr}, {"options", nullptr}, {"load_stacktraceplus", nullptr}, -- cgit v1.2.3-55-g6feb