diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-08-05 11:34:51 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-05 11:34:51 +0000 |
commit | 074c070c02e61033694f2f969a33a795036ad540 (patch) | |
tree | 221102b7325afba5619dbb61c8b80f9a27e24346 /gcc | |
parent | 15635e68bb65d5ce6963ce0c820719a06ca794cd (diff) | |
parent | af7cd8a9854c7949bfb2015d80387e75f556e034 (diff) | |
download | gcc-074c070c02e61033694f2f969a33a795036ad540.zip gcc-074c070c02e61033694f2f969a33a795036ad540.tar.gz gcc-074c070c02e61033694f2f969a33a795036ad540.tar.bz2 |
Merge #608
608: Add `parse_items()` function from `parse_crate()` r=philberty a=CohenArthur
In order to support multiple file parsing, we will need to be able to parse items from a file without specifically parsing an entire crate. This PR splits up the `parse_crate()` function in two and exposes the `parse_items()` function.
The function was previously hidden and didn't have the exact same behavior as the main loop of `parse_crate()`.
This will be especially useful when trying to expand
```rust
mod <file>;
```
to
```rust
mod <file> {
<items>
}
```
in the AST as we will need to parse the items present in the file.
Co-authored-by: CohenArthur <arthur.cohen@epita.fr>
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/parse/rust-parse-impl.h | 56 | ||||
-rw-r--r-- | gcc/rust/parse/rust-parse.h | 6 |
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 (); |