Skip to content

Commit

Permalink
fix: allow "match" on LHS of named_expression (#153)
Browse files Browse the repository at this point in the history
Fixes #150
  • Loading branch information
theHamsta committed Feb 7, 2022
1 parent 0d17ed6 commit d8521fa
Show file tree
Hide file tree
Showing 7 changed files with 29,368 additions and 29,312 deletions.
13 changes: 10 additions & 3 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,16 @@ module.exports = grammar({
),

named_expression: $ => seq(
field('name', $.identifier),
field('name', $._named_expresssion_lhs),
':=',
field('value', $.expression)
),

_named_expresssion_lhs: $ => choice(
$.identifier,
alias('match', $.identifier), // ambiguity with match statement: only ":" at end of line decides if "match" keyword
),

return_statement: $ => seq(
'return',
optional($._expressions)
Expand Down Expand Up @@ -261,7 +266,9 @@ module.exports = grammar({
commaSep1(field('subject', $.expression)),
optional(','),
':',
repeat(field('alternative', $.case_clause))),
$.case_block),

case_block: $ => repeat1(field('alternative', $.case_clause)),

case_clause: $ => seq(
'case',
Expand Down Expand Up @@ -499,7 +506,7 @@ module.exports = grammar({

pattern: $ => choice(
$.identifier,
alias("match", $.identifier), // ambiguity with match statement: only ":" at end of line decides if "match" keyword
alias('match', $.identifier), // ambiguity with match statement: only ":" at end of line decides if "match" keyword
$.keyword_identifier,
$.subscript,
$.attribute,
Expand Down
42 changes: 32 additions & 10 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@
"name": "name",
"content": {
"type": "SYMBOL",
"name": "identifier"
"name": "_named_expresssion_lhs"
}
},
{
Expand All @@ -621,6 +621,24 @@
}
]
},
"_named_expresssion_lhs": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "ALIAS",
"content": {
"type": "STRING",
"value": "match"
},
"named": true,
"value": "identifier"
}
]
},
"return_statement": {
"type": "SEQ",
"members": [
Expand Down Expand Up @@ -942,18 +960,22 @@
"value": ":"
},
{
"type": "REPEAT",
"content": {
"type": "FIELD",
"name": "alternative",
"content": {
"type": "SYMBOL",
"name": "case_clause"
}
}
"type": "SYMBOL",
"name": "case_block"
}
]
},
"case_block": {
"type": "REPEAT1",
"content": {
"type": "FIELD",
"name": "alternative",
"content": {
"type": "SYMBOL",
"name": "case_clause"
}
}
},
"case_clause": {
"type": "SEQ",
"members": [
Expand Down
36 changes: 26 additions & 10 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,22 @@
}
}
},
{
"type": "case_block",
"named": true,
"fields": {
"alternative": {
"multiple": true,
"required": true,
"types": [
{
"type": "case_clause",
"named": true
}
]
}
}
},
{
"type": "case_clause",
"named": true,
Expand Down Expand Up @@ -1901,16 +1917,6 @@
"type": "match_statement",
"named": true,
"fields": {
"alternative": {
"multiple": true,
"required": false,
"types": [
{
"type": "case_clause",
"named": true
}
]
},
"subject": {
"multiple": true,
"required": true,
Expand All @@ -1921,6 +1927,16 @@
}
]
}
},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "case_block",
"named": true
}
]
}
},
{
Expand Down
Loading

0 comments on commit d8521fa

Please sign in to comment.