#include #include #include #include #include #include #include "moon_ast.h" input& trim(input& s) { s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](input::value_type ch) { return !std::isspace(ch); })); s.erase(std::find_if(s.rbegin(), s.rend(), [](input::value_type ch) { return !std::isspace(ch); }).base(), s.end()); return s; } const input& AstLeaf::getValue() { if (_value.empty()) { _value.assign(m_begin.m_it, m_end.m_it); return trim(_value); } return _value; } #define AST_IMPL(type) \ ast __##type##_t(type); AST_IMPL(Num) AST_IMPL(_Name) AST_IMPL(Name) AST_IMPL(self) AST_IMPL(self_name) AST_IMPL(self_class) AST_IMPL(self_class_name) AST_IMPL(SelfName) AST_IMPL(KeyName) AST_IMPL(VarArg) AST_IMPL(local_flag) AST_IMPL(Seperator) AST_IMPL(NameList) AST_IMPL(Local) AST_IMPL(colon_import_name) AST_IMPL(ImportName) AST_IMPL(Import) AST_IMPL(ExpListLow) AST_IMPL(ExpList) AST_IMPL(Return) AST_IMPL(With) AST_IMPL(SwitchCase) AST_IMPL(Switch) AST_IMPL(IfCond) AST_IMPL(IfElseIf) AST_IMPL(If) AST_IMPL(Unless) AST_IMPL(While) AST_IMPL(for_step_value) AST_IMPL(For) AST_IMPL(ForEach) AST_IMPL(Do) AST_IMPL(Comprehension) AST_IMPL(comp_value) AST_IMPL(TblComprehension) AST_IMPL(star_exp) AST_IMPL(CompForEach) AST_IMPL(CompFor) AST_IMPL(CompClause) AST_IMPL(CompInner) AST_IMPL(Assign) AST_IMPL(update_op) AST_IMPL(Update) AST_IMPL(BinaryOperator) AST_IMPL(Assignable) AST_IMPL(exp_op_value) AST_IMPL(Exp) AST_IMPL(Callable) AST_IMPL(ChainValue) AST_IMPL(simple_table) AST_IMPL(SimpleValue) AST_IMPL(Chain) AST_IMPL(Value) AST_IMPL(LuaString) AST_IMPL(SingleString) AST_IMPL(double_string_inner) AST_IMPL(double_string_content) AST_IMPL(DoubleString) AST_IMPL(String) AST_IMPL(Parens) AST_IMPL(FnArgs) AST_IMPL(chain_call) AST_IMPL(chain_item) AST_IMPL(DotChainItem) AST_IMPL(ColonChainItem) AST_IMPL(chain_dot_chain) AST_IMPL(ChainItem) AST_IMPL(ChainItems) AST_IMPL(invoke_chain) AST_IMPL(ColonChain) AST_IMPL(default_value) AST_IMPL(Slice) AST_IMPL(Invoke) AST_IMPL(TableValue) AST_IMPL(TableLit) AST_IMPL(TableBlock) AST_IMPL(class_member_list) AST_IMPL(ClassLine) AST_IMPL(ClassBlock) AST_IMPL(ClassDecl) AST_IMPL(export_values) AST_IMPL(export_op) AST_IMPL(Export) AST_IMPL(variable_pair) AST_IMPL(normal_pair) AST_IMPL(KeyValue) AST_IMPL(FnArgDef) AST_IMPL(FnArgDefList) AST_IMPL(outer_var_shadow) AST_IMPL(FnArgsDef) AST_IMPL(fn_arrow) AST_IMPL(FunLit) AST_IMPL(NameOrDestructure) AST_IMPL(AssignableNameList) AST_IMPL(ArgBlock) AST_IMPL(invoke_args_with_table) AST_IMPL(InvokeArgs) AST_IMPL(const_value) AST_IMPL(unary_exp) AST_IMPL(Assignment) AST_IMPL(if_else_line) AST_IMPL(unless_line) AST_IMPL(statement_appendix) AST_IMPL(BreakLoop) AST_IMPL(Statement) AST_IMPL(Body) AST_IMPL(Line) AST_IMPL(Block) AST_IMPL(BlockEnd) #include int main() { std::string s = R"TestCodesHere( thing = { var: 10, hello: "world", func: => @var } import hello, \func from thing )TestCodesHere"; input i = Converter{}.from_bytes(s); error_list el; BlockEnd_t* root = nullptr; State st; if (parse(i, BlockEnd, el, root, &st)) { std::cout << "matched!\n"; int indent = 0; root->visit([&](ast_node* node) { if (std::string("Seperator") != node->getName()) { indent++; for (int i = 0; i < indent; i++) std::cout << " "; std::cout << "{" << node->getName() << "\n"; } return false; }, [&](ast_node* node) { if (std::string("Seperator") != node->getName()) { for (int i = 0; i < indent; i++) std::cout << " "; std::cout << "}\n" ; indent--; } return false; }); } else { std::cout << "not matched!\n"; for (error_list::iterator it = el.begin(); it != el.end(); ++it) { const error& err = *it; std::cout << "line " << err.m_begin.m_line << ", col " << err.m_begin.m_col << ": syntax error\n"; } } system("pause"); return 0; }