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 let
extraGrammars = { extraGrammars = {
tree-sitter-plpgsql = { tree-sitter-psql = {
language = "psql"; language = "psql";
src = ../.; src = ../.;
version = "0.0.0"; version = "0.0.0";
@ -10,14 +10,17 @@ let
}; };
tree-sitter = (pkgs.tree-sitter.override { inherit extraGrammars; }); 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 = '' postPatch = ''
rm -r parser ln -s ${extraGrammars.tree-sitter-psql.src}/queries queries/psql
ln -s ${grammars} parser
ln -s ${../.}/queries queries/psql
''; '';
}); });

View file

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

View file

@ -1,5 +1,5 @@
module.exports = grammar({ module.exports = grammar({
name: "plpgsql", name: "psql",
extras: ($) => [/\s\n/, /\s/, $.line_comment, $.block_comment], extras: ($) => [/\s\n/, /\s/, $.line_comment, $.block_comment],
@ -69,11 +69,13 @@ module.exports = grammar({
$.table_reference, $.table_reference,
optional(seq("(", field("refcolumn", $.identifier), ")")), optional(seq("(", field("refcolumn", $.identifier), ")")),
optional($._foreign_key_match), optional($._foreign_key_match),
optional(
choice( choice(
seq($._foreign_key_on_delete, $._foreign_key_on_update), seq($._foreign_key_on_delete, $._foreign_key_on_update),
seq($._foreign_key_on_update, $._foreign_key_on_delete) seq($._foreign_key_on_update, $._foreign_key_on_delete)
) )
) )
)
// TODO: CHECK // TODO: CHECK
// TODO: GENERATED // TODO: GENERATED
) )
@ -116,8 +118,8 @@ module.exports = grammar({
literal: ($) => literal: ($) =>
choice( choice(
$._number, $.number,
$._literal_string, $.literal_string,
$.keyword_true, $.keyword_true,
$.keyword_false, $.keyword_false,
$.keyword_null $.keyword_null
@ -291,8 +293,8 @@ module.exports = grammar({
// https://stackoverflow.com/questions/13014947/regex-to-match-a-c-style-multiline-comment // https://stackoverflow.com/questions/13014947/regex-to-match-a-c-style-multiline-comment
block_comment: (_) => seq("/*", /[^*]*\*+(?:[^/*][^*]*\*+)*/, "/"), block_comment: (_) => seq("/*", /[^*]*\*+(?:[^/*][^*]*\*+)*/, "/"),
_literal_string: ($) => choice(seq("'", /[^']*/, "'")), literal_string: ($) => choice(seq("'", /[^']*/, "'")),
_number: (_) => /\d+/, number: (_) => /\d+/,
identifier: ($) => choice($._identifier, seq('"', /[^"]+/, '"')), identifier: ($) => choice($._identifier, seq('"', /[^"]+/, '"')),
@ -316,9 +318,9 @@ function parametricType($, type, params = ["size"]) {
type, type,
"(", "(",
// first parameter is guaranteed, shift it out of the array // 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 // 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", "version": "1.0.0",
"description": "", "description": "",
"main": "grammar.js", "main": "grammar.js",
@ -8,7 +8,9 @@
"tree-sitter": [ "tree-sitter": [
{ {
"scope": "source.psql", "scope": "source.psql",
"file-types": ["psql"], "file-types": [
"psql"
],
"injection-regex": "^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", "word": "_identifier",
"rules": { "rules": {
"source_file": { "source_file": {
@ -341,6 +341,9 @@
} }
] ]
}, },
{
"type": "CHOICE",
"members": [
{ {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
@ -371,6 +374,11 @@
] ]
} }
] ]
},
{
"type": "BLANK"
}
]
} }
] ]
} }
@ -584,11 +592,11 @@
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_number" "name": "number"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_literal_string" "name": "literal_string"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -742,14 +750,9 @@
{ {
"type": "FIELD", "type": "FIELD",
"name": "precision", "name": "precision",
"content": {
"type": "ALIAS",
"content": { "content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_number" "name": "number"
},
"named": true,
"value": "literal"
} }
}, },
{ {
@ -785,14 +788,9 @@
{ {
"type": "FIELD", "type": "FIELD",
"name": "precision", "name": "precision",
"content": {
"type": "ALIAS",
"content": { "content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_number" "name": "number"
},
"named": true,
"value": "literal"
} }
}, },
{ {
@ -805,14 +803,9 @@
{ {
"type": "FIELD", "type": "FIELD",
"name": "scale", "name": "scale",
"content": {
"type": "ALIAS",
"content": { "content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_number" "name": "number"
},
"named": true,
"value": "literal"
} }
} }
] ]
@ -855,14 +848,9 @@
{ {
"type": "FIELD", "type": "FIELD",
"name": "precision", "name": "precision",
"content": {
"type": "ALIAS",
"content": { "content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_number" "name": "number"
},
"named": true,
"value": "literal"
} }
}, },
{ {
@ -898,14 +886,9 @@
{ {
"type": "FIELD", "type": "FIELD",
"name": "precision", "name": "precision",
"content": {
"type": "ALIAS",
"content": { "content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_number" "name": "number"
},
"named": true,
"value": "literal"
} }
}, },
{ {
@ -918,14 +901,9 @@
{ {
"type": "FIELD", "type": "FIELD",
"name": "scale", "name": "scale",
"content": {
"type": "ALIAS",
"content": { "content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_number" "name": "number"
},
"named": true,
"value": "literal"
} }
} }
] ]
@ -982,14 +960,9 @@
{ {
"type": "FIELD", "type": "FIELD",
"name": "size", "name": "size",
"content": {
"type": "ALIAS",
"content": { "content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_number" "name": "number"
},
"named": true,
"value": "literal"
} }
}, },
{ {
@ -1025,14 +998,9 @@
{ {
"type": "FIELD", "type": "FIELD",
"name": "size", "name": "size",
"content": {
"type": "ALIAS",
"content": { "content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_number" "name": "number"
},
"named": true,
"value": "literal"
} }
}, },
{ {
@ -1536,7 +1504,7 @@
} }
] ]
}, },
"_literal_string": { "literal_string": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
@ -1558,7 +1526,7 @@
} }
] ]
}, },
"_number": { "number": {
"type": "PATTERN", "type": "PATTERN",
"value": "\\d+" "value": "\\d+"
}, },

View file

@ -13,7 +13,7 @@
"required": false, "required": false,
"types": [ "types": [
{ {
"type": "literal", "type": "number",
"named": true "named": true
} }
] ]
@ -337,7 +337,7 @@
"required": false, "required": false,
"types": [ "types": [
{ {
"type": "literal", "type": "number",
"named": true "named": true
} }
] ]
@ -347,7 +347,7 @@
"required": false, "required": false,
"types": [ "types": [
{ {
"type": "literal", "type": "number",
"named": true "named": true
} }
] ]
@ -415,7 +415,7 @@
"fields": {}, "fields": {},
"children": { "children": {
"multiple": false, "multiple": false,
"required": false, "required": true,
"types": [ "types": [
{ {
"type": "keyword_false", "type": "keyword_false",
@ -428,10 +428,23 @@
{ {
"type": "keyword_true", "type": "keyword_true",
"named": true "named": true
},
{
"type": "literal_string",
"named": true
},
{
"type": "number",
"named": true
} }
] ]
} }
}, },
{
"type": "literal_string",
"named": true,
"fields": {}
},
{ {
"type": "numeric", "type": "numeric",
"named": true, "named": true,
@ -441,7 +454,7 @@
"required": false, "required": false,
"types": [ "types": [
{ {
"type": "literal", "type": "number",
"named": true "named": true
} }
] ]
@ -451,7 +464,7 @@
"required": false, "required": false,
"types": [ "types": [
{ {
"type": "literal", "type": "number",
"named": true "named": true
} }
] ]
@ -576,7 +589,7 @@
"required": false, "required": false,
"types": [ "types": [
{ {
"type": "literal", "type": "number",
"named": true "named": true
} }
] ]
@ -828,5 +841,9 @@
{ {
"type": "keyword_xml", "type": "keyword_xml",
"named": true "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) name: (identifier)
datatype: (numeric datatype: (numeric
(keyword_numeric) (keyword_numeric)
precision: (literal) precision: (number)
) )
) )
(column_definition (column_definition
@ -156,7 +156,7 @@ create table foo (
name: (identifier) name: (identifier)
datatype: (varchar datatype: (varchar
(keyword_varchar) (keyword_varchar)
size: (literal) size: (number)
) )
) )
(column_definition (column_definition
@ -220,7 +220,9 @@ create table foo (
) )
(column_constraint (column_constraint
(keyword_default) (keyword_default)
(literal) (literal
(literal_string)
)
) )
) )
) )
@ -347,7 +349,9 @@ create table foo (
(keyword_constraint) (keyword_constraint)
name: (identifier) name: (identifier)
(keyword_default) (keyword_default)
(literal) (literal
(literal_string)
)
) )
) )
(column_definition (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.