From af7cd8a9854c7949bfb2015d80387e75f556e034 Mon Sep 17 00:00:00 2001 From: CohenArthur Date: Wed, 4 Aug 2021 18:11:36 +0200 Subject: parser: Implement parse_items() as separate public function --- gcc/rust/parse/rust-parse-impl.h | 56 +++++++++++++--------------------------- gcc/rust/parse/rust-parse.h | 6 ++++- 2 files changed, 23 insertions(+), 39 deletions(-) (limited to 'gcc') 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::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 -AST::Crate -Parser::parse_crate () +std::vector> +Parser::parse_items () { - // parse inner attributes - AST::AttrVec inner_attrs = parse_inner_attributes (); - - // parse items std::vector> items; const_TokenPtr t = lexer.peek_token (); @@ -419,6 +416,20 @@ Parser::parse_crate () t = lexer.peek_token (); } + return items; +} + +// Parses a crate (compilation unit) - entry point +template +AST::Crate +Parser::parse_crate () +{ + // parse inner attributes + AST::AttrVec inner_attrs = parse_inner_attributes (); + + // parse items + std::vector> items = parse_items (); + // emit all errors for (const auto &error : error_table) error.emit_error (); @@ -992,37 +1003,6 @@ Parser::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 -std::vector> -Parser::parse_items () -{ - std::vector> 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 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 std::unique_ptr 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 parse_macro_match_repetition (); // Top-level item-related - std::vector > parse_items (); std::unique_ptr parse_item (bool called_from_statement); std::unique_ptr parse_vis_item (AST::AttrVec outer_attrs); std::unique_ptr 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 &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 > parse_items (); + // Main entry point for parser. AST::Crate parse_crate (); -- cgit v1.1