Skip to content

Commit 0a3dd53

Browse files
authored
Fix pattern definition (#38) (#39)
1 parent 98f7034 commit 0a3dd53

File tree

5 files changed

+68181
-67369
lines changed

5 files changed

+68181
-67369
lines changed

corpus/patterns.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ val x = y match {
3030
case 1 : Int => 2
3131
case a : B with C => d
3232
case _: B | _: C => 3
33+
case Object.Constant => 3
3334
}
3435

3536
---
@@ -47,6 +48,9 @@ val x = y match {
4748
(alternative_pattern
4849
(typed_pattern (wildcard) (type_identifier))
4950
(typed_pattern (wildcard) (type_identifier)))
51+
(integer_literal))
52+
(case_clause
53+
(stable_identifier (identifier) (identifier))
5054
(integer_literal))))))
5155

5256
============================
@@ -136,6 +140,7 @@ Capture patterns
136140
val x = y match {
137141
case a @ B(1) => a
138142
case b @ C(d @ (e @ X, _: Y)) => e
143+
case req @ (POST | GET) -> Root / "test" => 5
139144
}
140145

141146
---
@@ -156,4 +161,11 @@ val x = y match {
156161
(tuple_pattern
157162
(capture_pattern (identifier) (identifier))
158163
(typed_pattern (wildcard) (type_identifier))))))
159-
(identifier))))))
164+
(identifier))
165+
(case_clause
166+
(infix_pattern
167+
(infix_pattern
168+
(capture_pattern (identifier)
169+
(tuple_pattern (alternative_pattern (identifier) (identifier))))
170+
(operator_identifier) (identifier)) (operator_identifier) (string))
171+
(integer_literal))))))

grammar.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ module.exports = grammar({
474474

475475
_pattern: $ => choice(
476476
$.identifier,
477+
$.stable_identifier,
477478
$.capture_pattern,
478479
$.tuple_pattern,
479480
$.case_class_pattern,
@@ -497,20 +498,20 @@ module.exports = grammar({
497498
field('right', $._pattern),
498499
)),
499500

500-
capture_pattern: $ => prec(PREC.assign, seq(
501+
capture_pattern: $ => prec(PREC.field, seq(
501502
field('name', $.identifier),
502503
'@',
503504
field('pattern', $._pattern)
504505
)),
505506

506-
typed_pattern: $ => prec(-1, seq(
507+
typed_pattern: $ => prec.right(seq(
507508
field('pattern', $._pattern),
508509
':',
509510
field('type', $._type)
510511
)),
511512

512513
// TODO: Flatten this.
513-
alternative_pattern: $ => prec.left(-2, seq(
514+
alternative_pattern: $ => prec.left(-1, seq(
514515
$._pattern,
515516
'|',
516517
$._pattern

src/grammar.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2495,6 +2495,10 @@
24952495
"type": "SYMBOL",
24962496
"name": "identifier"
24972497
},
2498+
{
2499+
"type": "SYMBOL",
2500+
"name": "stable_identifier"
2501+
},
24982502
{
24992503
"type": "SYMBOL",
25002504
"name": "capture_pattern"
@@ -2640,7 +2644,7 @@
26402644
},
26412645
"capture_pattern": {
26422646
"type": "PREC",
2643-
"value": 2,
2647+
"value": 6,
26442648
"content": {
26452649
"type": "SEQ",
26462650
"members": [
@@ -2668,8 +2672,8 @@
26682672
}
26692673
},
26702674
"typed_pattern": {
2671-
"type": "PREC",
2672-
"value": -1,
2675+
"type": "PREC_RIGHT",
2676+
"value": 0,
26732677
"content": {
26742678
"type": "SEQ",
26752679
"members": [
@@ -2698,7 +2702,7 @@
26982702
},
26992703
"alternative_pattern": {
27002704
"type": "PREC_LEFT",
2701-
"value": -2,
2705+
"value": -1,
27022706
"content": {
27032707
"type": "SEQ",
27042708
"members": [

src/node-types.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@
101101
"type": "null_literal",
102102
"named": true
103103
},
104+
{
105+
"type": "stable_identifier",
106+
"named": true
107+
},
104108
{
105109
"type": "string",
106110
"named": true

0 commit comments

Comments
 (0)