Skip to content

Commit 9cc80de

Browse files
committed
unified: Merge Pattern and TypeExpr into Expr
1 parent 738c91f commit 9cc80de

104 files changed

Lines changed: 741 additions & 1173 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎unified/extractor/ast_types.yml‎

Lines changed: 48 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
supertypes:
22
expr:
33
- name_expr
4+
- named_pattern
45
- int_literal
56
- float_literal
67
- boolean_literal
@@ -33,25 +34,16 @@ supertypes:
3334
- switch_expr
3435
- unresolved_operator_sequence
3536
- unsupported_node
36-
- pattern
37-
expr_or_type:
38-
- expr
39-
- type_expr
37+
- or_pattern
38+
- conditional_pattern
39+
- bulk_importing_pattern
40+
- generic_type_expr
41+
- inferred_type_expr
4042
# An element of an `unresolved_operator_sequence`: either an operand (`expr`)
4143
# or one of the infix operators separating the operands.
4244
expr_or_operator:
4345
- expr
4446
- infix_operator
45-
pattern:
46-
- name_pattern
47-
- tuple_pattern
48-
- constructor_pattern
49-
- or_pattern
50-
- conditional_pattern
51-
- ignore_pattern
52-
- expr_equality_pattern
53-
- bulk_importing_pattern
54-
- unsupported_node
5547
# A statement is anything that can appear in a block.
5648
# This type contains all of 'expr' and has partial overlap with 'member'.
5749
# For example, type_alias_declaration can appear either as a stmt or member.
@@ -91,13 +83,6 @@ supertypes:
9183
- type_alias_declaration
9284
- associated_type_declaration
9385
- unsupported_node
94-
type_expr:
95-
- named_type_expr
96-
- generic_type_expr
97-
- tuple_type_expr
98-
- function_type_expr
99-
- inferred_type_expr
100-
- unsupported_node
10186
type_constraint:
10287
- equality_type_constraint
10388
- bound_type_constraint
@@ -111,10 +96,16 @@ named:
11196
top_level:
11297
body: block
11398

114-
# An identifier used in the context of an expression
99+
# An identifier used as a name reference.
115100
name_expr:
116101
identifier: identifier
117102

103+
# An identifier that introduces a binding, optionally applying a nested pattern.
104+
named_pattern:
105+
modifier*: modifier
106+
identifier: identifier
107+
sub_pattern?: expr
108+
118109
# An integer literal
119110
int_literal:
120111

@@ -175,11 +166,10 @@ named:
175166
#
176167
# Method calls are represented as a call whose `function` is a `member_access_expr`.
177168
#
178-
# Constructor calls are marked by a language-specific modifier, and the target may be
179-
# a `type_expr` if the parser can deduce that the target is a type.
169+
# Constructor calls are marked by a language-specific modifier.
180170
call_expr:
181171
modifier*: modifier
182-
callee: expr_or_type
172+
callee: expr
183173
argument*: argument
184174

185175
argument:
@@ -189,13 +179,9 @@ named:
189179

190180
# Member access, such as `obj.member`.
191181
#
192-
# The base may be a type expression when it is a static member access like `Array<Int>.method`.
193-
# In ambiguous cases where the parser cannot distinguish static and instance member access, the base
194-
# will be typically be an expression.
195-
#
196182
# For `super.x` the base will be an instance of `super_expr`.
197183
member_access_expr:
198-
base: expr_or_type
184+
base: expr
199185
member: identifier
200186

201187
# A type expression that refers to a type inferred from the contextual type.
@@ -211,8 +197,8 @@ named:
211197
modifier*: modifier
212198
capture_declaration*: variable_declaration
213199
parameter*: parameter
214-
return_type?: type_expr
215-
body: block
200+
return_type?: expr
201+
body?: block
216202

217203
array_literal:
218204
element*: expr
@@ -228,26 +214,25 @@ named:
228214
key: expr
229215
value: expr
230216

231-
# A tuple expression, such as `(a, b, c)`.
217+
# A tuple expression, pattern, or type, such as `(a, b, c)`.
232218
tuple_expr:
233-
element*: expr
219+
element*: argument
234220

235221
# A parameter.
236222
#
237223
# `type` is its declared type annotation (if any)
238224
#
239225
# `pattern` binds the parameter's internal name(s). For a simple parameter this is a
240-
# `name_pattern`, but may be an arbitrary pattern for languages where patterns may appear
241-
# in the parameter list.
226+
# `named_pattern`, but may be an arbitrary expression where languages allow destructuring.
242227
#
243228
# `external_name` is the name by which to call sites refer to the parameter, if the parameter
244229
# can be passed as a named parameter. For example, the Swift function `func greet(person id: String)`
245-
# would have `person` as the external name and a `name_pattern` wrapping `id` is the parameter's pattern.
230+
# would have `person` as the external name and a `named_pattern` wrapping `id` as the parameter's pattern.
246231
parameter:
247232
modifier*: modifier
248233
external_name?: identifier
249-
type?: type_expr
250-
pattern?: pattern
234+
type?: expr
235+
pattern?: expr
251236
default?: expr
252237

253238
# An expression that does nothing. Used where the grammar permits an
@@ -279,8 +264,8 @@ named:
279264
# `chained_declaration` modifier so the grouping can be recovered downstream.
280265
variable_declaration:
281266
modifier*: modifier
282-
pattern: pattern
283-
type?: type_expr
267+
pattern: expr
268+
type?: expr
284269
value?: expr
285270

286271
# Evaluate 'condition', and if false, execute 'else' which must break from the enclosing block scope (return, break, etc).
@@ -323,15 +308,15 @@ named:
323308
import_declaration:
324309
modifier*: modifier
325310
imported_expr: expr # Qualified names are encoded as a chain of member_access_expr ending with a name_expr
326-
pattern?: pattern # Binds local names in scope (possibly via bulk_importing_pattern)
311+
pattern?: expr # Binds local names in scope (possibly via bulk_importing_pattern)
327312

328313
# `typealias Name = Type`
329314
type_alias_declaration:
330315
modifier*: modifier
331316
name: identifier
332317
type_parameter*: type_parameter
333318
type_constraint*: type_constraint
334-
type: type_expr
319+
type: expr
335320

336321
# A top-level function declaration.
337322
function_declaration:
@@ -340,13 +325,13 @@ named:
340325
type_parameter*: type_parameter
341326
type_constraint*: type_constraint
342327
parameter*: parameter
343-
return_type?: type_expr
328+
return_type?: expr
344329
body?: block
345330

346331
# `for pattern in iterable [where guard] { body }`.
347332
for_each_stmt:
348333
modifier*: modifier
349-
pattern: pattern
334+
pattern: expr
350335
iterable: expr
351336
guard?: expr
352337
body?: block
@@ -372,7 +357,7 @@ named:
372357

373358
catch_clause:
374359
modifier*: modifier
375-
pattern?: pattern
360+
pattern?: expr
376361
body: block
377362

378363
# `switch value { case pattern: body case ...: default: body }`
@@ -386,7 +371,7 @@ named:
386371
# A `default:` entry has no pattern.
387372
switch_case:
388373
modifier*: modifier
389-
pattern?: pattern
374+
pattern?: expr
390375
body: block
391376

392377
# Evaluate 'expr' and match its result against 'pattern', and return true if it matches.
@@ -396,80 +381,35 @@ named:
396381
#
397382
# Java: 'if (x instanceof Foo y && w ...) { ... }'
398383
pattern_guard_expr:
399-
pattern: pattern
384+
pattern: expr
400385
value: expr
401386

402387
# A type cast expression, such as `x as T`, `x as? T`, or `x as! T`. The
403388
# operator distinguishes between the variants.
404389
type_cast_expr:
405390
expr: expr
406391
operator: infix_operator
407-
type: type_expr
392+
type: expr
408393

409394
# A type-test expression, such as `x is T`. Yields a boolean indicating
410395
# whether `expr` is an instance of `type`.
411396
type_test_expr:
412397
expr: expr
413-
operator: infix_operator
414-
type: type_expr
415-
416-
# An identifier that introduces a variable.
417-
#
418-
# When used as a pattern, the pattern matches anything and binds its incoming value to the variable
419-
name_pattern:
420-
modifier*: modifier
421-
identifier: identifier
422-
sub_pattern?: pattern
423-
424-
# A pattern matching anything, binding no variables, usually using the syntax "_"
425-
ignore_pattern:
426-
427-
# A pattern that matches if the incoming value is equal to the value of the given expression.
428-
# Used for literal patterns in switch (e.g. `case 1:`).
429-
expr_equality_pattern:
430-
expr: expr
431-
432-
# A tuple pattern such as `(a, b)` in `let (a, b) = pair`.
433-
#
434-
# Elements of the tuple pattern can have names, such as Swift's `let (foo: x, bar: y) = tuple`.
435-
tuple_pattern:
436-
modifier*: modifier
437-
element*: pattern_element
438-
439-
# A pattern such as `Some(x)` where `Some` is the constructor and `x` is an element.
440-
# The element names are interpreted as argument labels and/or field names.
441-
constructor_pattern:
442-
modifier*: modifier
443-
constructor: expr_or_type
444-
element*: pattern_element
398+
operator?: infix_operator
399+
type: expr
445400

446401
# A disjunction pattern that matches if any of its sub-patterns match.
447402
or_pattern:
448403
modifier*: modifier
449-
pattern*: pattern
404+
pattern*: expr
450405

451406
# A pattern that matches against a nested pattern, and subsequently checks a condition.
452407
# The match is rejected if the condition does not hold.
453408
# Variables bound in the nested pattern are in scope within the condition.
454409
conditional_pattern:
455410
modifier*: modifier
456411
condition: expr
457-
pattern: pattern
458-
459-
# A pattern with an optional associated name.
460-
pattern_element:
461-
modifier*: modifier
462-
key?: identifier
463-
pattern: pattern
464-
465-
# A pattern that checks if the incoming value has the given type, and if so, the
466-
# value is matched against the given nested pattern (and succeeds iff the nested match succeeds).
467-
#
468-
# In Swift: `if let y = x as? Foo` is a pattern_guard_expr containing a type_test_pattern
469-
# In Java: `x instanceof Foo y` is a type_test_pattern wrapping a name_pattern
470-
type_test_pattern:
471-
pattern: pattern
472-
type: type_expr
412+
pattern: expr
473413

474414
# A '*' pattern that imports all members of the incoming value into the local scope
475415
# Currently this can only appear in import declarations.
@@ -495,21 +435,21 @@ named:
495435
type_parameter:
496436
modifier*: modifier
497437
name: identifier
498-
bound?: type_expr
438+
bound?: expr
499439

500440
# A generic constraint of the form `T == U`, requiring two types to be
501441
# equal. Appears in `where` clauses on generic declarations
502442
# (e.g. Swift `func foo<T, U>() where T == U`).
503443
equality_type_constraint:
504-
left: type_expr
505-
right: type_expr
444+
left: expr
445+
right: expr
506446

507447
# A generic constraint of the form `T: Bound`, requiring a type parameter
508448
# to conform to (or inherit from) some other type. Appears in `where`
509449
# clauses on generic declarations (e.g. Swift `where T: Equatable`).
510450
bound_type_constraint:
511-
type: type_expr
512-
bound: type_expr
451+
type: expr
452+
bound: expr
513453

514454
# `infix operator +++` (and the like) — a declaration of a custom operator.
515455
operator_syntax_declaration:
@@ -541,7 +481,7 @@ named:
541481
# kind should be included as a modifier on this node.
542482
base_type:
543483
modifier*: modifier
544-
type: type_expr
484+
type: expr
545485

546486
constructor_declaration:
547487
modifier*: modifier
@@ -568,7 +508,7 @@ named:
568508
name: identifier
569509
accessor_kind: accessor_kind
570510
parameter*: parameter
571-
type?: type_expr
511+
type?: expr
572512
body?: block
573513

574514
# "get", "set", or a language-specific kind like "didSet"
@@ -582,29 +522,11 @@ named:
582522
associated_type_declaration:
583523
modifier*: modifier
584524
name: identifier
585-
bound?: type_expr
586-
587-
named_type_expr:
588-
qualifier?: type_expr
589-
name: identifier
525+
bound?: expr
590526

591527
generic_type_expr:
592-
base: type_expr
593-
type_argument*: type_expr
594-
595-
# A tuple type such as `(Int, String)` or `(a: A, b: B)`.
596-
tuple_type_expr:
597-
element*: tuple_type_element
598-
599-
# An element of a `tuple_type_expr`, optionally carrying a label.
600-
tuple_type_element:
601-
name?: identifier
602-
type: type_expr
603-
604-
# A function type such as `(Int, String) -> Bool` or `(x: Int) -> Bool`.
605-
function_type_expr:
606-
parameter*: parameter
607-
return_type: type_expr
528+
base: expr
529+
type_argument*: expr
608530

609531
# A modifier such as 'static', 'public', or 'async'. For now this is just a leaf node with a string value.
610532
modifier:

0 commit comments

Comments
 (0)