aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorCohenArthur <arthur.cohen@epita.fr>2021-08-04 18:11:36 +0200
committerCohenArthur <arthur.cohen@epita.fr>2021-08-05 12:25:57 +0200
commitaf7cd8a9854c7949bfb2015d80387e75f556e034 (patch)
tree221102b7325afba5619dbb61c8b80f9a27e24346 /gcc
parent15635e68bb65d5ce6963ce0c820719a06ca794cd (diff)
downloadgcc-af7cd8a9854c7949bfb2015d80387e75f556e034.zip
gcc-af7cd8a9854c7949bfb2015d80387e75f556e034.tar.gz
gcc-af7cd8a9854c7949bfb2015d80387e75f556e034.tar.bz2
parser: Implement parse_items() as separate public function
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/parse/rust-parse-impl.h56
-rw-r--r--gcc/rust/parse/rust-parse.h6
2 files changed, 23 insertions, 39 deletions
diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index 596a682..ccfff84 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -388,15 +388,12 @@ Parser<ManagedTokenSource>::done_end_of_file ()
return lexer.peek_token ()->get_id () == END_OF_FILE;
}
-// Parses a crate (compilation unit) - entry point
+// Parses a sequence of items within a module or the implicit top-level module
+// in a crate
template <typename ManagedTokenSource>
-AST::Crate
-Parser<ManagedTokenSource>::parse_crate ()
+std::vector<std::unique_ptr<AST::Item>>
+Parser<ManagedTokenSource>::parse_items ()
{
- // parse inner attributes
- AST::AttrVec inner_attrs = parse_inner_attributes ();
-
- // parse items
std::vector<std::unique_ptr<AST::Item>> items;
const_TokenPtr t = lexer.peek_token ();
@@ -419,6 +416,20 @@ Parser<ManagedTokenSource>::parse_crate ()
t = lexer.peek_token ();
}
+ return items;
+}
+
+// Parses a crate (compilation unit) - entry point
+template <typename ManagedTokenSource>
+AST::Crate
+Parser<ManagedTokenSource>::parse_crate ()
+{
+ // parse inner attributes
+ AST::AttrVec inner_attrs = parse_inner_attributes ();
+
+ // parse items
+ std::vector<std::unique_ptr<AST::Item>> items = parse_items ();
+
// emit all errors
for (const auto &error : error_table)
error.emit_error ();
@@ -992,37 +1003,6 @@ Parser<ManagedTokenSource>::parse_token_tree ()
}
}
-/* Parses a sequence of items within a module or the implicit top-level module
- * in a crate. Note: this is not currently used as parsing an item sequence
- * individually is pretty simple and allows for better error diagnostics and
- * detection. */
-template <typename ManagedTokenSource>
-std::vector<std::unique_ptr<AST::Item>>
-Parser<ManagedTokenSource>::parse_items ()
-{
- std::vector<std::unique_ptr<AST::Item>> items;
-
- // TODO: replace with do-while loop?
- // infinite loop to save on comparisons (may be a tight loop) - breaks when
- // next item is null
- while (true)
- {
- std::unique_ptr<AST::Item> item = parse_item (false);
-
- if (item != nullptr)
- {
- items.push_back (std::move (item));
- }
- else
- {
- break;
- }
- }
-
- items.shrink_to_fit ();
- return items;
-}
-
// Parses a single item
template <typename ManagedTokenSource>
std::unique_ptr<AST::Item>
diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h
index 1c7bd78..3920893 100644
--- a/gcc/rust/parse/rust-parse.h
+++ b/gcc/rust/parse/rust-parse.h
@@ -141,7 +141,6 @@ private:
std::unique_ptr<AST::MacroMatchRepetition> parse_macro_match_repetition ();
// Top-level item-related
- std::vector<std::unique_ptr<AST::Item> > parse_items ();
std::unique_ptr<AST::Item> parse_item (bool called_from_statement);
std::unique_ptr<AST::VisItem> parse_vis_item (AST::AttrVec outer_attrs);
std::unique_ptr<AST::MacroItem> parse_macro_item (AST::AttrVec outer_attrs);
@@ -580,11 +579,16 @@ private:
bool done_end_of_file ();
void add_error (Error error) { error_table.push_back (std::move (error)); }
+ std::vector<Error> &get_errors () { return error_table; }
public:
// Construct parser with specified "managed" token source.
Parser (ManagedTokenSource tokenSource) : lexer (std::move (tokenSource)) {}
+ // Parse items without parsing an entire crate. This function is the main
+ // parsing loop of AST::Crate::parse_crate().
+ std::vector<std::unique_ptr<AST::Item> > parse_items ();
+
// Main entry point for parser.
AST::Crate parse_crate ();