Skip to content

Commit

Permalink
Add support for alternative operators 'and, or, not' (tree-sitter#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
azabiong authored and mliszcz committed Sep 15, 2022
1 parent 7f313ba commit dc2b15e
Show file tree
Hide file tree
Showing 6 changed files with 262,368 additions and 251,908 deletions.
21 changes: 21 additions & 0 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,8 @@ module.exports = grammar(C, {
$.this,
$.raw_string_literal,
$.user_defined_literal,
$.alternative_unary,
$.alternative_binary,
$.fold_expression
),

Expand Down Expand Up @@ -1152,6 +1154,25 @@ module.exports = grammar(C, {
$.literal_suffix
),

alternative_unary: $ => prec.left(PREC.UNARY, seq(
field('operator', 'not'),
field('argument', $._expression)
)),

alternative_binary: $ => {
const table = [
['or', PREC.LOGICAL_OR],
['and', PREC.LOGICAL_AND],
];
return choice(...table.map(([operator, precedence]) => {
return prec.left(precedence, seq(
field('left', $._expression),
field('operator', operator),
field('right', $._expression)
))
}));
},

_namespace_identifier: $ => alias($.identifier, $.namespace_identifier)
}
});
Expand Down
8 changes: 8 additions & 0 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,11 @@
; Strings

(raw_string_literal) @string

; Operators

[
"and"
"or"
"not"
] @operator
104 changes: 104 additions & 0 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -5200,6 +5200,14 @@
"type": "SYMBOL",
"name": "user_defined_literal"
},
{
"type": "SYMBOL",
"name": "alternative_unary"
},
{
"type": "SYMBOL",
"name": "alternative_binary"
},
{
"type": "SYMBOL",
"name": "fold_expression"
Expand Down Expand Up @@ -12352,6 +12360,102 @@
}
]
},
"alternative_unary": {
"type": "PREC_LEFT",
"value": 13,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "not"
}
},
{
"type": "FIELD",
"name": "argument",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
}
},
"alternative_binary": {
"type": "CHOICE",
"members": [
{
"type": "PREC_LEFT",
"value": 1,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "or"
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
}
},
{
"type": "PREC_LEFT",
"value": 2,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "and"
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
}
}
]
},
"_namespace_identifier": {
"type": "ALIAS",
"content": {
Expand Down
86 changes: 86 additions & 0 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@
"type": "_expression",
"named": true,
"subtypes": [
{
"type": "alternative_binary",
"named": true
},
{
"type": "alternative_unary",
"named": true
},
{
"type": "assignment_expression",
"named": true
Expand Down Expand Up @@ -612,6 +620,72 @@
}
}
},
{
"type": "alternative_binary",
"named": true,
"fields": {
"left": {
"multiple": false,
"required": true,
"types": [
{
"type": "_expression",
"named": true
}
]
},
"operator": {
"multiple": false,
"required": true,
"types": [
{
"type": "and",
"named": false
},
{
"type": "or",
"named": false
}
]
},
"right": {
"multiple": false,
"required": true,
"types": [
{
"type": "_expression",
"named": true
}
]
}
}
},
{
"type": "alternative_unary",
"named": true,
"fields": {
"argument": {
"multiple": false,
"required": true,
"types": [
{
"type": "_expression",
"named": true
}
]
},
"operator": {
"multiple": false,
"required": true,
"types": [
{
"type": "not",
"named": false
}
]
}
}
},
{
"type": "argument_list",
"named": true,
Expand Down Expand Up @@ -6361,6 +6435,10 @@
"type": "_unaligned",
"named": false
},
{
"type": "and",
"named": false
},
{
"type": "auto",
"named": true
Expand Down Expand Up @@ -6537,6 +6615,10 @@
"type": "noexcept",
"named": false
},
{
"type": "not",
"named": false
},
{
"type": "null",
"named": true
Expand All @@ -6553,6 +6635,10 @@
"type": "operator",
"named": false
},
{
"type": "or",
"named": false
},
{
"type": "override",
"named": false
Expand Down
Loading

0 comments on commit dc2b15e

Please sign in to comment.