Files
carbon-lang/toolchain/parse/testdata/var/var_struct.carbon
T
oli-ej a683fb574b Add initial support for parsing struct patterns (#7446)
Implements parsing of struct patterns as per
https://github.com/carbon-language/carbon-lang/issues/6680.

This handles the full and short syntax given in the [design
doc](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/pattern_matching.md#struct-patterns),
but the handling of the shorthand syntax may need to change as I move on
to implementing Check support (currently the shorthand is represented as
one of `LetBindingPattern`, `VarBindingPattern`, or `VariablePattern`).

This implementation assumes that the answer to
https://github.com/carbon-language/carbon-lang/issues/7404 is that
trailing commas are allowed in the struct pattern, except for the case
where an `_` is present, in which case the next token must be the
closing brace `}`.
2026-07-30 16:37:46 +00:00

125 lines
8.0 KiB
Plaintext

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/parse/testdata/var/var_struct.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/parse/testdata/var/var_struct.carbon
// --- var_struct.carbon
var {.s = s: str, .i = i: 32} = {.s = "hello", .i = 0};
// --- var_struct_shorthand.carbon
var {s: str, i: 32} = {.s = "hello shorthand", .i = 1};
// --- var_struct_mixed.carbon
var {.s = s: str, i: 32} = {.s = "hello mixed", .i = 1};
// CHECK:STDOUT: - filename: var_struct.carbon
// CHECK:STDOUT: parse_tree: [
// CHECK:STDOUT: {kind: 'FileStart', text: ''},
// CHECK:STDOUT: {kind: 'VariableIntroducer', text: 'var'},
// CHECK:STDOUT: {kind: 'StructPatternStart', text: '{'},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeSignature', text: 's'},
// CHECK:STDOUT: {kind: 'StructFieldDesignator', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeSignature', text: 's'},
// CHECK:STDOUT: {kind: 'BindingPatternTypeStart', text: ':'},
// CHECK:STDOUT: {kind: 'StringTypeLiteral', text: 'str'},
// CHECK:STDOUT: {kind: 'VarBindingPattern', text: ':', subtree_size: 4},
// CHECK:STDOUT: {kind: 'StructPatternDesignatedField', text: '=', subtree_size: 7},
// CHECK:STDOUT: {kind: 'PatternListComma', text: ','},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeSignature', text: 'i'},
// CHECK:STDOUT: {kind: 'StructFieldDesignator', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeSignature', text: 'i'},
// CHECK:STDOUT: {kind: 'BindingPatternTypeStart', text: ':'},
// CHECK:STDOUT: {kind: 'IntLiteral', text: '32'},
// CHECK:STDOUT: {kind: 'VarBindingPattern', text: ':', subtree_size: 4},
// CHECK:STDOUT: {kind: 'StructPatternDesignatedField', text: '=', subtree_size: 7},
// CHECK:STDOUT: {kind: 'StructPattern', text: '}', subtree_size: 17},
// CHECK:STDOUT: {kind: 'VariablePattern', text: 'var', subtree_size: 18},
// CHECK:STDOUT: {kind: 'VariableInitializer', text: '='},
// CHECK:STDOUT: {kind: 'StructLiteralStart', text: '{'},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeSignature', text: 's'},
// CHECK:STDOUT: {kind: 'StructFieldDesignator', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'StringLiteral', text: '"hello"'},
// CHECK:STDOUT: {kind: 'StructLiteralField', text: '=', subtree_size: 4},
// CHECK:STDOUT: {kind: 'StructLiteralComma', text: ','},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeSignature', text: 'i'},
// CHECK:STDOUT: {kind: 'StructFieldDesignator', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'IntLiteral', text: '0'},
// CHECK:STDOUT: {kind: 'StructLiteralField', text: '=', subtree_size: 4},
// CHECK:STDOUT: {kind: 'StructLiteral', text: '}', subtree_size: 11},
// CHECK:STDOUT: {kind: 'VariableDecl', text: ';', subtree_size: 32},
// CHECK:STDOUT: {kind: 'FileEnd', text: ''},
// CHECK:STDOUT: ]
// CHECK:STDOUT: - filename: var_struct_shorthand.carbon
// CHECK:STDOUT: parse_tree: [
// CHECK:STDOUT: {kind: 'FileStart', text: ''},
// CHECK:STDOUT: {kind: 'VariableIntroducer', text: 'var'},
// CHECK:STDOUT: {kind: 'StructPatternStart', text: '{'},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeSignature', text: 's'},
// CHECK:STDOUT: {kind: 'BindingPatternTypeStart', text: ':'},
// CHECK:STDOUT: {kind: 'StringTypeLiteral', text: 'str'},
// CHECK:STDOUT: {kind: 'VarBindingPattern', text: ':', subtree_size: 4},
// CHECK:STDOUT: {kind: 'PatternListComma', text: ','},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeSignature', text: 'i'},
// CHECK:STDOUT: {kind: 'BindingPatternTypeStart', text: ':'},
// CHECK:STDOUT: {kind: 'IntLiteral', text: '32'},
// CHECK:STDOUT: {kind: 'VarBindingPattern', text: ':', subtree_size: 4},
// CHECK:STDOUT: {kind: 'StructPattern', text: '}', subtree_size: 11},
// CHECK:STDOUT: {kind: 'VariablePattern', text: 'var', subtree_size: 12},
// CHECK:STDOUT: {kind: 'VariableInitializer', text: '='},
// CHECK:STDOUT: {kind: 'StructLiteralStart', text: '{'},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeSignature', text: 's'},
// CHECK:STDOUT: {kind: 'StructFieldDesignator', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'StringLiteral', text: '"hello shorthand"'},
// CHECK:STDOUT: {kind: 'StructLiteralField', text: '=', subtree_size: 4},
// CHECK:STDOUT: {kind: 'StructLiteralComma', text: ','},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeSignature', text: 'i'},
// CHECK:STDOUT: {kind: 'StructFieldDesignator', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'IntLiteral', text: '1'},
// CHECK:STDOUT: {kind: 'StructLiteralField', text: '=', subtree_size: 4},
// CHECK:STDOUT: {kind: 'StructLiteral', text: '}', subtree_size: 11},
// CHECK:STDOUT: {kind: 'VariableDecl', text: ';', subtree_size: 26},
// CHECK:STDOUT: {kind: 'FileEnd', text: ''},
// CHECK:STDOUT: ]
// CHECK:STDOUT: - filename: var_struct_mixed.carbon
// CHECK:STDOUT: parse_tree: [
// CHECK:STDOUT: {kind: 'FileStart', text: ''},
// CHECK:STDOUT: {kind: 'VariableIntroducer', text: 'var'},
// CHECK:STDOUT: {kind: 'StructPatternStart', text: '{'},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeSignature', text: 's'},
// CHECK:STDOUT: {kind: 'StructFieldDesignator', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeSignature', text: 's'},
// CHECK:STDOUT: {kind: 'BindingPatternTypeStart', text: ':'},
// CHECK:STDOUT: {kind: 'StringTypeLiteral', text: 'str'},
// CHECK:STDOUT: {kind: 'VarBindingPattern', text: ':', subtree_size: 4},
// CHECK:STDOUT: {kind: 'StructPatternDesignatedField', text: '=', subtree_size: 7},
// CHECK:STDOUT: {kind: 'PatternListComma', text: ','},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeSignature', text: 'i'},
// CHECK:STDOUT: {kind: 'BindingPatternTypeStart', text: ':'},
// CHECK:STDOUT: {kind: 'IntLiteral', text: '32'},
// CHECK:STDOUT: {kind: 'VarBindingPattern', text: ':', subtree_size: 4},
// CHECK:STDOUT: {kind: 'StructPattern', text: '}', subtree_size: 14},
// CHECK:STDOUT: {kind: 'VariablePattern', text: 'var', subtree_size: 15},
// CHECK:STDOUT: {kind: 'VariableInitializer', text: '='},
// CHECK:STDOUT: {kind: 'StructLiteralStart', text: '{'},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeSignature', text: 's'},
// CHECK:STDOUT: {kind: 'StructFieldDesignator', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'StringLiteral', text: '"hello mixed"'},
// CHECK:STDOUT: {kind: 'StructLiteralField', text: '=', subtree_size: 4},
// CHECK:STDOUT: {kind: 'StructLiteralComma', text: ','},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeSignature', text: 'i'},
// CHECK:STDOUT: {kind: 'StructFieldDesignator', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'IntLiteral', text: '1'},
// CHECK:STDOUT: {kind: 'StructLiteralField', text: '=', subtree_size: 4},
// CHECK:STDOUT: {kind: 'StructLiteral', text: '}', subtree_size: 11},
// CHECK:STDOUT: {kind: 'VariableDecl', text: ';', subtree_size: 29},
// CHECK:STDOUT: {kind: 'FileEnd', text: ''},
// CHECK:STDOUT: ]