diff options
author | John Hodge <tpg@mutabah.net> | 2015-04-04 13:21:44 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2015-04-04 13:21:44 +0800 |
commit | c7fc4d733e3a6e2398a938949ff08235db807184 (patch) | |
tree | ee284a74a413b67c6a7b98efff2554e59f9b8d14 | |
parent | 3876c390cef37371236e43d2e6990c212e68c481 (diff) | |
download | mrust-c7fc4d733e3a6e2398a938949ff08235db807184.tar.gz |
Handle 'self::' paths, fix macro name resolution, fix handling of 'use ::'
-rw-r--r-- | src/macros.cpp | 11 | ||||
-rw-r--r-- | src/parse/expr.cpp | 16 | ||||
-rw-r--r-- | src/parse/root.cpp | 40 |
3 files changed, 47 insertions, 20 deletions
diff --git a/src/macros.cpp b/src/macros.cpp index a614f2fb..ea26fd43 100644 --- a/src/macros.cpp +++ b/src/macros.cpp @@ -110,7 +110,6 @@ public: const auto it_range = m_inner.equal_range( t_mapping_key { {layer, iteration}, name } );
if( it_range.first == it_range.second ) {
DEBUG("m_mappings = " << m_inner);
- DEBUG("Can't find mapping");
return nullptr;
}
@@ -550,9 +549,13 @@ Token MacroExpander::realGetToken() // - Expand to a parameter
else if( ent.name != "" )
{
- unsigned int parent_iter = (layer > 0 ? m_layer_iters.at(layer-1) : 0);
- const size_t iter_idx = m_offsets.back().second;
- const TokenTree* tt = m_mappings.get(layer, parent_iter, ent.name.c_str(), iter_idx);
+ const TokenTree* tt;
+ unsigned int search_layer = layer;
+ do {
+ unsigned int parent_iter = (search_layer > 0 ? m_layer_iters.at(search_layer-1) : 0);
+ const size_t iter_idx = m_offsets.at(search_layer).second;
+ tt = m_mappings.get(search_layer, parent_iter, ent.name.c_str(), iter_idx);
+ } while( !tt && search_layer-- > 0 );
if( ! tt )
{
throw ParseError::Generic(*this, FMT("Cannot find '" << ent.name << "' for " << layer));
diff --git a/src/parse/expr.cpp b/src/parse/expr.cpp index e83d2dbe..b4138057 100644 --- a/src/parse/expr.cpp +++ b/src/parse/expr.cpp @@ -640,6 +640,8 @@ bool Parse_IsTokValue(eTokenType tok_type) case TOK_MACRO:
+ case TOK_EXCLAM:
+ case TOK_DASH:
case TOK_STAR:
case TOK_AMP:
return true;
@@ -995,6 +997,18 @@ ExprNodeP Parse_ExprVal(TokenStream& lex) path = Parse_PathFrom(lex, AST::Path(AST::Path::TagUfcs(), ty, trait), PATH_GENERIC_EXPR);
}
if(0)
+ case TOK_RWORD_SELF:
+ {
+ if( LOOK_AHEAD(lex) != TOK_DOUBLE_COLON ) {
+ return NEWNODE( AST::ExprNode_NamedValue, AST::Path(AST::Path::TagLocal(), "self") );
+ }
+ else
+ {
+ GET_CHECK_TOK(tok, lex, TOK_DOUBLE_COLON);
+ path = Parse_Path(lex, false, PATH_GENERIC_EXPR);
+ }
+ }
+ if(0)
case TOK_RWORD_SUPER:
{
GET_CHECK_TOK(tok, lex, TOK_DOUBLE_COLON);
@@ -1040,8 +1054,6 @@ ExprNodeP Parse_ExprVal(TokenStream& lex) return NEWNODE( AST::ExprNode_Bool, true );
case TOK_RWORD_FALSE:
return NEWNODE( AST::ExprNode_Bool, false );
- case TOK_RWORD_SELF:
- return NEWNODE( AST::ExprNode_NamedValue, AST::Path(AST::Path::TagLocal(), "self") );
case TOK_PAREN_OPEN:
if( GET_TOK(tok, lex) == TOK_PAREN_CLOSE )
{
diff --git a/src/parse/root.cpp b/src/parse/root.cpp index 5c1dd859..89843f34 100644 --- a/src/parse/root.cpp +++ b/src/parse/root.cpp @@ -350,12 +350,21 @@ AST::Struct Parse_Struct(TokenStream& lex, const AST::MetaItems meta_items) ::std::vector<AST::StructItem> refs;
while(GET_TOK(tok, lex) != TOK_PAREN_CLOSE)
{
+ AST::MetaItems item_attrs;
+ while( tok.type() == TOK_ATTR_OPEN )
+ {
+ item_attrs.push_back( Parse_MetaItem(lex) );
+ GET_CHECK_TOK(tok, lex, TOK_SQUARE_CLOSE);
+ GET_TOK(tok, lex);
+ }
+
bool is_pub = false;
if(tok.type() == TOK_RWORD_PUB)
is_pub = true;
else
lex.putback(tok);
+ // TODO: Save item_attrs
refs.push_back( AST::StructItem( "", Parse_Type(lex), is_pub ) );
if( GET_TOK(tok, lex) != TOK_COMMA )
break;
@@ -394,9 +403,10 @@ AST::Struct Parse_Struct(TokenStream& lex, const AST::MetaItems meta_items) ::std::string name = tok.str();
GET_CHECK_TOK(tok, lex, TOK_COLON);
TypeRef type = Parse_Type(lex);
+
+ // TODO: Save item_attrs
items.push_back( AST::StructItem( ::std::move(name), ::std::move(type), is_pub ) );
- tok = lex.getToken();
- if(tok.type() == TOK_BRACE_CLOSE)
+ if(GET_TOK(tok, lex) == TOK_BRACE_CLOSE)
break;
CHECK_TOK(tok, TOK_COMMA);
}
@@ -849,18 +859,6 @@ void Parse_Use(TokenStream& lex, ::std::function<void(AST::Path, ::std::string)> Token tok;
AST::Path path = AST::Path( AST::Path::TagAbsolute() );
- // Leading :: is allowed and ignored for the $crate feature
- if( LOOK_AHEAD(lex) == TOK_DOUBLE_COLON ) {
- GET_TOK(tok, lex);
- // HACK! mrustc emits $crate as `::"crate-name"`
- if( LOOK_AHEAD(lex) == TOK_STRING )
- {
- GET_CHECK_TOK(tok, lex, TOK_STRING);
- path.set_crate(tok.str());
- GET_CHECK_TOK(tok, lex, TOK_DOUBLE_COLON);
- }
- }
-
switch( GET_TOK(tok, lex) )
{
case TOK_RWORD_SELF:
@@ -872,6 +870,20 @@ void Parse_Use(TokenStream& lex, ::std::function<void(AST::Path, ::std::string)> case TOK_IDENT:
path.append( AST::PathNode(tok.str(), {}) );
break;
+ // Leading :: is allowed and ignored for the $crate feature
+ case TOK_DOUBLE_COLON:
+ // Absolute path
+ // HACK! mrustc emits $crate as `::"crate-name"`
+ if( LOOK_AHEAD(lex) == TOK_STRING )
+ {
+ GET_CHECK_TOK(tok, lex, TOK_STRING);
+ path.set_crate(tok.str());
+ GET_CHECK_TOK(tok, lex, TOK_DOUBLE_COLON);
+ }
+ else {
+ lex.putback(tok);
+ }
+ break;
default:
throw ParseError::Unexpected(lex, tok);
}
|