summaryrefslogtreecommitdiff
path: root/src/expand/macro_rules.cpp
diff options
context:
space:
mode:
authorJohn Hodge <tpg@mutabah.net>2016-03-07 11:09:38 +0800
committerJohn Hodge <tpg@mutabah.net>2016-03-07 11:09:38 +0800
commit30edd863f98df929326f2706c9a2ed32730a225b (patch)
tree300ada6a6d231ecff9b002b0a09d4866f273cd20 /src/expand/macro_rules.cpp
parent150a481100bba025bc5132338ae3d37f19de5bfc (diff)
downloadmrust-30edd863f98df929326f2706c9a2ed32730a225b.tar.gz
Expand - Start on macro_rules expanding
Diffstat (limited to 'src/expand/macro_rules.cpp')
-rw-r--r--src/expand/macro_rules.cpp50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/expand/macro_rules.cpp b/src/expand/macro_rules.cpp
index d860aaa3..4ef39c01 100644
--- a/src/expand/macro_rules.cpp
+++ b/src/expand/macro_rules.cpp
@@ -3,6 +3,7 @@
#include "../ast/expr.hpp"
#include "../ast/ast.hpp"
#include "../parse/common.hpp"
+#include "macro_rules.hpp"
class CMacroRulesExpander:
public ExpandProcMacro
@@ -18,11 +19,60 @@ class CMacroRulesExpander:
TTStream lex(tt);
auto mac = Parse_MacroRules(lex);
// TODO: Place into current module using `ident` as the name
+ mod.add_macro( false, ident, mac );
return AST::Expr();
}
};
+class CMacroUseHandler:
+ public ExpandDecorator
+{
+ AttrStage stage() const override { return AttrStage::EarlyPost; }
+
+ void handle(const AST::MetaItem& mi, AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item& i) const override
+ {
+ if( !i.is_Module() )
+ throw ::std::runtime_error("ERROR: Use of #[macro_use] on non-module");
+
+ const auto& submod = i.as_Module().e;
+
+ if( mi.has_sub_items() )
+ {
+ }
+ else
+ {
+ for( const auto& mr : submod.macros() )
+ {
+ mod.add_macro_import( mr.name, mr.data );
+ }
+ }
+
+ throw ::std::runtime_error("TODO: #[macro_use]");
+ }
+
+};
+
+AST::Expr Macro_Invoke(const char* name, const MacroRules& rules, const TokenTree& tt, AST::Module& mod, MacroPosition position)
+{
+ auto out_tt = Macro_InvokeRules(name, rules, tt);
+ TokenStream& lex = *out_tt;
+ // TODO: Expand out_tt
+ switch(position)
+ {
+ case MacroPosition::Item: {
+ LList<AST::Module*> l(nullptr, &mod);
+ Parse_ModRoot_Items(lex, mod, l, false, "-");
+ return AST::Expr();
+ }
+ default:
+ throw ::std::runtime_error("TODO: Macro in non-item position");
+ //case MacroPosition::Stmt: {
+ // break; }
+ }
+}
+
STATIC_MACRO("macro_rules", CMacroRulesExpander);
+STATIC_DECORATOR("macro_use", CMacroUseHandler);