From 261f2aea278e1185f69b06e65501c1ca718df47c Mon Sep 17 00:00:00 2001 From: John Hodge Date: Fri, 28 Aug 2015 16:14:00 +0800 Subject: Tagged union for Path --- src/parse/expr.cpp | 2 +- src/parse/paths.cpp | 20 ++++++++++++++------ src/parse/root.cpp | 16 ++++++++-------- 3 files changed, 23 insertions(+), 15 deletions(-) (limited to 'src/parse') diff --git a/src/parse/expr.cpp b/src/parse/expr.cpp index 15e983f7..eb53ac98 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::TagVariable(), "self") ); + return NEWNODE( AST::ExprNode_NamedValue, AST::Path(AST::Path::TagLocal(), "self") ); } else { diff --git a/src/parse/paths.cpp b/src/parse/paths.cpp index e3b290d0..43346172 100644 --- a/src/parse/paths.cpp +++ b/src/parse/paths.cpp @@ -41,9 +41,9 @@ AST::Path Parse_Path(TokenStream& lex, eParsePathGenericMode generic_mode) } AST::Path Parse_Path(TokenStream& lex, bool is_abs, eParsePathGenericMode generic_mode) { + Token tok; if( is_abs ) { - Token tok; if( GET_TOK(tok, lex) == TOK_STRING ) { ::std::string cratename = tok.str(); GET_CHECK_TOK(tok, lex, TOK_DOUBLE_COLON); @@ -54,8 +54,16 @@ AST::Path Parse_Path(TokenStream& lex, bool is_abs, eParsePathGenericMode generi return Parse_PathFrom(lex, AST::Path(AST::Path::TagAbsolute()), generic_mode); } } - else - return Parse_PathFrom(lex, AST::Path(AST::Path::TagRelative()), generic_mode); + else { + //assert( GET_TOK(tok, lex) == TOK_IDENT ); + //if( lex.lookahead(0) != TOK_DOUBLE_COLON ) { + // return AST::Path( tok.str() ); + //} + //else { + // lex.putback(tok); + return Parse_PathFrom(lex, AST::Path(AST::Path::TagRelative()), generic_mode); + //} + } } AST::Path Parse_PathFrom(TokenStream& lex, AST::Path path, eParsePathGenericMode generic_mode) @@ -145,9 +153,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()); - } + //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 1fd62741..369cc95a 100644 --- a/src/parse/root.cpp +++ b/src/parse/root.cpp @@ -725,7 +725,7 @@ void Parse_Impl(TokenStream& lex, AST::Module& mod, bool is_unsafe/*=false*/) { if( !impl_type.is_path() ) throw ParseError::Generic(lex, "Trait was not a path"); - trait_path = impl_type.path(); + trait_path = mv$(impl_type.path()); // Implementing a trait for another type, get the target type if( GET_TOK(tok, lex) == TOK_DOUBLE_DOT ) { @@ -885,9 +885,9 @@ void Parse_ExternBlock(TokenStream& lex, AST::Module& mod, ::std::string abi) } } -void Parse_Use_Wildcard(const AST::Path& base_path, ::std::function fcn) +void Parse_Use_Wildcard(AST::Path base_path, ::std::function fcn) { - fcn(base_path, ""); // HACK! Empty path indicates wilcard import + fcn( mv$(base_path), ""); // HACK! Empty path indicates wilcard import } void Parse_Use_Set(TokenStream& lex, const AST::Path& base_path, ::std::function fcn) { @@ -911,7 +911,8 @@ void Parse_Use(TokenStream& lex, ::std::function TRACE_FUNCTION; Token tok; - AST::Path path = AST::Path( AST::Path::TagAbsolute() ); + AST::Path path = AST::Path(AST::Path::TagAbsolute()); + ::std::vector nodes; switch( GET_TOK(tok, lex) ) { @@ -945,7 +946,6 @@ void Parse_Use(TokenStream& lex, ::std::function default: throw ParseError::Unexpected(lex, tok); } - // TODO: Use from crate root while( GET_TOK(tok, lex) == TOK_DOUBLE_COLON ) { if( GET_TOK(tok, lex) == TOK_IDENT ) @@ -961,7 +961,7 @@ void Parse_Use(TokenStream& lex, ::std::function GET_CHECK_TOK(tok, lex, TOK_BRACE_CLOSE); break ; case TOK_STAR: - Parse_Use_Wildcard(path, fcn); + Parse_Use_Wildcard( mv$(path), fcn ); break ; default: throw ParseError::Unexpected(lex, tok); @@ -1309,7 +1309,7 @@ void Parse_ModRoot_Items(TokenStream& lex, AST::Crate& crate, AST::Module& mod, case TOK_RWORD_USE: 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); + mod.add_alias(is_public, mv$(p), s); }); GET_CHECK_TOK(tok, lex, TOK_SEMICOLON); break; @@ -1614,7 +1614,7 @@ void Parse_ModRoot(TokenStream& lex, AST::Crate& crate, AST::Module& mod, LList< { // Import the prelude AST::Path prelude_path = AST::Path( "std", { AST::PathNode("prelude", {}), AST::PathNode("v1", {}) } ); - Parse_Use_Wildcard(prelude_path, + Parse_Use_Wildcard( mv$(prelude_path), [&mod](AST::Path p, std::string s) { mod.add_alias(false, p, s); } -- cgit v1.2.3