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 5d8f09c
Show file tree
Hide file tree
Showing 5 changed files with 28,177 additions and 26,110 deletions.
15 changes: 15 additions & 0 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ module.exports = grammar({

_compound_statement: $ => choice(
$.if_statement,
$.match_statement,
$.for_statement,
$.while_statement,
$.try_statement,
Expand Down Expand Up @@ -253,6 +254,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
65 changes: 65 additions & 0 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
64 changes: 64 additions & 0 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 @@ -754,6 +758,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 +1821,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 @@ -2711,6 +2767,10 @@
"type": "break",
"named": false
},
{
"type": "case",
"named": false
},
{
"type": "class",
"named": false
Expand Down Expand Up @@ -2807,6 +2867,10 @@
"type": "lambda",
"named": false
},
{
"type": "match",
"named": false
},
{
"type": "none",
"named": true
Expand Down
Loading

0 comments on commit 5d8f09c

Please sign in to comment.