summaryrefslogtreecommitdiff
path: root/src/expand
diff options
context:
space:
mode:
authorJohn Hodge <tpg@mutabah.net>2016-03-09 14:12:48 +0800
committerJohn Hodge <tpg@mutabah.net>2016-03-09 14:12:48 +0800
commitaeccbb23b717985e8719b3758e3cd28a0c0052f4 (patch)
tree7ccad85d2354817991c6148bb663f733a72151dc /src/expand
parent0571d0f4741106bcb43b512a66747e582b12ead7 (diff)
downloadmrust-aeccbb23b717985e8719b3758e3cd28a0c0052f4.tar.gz
Parse - Remove module stack (was for macros), yield stream from macros
Diffstat (limited to 'src/expand')
-rw-r--r--src/expand/macro_rules.cpp23
-rw-r--r--src/expand/macro_rules.hpp3
-rw-r--r--src/expand/mod.cpp36
3 files changed, 33 insertions, 29 deletions
diff --git a/src/expand/macro_rules.cpp b/src/expand/macro_rules.cpp
index fac6dec1..5a8e02c4 100644
--- a/src/expand/macro_rules.cpp
+++ b/src/expand/macro_rules.cpp
@@ -10,7 +10,7 @@ class CMacroRulesExpander:
{
bool expand_early() const override { return true; }
- AST::Expr expand(const ::std::string& ident, const TokenTree& tt, AST::Module& mod, MacroPosition position)
+ ::std::unique_ptr<TokenStream> expand(const ::std::string& ident, const TokenTree& tt, AST::Module& mod) override
{
if( ident == "" ) {
throw ::std::runtime_error( "ERROR: macro_rules! requires an identifier" );
@@ -21,7 +21,8 @@ class CMacroRulesExpander:
// TODO: Place into current module using `ident` as the name
mod.add_macro( false, ident, mac );
- return AST::Expr();
+ static TokenTree empty_tt;
+ return box$( TTStream(empty_tt) );
}
};
@@ -59,23 +60,9 @@ class CMacroUseHandler:
};
-AST::Expr Macro_Invoke(const char* name, const MacroRules& rules, const TokenTree& tt, AST::Module& mod, MacroPosition position)
+::std::unique_ptr<TokenStream> Macro_Invoke(const char* name, const MacroRules& rules, const TokenTree& tt, AST::Module& mod)
{
- 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; }
- }
+ return Macro_InvokeRules(name, rules, tt);
}
diff --git a/src/expand/macro_rules.hpp b/src/expand/macro_rules.hpp
index 81e9b18a..b4e6e1e0 100644
--- a/src/expand/macro_rules.hpp
+++ b/src/expand/macro_rules.hpp
@@ -11,5 +11,6 @@ namespace AST {
class Module;
}
class TokenTree;
+class TokenStream;
-extern AST::Expr Macro_Invoke(const char* name, const MacroRules& rules, const TokenTree& tt, AST::Module& mod, MacroPosition position);
+extern ::std::unique_ptr<TokenStream> Macro_Invoke(const char* name, const MacroRules& rules, const TokenTree& tt, AST::Module& mod);
diff --git a/src/expand/mod.cpp b/src/expand/mod.cpp
index 3a19ff4e..527fa0d3 100644
--- a/src/expand/mod.cpp
+++ b/src/expand/mod.cpp
@@ -6,6 +6,7 @@
#include <synext.hpp>
#include <map>
#include "macro_rules.hpp"
+#include "../parse/common.hpp" // For reparse from macros
::std::map< ::std::string, ::std::unique_ptr<ExpandDecorator> > g_decorators;
::std::map< ::std::string, ::std::unique_ptr<ExpandProcMacro> > g_macros;
@@ -32,13 +33,13 @@ void Expand_Attrs(const ::AST::MetaItems& attrs, AttrStage stage, ::AST::Crate&
}
}
-AST::Expr Expand_Macro(bool is_early, LList<const AST::Module*> modstack, ::AST::Module& mod, ::AST::MacroInvocation& mi, MacroPosition pos)
+::std::unique_ptr<TokenStream> Expand_Macro(bool is_early, LList<const AST::Module*> modstack, ::AST::Module& mod, ::AST::MacroInvocation& mi)
{
for( const auto& m : g_macros )
{
if( mi.name() == m.first && m.second->expand_early() == is_early )
{
- auto e = m.second->expand(mi.input_ident(), mi.input_tt(), mod, MacroPosition::Item);
+ auto e = m.second->expand(mi.input_ident(), mi.input_tt(), mod);
mi.clear();
return e;
}
@@ -57,7 +58,7 @@ AST::Expr Expand_Macro(bool is_early, LList<const AST::Module*> modstack, ::AST:
if( mi.input_ident() != "" )
; // TODO: ERROR - macro_rules! macros can't take an ident
- auto e = Macro_Invoke(mr.name.c_str(), mr.data, mi.input_tt(), mod, MacroPosition::Item);
+ auto e = Macro_Invoke(mr.name.c_str(), mr.data, mi.input_tt(), mod);
mi.clear();
return e;
}
@@ -70,7 +71,7 @@ AST::Expr Expand_Macro(bool is_early, LList<const AST::Module*> modstack, ::AST:
if( mi.input_ident() != "" )
; // TODO: ERROR - macro_rules! macros can't take an ident
- auto e = Macro_Invoke(mi.name().c_str(), *mri.data, mi.input_tt(), mod, MacroPosition::Item);
+ auto e = Macro_Invoke(mi.name().c_str(), *mri.data, mi.input_tt(), mod);
mi.clear();
return e;
}
@@ -83,7 +84,7 @@ AST::Expr Expand_Macro(bool is_early, LList<const AST::Module*> modstack, ::AST:
}
// Leave valid and return an empty expression
- return AST::Expr();
+ return ::std::unique_ptr<TokenStream>();
}
void Expand_Mod(bool is_early, ::AST::Crate& crate, LList<const AST::Module*> modstack, ::AST::Path modpath, ::AST::Module& mod)
@@ -93,6 +94,9 @@ void Expand_Mod(bool is_early, ::AST::Crate& crate, LList<const AST::Module*> mo
for( const auto& mi: mod.macro_imports_res() )
DEBUG("- Imports '" << mi.name << "'");
+ // TODO: Have the AST representation of a module include the definition order,
+ // mixing macro invocations, general items, use statements, and `impl`s
+
// 1. Macros first
//for( auto& mi : mod.macro_invs() )
for(unsigned int i = 0; i < mod.macro_invs().size(); i ++ )
@@ -104,14 +108,18 @@ void Expand_Mod(bool is_early, ::AST::Crate& crate, LList<const AST::Module*> mo
// Move out of the module to avoid invalidation if a new macro invocation is added
auto mi_owned = mv$(mi);
- auto rv = Expand_Macro(is_early, modstack, mod, mi_owned, MacroPosition::Item);
- if( rv.is_valid() )
- ; // TODO: ERROR - macro yeilded an expression in item position
+ auto ttl = Expand_Macro(is_early, modstack, mod, mi_owned);
if( mi_owned.name() != "" )
{
mod.macro_invs()[i] = mv$(mi_owned);
}
+ else
+ {
+ // Re-parse tt
+ assert(ttl.get());
+ Parse_ModRoot_Items(*ttl, mod, false, "-");
+ }
}
}
@@ -140,24 +148,32 @@ void Expand_Mod(bool is_early, ::AST::Crate& crate, LList<const AST::Module*> mo
// TODO: Struct items
),
(Enum,
+ // TODO: Enum variants
),
(Trait,
+ // TODO: Trait definition
),
(Type,
// TODO: Do type aliases require recursion?
),
(Function,
- // TODO:
+ // TODO: Recurse into function code (and types)
),
(Static,
- // TODO:
+ // TODO: Recurse into static values
)
)
Expand_Attrs(i.data.attrs, (is_early ? AttrStage::EarlyPost : AttrStage::LatePost), crate, path, mod, i.data);
}
+ DEBUG("Impls");
+ for( auto& i : mod.impls() )
+ {
+ DEBUG("- " << i);
+ }
+
for( const auto& mi: mod.macro_imports_res() )
DEBUG("- Imports '" << mi.name << "'");