Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add missing (postgres) operators. #211

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 65 additions & 6 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ module.exports = grammar({
'binary_exp',
'binary_times',
'binary_plus',
'unary_other',
'binary_other',
'binary_in',
'binary_compare',
'binary_relation',
'binary_concat',
'pattern_matching',
'between',
'clause_connective',
Expand Down Expand Up @@ -3001,6 +3002,52 @@ module.exports = grammar({
)
),

op_other: $ => token(
choice(
'->',
'->>',
'#>',
'#>>',
'~',
'!~',
'~*',
'!~*',
'|',
'&',
'#',
'<<',
'>>',
'<<=',
'>>=',
'##',
'<->',
'@>',
'<@',
'&<',
'&>',
'|>>',
'<<|',
'&<|',
'|&>',
'<^',
'^>',
'?#',
'?-',
'?|',
'?-|',
'?||',
'@@',
'@@@',
'@?',
'#-',
'?&',
'?',
'-|-',
'||',
'^@',
),
),

binary_expression: $ => choice(
...[
['+', 'binary_plus'],
Expand All @@ -3009,18 +3056,14 @@ module.exports = grammar({
['/', 'binary_times'],
['%', 'binary_times'],
['^', 'binary_exp'],
['||', 'binary_concat'],
['=', 'binary_relation'],
['<', 'binary_relation'],
['<=', 'binary_relation'],
['!=', 'binary_relation'],
['>=', 'binary_relation'],
['>', 'binary_relation'],
['<>', 'binary_relation'],
['->', 'binary_relation'],
['->>', 'binary_relation'],
['#>', 'binary_relation'],
['#>>', 'binary_relation'],
[$.op_other, 'binary_other'],
[$.keyword_is, 'binary_is'],
[$.is_not, 'binary_is'],
[$.keyword_like, 'pattern_matching'],
Expand Down Expand Up @@ -3060,13 +3103,29 @@ module.exports = grammar({
),
),

op_unary_other: $ => token(
choice(
'|/',
'||/',
'@',
'~',
'@-@',
'@@',
'#',
'?-',
'?|',
'!!',
),
),

unary_expression: $ => choice(
...[
[$.keyword_not, 'unary_not'],
[$.bang, 'unary_not'],
[$.keyword_any, 'unary_not'],
[$.keyword_some, 'unary_not'],
[$.keyword_all, 'unary_not'],
[$.op_unary_other, 'unary_other'],
].map(([operator, precedence]) =>
prec.left(precedence, seq(
field('operator', operator),
Expand Down
6 changes: 2 additions & 4 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,8 @@
">="
">"
"<>"
"->"
"->>"
"#>"
"#>>"
(op_other)
(op_unary_other)
] @operator

[
Expand Down
49 changes: 49 additions & 0 deletions test/corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,52 @@ WHERE
left: (field
name: (identifier))
right: (literal))))))

================================================================================
string operator concat and start with
================================================================================

SELECT 'abc' ^@ 'a' || 'z';

--------------------------------------------------------------------------------

(program
(statement
(select
(keyword_select)
(select_expression
(term
(binary_expression
(binary_expression
(literal)
(op_other)
(literal))
(op_other)
(literal)))))))

================================================================================
full text search operators
================================================================================

SELECT 'fat cats ate rats' @@ !! ('cat' <-> 'rat'::tsquery);

--------------------------------------------------------------------------------

(program
(statement
(select
(keyword_select)
(select_expression
(term
(binary_expression
(literal)
(op_other)
(unary_expression
(op_unary_other)
(cast
(binary_expression
(literal)
(op_other)
(literal))
(object_reference
(identifier))))))))))
7 changes: 7 additions & 0 deletions test/corpus/json.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,37 @@ FROM users;
(term
value: (binary_expression
left: (literal)
operator: (op_other)
right: (literal))
(keyword_as)
alias: (identifier))
(term
value: (binary_expression
left: (binary_expression
left: (literal)
operator: (op_other)
right: (literal))
operator: (op_other)
right: (literal)))
(term
value: (binary_expression
left: (binary_expression
left: (literal)
operator: (op_other)
right: (literal))
operator: (op_other)
right: (literal))
(keyword_as)
alias: (identifier))
(term
value: (binary_expression
left: (literal)
operator: (op_other)
right: (literal)))
(term
value: (binary_expression
left: (literal)
operator: (op_other)
right: (literal))
(keyword_as)
alias: (identifier))))
Expand Down
2 changes: 2 additions & 0 deletions test/corpus/select.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2081,7 +2081,9 @@ SELECT id || '-' || name FROM my_table;
left: (binary_expression
left: (field
name: (identifier))
operator: (op_other)
right: (literal))
operator: (op_other)
right: (field
name: (identifier))))))
(from
Expand Down