Skip to content

Commit 84d97a0

Browse files
pretty good
1 parent 38133ef commit 84d97a0

File tree

2 files changed

+77
-18
lines changed

2 files changed

+77
-18
lines changed

crates/pgls_treesitter_grammar/grammar.js

Lines changed: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,37 @@ module.exports = grammar({
6868
program: ($) =>
6969
seq(
7070
// any number of transactions, statements, or blocks with a terminating ;
71-
repeat(seq(choice($.transaction, $.statement, $.block), ";")),
71+
repeat(
72+
seq(
73+
choice(
74+
$.transaction,
75+
$.statement,
76+
$.block
77+
// oneOfKeywords([
78+
// "select",
79+
// "create",
80+
// "alter",
81+
// "drop",
82+
// "vacuum",
83+
// "merge",
84+
// "comment",
85+
// "set",
86+
// "reset",
87+
// "revoke",
88+
// "grant",
89+
// "delete",
90+
// "insert",
91+
// "update",
92+
// "truncate",
93+
// "copy",
94+
// "select",
95+
// "show",
96+
// "unload",
97+
// ])
98+
),
99+
";"
100+
)
101+
),
72102
// optionally, a single statement without a terminating ;
73103
optional($.statement)
74104
),
@@ -953,7 +983,10 @@ module.exports = grammar({
953983
),
954984

955985
delete: ($) =>
956-
seq(completableKeyword($, "delete", { minLength: 2 }), optional($.index_hint)),
986+
seq(
987+
completableKeyword($, "delete", { minLength: 2 }),
988+
optional($.index_hint)
989+
),
957990

958991
_create_statement: ($) =>
959992
seq(
@@ -1048,7 +1081,11 @@ module.exports = grammar({
10481081

10491082
alter_policy: ($) =>
10501083
seq(
1051-
seq(completableKeyword($, "alter", { minLength: 2 }), $.keyword_policy, $.policy_identifier),
1084+
seq(
1085+
completableKeyword($, "alter", { minLength: 2 }),
1086+
$.keyword_policy,
1087+
$.policy_identifier
1088+
),
10521089
optional(
10531090
seq(
10541091
$.keyword_on,
@@ -1772,7 +1809,10 @@ module.exports = grammar({
17721809
seq($.keyword_default, $._expression)
17731810
)
17741811
),
1775-
seq(completableKeyword($, "drop", { minLength: 2 }), $.keyword_default)
1812+
seq(
1813+
completableKeyword($, "drop", { minLength: 2 }),
1814+
$.keyword_default
1815+
)
17761816
)
17771817
),
17781818

@@ -2949,7 +2989,10 @@ module.exports = grammar({
29492989
),
29502990

29512991
where: ($) =>
2952-
seq(completableKeyword($, "where", { minLength: 1 }), field("predicate", $._expression)),
2992+
seq(
2993+
completableKeyword($, "where", { minLength: 1 }),
2994+
field("predicate", $._expression)
2995+
),
29532996

29542997
group_by: ($) =>
29552998
seq(
@@ -2988,13 +3031,20 @@ module.exports = grammar({
29883031
),
29893032

29903033
limit: ($) =>
2991-
seq(completableKeyword($, "limit", { minLength: 1 }), $.literal, optional($.offset)),
3034+
seq(
3035+
completableKeyword($, "limit", { minLength: 1 }),
3036+
$.literal,
3037+
optional($.offset)
3038+
),
29923039

29933040
offset: ($) =>
29943041
seq(completableKeyword($, "offset", { minLength: 2 }), $.literal),
29953042

29963043
returning: ($) =>
2997-
seq(completableKeyword($, "returning", { minLength: 3 }), $.select_expression),
3044+
seq(
3045+
completableKeyword($, "returning", { minLength: 3 }),
3046+
$.select_expression
3047+
),
29983048

29993049
grant_statement: ($) =>
30003050
prec.left(
@@ -3639,7 +3689,6 @@ function partialSeq(...rules) {
36393689
*
36403690
* @typedef {Object} CompletableKeywordOpts
36413691
* @property {number} [minLength]
3642-
* @property {boolean} [unambiguous]
36433692
*/
36443693

36453694
/**
@@ -3651,7 +3700,6 @@ function completableKeyword($, keyword, opts = {}) {
36513700
let matcher = "";
36523701

36533702
const minLength = opts.minLength ?? 1;
3654-
const unambiguous = opts.unambiguous ?? false;
36553703

36563704
for (let i = minLength; i < keyword.length; i++) {
36573705
if (matcher.length > 0) {
@@ -3666,15 +3714,26 @@ function completableKeyword($, keyword, opts = {}) {
36663714

36673715
const noMatchAlias = `partial_keyword:${keyword}`;
36683716

3669-
const options = [$[`keyword_${keyword}`], alias(partialMatch, noMatchAlias)];
3717+
return choice($[`keyword_${keyword}`], alias(partialMatch, noMatchAlias));
3718+
}
36703719

3671-
if (unambiguous) {
3672-
options.push(alias(new RegExp("REPLACED_TOKEN", "i"), noMatchAlias));
3720+
/**
3721+
* Provides an alternative node before we match any other keyword.
3722+
* The LSP will be able to infer the possible keywords from the node kind.
3723+
*
3724+
* @param {string[]} keywords
3725+
*/
3726+
function oneOfKeywords(keywords) {
3727+
const kwAlias = `any_keyword:${keywords.join(":")}`;
36733728

3674-
options.push(
3675-
alias(new RegExp("REPLACED_TOKEN_WITH_QUOTE", "i"), noMatchAlias)
3676-
);
3677-
}
3729+
const anything = new RegExp("[a-z]", "i");
36783730

3679-
return choice(...options);
3731+
return prec(
3732+
-10, // low precedence, since this should serve as a fallback
3733+
choice(
3734+
alias(anything, kwAlias),
3735+
alias(new RegExp("REPLACED_TOKEN", "i"), kwAlias),
3736+
alias(new RegExp("REPLACED_TOKEN_WITH_QUOTE", "i"), kwAlias)
3737+
)
3738+
);
36803739
}

crates/pgls_treesitter_grammar/src/parser.c

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)