mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Support for parsing expression patterns (#6977)
When parsing a pattern, if we encounter something that isn't pattern syntax, try parsing as an expression instead. We only need one-token lookahead to distinguish pattern syntax from expression syntax. Track a precedence group through pattern parsing so that we can allow different kinds of expressions in a top-level pattern (such as the operand of `let`) and in a nested pattern (such as a subpattern of a tuple pattern or within grouping parens). For example, we do not allow `case if ...`, and for now I've chosen to also not allow logical or relational operators at the top level of a pattern, so `case 1 + 1` is OK, but `case 1 == 1` and `case true and false` require parentheses. This decision should be ratified or revisited by a design proposal. Very basic check support is also provided, only sufficient to form an `ExprPattern` instruction and nothing beyond that. For now, all pattern matching against an `ExprPattern` fails with a TODO error. To support that, I've switched from calling `BeginSubpattern` in the parent handler of a pattern and `EndSubpatternAs*` in the pattern handler itself to calling both functions in parent handlers, with `EndSubpattern` converting an expression into an expression pattern where needed. Depends on #6976. Assisted-by: Gemini via Google Antigravity
This commit is contained in:
+5
-11
@@ -146,12 +146,8 @@ fn F();
|
||||
struct X { fn () }
|
||||
fn F();
|
||||
|
||||
// --- fail_with_identifier_as_param.carbon
|
||||
// --- with_identifier_as_param.carbon
|
||||
|
||||
// CHECK:STDERR: fail_with_identifier_as_param.carbon:[[@LINE+4]]:11: error: expected `:`, `:!`, or `:?` in binding pattern [ExpectedBindingPattern]
|
||||
// CHECK:STDERR: fn foo(bar);
|
||||
// CHECK:STDERR: ^
|
||||
// CHECK:STDERR:
|
||||
fn foo(bar);
|
||||
|
||||
// --- fail_without_name_and_many_tokens_in_params.carbon
|
||||
@@ -426,17 +422,15 @@ fn ComplexReturnForm() ->? X.Y(Z);
|
||||
// CHECK:STDOUT: {kind: 'FunctionDecl', text: ';', subtree_size: 5},
|
||||
// CHECK:STDOUT: {kind: 'FileEnd', text: ''},
|
||||
// CHECK:STDOUT: ]
|
||||
// CHECK:STDOUT: - filename: fail_with_identifier_as_param.carbon
|
||||
// CHECK:STDOUT: - filename: with_identifier_as_param.carbon
|
||||
// CHECK:STDOUT: parse_tree: [
|
||||
// CHECK:STDOUT: {kind: 'FileStart', text: ''},
|
||||
// CHECK:STDOUT: {kind: 'FunctionIntroducer', text: 'fn'},
|
||||
// CHECK:STDOUT: {kind: 'IdentifierNameMaybeBeforeSignature', text: 'foo'},
|
||||
// CHECK:STDOUT: {kind: 'ExplicitParamListStart', text: '('},
|
||||
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeSignature', text: 'bar'},
|
||||
// CHECK:STDOUT: {kind: 'InvalidParse', text: ')', has_error: yes},
|
||||
// CHECK:STDOUT: {kind: 'LetBindingPattern', text: 'bar', has_error: yes, subtree_size: 3},
|
||||
// CHECK:STDOUT: {kind: 'ExplicitParamList', text: ')', has_error: yes, subtree_size: 5},
|
||||
// CHECK:STDOUT: {kind: 'FunctionDecl', text: ';', subtree_size: 8},
|
||||
// CHECK:STDOUT: {kind: 'IdentifierNameExpr', text: 'bar'},
|
||||
// CHECK:STDOUT: {kind: 'ExplicitParamList', text: ')', subtree_size: 3},
|
||||
// CHECK:STDOUT: {kind: 'FunctionDecl', text: ';', subtree_size: 6},
|
||||
// CHECK:STDOUT: {kind: 'FileEnd', text: ''},
|
||||
// CHECK:STDOUT: ]
|
||||
// CHECK:STDOUT: - filename: fail_without_name_and_many_tokens_in_params.carbon
|
||||
|
||||
Reference in New Issue
Block a user