summaryrefslogtreecommitdiff
path: root/src/parse
diff options
context:
space:
mode:
authorJohn Hodge <tpg@mutabah.net>2016-02-18 19:30:27 +1100
committerJohn Hodge <tpg@mutabah.net>2016-02-18 19:30:27 +1100
commitcf87314dde3af9468f3e24e29191412e8a2d19f7 (patch)
tree8c240a20e46458554e91a95ad704ef8670af05b0 /src/parse
parent651ab293f3bfa117dae618edb9a4a9f328e07a91 (diff)
downloadmrust-cf87314dde3af9468f3e24e29191412e8a2d19f7.tar.gz
(semibroken) Defer macro expansion
Diffstat (limited to 'src/parse')
-rw-r--r--src/parse/expr.cpp40
-rw-r--r--src/parse/lex.cpp6
-rw-r--r--src/parse/root.cpp59
-rw-r--r--src/parse/tokentree.hpp5
4 files changed, 52 insertions, 58 deletions
diff --git a/src/parse/expr.cpp b/src/parse/expr.cpp
index 5c7bfda5..47fe444a 100644
--- a/src/parse/expr.cpp
+++ b/src/parse/expr.cpp
@@ -1141,34 +1141,20 @@ ExprNodeP Parse_ExprVal(TokenStream& lex)
}
}
throw ParseError::BugCheck(lex, "Array literal fell");
- case TOK_MACRO:
- if( CHECK_PARSE_FLAG(lex, no_expand_macros) )
- {
- ::std::string name = tok.str();
- TokenTree tt = Parse_TT(lex, true);
- if( tt.is_token() ) {
- throw ParseError::Unexpected(lex, tt.tok());
- }
- return NEWNODE(AST::ExprNode_Macro, ::std::move(name), ::std::move(tt));
+ case TOK_MACRO: {
+ ::std::string name = tok.str();
+ ::std::string ident;
+ if( GET_TOK(tok, lex) == TOK_IDENT ) {
+ ident = mv$(tok.str());
}
- else
- {
- TokenTree tt = Parse_TT(lex, true);
- if( tt.is_token() ) {
- throw ParseError::Unexpected(lex, tt.tok());
- }
- ::std::string name = tok.str();
-
- if( name == "format_args" )
- {
- TTStream slex(tt);
- return Parse_FormatArgs(slex);
- }
- else
- {
- auto expanded_macro = Macro_Invoke(lex, name, tt);
- return Parse_Expr0(*expanded_macro);
- }
+ else {
+ lex.putback(tok);
+ }
+ TokenTree tt = Parse_TT(lex, true);
+ if( tt.is_token() ) {
+ throw ParseError::Unexpected(lex, tt.tok());
+ }
+ return NEWNODE(AST::ExprNode_Macro, mv$(name), mv$(ident), mv$(tt));
}
default:
throw ParseError::Unexpected(lex, tok);
diff --git a/src/parse/lex.cpp b/src/parse/lex.cpp
index a3187cda..cb97022d 100644
--- a/src/parse/lex.cpp
+++ b/src/parse/lex.cpp
@@ -1042,3 +1042,9 @@ Span TokenStream::end_span(ProtoSpan ps) const
);
}
+
+SERIALISE_TYPE_A(TokenTree::, "TokenTree", {
+ s.item(m_tok);
+ s.item(m_subtrees);
+})
+
diff --git a/src/parse/root.cpp b/src/parse/root.cpp
index 5ba66dbc..93b360f1 100644
--- a/src/parse/root.cpp
+++ b/src/parse/root.cpp
@@ -829,25 +829,25 @@ void Parse_Impl_Item(TokenStream& lex, AST::Impl& impl)
::std::string abi = "rust";
switch(tok.type())
{
- case TOK_MACRO:
- {
- TokenTree tt = Parse_TT(lex, true);
- if( tt.is_token() ) {
- DEBUG("TT was a single token (not a sub-tree)");
- throw ParseError::Unexpected(lex, tt.tok());
- }
-
- auto expanded_macro = Macro_Invoke(lex, tok.str().c_str(), tt);
- auto& lex = *expanded_macro;
- while( GET_TOK(tok, lex) != TOK_EOF )
- {
- lex.putback(tok);
- Parse_Impl_Item(lex, impl);
- }
- }
- if(GET_TOK(tok, lex) != TOK_SEMICOLON)
- lex.putback(tok);
- break;
+ //case TOK_MACRO:
+ // {
+ // TokenTree tt = Parse_TT(lex, true);
+ // if( tt.is_token() ) {
+ // DEBUG("TT was a single token (not a sub-tree)");
+ // throw ParseError::Unexpected(lex, tt.tok());
+ // }
+ //
+ // auto expanded_macro = Macro_Invoke(lex, tok.str().c_str(), tt);
+ // auto& lex = *expanded_macro;
+ // while( GET_TOK(tok, lex) != TOK_EOF )
+ // {
+ // lex.putback(tok);
+ // Parse_Impl_Item(lex, impl);
+ // }
+ // }
+ // if(GET_TOK(tok, lex) != TOK_SEMICOLON)
+ // lex.putback(tok);
+ // break;
case TOK_RWORD_TYPE: {
GET_CHECK_TOK(tok, lex, TOK_IDENT);
::std::string name = tok.str();
@@ -1308,24 +1308,23 @@ void Parse_ModRoot_Items(TokenStream& lex, AST::Crate& crate, AST::Module& mod,
// root-level macros
if( GET_TOK(tok, lex) == TOK_MACRO )
{
+ ::std::string name = mv$(tok.str());
// `macro_rules! ...`
- if( tok.str() == "macro_rules" )
+ if( name == "macro_rules" )
{
Parse_MacroRules(lex, mod, mv$(meta_items));
}
else
{
- DEBUG("Invoke macro '"<<tok.str()<<"'");
- TokenTree tt = Parse_TT(lex, true);
- if( tt.is_token() ) {
- DEBUG("TT was a single token (not a sub-tree)");
- throw ParseError::Unexpected(lex, tt.tok());
+ ::std::string ident;
+ if( GET_TOK(tok, lex) == TOK_IDENT ) {
+ ident = mv$(tok.str());
}
- ::std::string name = tok.str();
-
- auto expanded_macro = Macro_Invoke(lex, name.c_str(), tt);
- // Pass "!" as 'path' to allow termination on EOF
- Parse_ModRoot_Items(*expanded_macro, crate, mod, modstack, "!");
+ else {
+ lex.putback(tok);
+ }
+ TokenTree tt = Parse_TT(lex, true);
+ mod.add_macro_invocation( ::AST::MacroItem( mv$(meta_items), mv$(name), mv$(ident), mv$(tt)) );
}
// - Silently consume ';' after the macro
if( GET_TOK(tok, lex) != TOK_SEMICOLON )
diff --git a/src/parse/tokentree.hpp b/src/parse/tokentree.hpp
index cde35c8c..aa72a522 100644
--- a/src/parse/tokentree.hpp
+++ b/src/parse/tokentree.hpp
@@ -4,7 +4,8 @@
#include "lex.hpp"
-class TokenTree
+class TokenTree:
+ public Serialisable
{
Token m_tok;
::std::vector<TokenTree> m_subtrees;
@@ -38,6 +39,8 @@ public:
else
return os << "TokenTree([" << tt.m_subtrees << "])";
}
+
+ SERIALISABLE_PROTOTYPES();
};
class TTStream: