From 1f9a4180a3b54f85f37919ba4ca709f8e8250bb6 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 22 May 2016 22:39:26 +0800 Subject: macro_rules - Rework pattern matching to not use try-catch --- src/macro_rules/mod.cpp | 203 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 203 insertions(+) create mode 100644 src/macro_rules/mod.cpp (limited to 'src/macro_rules/mod.cpp') diff --git a/src/macro_rules/mod.cpp b/src/macro_rules/mod.cpp new file mode 100644 index 00000000..e3699a3d --- /dev/null +++ b/src/macro_rules/mod.cpp @@ -0,0 +1,203 @@ +/* + */ +#include +#include "macro_rules.hpp" +#include +#include +#include +#include + +#include "pattern_checks.hpp" + +bool is_token_path(eTokenType tt) { + switch(tt) + { + case TOK_IDENT: + case TOK_DOUBLE_COLON: + case TOK_LT: + case TOK_DOUBLE_LT: + case TOK_RWORD_SELF: + case TOK_RWORD_SUPER: + return true; + default: + return false; + } +} +bool is_token_pat(eTokenType tt) { + if( is_token_path(tt) ) + return true; + switch( tt ) + { + case TOK_PAREN_OPEN: + case TOK_SQUARE_OPEN: + + case TOK_AMP: + case TOK_RWORD_BOX: + case TOK_RWORD_REF: + case TOK_RWORD_MUT: + case TOK_STRING: + case TOK_INTEGER: + case TOK_CHAR: + return true; + default: + return false; + } +} +bool is_token_type(eTokenType tt) { + if( is_token_path(tt) ) + return true; + switch( tt ) + { + case TOK_PAREN_OPEN: + case TOK_SQUARE_OPEN: + case TOK_STAR: + case TOK_AMP: + return true; + default: + return false; + } +} +bool is_token_expr(eTokenType tt) { + if( is_token_path(tt) ) + return true; + switch( tt ) + { + case TOK_AMP: + case TOK_STAR: + case TOK_PAREN_OPEN: + case TOK_MACRO: + case TOK_DASH: + + case TOK_INTEGER: + case TOK_STRING: + return true; + default: + return false; + } +} +bool is_token_stmt(eTokenType tt) { + if( is_token_expr(tt) ) + return true; + switch( tt ) + { + case TOK_BRACE_OPEN: + case TOK_RWORD_LET: + return true; + default: + return false; + } +} + +MacroRulesPtr::~MacroRulesPtr() +{ + if(m_ptr) + { + delete m_ptr; + m_ptr = nullptr; + } +} +SERIALISE_TYPE(MacroRulesPtr::, "MacroRulesPtr", { +},{ +}) + +SERIALISE_TYPE_S(MacroRulesArm, { +}) +SERIALISE_TYPE_S(MacroRulesPatFrag, { +}) + +void operator%(Serialiser& s, MacroPatEnt::Type c) { + switch(c) { + #define _(v) case MacroPatEnt::v: s << #v; return + _(PAT_TOKEN); + _(PAT_TT); + _(PAT_PAT); + _(PAT_TYPE); + _(PAT_EXPR); + _(PAT_LOOP); + _(PAT_STMT); + _(PAT_PATH); + _(PAT_BLOCK); + _(PAT_META); + _(PAT_IDENT); + #undef _ + } +} +void operator%(::Deserialiser& s, MacroPatEnt::Type& c) { + ::std::string n; + s.item(n); + #define _(v) else if(n == #v) c = MacroPatEnt::v + if(0) ; + _(PAT_TOKEN); + _(PAT_TT); + _(PAT_PAT); + _(PAT_TYPE); + _(PAT_EXPR); + _(PAT_LOOP); + //_(PAT_OPTLOOP); + _(PAT_STMT); + _(PAT_PATH); + _(PAT_BLOCK); + _(PAT_META); + _(PAT_IDENT); + else + throw ::std::runtime_error( FMT("No conversion for '" << n << "'") ); + #undef _ +} +SERIALISE_TYPE_S(MacroPatEnt, { + s % type; + s.item(name); + s.item(tok); + s.item(subpats); +}); +::std::ostream& operator<<(::std::ostream& os, const MacroPatEnt& x) +{ + switch(x.type) + { + case MacroPatEnt::PAT_TOKEN: os << "=" << x.tok; break; + case MacroPatEnt::PAT_LOOP: os << "loop w/ " << x.tok << " [" << x.subpats << "]"; break; + default: + os << "$" << x.name << ":"; + switch(x.type) + { + case MacroPatEnt::PAT_TOKEN: throw ""; + case MacroPatEnt::PAT_LOOP: throw ""; + case MacroPatEnt::PAT_TT: os << "tt"; break; + case MacroPatEnt::PAT_PAT: os << "pat"; break; + case MacroPatEnt::PAT_IDENT: os << "ident"; break; + case MacroPatEnt::PAT_PATH: os << "path"; break; + case MacroPatEnt::PAT_TYPE: os << "type"; break; + case MacroPatEnt::PAT_EXPR: os << "expr"; break; + case MacroPatEnt::PAT_STMT: os << "stmt"; break; + case MacroPatEnt::PAT_BLOCK: os << "block"; break; + case MacroPatEnt::PAT_META: os << "meta"; break; + } + break; + } + return os; +} + +SERIALISE_TYPE_S(MacroRuleEnt, { + s.item(name); + s.item(tok); + s.item(subpats); +}); + +::std::ostream& operator<<(::std::ostream& os, const MacroRuleEnt& x) +{ + if(x.name.size()) + os << "$"<