Skip to content

Commit

Permalink
feat: add support for pattern matching
Browse files Browse the repository at this point in the history
  • Loading branch information
theHamsta committed Dec 3, 2021
1 parent 24b530c commit 2e38411
Show file tree
Hide file tree
Showing 8 changed files with 29,853 additions and 27,450 deletions.
31 changes: 26 additions & 5 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module.exports = grammar({
[$.tuple, $.tuple_pattern],
[$.list, $.list_pattern],
[$.with_item, $._collection_elements],
[$.named_expression, $.as_pattern],
],

supertypes: $ => [
Expand Down Expand Up @@ -222,6 +223,7 @@ module.exports = grammar({

_compound_statement: $ => choice(
$.if_statement,
$.match_statement,
$.for_statement,
$.while_statement,
$.try_statement,
Expand Down Expand Up @@ -253,6 +255,20 @@ module.exports = grammar({
field('body', $._suite)
),

match_statement: $ => seq(
'match',
field('subject', $.expression),
':',
repeat(field('alternative', $.case_clause)),
),

case_clause: $ => seq(
'case',
field('pattern', $.expression),
':',
field('consequence', $._suite)
),

for_statement: $ => seq(
optional('async'),
'for',
Expand Down Expand Up @@ -320,10 +336,6 @@ module.exports = grammar({

with_item: $ => prec.dynamic(-1, seq(
field('value', $.expression),
optional(seq(
'as',
field('alias', $.pattern)
))
)),

function_definition: $ => seq(
Expand Down Expand Up @@ -522,6 +534,14 @@ module.exports = grammar({
choice($.identifier, $.keyword_identifier, $.subscript, $.attribute)
),

// Extended patterns (patterns allowed in match statement are far more flexible than simple patterns

as_pattern: $ => prec.left(seq(
$.expression,
'as',
$.expression
)),

// Expressions

_expression_within_for_in_clause: $ => choice(
Expand All @@ -537,7 +557,8 @@ module.exports = grammar({
$.lambda,
$.primary_expression,
$.conditional_expression,
$.named_expression
$.named_expression,
$.as_pattern
),

primary_expression: $ => choice(
Expand Down
119 changes: 94 additions & 25 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,10 @@
"type": "SYMBOL",
"name": "if_statement"
},
{
"type": "SYMBOL",
"name": "match_statement"
},
{
"type": "SYMBOL",
"name": "for_statement"
Expand Down Expand Up @@ -881,6 +885,67 @@
}
]
},
"match_statement": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "match"
},
{
"type": "FIELD",
"name": "subject",
"content": {
"type": "SYMBOL",
"name": "expression"
}
},
{
"type": "STRING",
"value": ":"
},
{
"type": "REPEAT",
"content": {
"type": "FIELD",
"name": "alternative",
"content": {
"type": "SYMBOL",
"name": "case_clause"
}
}
}
]
},
"case_clause": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "case"
},
{
"type": "FIELD",
"name": "pattern",
"content": {
"type": "SYMBOL",
"name": "expression"
}
},
{
"type": "STRING",
"value": ":"
},
{
"type": "FIELD",
"name": "consequence",
"content": {
"type": "SYMBOL",
"name": "_suite"
}
}
]
},
"for_statement": {
"type": "SEQ",
"members": [
Expand Down Expand Up @@ -1260,31 +1325,6 @@
"type": "SYMBOL",
"name": "expression"
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "as"
},
{
"type": "FIELD",
"name": "alias",
"content": {
"type": "SYMBOL",
"name": "pattern"
}
}
]
},
{
"type": "BLANK"
}
]
}
]
}
Expand Down Expand Up @@ -2248,6 +2288,27 @@
}
]
},
"as_pattern": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "STRING",
"value": "as"
},
{
"type": "SYMBOL",
"name": "expression"
}
]
}
},
"_expression_within_for_in_clause": {
"type": "CHOICE",
"members": [
Expand Down Expand Up @@ -2300,6 +2361,10 @@
{
"type": "SYMBOL",
"name": "named_expression"
},
{
"type": "SYMBOL",
"name": "as_pattern"
}
]
},
Expand Down Expand Up @@ -4914,6 +4979,10 @@
[
"with_item",
"_collection_elements"
],
[
"named_expression",
"as_pattern"
]
],
"precedences": [],
Expand Down
93 changes: 83 additions & 10 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
"type": "if_statement",
"named": true
},
{
"type": "match_statement",
"named": true
},
{
"type": "try_statement",
"named": true
Expand Down Expand Up @@ -107,6 +111,10 @@
"type": "expression",
"named": true,
"subtypes": [
{
"type": "as_pattern",
"named": true
},
{
"type": "await",
"named": true
Expand Down Expand Up @@ -360,6 +368,21 @@
]
}
},
{
"type": "as_pattern",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "expression",
"named": true
}
]
}
},
{
"type": "assert_statement",
"named": true,
Expand Down Expand Up @@ -754,6 +777,32 @@
}
}
},
{
"type": "case_clause",
"named": true,
"fields": {
"consequence": {
"multiple": false,
"required": true,
"types": [
{
"type": "block",
"named": true
}
]
},
"pattern": {
"multiple": false,
"required": true,
"types": [
{
"type": "expression",
"named": true
}
]
}
}
},
{
"type": "chevron",
"named": true,
Expand Down Expand Up @@ -1791,6 +1840,32 @@
]
}
},
{
"type": "match_statement",
"named": true,
"fields": {
"alternative": {
"multiple": true,
"required": false,
"types": [
{
"type": "case_clause",
"named": true
}
]
},
"subject": {
"multiple": false,
"required": true,
"types": [
{
"type": "expression",
"named": true
}
]
}
}
},
{
"type": "module",
"named": true,
Expand Down Expand Up @@ -2444,16 +2519,6 @@
"type": "with_item",
"named": true,
"fields": {
"alias": {
"multiple": false,
"required": false,
"types": [
{
"type": "pattern",
"named": true
}
]
},
"value": {
"multiple": false,
"required": true,
Expand Down Expand Up @@ -2711,6 +2776,10 @@
"type": "break",
"named": false
},
{
"type": "case",
"named": false
},
{
"type": "class",
"named": false
Expand Down Expand Up @@ -2807,6 +2876,10 @@
"type": "lambda",
"named": false
},
{
"type": "match",
"named": false
},
{
"type": "none",
"named": true
Expand Down
Loading

0 comments on commit 2e38411

Please sign in to comment.