refac #2

Merged
pleshevskiy merged 12 commits from refac into main 2023-03-16 13:22:15 +03:00
11 changed files with 2074 additions and 1866 deletions
Showing only changes of commit acda125c15 - Show all commits

View file

@ -2,7 +2,7 @@
let
extraGrammars = {
tree-sitter-plpgsql = {
tree-sitter-psql = {
language = "psql";
src = ../.;
version = "0.0.0";
@ -10,14 +10,17 @@ let
};
tree-sitter = (pkgs.tree-sitter.override { inherit extraGrammars; });
grammars = tree-sitter.withPlugins (g: tree-sitter.allGrammars);
grammars = tree-sitter.withPlugins (g: [ g.tree-sitter-psql ]);
nvim-treesitter = pkgs.vimPlugins.nvim-treesitter.overrideAttrs (oldAttrs: {
nvim-treesitter = pkgs.vimPlugins.nvim-treesitter.withAllGrammars.overrideAttrs (oldAttrs: {
passthru.dependencies = oldAttrs.passthru.dependencies ++ [
(pkgs.runCommand "nvim-treesitter-psql-grammar" { } ''
mkdir -p $out/parser
ln -s ${grammars}/psql.so $out/parser/psql.so
'')
];
postPatch = ''
rm -r parser
ln -s ${grammars} parser
ln -s ${../.}/queries queries/psql
ln -s ${extraGrammars.tree-sitter-psql.src}/queries queries/psql
'';
});

View file

@ -17,11 +17,11 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1672756850,
"narHash": "sha256-Smbq3+fitwA13qsTMeaaurv09/KVbZfW7m7lINwzDGA=",
"lastModified": 1672997035,
"narHash": "sha256-DNaNjsGMRYefBTAxFIrVOB2ok477cj1FTpqnu/mKRf4=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "298add347c2bbce14020fcb54051f517c391196b",
"rev": "f1ffcf798e93b169321106a4aef79526a2b4bd0a",
"type": "github"
},
"original": {

View file

@ -1,5 +1,5 @@
module.exports = grammar({
name: "plpgsql",
name: "psql",
extras: ($) => [/\s\n/, /\s/, $.line_comment, $.block_comment],
@ -69,9 +69,11 @@ module.exports = grammar({
$.table_reference,
optional(seq("(", field("refcolumn", $.identifier), ")")),
optional($._foreign_key_match),
choice(
seq($._foreign_key_on_delete, $._foreign_key_on_update),
seq($._foreign_key_on_update, $._foreign_key_on_delete)
optional(
choice(
seq($._foreign_key_on_delete, $._foreign_key_on_update),
seq($._foreign_key_on_update, $._foreign_key_on_delete)
)
)
)
// TODO: CHECK
@ -116,8 +118,8 @@ module.exports = grammar({
literal: ($) =>
choice(
$._number,
$._literal_string,
$.number,
$.literal_string,
$.keyword_true,
$.keyword_false,
$.keyword_null
@ -291,8 +293,8 @@ module.exports = grammar({
// https://stackoverflow.com/questions/13014947/regex-to-match-a-c-style-multiline-comment
block_comment: (_) => seq("/*", /[^*]*\*+(?:[^/*][^*]*\*+)*/, "/"),
_literal_string: ($) => choice(seq("'", /[^']*/, "'")),
_number: (_) => /\d+/,
literal_string: ($) => choice(seq("'", /[^']*/, "'")),
number: (_) => /\d+/,
identifier: ($) => choice($._identifier, seq('"', /[^"]+/, '"')),
@ -316,9 +318,9 @@ function parametricType($, type, params = ["size"]) {
type,
"(",
// first parameter is guaranteed, shift it out of the array
field(params.shift(), alias($._number, $.literal)),
field(params.shift(), $.number),
// then, fill in the ", next" until done
...params.map((p) => seq(",", field(p, alias($._number, $.literal)))),
...params.map((p) => seq(",", field(p, $.number))),
")"
)
)

View file

@ -1,5 +1,5 @@
{
"name": "tree-sitter-plpgsql",
"name": "tree-sitter-psql",
"version": "1.0.0",
"description": "",
"main": "grammar.js",
@ -8,7 +8,9 @@
"tree-sitter": [
{
"scope": "source.psql",
"file-types": ["psql"],
"file-types": [
"psql"
],
"injection-regex": "^psql$"
}
]

71
queries/highlights.scm Normal file
View file

@ -0,0 +1,71 @@
; Keywords
[
(keyword_cascade)
(keyword_constraint)
(keyword_create)
(keyword_default)
(keyword_delete)
(keyword_key)
(keyword_not)
(keyword_null)
(keyword_on)
(keyword_primary)
(keyword_references)
(keyword_table)
(keyword_update)
] @keyword
; Identifiers
(column_definition name: (identifier) @variable)
(column_constraint name: (identifier) @variable)
(table_reference schema: (identifier) @namespace)
(table_reference name: (identifier) @constant)
; Types
[
(keyword_smallint)
(keyword_integer)
(keyword_bigint)
(keyword_smallserial)
(keyword_serial)
(keyword_bigserial)
(keyword_real)
(double)
(decimal)
(numeric)
(keyword_uuid)
(keyword_text)
(char)
(varchar)
(keyword_json)
(keyword_jsonb)
(keyword_date)
(keyword_datetime)
(keyword_time)
(keyword_timestamp)
(keyword_timestamptz)
(keyword_bytea)
(keyword_money)
(keyword_boolean)
(keyword_xml)
] @type.builtin
; Literal
[(keyword_true) (keyword_false)] @boolean
(number) @number
; Comments
(line_comment) @comment.line
(block_comment) @comment.block
; Punctuation
[
";"
"."
","
] @punctuation.delimiter
[
"("
")"
] @punctuation.brackets

View file

@ -1,5 +1,5 @@
{
"name": "plpgsql",
"name": "psql",
"word": "_identifier",
"rules": {
"source_file": {
@ -345,30 +345,38 @@
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_foreign_key_on_delete"
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_foreign_key_on_delete"
},
{
"type": "SYMBOL",
"name": "_foreign_key_on_update"
}
]
},
{
"type": "SYMBOL",
"name": "_foreign_key_on_update"
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_foreign_key_on_update"
},
{
"type": "SYMBOL",
"name": "_foreign_key_on_delete"
}
]
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_foreign_key_on_update"
},
{
"type": "SYMBOL",
"name": "_foreign_key_on_delete"
}
]
"type": "BLANK"
}
]
}
@ -584,11 +592,11 @@
"members": [
{
"type": "SYMBOL",
"name": "_number"
"name": "number"
},
{
"type": "SYMBOL",
"name": "_literal_string"
"name": "literal_string"
},
{
"type": "SYMBOL",
@ -743,13 +751,8 @@
"type": "FIELD",
"name": "precision",
"content": {
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_number"
},
"named": true,
"value": "literal"
"type": "SYMBOL",
"name": "number"
}
},
{
@ -786,13 +789,8 @@
"type": "FIELD",
"name": "precision",
"content": {
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_number"
},
"named": true,
"value": "literal"
"type": "SYMBOL",
"name": "number"
}
},
{
@ -806,13 +804,8 @@
"type": "FIELD",
"name": "scale",
"content": {
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_number"
},
"named": true,
"value": "literal"
"type": "SYMBOL",
"name": "number"
}
}
]
@ -856,13 +849,8 @@
"type": "FIELD",
"name": "precision",
"content": {
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_number"
},
"named": true,
"value": "literal"
"type": "SYMBOL",
"name": "number"
}
},
{
@ -899,13 +887,8 @@
"type": "FIELD",
"name": "precision",
"content": {
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_number"
},
"named": true,
"value": "literal"
"type": "SYMBOL",
"name": "number"
}
},
{
@ -919,13 +902,8 @@
"type": "FIELD",
"name": "scale",
"content": {
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_number"
},
"named": true,
"value": "literal"
"type": "SYMBOL",
"name": "number"
}
}
]
@ -983,13 +961,8 @@
"type": "FIELD",
"name": "size",
"content": {
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_number"
},
"named": true,
"value": "literal"
"type": "SYMBOL",
"name": "number"
}
},
{
@ -1026,13 +999,8 @@
"type": "FIELD",
"name": "size",
"content": {
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_number"
},
"named": true,
"value": "literal"
"type": "SYMBOL",
"name": "number"
}
},
{
@ -1536,7 +1504,7 @@
}
]
},
"_literal_string": {
"literal_string": {
"type": "CHOICE",
"members": [
{
@ -1558,7 +1526,7 @@
}
]
},
"_number": {
"number": {
"type": "PATTERN",
"value": "\\d+"
},

View file

@ -13,7 +13,7 @@
"required": false,
"types": [
{
"type": "literal",
"type": "number",
"named": true
}
]
@ -337,7 +337,7 @@
"required": false,
"types": [
{
"type": "literal",
"type": "number",
"named": true
}
]
@ -347,7 +347,7 @@
"required": false,
"types": [
{
"type": "literal",
"type": "number",
"named": true
}
]
@ -415,7 +415,7 @@
"fields": {},
"children": {
"multiple": false,
"required": false,
"required": true,
"types": [
{
"type": "keyword_false",
@ -428,10 +428,23 @@
{
"type": "keyword_true",
"named": true
},
{
"type": "literal_string",
"named": true
},
{
"type": "number",
"named": true
}
]
}
},
{
"type": "literal_string",
"named": true,
"fields": {}
},
{
"type": "numeric",
"named": true,
@ -441,7 +454,7 @@
"required": false,
"types": [
{
"type": "literal",
"type": "number",
"named": true
}
]
@ -451,7 +464,7 @@
"required": false,
"types": [
{
"type": "literal",
"type": "number",
"named": true
}
]
@ -576,7 +589,7 @@
"required": false,
"types": [
{
"type": "literal",
"type": "number",
"named": true
}
]
@ -828,5 +841,9 @@
{
"type": "keyword_xml",
"named": true
},
{
"type": "number",
"named": true
}
]

File diff suppressed because it is too large Load diff

View file

@ -145,7 +145,7 @@ create table foo (
name: (identifier)
datatype: (numeric
(keyword_numeric)
precision: (literal)
precision: (number)
)
)
(column_definition
@ -156,7 +156,7 @@ create table foo (
name: (identifier)
datatype: (varchar
(keyword_varchar)
size: (literal)
size: (number)
)
)
(column_definition
@ -220,7 +220,9 @@ create table foo (
)
(column_constraint
(keyword_default)
(literal)
(literal
(literal_string)
)
)
)
)
@ -347,7 +349,9 @@ create table foo (
(keyword_constraint)
name: (identifier)
(keyword_default)
(literal)
(literal
(literal_string)
)
)
)
(column_definition

View file

@ -0,0 +1,38 @@
create table public.foo (
-- <- keyword
-- ^ keyword
-- ^ namespace
-- ^ punctuation.delimiter
-- ^ constant
-- ^ punctuation.brackets
id uuid constraint foo_pkey primary key,
-- <- variable
-- ^ type.builtin
-- ^ keyword
-- ^ variable
-- ^ keyword
-- ^ keyword
-- ^ punctuation.delimiter
confirmed boolean not null default true
-- <- variable
-- ^ type.builtin
-- ^ keyword
-- ^ keyword
-- ^ keyword
-- ^ boolean
bar_id uuid not null constraint foo_bar_id_fkey references bar on update cascade on delete cascade,
-- ^ keyword
-- ^ variable
-- ^ keyword
-- ^ constant
-- ^ keyword
-- ^ keyword
-- ^ keyword
-- ^ keyword
-- ^ keyword
-- ^ keyword
) ;
-- <- punctuation.brackets
-- ^ punctuation.delimiter

BIN
tree-sitter-psql.wasm Executable file

Binary file not shown.