diff options
author | John Hodge <tpg@mutabah.net> | 2016-09-25 22:18:33 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2016-09-25 22:18:33 +0800 |
commit | 72e2a322f52edd7bf3f37ad840f2d48be4192d62 (patch) | |
tree | de50a148c0f0a77a97265652ccd0f66951678a3d /src | |
parent | e7dfda0f39c3d89eaa17c5329a62d2faaa053cfa (diff) | |
download | mrust-72e2a322f52edd7bf3f37ad840f2d48be4192d62.tar.gz |
Parse - Move `pub` parsing to Parse_Mod_Item_S
Diffstat (limited to 'src')
-rw-r--r-- | src/macro_rules/eval.cpp | 3 | ||||
-rw-r--r-- | src/parse/common.hpp | 4 | ||||
-rw-r--r-- | src/parse/expr.cpp | 6 | ||||
-rw-r--r-- | src/parse/root.cpp | 47 |
4 files changed, 33 insertions, 27 deletions
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<void(AST::UseStmt, ::std::strin }
// TODO: Extract single-item parsing into a method that returns just the item
-::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)
+::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<void(AST::UseStmt, ::std::strin ::std::string item_name;
::AST::Item item_data;
+
+ bool is_public = false;
+ if( GET_TOK(tok, lex) == TOK_RWORD_PUB ) {
+ is_public = true;
+ }
+ else {
+ PUTBACK(tok, lex);
+ }
switch( GET_TOK(tok, lex) )
{
+ case TOK_RWORD_USE:
+ TODO(lex.getPosition(), "Encode a single use statement as a single Item");
+
case TOK_RWORD_EXTERN:
switch( GET_TOK(tok, lex) )
{
@@ -1457,6 +1468,8 @@ void Parse_Use(TokenStream& lex, ::std::function<void(AST::UseStmt, ::std::strin item_data = ::AST::Item( Parse_TraitDef(lex, meta_items) );
break;
// TODO: `unsafe impl` (Doesn't currently exist as an Item)
+ case TOK_RWORD_IMPL:
+ TODO(lex.getPosition(), "Encode impl blocks as in AST::Item");
default:
throw ParseError::Unexpected(lex, tok, {TOK_RWORD_FN, TOK_RWORD_TRAIT, TOK_RWORD_IMPL});
}
@@ -1487,10 +1500,9 @@ void Parse_Use(TokenStream& lex, ::std::function<void(AST::UseStmt, ::std::strin item_data = ::AST::Item( Parse_EnumDef(lex, meta_items) );
break;
// `impl`
- // TODO: Convert `Parse_Impl` to return an item
- //case TOK_RWORD_IMPL:
- // Parse_Impl(lex, mod, mv$(meta_items));
- // break;
+ case TOK_RWORD_IMPL:
+ // TODO: Convert `Parse_Impl` to return an item
+ TODO(lex.getPosition(), "Encode impl blocks as in AST::Item");
// `trait`
case TOK_RWORD_TRAIT:
GET_CHECK_TOK(tok, lex, TOK_IDENT);
@@ -1604,7 +1616,7 @@ void Parse_Use(TokenStream& lex, ::std::function<void(AST::UseStmt, ::std::strin return ::AST::Named< ::AST::Item> { 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));
}
}
|