From 72e2a322f52edd7bf3f37ad840f2d48be4192d62 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 25 Sep 2016 22:18:33 +0800 Subject: Parse - Move `pub` parsing to Parse_Mod_Item_S --- src/macro_rules/eval.cpp | 3 +-- src/parse/common.hpp | 4 ++-- src/parse/expr.cpp | 6 +++--- src/parse/root.cpp | 47 +++++++++++++++++++++++++++-------------------- 4 files changed, 33 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/macro_rules/eval.cpp b/src/macro_rules/eval.cpp index 50b92466..19dc1457 100644 --- a/src/macro_rules/eval.cpp +++ b/src/macro_rules/eval.cpp @@ -502,8 +502,7 @@ void Macro_HandlePatternCap(TTStream& lex, unsigned int index, MacroPatEnt::Type case MacroPatEnt::PAT_ITEM: { assert( lex.parse_state().module ); const auto& cur_mod = *lex.parse_state().module; - // TODO: Pass the filename for `cur_mod` and if it controls its dir - bound_tts.insert( index, iterations, InterpolatedFragment( Parse_Mod_Item_S(lex, cur_mod.m_file_info, cur_mod.path(), false, AST::MetaItems{}) ) ); + bound_tts.insert( index, iterations, InterpolatedFragment( Parse_Mod_Item_S(lex, cur_mod.m_file_info, cur_mod.path(), AST::MetaItems{}) ) ); } break; case MacroPatEnt::PAT_IDENT: GET_CHECK_TOK(tok, lex, TOK_IDENT); diff --git a/src/parse/common.hpp b/src/parse/common.hpp index c717a2f5..f7168d33 100644 --- a/src/parse/common.hpp +++ b/src/parse/common.hpp @@ -45,8 +45,8 @@ extern TypeRef Parse_Type(TokenStream& lex, bool allow_trait_list = true); extern AST::Pattern Parse_Pattern(TokenStream& lex, bool is_refutable); extern void Parse_Impl_Item(TokenStream& lex, AST::Impl& impl); -extern void Parse_Mod_Item(TokenStream& lex, AST::Module& mod, bool is_public, AST::MetaItems meta_items); -extern ::AST::Named<::AST::Item> Parse_Mod_Item_S(TokenStream& lex, const AST::Module::FileInfo& mod_fileinfo, const ::AST::Path& mod_path, bool is_public, AST::MetaItems meta_items); +extern void Parse_Mod_Item(TokenStream& lex, AST::Module& mod, AST::MetaItems meta_items); +extern ::AST::Named<::AST::Item> Parse_Mod_Item_S(TokenStream& lex, const AST::Module::FileInfo& mod_fileinfo, const ::AST::Path& mod_path, AST::MetaItems meta_items); extern void Parse_ModRoot_Items(TokenStream& lex, AST::Module& mod); diff --git a/src/parse/expr.cpp b/src/parse/expr.cpp index f617bcdc..46be8e63 100644 --- a/src/parse/expr.cpp +++ b/src/parse/expr.cpp @@ -77,7 +77,7 @@ ExprNodeP Parse_ExprBlockNode(TokenStream& lex, bool is_unsafe/*=false*/) break; // Items: case TOK_RWORD_PUB: - GET_TOK(tok, lex); + ERROR(lex.getPosition(), E0000, "`pub` is useless within expression modules"); case TOK_RWORD_TYPE: case TOK_RWORD_USE: case TOK_RWORD_EXTERN: @@ -93,7 +93,7 @@ ExprNodeP Parse_ExprBlockNode(TokenStream& lex, bool is_unsafe/*=false*/) if( !local_mod ) { local_mod = lex.parse_state().get_current_mod().add_anon(); } - Parse_Mod_Item(lex, *local_mod, false, mv$(item_attrs)); + Parse_Mod_Item(lex, *local_mod, mv$(item_attrs)); break; // 'unsafe' - Check if the next token isn't a `{`, if so it's an item. Otherwise, fall through case TOK_RWORD_UNSAFE: @@ -103,7 +103,7 @@ ExprNodeP Parse_ExprBlockNode(TokenStream& lex, bool is_unsafe/*=false*/) if( !local_mod ) { local_mod = lex.parse_state().get_current_mod().add_anon(); } - Parse_Mod_Item(lex, *local_mod, false, mv$(item_attrs)); + Parse_Mod_Item(lex, *local_mod, mv$(item_attrs)); break; } // fall diff --git a/src/parse/root.cpp b/src/parse/root.cpp index a7066180..1b6bf15a 100644 --- a/src/parse/root.cpp +++ b/src/parse/root.cpp @@ -1280,7 +1280,7 @@ void Parse_Use(TokenStream& lex, ::std::function Parse_Mod_Item_S(TokenStream& lex, const AST::Module::FileInfo& mod_fileinfo, const ::AST::Path& mod_path, bool is_public, AST::MetaItems meta_items) +::AST::Named<::AST::Item> Parse_Mod_Item_S(TokenStream& lex, const AST::Module::FileInfo& mod_fileinfo, const ::AST::Path& mod_path, AST::MetaItems meta_items) { Token tok; @@ -1288,9 +1288,20 @@ void Parse_Use(TokenStream& lex, ::std::function { mv$(item_name), mv$(item_data), is_public }; } -void Parse_Mod_Item(TokenStream& lex, AST::Module& mod, bool is_public, AST::MetaItems meta_items) +void Parse_Mod_Item(TokenStream& lex, AST::Module& mod, AST::MetaItems meta_items) { SET_MODULE(lex, mod); lex.parse_state().parent_attrs = &meta_items; @@ -1613,9 +1625,14 @@ void Parse_Mod_Item(TokenStream& lex, AST::Module& mod, bool is_public, AST::Met Token tok; // `use ...` - if( LOOK_AHEAD(lex) == TOK_RWORD_USE ) + if( LOOK_AHEAD(lex) == TOK_RWORD_USE || (lex.lookahead(0) == TOK_RWORD_PUB && lex.lookahead(1) == TOK_RWORD_USE) ) { - GET_TOK(tok, lex); + bool is_public = false; + if( GET_TOK(tok, lex) == TOK_RWORD_PUB ) { + is_public = true; + GET_TOK(tok, lex); + } + Parse_Use(lex, [&mod,is_public,&meta_items](AST::UseStmt p, std::string s) { DEBUG(mod.path() << " - use " << p << " as '" << s << "'"); mod.add_alias(is_public, mv$(p), s, meta_items.clone()); @@ -1649,7 +1666,7 @@ void Parse_Mod_Item(TokenStream& lex, AST::Module& mod, bool is_public, AST::Met } else { - mod.add_item( Parse_Mod_Item_S(lex, mod.m_file_info, mod.path(), is_public, mv$(meta_items)) ); + mod.add_item( Parse_Mod_Item_S(lex, mod.m_file_info, mod.path(), mv$(meta_items)) ); } } @@ -1696,18 +1713,8 @@ void Parse_ModRoot_Items(TokenStream& lex, AST::Module& mod) else { PUTBACK(tok, lex); } - - // Module visibility - // TODO: Move this into Parse_Mod_Item? - bool is_public = false; - if( GET_TOK(tok, lex) == TOK_RWORD_PUB ) { - is_public = true; - } - else { - PUTBACK(tok, lex); - } - Parse_Mod_Item(lex, mod, is_public, mv$(meta_items)); + Parse_Mod_Item(lex, mod, mv$(meta_items)); } } -- cgit v1.2.3