* "has_whatever" pairs with
* optional types (std::optional or boost::optional)? */
+// Loop label expression AST node used with break and continue expressions
+// TODO: inline?
+class LoopLabel /*: public Node*/
+{
+ Lifetime label; // or type LIFETIME_OR_LABEL
+ location_t locus;
+
+ NodeId node_id;
+
+public:
+ std::string as_string () const;
+
+ LoopLabel (Lifetime loop_label, location_t locus = UNDEF_LOCATION)
+ : label (std::move (loop_label)), locus (locus),
+ node_id (Analysis::Mappings::get ()->get_next_node_id ())
+ {}
+
+ // Returns whether the LoopLabel is in an error state.
+ bool is_error () const { return label.is_error (); }
+
+ // Creates an error state LoopLabel.
+ static LoopLabel error () { return LoopLabel (Lifetime::error ()); }
+
+ location_t get_locus () const { return locus; }
+
+ Lifetime &get_lifetime () { return label; }
+
+ NodeId get_node_id () const { return node_id; }
+};
+
// AST node for an expression with an accompanying block - abstract
class ExprWithBlock : public Expr
{
std::vector<Attribute> inner_attrs;
std::vector<std::unique_ptr<Stmt> > statements;
std::unique_ptr<Expr> expr;
+ LoopLabel label;
location_t start_locus;
location_t end_locus;
bool marked_for_strip = false;
BlockExpr (std::vector<std::unique_ptr<Stmt> > block_statements,
std::unique_ptr<Expr> block_expr,
std::vector<Attribute> inner_attribs,
- std::vector<Attribute> outer_attribs, location_t start_locus,
- location_t end_locus)
+ std::vector<Attribute> outer_attribs, LoopLabel label,
+ location_t start_locus, location_t end_locus)
: outer_attrs (std::move (outer_attribs)),
inner_attrs (std::move (inner_attribs)),
statements (std::move (block_statements)), expr (std::move (block_expr)),
- start_locus (start_locus), end_locus (end_locus)
+ label (std::move (label)), start_locus (start_locus),
+ end_locus (end_locus)
{}
// Copy constructor with clone
BlockExpr (BlockExpr const &other)
: ExprWithBlock (other), outer_attrs (other.outer_attrs),
- inner_attrs (other.inner_attrs), start_locus (other.start_locus),
- end_locus (other.end_locus), marked_for_strip (other.marked_for_strip)
+ inner_attrs (other.inner_attrs), label (other.label),
+ start_locus (other.start_locus), end_locus (other.end_locus),
+ marked_for_strip (other.marked_for_strip)
{
// guard to protect from null pointer dereference
if (other.expr != nullptr)
outer_attrs = std::move (new_attrs);
}
+ bool has_label () { return !label.is_error (); }
+ LoopLabel &get_label () { return label; }
+
protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
}
};
-// Loop label expression AST node used with break and continue expressions
-// TODO: inline?
-class LoopLabel /*: public Node*/
-{
- Lifetime label; // or type LIFETIME_OR_LABEL
- location_t locus;
-
- NodeId node_id;
-
-public:
- std::string as_string () const;
-
- LoopLabel (Lifetime loop_label, location_t locus = UNDEF_LOCATION)
- : label (std::move (loop_label)), locus (locus),
- node_id (Analysis::Mappings::get ()->get_next_node_id ())
- {}
-
- // Returns whether the LoopLabel is in an error state.
- bool is_error () const { return label.is_error (); }
-
- // Creates an error state LoopLabel.
- static LoopLabel error () { return LoopLabel (Lifetime::error ()); }
-
- location_t get_locus () const { return locus; }
-
- Lifetime &get_lifetime () { return label; }
-
- NodeId get_node_id () const { return node_id; }
-};
-
// Base loop expression AST node - aka LoopExpr
class BaseLoopExpr : public ExprWithBlock
{
template <typename ManagedTokenSource>
std::unique_ptr<AST::BlockExpr>
Parser<ManagedTokenSource>::parse_block_expr (AST::AttrVec outer_attrs,
+ AST::LoopLabel label,
location_t pratt_parsed_loc)
{
location_t locus = pratt_parsed_loc;
return std::unique_ptr<AST::BlockExpr> (
new AST::BlockExpr (std::move (stmts), std::move (expr),
- std::move (inner_attrs), std::move (outer_attrs), locus,
- end_locus));
+ std::move (inner_attrs), std::move (outer_attrs),
+ std::move (label), locus, end_locus));
}
/* Parses a "grouped" expression (expression in parentheses), used to control
// Parses a loop expression with label (any kind of loop - disambiguates).
template <typename ManagedTokenSource>
-std::unique_ptr<AST::BaseLoopExpr>
+std::unique_ptr<AST::Expr>
Parser<ManagedTokenSource>::parse_labelled_loop_expr (const_TokenPtr tok,
AST::AttrVec outer_attrs)
{
return parse_while_loop_expr (std::move (outer_attrs),
std::move (label));
}
+ case LEFT_CURLY:
+ return parse_block_expr (std::move (outer_attrs), std::move (label));
default:
// error
add_error (Error (t->get_locus (),
return parse_continue_expr (std::move (outer_attrs), tok->get_locus ());
case LEFT_CURLY:
// ok - this is an expression with block for once.
- return parse_block_expr (std::move (outer_attrs), tok->get_locus ());
+ return parse_block_expr (std::move (outer_attrs),
+ AST::LoopLabel::error (), tok->get_locus ());
case IF:
// if or if let, so more lookahead to find out
if (lexer.peek_token ()->get_id () == LET)
std::unique_ptr<AST::BlockExpr>
parse_block_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
+ AST::LoopLabel label = AST::LoopLabel::error (),
location_t pratt_parsed_loc = UNKNOWN_LOCATION);
bool is_macro_rules_def (const_TokenPtr t);
AST::MatchArm parse_match_arm ();
std::vector<std::unique_ptr<AST::Pattern> >
parse_match_arm_patterns (TokenId end_token_id);
- std::unique_ptr<AST::BaseLoopExpr>
- parse_labelled_loop_expr (const_TokenPtr tok,
- AST::AttrVec outer_attrs = AST::AttrVec ());
+ std::unique_ptr<AST::Expr> parse_labelled_loop_expr (const_TokenPtr tok,
+ AST::AttrVec outer_attrs
+ = AST::AttrVec ());
AST::LoopLabel parse_loop_label (const_TokenPtr tok);
std::unique_ptr<AST::AsyncBlockExpr>
parse_async_block_expr (AST::AttrVec outer_attrs = AST::AttrVec ());