Skip to content

Commit

Permalink
Support quantitive like operator ASTs (#48)
Browse files Browse the repository at this point in the history
* Resolve #47: Support quantitive like operators

* Update fixtures
  • Loading branch information
takegue committed Aug 13, 2023
1 parent a13f120 commit b62cf2c
Show file tree
Hide file tree
Showing 6 changed files with 643,214 additions and 630,284 deletions.
32 changes: 27 additions & 5 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -1649,15 +1649,14 @@ module.exports = grammar({
['binary_and', $._keyword_and],
['binary_or', $._keyword_or],
['operator_compare', seq(optional($._keyword_not), $._keyword_in)],
['operator_compare', seq(optional($._keyword_not), $._keyword_like)],
// ['operator_compare', seq(optional($._keyword_not), $._keyword_like)],
[
'operator_compare',
seq($._keyword_is, optional($._keyword_not), kw('DISTINCT FROM')),
],
];

return choice(
...table.map(([precedence, operator]) =>
const exps = table.map(([precedence, operator]) =>
prec.left(
precedence,
seq(
Expand All @@ -1666,9 +1665,32 @@ module.exports = grammar({
field('right', $._expression),
),
)
),
);
)

const specials = [
'operator_compare',
prec.left(seq(
field('left', $._expression),
choice(
seq(
field('operator', $.like_operator),
field('right', alias($.string, $.like_pattern)),
),
seq(
field('operator', $.quantitve_like_operator),
field('right', $.quantitve_like_patterns),
)
)
))
]

return choice(...exps, ...specials);
},

like_operator: ($) => seq(optional($._keyword_not), $._keyword_like),
quantitve_like_operator: ($) => seq(optional($._keyword_not), $._keyword_like, choice(kw('ANY'), kw('SOME'), kw('ALL'))),
quantitve_like_patterns : ($) => seq('(', commaSep1(alias($.string, $.like_pattern)), ')'),

between_operator: ($) =>
prec.left(
'operator_compare',
Expand Down
241 changes: 193 additions & 48 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -11985,6 +11985,10 @@
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_keyword_is"
},
{
"type": "CHOICE",
"members": [
Expand All @@ -11998,8 +12002,22 @@
]
},
{
"type": "SYMBOL",
"name": "_keyword_like"
"type": "ALIAS",
"content": {
"type": "SEQ",
"members": [
{
"type": "PATTERN",
"value": "[dD][iI][sS][tT][iI][nN][cC][tT]"
},
{
"type": "PATTERN",
"value": "[fF][rR][oO][mM]"
}
]
},
"named": false,
"value": "DISTINCT_FROM"
}
]
}
Expand All @@ -12015,9 +12033,13 @@
]
}
},
{
"type": "STRING",
"value": "operator_compare"
},
{
"type": "PREC_LEFT",
"value": "operator_compare",
"value": 0,
"content": {
"type": "SEQ",
"members": [
Expand All @@ -12030,61 +12052,184 @@
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_keyword_is"
},
{
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "SYMBOL",
"name": "_keyword_not"
},
{
"type": "BLANK"
"name": "like_operator"
}
]
},
{
"type": "ALIAS",
"content": {
"type": "SEQ",
"members": [
{
"type": "PATTERN",
"value": "[dD][iI][sS][tT][iI][nN][cC][tT]"
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "string"
},
{
"type": "PATTERN",
"value": "[fF][rR][oO][mM]"
}
]
"named": true,
"value": "like_pattern"
}
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "SYMBOL",
"name": "quantitve_like_operator"
}
},
"named": false,
"value": "DISTINCT_FROM"
}
]
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "quantitve_like_patterns"
}
}
]
}
]
}
]
}
}
]
},
"like_operator": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_keyword_not"
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "_keyword_like"
}
]
},
"quantitve_like_operator": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_keyword_not"
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "_keyword_like"
},
{
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "PATTERN",
"value": "[aA][nN][yY]"
},
"named": false,
"value": "ANY"
},
{
"type": "ALIAS",
"content": {
"type": "PATTERN",
"value": "[sS][oO][mM][eE]"
},
"named": false,
"value": "SOME"
},
{
"type": "ALIAS",
"content": {
"type": "PATTERN",
"value": "[aA][lL][lL]"
},
"named": false,
"value": "ALL"
}
]
}
]
},
"quantitve_like_patterns": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "("
},
{
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "string"
},
"named": true,
"value": "like_pattern"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "string"
},
"named": true,
"value": "like_pattern"
}
]
}
}
]
},
{
"type": "STRING",
"value": ")"
}
]
},
"between_operator": {
"type": "PREC_LEFT",
"value": "operator_compare",
Expand Down

0 comments on commit b62cf2c

Please sign in to comment.