diff options
author | Mark Wielaard <mark@klomp.org> | 2021-07-29 00:00:55 +0200 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2021-08-11 10:31:10 +0100 |
commit | 02424cf671eeee25ca371d17eef4a65c71de8eee (patch) | |
tree | ba6ff3a0b5287855dab4347726cf27d0061912c1 /gcc/rust/parse/rust-parse.h | |
parent | e10f3d65566101c774a308f8286e2827455941ed (diff) | |
download | gcc-02424cf671eeee25ca371d17eef4a65c71de8eee.zip gcc-02424cf671eeee25ca371d17eef4a65c71de8eee.tar.gz gcc-02424cf671eeee25ca371d17eef4a65c71de8eee.tar.bz2 |
Pass pratt parsed location to expr parser functions to fix expr locus
The pratt parser skips the first token of an expression before
invoking the actual expression parsing function. This makes getting
the correct starting location of a pratt parsed expression hard. The
"correction" of the location by subtracting an integer is often wrong
(since there may be arbitrary whitespace or comments between
tokens). Fix this by passing the location of the skipped token to the
expression parsing functions (instead of just providing a pratt_parse
boolean). Use this location to set the start of the expression (and as
indicator to not try to parse the first token of the expression).
Before gccrs would generate the following error message:
return.rs:3:22: error: cannot ‘break’ outside of a loop
3 | let x = 5 - break return (16 + 2);
| ^~~~~~~~~~~~~~~~~
Now we get:
return.rs:3:17: error: cannot ‘break’ outside of a loop
3 | let x = 5 - break return (16 + 2);
| ^
Diffstat (limited to 'gcc/rust/parse/rust-parse.h')
-rw-r--r-- | gcc/rust/parse/rust-parse.h | 72 |
1 files changed, 44 insertions, 28 deletions
diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h index 3920893..86e0d2a 100644 --- a/gcc/rust/parse/rust-parse.h +++ b/gcc/rust/parse/rust-parse.h @@ -121,8 +121,14 @@ private: AST::PathInExpression parse_path_in_expression (); AST::PathExprSegment parse_path_expr_segment (); AST::QualifiedPathInExpression - parse_qualified_path_in_expression (bool pratt_parse = false); - AST::QualifiedPathType parse_qualified_path_type (bool pratt_parse = false); + // When given a pratt_parsed_loc, use it as the location of the + // first token parsed in the expression (the parsing of that first + // token should be skipped). + parse_qualified_path_in_expression (Location pratt_parsed_loc + = Linemap::unknown_location ()); + AST::QualifiedPathType + parse_qualified_path_type (Location pratt_parsed_loc + = Linemap::unknown_location ()); AST::QualifiedPathInType parse_qualified_path_in_type (); // Token tree or macro related @@ -469,32 +475,36 @@ private: parse_expr_with_block (AST::AttrVec outer_attrs); std::unique_ptr<AST::ExprWithoutBlock> parse_expr_without_block (AST::AttrVec outer_attrs = AST::AttrVec ()); - std::unique_ptr<AST::BlockExpr> parse_block_expr (AST::AttrVec outer_attrs - = AST::AttrVec (), - bool pratt_parse = false); - std::unique_ptr<AST::IfExpr> parse_if_expr (AST::AttrVec outer_attrs - = AST::AttrVec (), - bool pratt_parse = false); - std::unique_ptr<AST::IfLetExpr> parse_if_let_expr (AST::AttrVec outer_attrs - = AST::AttrVec (), - bool pratt_parse = false); + // When given a pratt_parsed_loc, use it as the location of the + // first token parsed in the expression (the parsing of that first + // token should be skipped). + std::unique_ptr<AST::BlockExpr> + parse_block_expr (AST::AttrVec outer_attrs = AST::AttrVec (), + Location pratt_parsed_loc = Linemap::unknown_location ()); + std::unique_ptr<AST::IfExpr> + parse_if_expr (AST::AttrVec outer_attrs = AST::AttrVec (), + Location pratt_parsed_loc = Linemap::unknown_location ()); + std::unique_ptr<AST::IfLetExpr> + parse_if_let_expr (AST::AttrVec outer_attrs = AST::AttrVec (), + Location pratt_parsed_loc = Linemap::unknown_location ()); std::unique_ptr<AST::LoopExpr> parse_loop_expr (AST::AttrVec outer_attrs = AST::AttrVec (), AST::LoopLabel label = AST::LoopLabel::error (), - bool pratt_parse = false); + Location pratt_parsed_loc = Linemap::unknown_location ()); std::unique_ptr<AST::WhileLoopExpr> parse_while_loop_expr (AST::AttrVec outer_attrs = AST::AttrVec (), AST::LoopLabel label = AST::LoopLabel::error (), - bool pratt_parse = false); + Location pratt_parsed_loc + = Linemap::unknown_location ()); std::unique_ptr<AST::WhileLetLoopExpr> parse_while_let_loop_expr (AST::AttrVec outer_attrs = AST::AttrVec (), AST::LoopLabel label = AST::LoopLabel::error ()); std::unique_ptr<AST::ForLoopExpr> parse_for_loop_expr (AST::AttrVec outer_attrs = AST::AttrVec (), AST::LoopLabel label = AST::LoopLabel::error ()); - std::unique_ptr<AST::MatchExpr> parse_match_expr (AST::AttrVec outer_attrs - = AST::AttrVec (), - bool pratt_parse = false); + std::unique_ptr<AST::MatchExpr> + parse_match_expr (AST::AttrVec outer_attrs = AST::AttrVec (), + Location pratt_parsed_loc = Linemap::unknown_location ()); AST::MatchArm parse_match_arm (); std::vector<std::unique_ptr<AST::Pattern> > parse_match_arm_patterns (TokenId end_token_id); @@ -510,24 +520,30 @@ private: AST::ClosureParam parse_closure_param (); std::unique_ptr<AST::LiteralExpr> parse_literal_expr (AST::AttrVec outer_attrs = AST::AttrVec ()); - std::unique_ptr<AST::ReturnExpr> parse_return_expr (AST::AttrVec outer_attrs - = AST::AttrVec (), - bool pratt_parse = false); - std::unique_ptr<AST::BreakExpr> parse_break_expr (AST::AttrVec outer_attrs - = AST::AttrVec (), - bool pratt_parse = false); + // When given a pratt_parsed_loc, use it as the location of the + // first token parsed in the expression (the parsing of that first + // token should be skipped). + std::unique_ptr<AST::ReturnExpr> + parse_return_expr (AST::AttrVec outer_attrs = AST::AttrVec (), + Location pratt_parsed_loc = Linemap::unknown_location ()); + std::unique_ptr<AST::BreakExpr> + parse_break_expr (AST::AttrVec outer_attrs = AST::AttrVec (), + Location pratt_parsed_loc = Linemap::unknown_location ()); std::unique_ptr<AST::ContinueExpr> parse_continue_expr (AST::AttrVec outer_attrs = AST::AttrVec (), - bool pratt_parse = false); + Location pratt_parsed_loc + = Linemap::unknown_location ()); std::unique_ptr<AST::UnsafeBlockExpr> parse_unsafe_block_expr (AST::AttrVec outer_attrs = AST::AttrVec (), - bool pratt_parse = false); - std::unique_ptr<AST::ArrayExpr> parse_array_expr (AST::AttrVec outer_attrs - = AST::AttrVec (), - bool pratt_parse = false); + Location pratt_parsed_loc + = Linemap::unknown_location ()); + std::unique_ptr<AST::ArrayExpr> + parse_array_expr (AST::AttrVec outer_attrs = AST::AttrVec (), + Location pratt_parsed_loc = Linemap::unknown_location ()); std::unique_ptr<AST::ExprWithoutBlock> parse_grouped_or_tuple_expr (AST::AttrVec outer_attrs = AST::AttrVec (), - bool pratt_parse = false); + Location pratt_parsed_loc + = Linemap::unknown_location ()); std::unique_ptr<AST::StructExprField> parse_struct_expr_field (); bool will_be_expr_with_block (); |