Change array syntax from [T; N] to array(T, N) (#4981)

In line with the proposal in #4682, this changes the array syntax to be
array(T, N). `array` is a builtin keyword which must be followed by
parens containing two expressions and a separating comma.

The array type expression is still fully builtin, it does not forward to
a Core.Array library type yet. It merely adds the `ArrayType`
instruction, as was done with the previous syntax.

Followup work will change the instruction to reference to Core.Array,
once the library type exists and can be used directly.

---------

Co-authored-by: zygoloid <richard@metafoo.co.uk>
This commit is contained in:
Dana Jansens
2025-02-20 22:42:47 +00:00
committed by GitHub
co-authored by zygoloid
parent fc7b0016ce
commit 24bde46181
125 changed files with 1912 additions and 1889 deletions
+17 -15
View File
@@ -13,28 +13,30 @@ namespace Carbon::Parse {
auto HandleArrayExpr(Context& context) -> void {
auto state = context.PopState();
context.AddLeafNode(NodeKind::ArrayExprStart,
context.ConsumeChecked(Lex::TokenKind::OpenSquareBracket),
state.has_error);
context.PushState(state, State::ArrayExprSemi);
auto array_token = context.ConsumeChecked(Lex::TokenKind::Array);
context.AddLeafNode(NodeKind::ArrayExprKeyword, array_token, state.has_error);
if (auto open_paren = context.ConsumeAndAddOpenParen(
array_token, NodeKind::ArrayExprOpenParen)) {
state.token = *open_paren;
} else {
state.has_error = true;
}
context.PushState(state, State::ArrayExprComma);
context.PushState(State::Expr);
}
auto HandleArrayExprSemi(Context& context) -> void {
auto HandleArrayExprComma(Context& context) -> void {
auto state = context.PopState();
auto semi = context.ConsumeIf(Lex::TokenKind::Semi);
if (!semi) {
context.AddNode(NodeKind::ArrayExprSemi, *context.position(), true);
CARBON_DIAGNOSTIC(ExpectedArraySemi, Error, "expected `;` in array type");
context.emitter().Emit(*context.position(), ExpectedArraySemi);
if (!context.ConsumeAndAddLeafNodeIf(Lex::TokenKind::Comma,
NodeKind::ArrayExprComma)) {
context.AddLeafNode(NodeKind::ArrayExprComma, *context.position(), true);
CARBON_DIAGNOSTIC(ExpectedArrayComma, Error,
"expected `,` in `array(Type, Count)`");
context.emitter().Emit(*context.position(), ExpectedArrayComma);
state.has_error = true;
} else {
context.AddNode(NodeKind::ArrayExprSemi, *semi, state.has_error);
}
context.PushState(state, State::ArrayExprFinish);
if (!context.PositionIs(Lex::TokenKind::CloseSquareBracket)) {
context.PushState(State::Expr);
}
context.PushState(State::Expr);
}
auto HandleArrayExprFinish(Context& context) -> void {