Skip to content

Commit 9ed37cd

Browse files
committed
Add list and slice pattern support #223
1 parent 7bfc87d commit 9ed37cd

File tree

5 files changed

+431717
-426533
lines changed

5 files changed

+431717
-426533
lines changed

corpus/expressions.txt

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,9 +1877,9 @@ var x = c is < '0' or >= 'A' and <= 'Z';
18771877
(equals_value_clause
18781878
(is_pattern_expression
18791879
(identifier)
1880-
(binary_pattern
1880+
(or_pattern
18811881
(relational_pattern (character_literal))
1882-
(binary_pattern
1882+
(and_pattern
18831883
(relational_pattern (character_literal))
18841884
(relational_pattern (character_literal)))))))))))
18851885

@@ -2019,7 +2019,7 @@ var c = o is int; //is_expression with type
20192019
(equals_value_clause
20202020
(is_pattern_expression
20212021
(identifier)
2022-
(binary_pattern
2022+
(or_pattern
20232023
(type_pattern (predefined_type))
20242024
(type_pattern (predefined_type))))))))) (comment)
20252025
(global_statement
@@ -2033,6 +2033,31 @@ var c = o is int; //is_expression with type
20332033
(identifier)
20342034
(predefined_type))))))) (comment))
20352035

2036+
=====================================
2037+
List patterns
2038+
=====================================
2039+
2040+
var a = p is [1,2,x,] and [] or [2,..];
2041+
2042+
---
2043+
2044+
(compilation_unit
2045+
(global_statement
2046+
(local_declaration_statement
2047+
(variable_declaration (implicit_type)
2048+
(variable_declarator (identifier)
2049+
(equals_value_clause
2050+
(is_pattern_expression (identifier)
2051+
(or_pattern
2052+
(and_pattern
2053+
(list_pattern
2054+
(constant_pattern (integer_literal))
2055+
(constant_pattern (integer_literal))
2056+
(constant_pattern (identifier)))
2057+
(list_pattern))
2058+
(list_pattern
2059+
(constant_pattern (integer_literal))
2060+
(slice_pattern))))))))))
20362061

20372062
=====================================
20382063
Conditional expression with member accesses

grammar.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ module.exports = grammar({
7474
[$._contextual_keywords, $.type_parameter_constraint],
7575

7676
[$._type, $.array_creation_expression],
77+
[$._type, $.attribute],
7778
[$._type, $.stack_alloc_array_creation_expression],
7879
[$._type, $._nullable_base_type],
7980
[$._type, $._nullable_base_type, $.array_creation_expression],
@@ -924,12 +925,22 @@ module.exports = grammar({
924925
$.negated_pattern,
925926
$.parenthesized_pattern,
926927
$.relational_pattern,
927-
$.binary_pattern,
928+
$.or_pattern,
929+
$.and_pattern,
930+
$.list_pattern,
928931
$.type_pattern
929932
),
930933

931934
type_pattern: $ => $._type,
932935

936+
list_pattern: $ => seq(
937+
'[',
938+
optional(seq(commaSep(choice($._pattern, $.slice_pattern)), optional(','))),
939+
']'
940+
),
941+
942+
slice_pattern: $ => '..',
943+
933944
parenthesized_pattern: $ => seq('(', $._pattern, ')'),
934945

935946
relational_pattern: $ => prec.left(choice(
@@ -941,18 +952,17 @@ module.exports = grammar({
941952

942953
negated_pattern: $ => seq('not', $._pattern),
943954

944-
binary_pattern: $ => choice(
945-
prec.left(PREC.AND, seq(
946-
field('left', $._pattern),
947-
field('operator', 'and'),
948-
field('right', $._pattern)
949-
)),
950-
prec.left(PREC.OR, seq(
951-
field('left', $._pattern),
952-
field('operator', 'or'),
953-
field('right', $._pattern)
954-
)),
955-
),
955+
and_pattern: $ => prec.left(PREC.AND, seq(
956+
field('left', $._pattern),
957+
field('operator', 'and'),
958+
field('right', $._pattern)
959+
)),
960+
961+
or_pattern: $ => prec.left(PREC.OR, seq(
962+
field('left', $._pattern),
963+
field('operator', 'or'),
964+
field('right', $._pattern)
965+
)),
956966

957967
//We may need to expand this list if more things can be evaluated at compile time
958968
constant_pattern: $ => choice(

src/grammar.json

Lines changed: 169 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4875,7 +4875,15 @@
48754875
},
48764876
{
48774877
"type": "SYMBOL",
4878-
"name": "binary_pattern"
4878+
"name": "or_pattern"
4879+
},
4880+
{
4881+
"type": "SYMBOL",
4882+
"name": "and_pattern"
4883+
},
4884+
{
4885+
"type": "SYMBOL",
4886+
"name": "list_pattern"
48794887
},
48804888
{
48814889
"type": "SYMBOL",
@@ -4887,6 +4895,99 @@
48874895
"type": "SYMBOL",
48884896
"name": "_type"
48894897
},
4898+
"list_pattern": {
4899+
"type": "SEQ",
4900+
"members": [
4901+
{
4902+
"type": "STRING",
4903+
"value": "["
4904+
},
4905+
{
4906+
"type": "CHOICE",
4907+
"members": [
4908+
{
4909+
"type": "SEQ",
4910+
"members": [
4911+
{
4912+
"type": "CHOICE",
4913+
"members": [
4914+
{
4915+
"type": "SEQ",
4916+
"members": [
4917+
{
4918+
"type": "CHOICE",
4919+
"members": [
4920+
{
4921+
"type": "SYMBOL",
4922+
"name": "_pattern"
4923+
},
4924+
{
4925+
"type": "SYMBOL",
4926+
"name": "slice_pattern"
4927+
}
4928+
]
4929+
},
4930+
{
4931+
"type": "REPEAT",
4932+
"content": {
4933+
"type": "SEQ",
4934+
"members": [
4935+
{
4936+
"type": "STRING",
4937+
"value": ","
4938+
},
4939+
{
4940+
"type": "CHOICE",
4941+
"members": [
4942+
{
4943+
"type": "SYMBOL",
4944+
"name": "_pattern"
4945+
},
4946+
{
4947+
"type": "SYMBOL",
4948+
"name": "slice_pattern"
4949+
}
4950+
]
4951+
}
4952+
]
4953+
}
4954+
}
4955+
]
4956+
},
4957+
{
4958+
"type": "BLANK"
4959+
}
4960+
]
4961+
},
4962+
{
4963+
"type": "CHOICE",
4964+
"members": [
4965+
{
4966+
"type": "STRING",
4967+
"value": ","
4968+
},
4969+
{
4970+
"type": "BLANK"
4971+
}
4972+
]
4973+
}
4974+
]
4975+
},
4976+
{
4977+
"type": "BLANK"
4978+
}
4979+
]
4980+
},
4981+
{
4982+
"type": "STRING",
4983+
"value": "]"
4984+
}
4985+
]
4986+
},
4987+
"slice_pattern": {
4988+
"type": "STRING",
4989+
"value": ".."
4990+
},
48904991
"parenthesized_pattern": {
48914992
"type": "SEQ",
48924993
"members": [
@@ -4978,76 +5079,71 @@
49785079
}
49795080
]
49805081
},
4981-
"binary_pattern": {
4982-
"type": "CHOICE",
4983-
"members": [
4984-
{
4985-
"type": "PREC_LEFT",
4986-
"value": 8,
4987-
"content": {
4988-
"type": "SEQ",
4989-
"members": [
4990-
{
4991-
"type": "FIELD",
4992-
"name": "left",
4993-
"content": {
4994-
"type": "SYMBOL",
4995-
"name": "_pattern"
4996-
}
4997-
},
4998-
{
4999-
"type": "FIELD",
5000-
"name": "operator",
5001-
"content": {
5002-
"type": "STRING",
5003-
"value": "and"
5004-
}
5005-
},
5006-
{
5007-
"type": "FIELD",
5008-
"name": "right",
5009-
"content": {
5010-
"type": "SYMBOL",
5011-
"name": "_pattern"
5012-
}
5013-
}
5014-
]
5082+
"and_pattern": {
5083+
"type": "PREC_LEFT",
5084+
"value": 8,
5085+
"content": {
5086+
"type": "SEQ",
5087+
"members": [
5088+
{
5089+
"type": "FIELD",
5090+
"name": "left",
5091+
"content": {
5092+
"type": "SYMBOL",
5093+
"name": "_pattern"
5094+
}
5095+
},
5096+
{
5097+
"type": "FIELD",
5098+
"name": "operator",
5099+
"content": {
5100+
"type": "STRING",
5101+
"value": "and"
5102+
}
5103+
},
5104+
{
5105+
"type": "FIELD",
5106+
"name": "right",
5107+
"content": {
5108+
"type": "SYMBOL",
5109+
"name": "_pattern"
5110+
}
50155111
}
5016-
},
5017-
{
5018-
"type": "PREC_LEFT",
5019-
"value": 6,
5020-
"content": {
5021-
"type": "SEQ",
5022-
"members": [
5023-
{
5024-
"type": "FIELD",
5025-
"name": "left",
5026-
"content": {
5027-
"type": "SYMBOL",
5028-
"name": "_pattern"
5029-
}
5030-
},
5031-
{
5032-
"type": "FIELD",
5033-
"name": "operator",
5034-
"content": {
5035-
"type": "STRING",
5036-
"value": "or"
5037-
}
5038-
},
5039-
{
5040-
"type": "FIELD",
5041-
"name": "right",
5042-
"content": {
5043-
"type": "SYMBOL",
5044-
"name": "_pattern"
5045-
}
5046-
}
5047-
]
5112+
]
5113+
}
5114+
},
5115+
"or_pattern": {
5116+
"type": "PREC_LEFT",
5117+
"value": 6,
5118+
"content": {
5119+
"type": "SEQ",
5120+
"members": [
5121+
{
5122+
"type": "FIELD",
5123+
"name": "left",
5124+
"content": {
5125+
"type": "SYMBOL",
5126+
"name": "_pattern"
5127+
}
5128+
},
5129+
{
5130+
"type": "FIELD",
5131+
"name": "operator",
5132+
"content": {
5133+
"type": "STRING",
5134+
"value": "or"
5135+
}
5136+
},
5137+
{
5138+
"type": "FIELD",
5139+
"name": "right",
5140+
"content": {
5141+
"type": "SYMBOL",
5142+
"name": "_pattern"
5143+
}
50485144
}
5049-
}
5050-
]
5145+
]
5146+
}
50515147
},
50525148
"constant_pattern": {
50535149
"type": "CHOICE",
@@ -10084,6 +10180,10 @@
1008410180
"_type",
1008510181
"array_creation_expression"
1008610182
],
10183+
[
10184+
"_type",
10185+
"attribute"
10186+
],
1008710187
[
1008810188
"_type",
1008910189
"stack_alloc_array_creation_expression"

0 commit comments

Comments
 (0)