Skip to content

Commit

Permalink
Merge pull request #140 from theHamsta/pattern_matching
Browse files Browse the repository at this point in the history
feat: add support for pattern matching
  • Loading branch information
dcreager authored Jan 28, 2022
2 parents b1a3825 + 43c34e4 commit ed0fe62
Show file tree
Hide file tree
Showing 12 changed files with 41,591 additions and 29,087 deletions.
50 changes: 38 additions & 12 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,22 @@ module.exports = grammar({
field('body', $._suite)
),

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

case_clause: $ => seq(
'case',
commaSep1(field('pattern', choice($.expression, $.list_splat_pattern))),
optional(','),
optional(field('guard', $.if_clause)),
':',
field('consequence', $._suite)
),

for_statement: $ => seq(
optional('async'),
'for',
Expand Down Expand Up @@ -320,10 +339,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 @@ -479,6 +494,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 @@ -523,6 +539,14 @@ module.exports = grammar({
choice($.identifier, $.keyword_identifier, $.subscript, $.attribute)
),

// Extended patterns (patterns allowed in match statement are far more flexible than simple patterns though still a subset of "expression")

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

// Expressions

_expression_within_for_in_clause: $ => choice(
Expand All @@ -538,12 +562,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 @@ -632,8 +658,8 @@ module.exports = grammar({
'is',
seq('is', 'not')
)),
$.primary_expression
))
$.primary_expression
))
)),

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

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

pattern_list: $ => seq(
Expand Down Expand Up @@ -750,7 +776,7 @@ module.exports = grammar({
type: $ => $.expression,

keyword_argument: $ => seq(
field('name', choice($.identifier, $.keyword_identifier)),
field('name', choice($.identifier, $.keyword_identifier, alias("match", $.identifier))),
'=',
field('value', $.expression)
),
Expand Down Expand Up @@ -974,10 +1000,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
2 changes: 2 additions & 0 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,6 @@
"while"
"with"
"yield"
"match"
"case"
] @keyword
Loading

0 comments on commit ed0fe62

Please sign in to comment.