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 5, 2021
1 parent 24b530c commit b6e4254
Show file tree
Hide file tree
Showing 9 changed files with 33,212 additions and 29,557 deletions.
42 changes: 31 additions & 11 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ module.exports = grammar({
[$.tuple, $.tuple_pattern],
[$.list, $.list_pattern],
[$.with_item, $._collection_elements],
[$.named_expression, $.as_pattern],
[$.match_statement, $.primary_expression],
],

supertypes: $ => [
Expand Down Expand Up @@ -228,7 +230,8 @@ module.exports = grammar({
$.with_statement,
$.function_definition,
$.class_definition,
$.decorated_definition
$.decorated_definition,
$.match_statement,
),

if_statement: $ => seq(
Expand All @@ -253,6 +256,15 @@ 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 +332,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 @@ -478,6 +486,7 @@ module.exports = grammar({

pattern: $ => choice(
$.identifier,
alias("match", $.identifier), // ambiguity with match statement: only ":" at end of line decides if "match" keyword
$.keyword_identifier,
$.subscript,
$.attribute,
Expand Down Expand Up @@ -522,6 +531,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,12 +554,14 @@ module.exports = grammar({
$.lambda,
$.primary_expression,
$.conditional_expression,
$.named_expression
$.named_expression,
$.as_pattern
),

primary_expression: $ => choice(
$.binary_operator,
$.identifier,
alias("match", $.identifier),
$.keyword_identifier,
$.string,
$.concatenated_string,
Expand Down Expand Up @@ -631,8 +650,8 @@ module.exports = grammar({
'is',
seq('is', 'not')
)),
$.primary_expression
))
$.primary_expression
))
)),

lambda: $ => prec(PREC.lambda, seq(
Expand Down Expand Up @@ -669,7 +688,7 @@ module.exports = grammar({

_left_hand_side: $ => choice(
$.pattern,
$.pattern_list
$.pattern_list,
),

pattern_list: $ => seq(
Expand Down Expand Up @@ -952,6 +971,7 @@ module.exports = grammar({
'exec',
'async',
'await',
'match',
),
$.identifier
)),
Expand All @@ -969,10 +989,10 @@ module.exports = grammar({
}
})

function commaSep1 (rule) {
function commaSep1(rule) {
return sep1(rule, ',')
}

function sep1 (rule, separator) {
function sep1(rule, separator) {
return seq(rule, repeat(seq(separator, rule)))
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
"author": "Max Brunsfeld",
"license": "MIT",
"dependencies": {
"nan": "^2.14.0"
"nan": "^2.15.0"
},
"devDependencies": {
"tree-sitter-cli": "^0.19.3"
"tree-sitter-cli": "^0.20.1"
},
"scripts": {
"build": "tree-sitter generate && node-gyp build",
Expand Down
145 changes: 120 additions & 25 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,10 @@
{
"type": "SYMBOL",
"name": "decorated_definition"
},
{
"type": "SYMBOL",
"name": "match_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 @@ -2046,6 +2086,15 @@
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "ALIAS",
"content": {
"type": "STRING",
"value": "match"
},
"named": true,
"value": "identifier"
},
{
"type": "SYMBOL",
"name": "keyword_identifier"
Expand Down Expand Up @@ -2248,6 +2297,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 +2370,10 @@
{
"type": "SYMBOL",
"name": "named_expression"
},
{
"type": "SYMBOL",
"name": "as_pattern"
}
]
},
Expand All @@ -2314,6 +2388,15 @@
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "ALIAS",
"content": {
"type": "STRING",
"value": "match"
},
"named": true,
"value": "identifier"
},
{
"type": "SYMBOL",
"name": "keyword_identifier"
Expand Down Expand Up @@ -4831,6 +4914,10 @@
{
"type": "STRING",
"value": "await"
},
{
"type": "STRING",
"value": "match"
}
]
},
Expand Down Expand Up @@ -4914,6 +5001,14 @@
[
"with_item",
"_collection_elements"
],
[
"named_expression",
"as_pattern"
],
[
"match_statement",
"primary_expression"
]
],
"precedences": [],
Expand Down
Loading

0 comments on commit b6e4254

Please sign in to comment.