@@ -1632,7 +1632,7 @@ absl::StatusOr<Expr*> Parser::ParseTerm(Bindings& outer_bindings,
1632
1632
lhs, std::move (args)));
1633
1633
break ;
1634
1634
}
1635
- case TokenKind::kDot : {
1635
+ case TokenKind::kDot : { // Attribute or tuple index access.
1636
1636
DropTokenOrDie ();
1637
1637
XLS_ASSIGN_OR_RETURN (Token tok, PopToken ());
1638
1638
const Span span (new_pos, GetPos ());
@@ -1651,15 +1651,19 @@ absl::StatusOr<Expr*> Parser::ParseTerm(Bindings& outer_bindings,
1651
1651
}
1652
1652
break ;
1653
1653
}
1654
- case TokenKind::kOBrack : {
1654
+ case TokenKind::kOBrack : { // Indexing.
1655
1655
DropTokenOrDie ();
1656
+
1656
1657
XLS_ASSIGN_OR_RETURN (bool dropped_colon,
1657
1658
TryDropToken (TokenKind::kColon ));
1658
1659
if (dropped_colon) { // Slice-from-beginning.
1659
1660
XLS_ASSIGN_OR_RETURN (lhs, ParseBitSlice (new_pos, lhs, outer_bindings,
1660
1661
/* start=*/ nullptr ));
1661
1662
break ;
1662
1663
}
1664
+
1665
+ // Index may be followed by a `:` for a slice, or a `+:` for a
1666
+ // type-sized slice.
1663
1667
XLS_ASSIGN_OR_RETURN (Expr * index, ParseExpression (outer_bindings));
1664
1668
XLS_ASSIGN_OR_RETURN (peek, PeekToken ());
1665
1669
switch (peek->kind ()) {
@@ -1680,20 +1684,34 @@ absl::StatusOr<Expr*> Parser::ParseTerm(Bindings& outer_bindings,
1680
1684
lhs, ParseBitSlice (new_pos, lhs, outer_bindings, index));
1681
1685
break ;
1682
1686
}
1683
- default :
1687
+ default : {
1684
1688
XLS_RETURN_IF_ERROR (DropTokenOrError (TokenKind::kCBrack ));
1685
1689
1686
1690
// It's either an Index if the LHS is a NameRef or
1687
1691
// ColonRef-to-ConstantDef, or an Array if lhs is a
1688
1692
// ColonRef-to-type.
1689
- Span span (new_pos, GetPos ());
1693
+ const Span span (new_pos, GetPos ());
1690
1694
XLS_ASSIGN_OR_RETURN (peek, PeekToken ());
1695
+ // Array type before literal, e.g. Foo[2]:[...]
1696
+ // this colon ----------^
1691
1697
if (peek->kind () == TokenKind::kColon ) {
1692
1698
DropTokenOrDie ();
1699
+ absl::StatusOr<TypeDefinition> type_definition_or =
1700
+ ToTypeDefinition (lhs);
1701
+ if (!type_definition_or.ok ()) {
1702
+ const Span error_span (lhs->span ().start (),
1703
+ index->span ().limit ());
1704
+ return ParseErrorStatus (
1705
+ error_span,
1706
+ absl::StrFormat (
1707
+ " Type before ':' for presumed array literal was not a "
1708
+ " type definition; got `%s` (kind: %v)" ,
1709
+ lhs->ToString (), lhs->kind ()));
1710
+ }
1693
1711
// TODO(rspringer): We can't currently support parameterized
1694
1712
// ColonRef-to-types with this function structure.
1695
1713
auto * type_ref =
1696
- module_->Make <TypeRef>(span, down_cast<ColonRef*>(lhs ));
1714
+ module_->Make <TypeRef>(span, type_definition_or. value ( ));
1697
1715
auto * type_ref_type = module_->Make <TypeRefTypeAnnotation>(
1698
1716
span, type_ref, /* parametrics=*/ std::vector<ExprOrType>());
1699
1717
auto * array_type = module_->Make <ArrayTypeAnnotation>(
@@ -1705,6 +1723,7 @@ absl::StatusOr<Expr*> Parser::ParseTerm(Bindings& outer_bindings,
1705
1723
} else {
1706
1724
lhs = module_->Make <Index>(span, lhs, index);
1707
1725
}
1726
+ }
1708
1727
}
1709
1728
break ;
1710
1729
}
0 commit comments