From f15fcf3c440e3c9c40fa606b0904fb85a8510464 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Thu, 4 Jun 2015 14:41:34 +0800 Subject: Rework path handling and resolve to better handle Self --- src/parse/expr.cpp | 2 +- src/parse/paths.cpp | 5 ++++- src/parse/root.cpp | 21 ++++++++++++--------- 3 files changed, 17 insertions(+), 11 deletions(-) (limited to 'src/parse') diff --git a/src/parse/expr.cpp b/src/parse/expr.cpp index 12aa3d3a..a12eb526 100644 --- a/src/parse/expr.cpp +++ b/src/parse/expr.cpp @@ -1001,7 +1001,7 @@ ExprNodeP Parse_ExprVal(TokenStream& lex) case TOK_RWORD_SELF: { if( LOOK_AHEAD(lex) != TOK_DOUBLE_COLON ) { - return NEWNODE( AST::ExprNode_NamedValue, AST::Path(AST::Path::TagLocal(), "self") ); + return NEWNODE( AST::ExprNode_NamedValue, AST::Path(AST::Path::TagVariable(), "self") ); } else { diff --git a/src/parse/paths.cpp b/src/parse/paths.cpp index a8c6802d..e3b290d0 100644 --- a/src/parse/paths.cpp +++ b/src/parse/paths.cpp @@ -55,7 +55,7 @@ AST::Path Parse_Path(TokenStream& lex, bool is_abs, eParsePathGenericMode generi } } else - return Parse_PathFrom(lex, AST::Path(), generic_mode); + return Parse_PathFrom(lex, AST::Path(AST::Path::TagRelative()), generic_mode); } AST::Path Parse_PathFrom(TokenStream& lex, AST::Path path, eParsePathGenericMode generic_mode) @@ -145,6 +145,9 @@ AST::Path Parse_PathFrom(TokenStream& lex, AST::Path path, eParsePathGenericMode path.append( AST::PathNode(component, params) ); } lex.putback(tok); + if( path.is_trivial() ) { + path = AST::Path(path[0].name()); + } DEBUG("path = " << path); return path; } diff --git a/src/parse/root.cpp b/src/parse/root.cpp index eeeafb0e..2ae39d91 100644 --- a/src/parse/root.cpp +++ b/src/parse/root.cpp @@ -894,7 +894,7 @@ void Parse_Use(TokenStream& lex, ::std::function switch( GET_TOK(tok, lex) ) { case TOK_RWORD_SELF: - path = AST::Path( ); // relative path + path = AST::Path( AST::Path::TagSelf() ); // relative path break; case TOK_RWORD_SUPER: path = AST::Path( AST::Path::TagSuper() ); @@ -941,21 +941,21 @@ void Parse_Use(TokenStream& lex, ::std::function } } while( GET_TOK(tok, lex) == TOK_COMMA ); CHECK_TOK(tok, TOK_BRACE_CLOSE); - return; + break ; case TOK_STAR: Parse_Use_Wildcard(path, fcn); - // early return - can't have anything else after - return; + break ; default: throw ParseError::Unexpected(lex, tok); } - GET_TOK(tok, lex); - break; + // early return - This branch is either the end of the use statement, or a syntax error + return ; } } ::std::string name; - // TODO: This should only be allowed if the last token was an ident + // This should only be allowed if the last token was an ident + // - Above checks ensure this if( tok.type() == TOK_RWORD_AS ) { GET_CHECK_TOK(tok, lex, TOK_IDENT); @@ -964,7 +964,7 @@ void Parse_Use(TokenStream& lex, ::std::function else { lex.putback(tok); - name = path[path.size()-1].name(); + name = path.nodes().back().name(); } fcn(path, name); @@ -1289,7 +1289,10 @@ void Parse_ModRoot_Items(TokenStream& lex, AST::Crate& crate, AST::Module& mod, { case TOK_RWORD_USE: - Parse_Use(lex, [&mod,is_public](AST::Path p, std::string s) { mod.add_alias(is_public, p, s); }); + Parse_Use(lex, [&mod,is_public,&path](AST::Path p, std::string s) { + DEBUG(path << " - use " << p << " as '" << s << "'"); + mod.add_alias(is_public, p, s); + }); GET_CHECK_TOK(tok, lex, TOK_SEMICOLON); break; -- cgit v1.2.3