diff options
author | John Hodge <tpg@mutabah.net> | 2016-05-22 22:39:26 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2016-05-22 22:39:26 +0800 |
commit | 1f9a4180a3b54f85f37919ba4ca709f8e8250bb6 (patch) | |
tree | c00cef798d26bf5f68f1657b01a490533365f015 /src/macro_rules/mod.cpp | |
parent | eba9d8f840a5b81f0188d109e63631c9a34c28e9 (diff) | |
download | mrust-1f9a4180a3b54f85f37919ba4ca709f8e8250bb6.tar.gz |
macro_rules - Rework pattern matching to not use try-catch
Diffstat (limited to 'src/macro_rules/mod.cpp')
-rw-r--r-- | src/macro_rules/mod.cpp | 203 |
1 files changed, 203 insertions, 0 deletions
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 <common.hpp>
+#include "macro_rules.hpp"
+#include <parse/parseerror.hpp>
+#include <parse/tokentree.hpp>
+#include <parse/common.hpp>
+#include <limits.h>
+
+#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 << "$"<<x.name;
+ else if( x.subpats.size() )
+ os << "expand w/ " << x.tok << " [" << x.subpats << "]";
+ else
+ os << "=" << x.tok;
+ return os;
+}
+
+MacroRules::~MacroRules()
+{
+}
+SERIALISE_TYPE_S(MacroRules, {
+ s.item( m_exported );
+ s.item( m_rules );
+});
+
|