commit 78f290713f66fad089a71b78ef917e3f47db6afe Author: Christian De la Hoz Date: Sat Aug 21 19:15:42 2021 +0200 create_function diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..051d09d --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +eval "$(lorri direnv)" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..faca9b7 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "tree-sitter-YOUR-LANGUAGE-NAME" +description = "YOUR-LANGUAGE-NAME grammar for the tree-sitter parsing library" +version = "0.0.1" +keywords = ["incremental", "parsing", "YOUR-LANGUAGE-NAME"] +categories = ["parsing", "text-editors"] +repository = "https://github.com/tree-sitter/tree-sitter-YOUR-LANGUAGE-NAME" +edition = "2018" +license = "MIT" + +build = "bindings/rust/build.rs" +include = [ + "bindings/rust/*", + "grammar.js", + "queries/*", + "src/*", +] + +[lib] +path = "bindings/rust/lib.rs" + +[dependencies] +tree-sitter = "~0.20.0" + +[build-dependencies] +cc = "1.0" diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8ea07ae --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +.DEFAULT_GOAL := test +.PHONY: build test bless + +build: + tree-sitter generate + +test: + @$(MAKE) --no-print-directory build + tree-sitter test + # watchexec --clear dune test + +bless: + @$(MAKE) --no-print-directory build + tree-sitter test --update diff --git a/binding.gyp b/binding.gyp new file mode 100644 index 0000000..7079205 --- /dev/null +++ b/binding.gyp @@ -0,0 +1,19 @@ +{ + "targets": [ + { + "target_name": "tree_sitter_YOUR_LANGUAGE_NAME_binding", + "include_dirs": [ + " +#include "nan.h" + +using namespace v8; + +extern "C" TSLanguage * tree_sitter_YOUR_LANGUAGE_NAME(); + +namespace { + +NAN_METHOD(New) {} + +void Init(Local exports, Local module) { + Local tpl = Nan::New(New); + tpl->SetClassName(Nan::New("Language").ToLocalChecked()); + tpl->InstanceTemplate()->SetInternalFieldCount(1); + + Local constructor = Nan::GetFunction(tpl).ToLocalChecked(); + Local instance = constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked(); + Nan::SetInternalFieldPointer(instance, 0, tree_sitter_YOUR_LANGUAGE_NAME()); + + Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("YOUR_LANGUAGE_NAME").ToLocalChecked()); + Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance); +} + +NODE_MODULE(tree_sitter_YOUR_LANGUAGE_NAME_binding, Init) + +} // namespace diff --git a/bindings/node/index.js b/bindings/node/index.js new file mode 100644 index 0000000..814d8b0 --- /dev/null +++ b/bindings/node/index.js @@ -0,0 +1,19 @@ +try { + module.exports = require("../../build/Release/tree_sitter_YOUR_LANGUAGE_NAME_binding"); +} catch (error1) { + if (error1.code !== 'MODULE_NOT_FOUND') { + throw error1; + } + try { + module.exports = require("../../build/Debug/tree_sitter_YOUR_LANGUAGE_NAME_binding"); + } catch (error2) { + if (error2.code !== 'MODULE_NOT_FOUND') { + throw error2; + } + throw error1 + } +} + +try { + module.exports.nodeTypeInfo = require("../../src/node-types.json"); +} catch (_) {} diff --git a/bindings/rust/build.rs b/bindings/rust/build.rs new file mode 100644 index 0000000..c6061f0 --- /dev/null +++ b/bindings/rust/build.rs @@ -0,0 +1,40 @@ +fn main() { + let src_dir = std::path::Path::new("src"); + + let mut c_config = cc::Build::new(); + c_config.include(&src_dir); + c_config + .flag_if_supported("-Wno-unused-parameter") + .flag_if_supported("-Wno-unused-but-set-variable") + .flag_if_supported("-Wno-trigraphs"); + let parser_path = src_dir.join("parser.c"); + c_config.file(&parser_path); + + // If your language uses an external scanner written in C, + // then include this block of code: + + /* + let scanner_path = src_dir.join("scanner.c"); + c_config.file(&scanner_path); + println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap()); + */ + + c_config.compile("parser"); + println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap()); + + // If your language uses an external scanner written in C++, + // then include this block of code: + + /* + let mut cpp_config = cc::Build::new(); + cpp_config.cpp(true); + cpp_config.include(&src_dir); + cpp_config + .flag_if_supported("-Wno-unused-parameter") + .flag_if_supported("-Wno-unused-but-set-variable"); + let scanner_path = src_dir.join("scanner.cc"); + cpp_config.file(&scanner_path); + cpp_config.compile("scanner"); + println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap()); + */ +} diff --git a/bindings/rust/lib.rs b/bindings/rust/lib.rs new file mode 100644 index 0000000..b3d2209 --- /dev/null +++ b/bindings/rust/lib.rs @@ -0,0 +1,52 @@ +//! This crate provides YOUR_LANGUAGE_NAME language support for the [tree-sitter][] parsing library. +//! +//! Typically, you will use the [language][language func] function to add this language to a +//! tree-sitter [Parser][], and then use the parser to parse some code: +//! +//! ``` +//! let code = ""; +//! let mut parser = tree_sitter::Parser::new(); +//! parser.set_language(tree_sitter_YOUR_LANGUAGE_NAME::language()).expect("Error loading YOUR_LANGUAGE_NAME grammar"); +//! let tree = parser.parse(code, None).unwrap(); +//! ``` +//! +//! [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html +//! [language func]: fn.language.html +//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html +//! [tree-sitter]: https://tree-sitter.github.io/ + +use tree_sitter::Language; + +extern "C" { + fn tree_sitter_YOUR_LANGUAGE_NAME() -> Language; +} + +/// Get the tree-sitter [Language][] for this grammar. +/// +/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html +pub fn language() -> Language { + unsafe { tree_sitter_YOUR_LANGUAGE_NAME() } +} + +/// The content of the [`node-types.json`][] file for this grammar. +/// +/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types +pub const NODE_TYPES: &'static str = include_str!("../../src/node-types.json"); + +// Uncomment these to include any queries that this grammar contains + +// pub const HIGHLIGHTS_QUERY: &'static str = include_str!("../../queries/highlights.scm"); +// pub const INJECTIONS_QUERY: &'static str = include_str!("../../queries/injections.scm"); +// pub const LOCALS_QUERY: &'static str = include_str!("../../queries/locals.scm"); +// pub const TAGS_QUERY: &'static str = include_str!("../../queries/tags.scm"); + +#[cfg(test)] +mod tests { + #[test] + fn test_can_load_grammar() { + let mut parser = tree_sitter::Parser::new(); + parser + .set_language(super::language()) + .expect("Error loading YOUR_LANGUAGE_NAME language"); + } +} diff --git a/corpus/create_function/body.txt b/corpus/create_function/body.txt new file mode 100644 index 0000000..b5a4663 --- /dev/null +++ b/corpus/create_function/body.txt @@ -0,0 +1,16 @@ +================================================================================ +Empty # todo: body as string, body with code +================================================================================ +CREATE FUNCTION FOO () RETURNS BAZ AS $$ $$ LANGUAGE sql; +-------------------------------------------------------------------------------- + +(source_file + (create_function_statement + (function_signature + (identifier) + (function_parameters)) + (identifier) + (block + (dollar_quote) + (dollar_quote)) + (identifier))) diff --git a/corpus/create_function/general.txt b/corpus/create_function/general.txt new file mode 100644 index 0000000..f8b6d74 --- /dev/null +++ b/corpus/create_function/general.txt @@ -0,0 +1,85 @@ +================================================================================ +or replace +================================================================================ +CREATE OR REPLACE FUNCTION FOO () AS $$ $$ LANGUAGE sql; +-------------------------------------------------------------------------------- + +(source_file + (create_function_statement + (or_replace) + (function_signature + (identifier) + (function_parameters)) + (block + (dollar_quote) + (dollar_quote)) + (identifier))) + +================================================================================ +immutable +================================================================================ +CREATE FUNCTION FOO () AS $$ $$ LANGUAGE sql IMMUTABLE; +-------------------------------------------------------------------------------- + +(source_file + (create_function_statement + (function_signature + (identifier) + (function_parameters)) + (block + (dollar_quote) + (dollar_quote)) + (identifier) + (function_volatility))) + +================================================================================ +stable +================================================================================ +CREATE FUNCTION FOO () AS $$ $$ LANGUAGE sql STABLE; +-------------------------------------------------------------------------------- + +(source_file + (create_function_statement + (function_signature + (identifier) + (function_parameters)) + (block + (dollar_quote) + (dollar_quote)) + (identifier) + (function_volatility))) + +================================================================================ +volatile +================================================================================ +CREATE FUNCTION FOO () AS $$ $$ LANGUAGE sql VOLATILE; +-------------------------------------------------------------------------------- + +(source_file + (create_function_statement + (function_signature + (identifier) + (function_parameters)) + (block + (dollar_quote) + (dollar_quote)) + (identifier) + (function_volatility))) + +================================================================================ +wrong - many volatilities +================================================================================ +CREATE FUNCTION FOO () AS $$ $$ LANGUAGE sql STABLE VOLATILE; +-------------------------------------------------------------------------------- + +(source_file + (create_function_statement + (function_signature + (identifier) + (function_parameters)) + (block + (dollar_quote) + (dollar_quote)) + (identifier) + (function_volatility)) + (ERROR)) diff --git a/corpus/create_function/parameters.txt b/corpus/create_function/parameters.txt new file mode 100644 index 0000000..693729a --- /dev/null +++ b/corpus/create_function/parameters.txt @@ -0,0 +1,139 @@ +================================================================================ +Empty +================================================================================ +CREATE FUNCTION FOO () AS $$ $$ LANGUAGE sql; +-------------------------------------------------------------------------------- + +(source_file + (create_function_statement + (function_signature + (identifier) + (function_parameters)) + (block + (dollar_quote) + (dollar_quote)) + (identifier))) + +================================================================================ +With argument +================================================================================ +CREATE FUNCTION FOO (_foo bar.baz) AS $$ $$ LANGUAGE sql; +-------------------------------------------------------------------------------- + +(source_file + (create_function_statement + (function_signature + (identifier) + (function_parameters + (function_parameter + (identifier) + (identifier)))) + (block + (dollar_quote) + (dollar_quote)) + (identifier))) + +================================================================================ +With arguments +================================================================================ +CREATE FUNCTION FOO (foo bar, foo bar) AS $$ $$ LANGUAGE sql; +-------------------------------------------------------------------------------- + +(source_file + (create_function_statement + (function_signature + (identifier) + (function_parameters + (function_parameter + (identifier) + (identifier)) + (function_parameter + (identifier) + (identifier)))) + (block + (dollar_quote) + (dollar_quote)) + (identifier))) + +================================================================================ +Wrong - trailing comma (1) +================================================================================ +CREATE FUNCTION FOO (foo bar,) AS $$ $$ LANGUAGE sql; +-------------------------------------------------------------------------------- + +(source_file + (create_function_statement + (function_signature + (identifier) + (function_parameters + (function_parameter + (identifier) + (identifier)) + (ERROR))) + (block + (dollar_quote) + (dollar_quote)) + (identifier))) + +================================================================================ +Wrong - trailing comma (2) +================================================================================ +CREATE FUNCTION FOO (foo bar, foo bar,) AS $$ $$ LANGUAGE sql; +-------------------------------------------------------------------------------- + +(source_file + (create_function_statement + (function_signature + (identifier) + (function_parameters + (function_parameter + (identifier) + (identifier)) + (function_parameter + (identifier) + (identifier)) + (ERROR))) + (block + (dollar_quote) + (dollar_quote)) + (identifier))) + +================================================================================ +Wrong - missing type +================================================================================ +CREATE FUNCTION FOO (foo,) AS $$ $$ LANGUAGE sql; +-------------------------------------------------------------------------------- + +(source_file + (create_function_statement + (function_signature + (identifier) + (function_parameters + (ERROR + (identifier)))) + (block + (dollar_quote) + (dollar_quote)) + (identifier))) + +================================================================================ +arrays +================================================================================ +CREATE FUNCTION FOO (foo bar[], foo bar[][]) AS $$ $$ LANGUAGE sql; +-------------------------------------------------------------------------------- + +(source_file + (create_function_statement + (function_signature + (identifier) + (function_parameters + (function_parameter + (identifier) + (identifier)) + (function_parameter + (identifier) + (identifier)))) + (block + (dollar_quote) + (dollar_quote)) + (identifier))) diff --git a/grammar.js b/grammar.js new file mode 100644 index 0000000..56d22cf --- /dev/null +++ b/grammar.js @@ -0,0 +1,83 @@ +function kw(keyword, aliasAsWord = true) { + let result = new RegExp(keyword + .split('') + .map(l => l !== l.toUpperCase() ? `[${l}${l.toUpperCase()}]` : l) + .join('') + ); + if (aliasAsWord) { + result = alias(result, keyword); + } + return result; +} + +function delimited(left, rule, right = left) { + return seq(left, rule, right) +} + +function separated(separator, rule) { + return choice( + optional(rule), + seq(rule, repeat1(seq(",", rule)))) +} + +module.exports = grammar({ + name: 'plpgsql', + + rules: { + source_file: $ => repeat($._statement), + + _statement: $ => seq( + choice( + $.create_function_statement, + ), + ";" + ), + + create_function_statement: $ => seq( + kw("CREATE"), optional($.or_replace), kw("FUNCTION"), + $.function_signature, + optional(seq(kw("RETURNS"), $.identifier)), + kw("AS"), $.block, + kw("LANGUAGE"), $.identifier, + optional($.function_volatility) + ), + + function_volatility: $ => choice( + kw("IMMUTABLE"), + kw("STABLE"), + kw("VOLATILE"), + ), + + block: $ => seq( + $.dollar_quote, + $.dollar_quote, + ), + + dollar_quote: $ => delimited("$", optional($.identifier)), + + function_signature: $ => seq( + $.identifier, + $.function_parameters, + ), + + function_parameters: $ => seq( + "(", + separated(",", $.function_parameter), + ")" + ), + + function_parameter: $ => seq( + field("name", $.identifier), + field("type", $._type), + ), + + or_replace: $ => seq("OR", "REPLACE"), + + _type: $ => choice( + $.identifier, + seq($._type, "[", "]"), + ), + + identifier: $ => /[a-zA-Z0-9_]+[.a-zA-Z0-9_]*/, + } +}); diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..af81951 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,18 @@ +{ + "name": "tree-sitter-plpgsql", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "nan": { + "version": "2.15.0", + "resolved": "https://registry.npmjs.org/nan/-/nan-2.15.0.tgz", + "integrity": "sha512-8ZtvEnA2c5aYCZYd1cvgdnU6cqwixRoYg70xPLWUws5ORTa/lnw+u4amixRS/Ac5U5mQVgp9pnlSUnbNWFaWZQ==" + }, + "tree-sitter-cli": { + "version": "0.20.0", + "resolved": "https://registry.npmjs.org/tree-sitter-cli/-/tree-sitter-cli-0.20.0.tgz", + "integrity": "sha512-4D1qapWbJXZ5rrSUGM5rcw5Vuq/smzn9KbiFRhlON6KeuuXjra+KAtDYVrDgAoLIG4ku+jbEEGrJxCptUGi3dg==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..ac1441e --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "tree-sitter-plpgsql", + "version": "1.0.0", + "description": "", + "main": "bindings/node", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "nan": "^2.15.0", + "tree-sitter-cli": "^0.20.0" + } +} diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..6521448 --- /dev/null +++ b/shell.nix @@ -0,0 +1,13 @@ +let + pkgs = import { }; + # openssl = pkgs.openssl.override { static = true; }; +in pkgs.mkShell { + buildInputs = with pkgs; [ + nodejs + gcc + tree-sitter + ]; + # buildInputs = with pkgs; [ openssl pkg-config lld ]; + # LD_LIBRARY_PATH="${pkgs.openssl.out}/lib"; + # LD_LIBRARY_PATH = "${openssl}/lib"; +} diff --git a/src/grammar.json b/src/grammar.json new file mode 100644 index 0000000..6502564 --- /dev/null +++ b/src/grammar.json @@ -0,0 +1,347 @@ +{ + "name": "plpgsql", + "rules": { + "source_file": { + "type": "REPEAT", + "content": { + "type": "SYMBOL", + "name": "_statement" + } + }, + "_statement": { + "type": "SEQ", + "members": [ + { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "create_function_statement" + } + ] + }, + { + "type": "STRING", + "value": ";" + } + ] + }, + "create_function_statement": { + "type": "SEQ", + "members": [ + { + "type": "ALIAS", + "content": { + "type": "PATTERN", + "value": "CREATE" + }, + "named": false, + "value": "CREATE" + }, + { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "or_replace" + }, + { + "type": "BLANK" + } + ] + }, + { + "type": "ALIAS", + "content": { + "type": "PATTERN", + "value": "FUNCTION" + }, + "named": false, + "value": "FUNCTION" + }, + { + "type": "SYMBOL", + "name": "function_signature" + }, + { + "type": "CHOICE", + "members": [ + { + "type": "SEQ", + "members": [ + { + "type": "ALIAS", + "content": { + "type": "PATTERN", + "value": "RETURNS" + }, + "named": false, + "value": "RETURNS" + }, + { + "type": "SYMBOL", + "name": "identifier" + } + ] + }, + { + "type": "BLANK" + } + ] + }, + { + "type": "ALIAS", + "content": { + "type": "PATTERN", + "value": "AS" + }, + "named": false, + "value": "AS" + }, + { + "type": "SYMBOL", + "name": "block" + }, + { + "type": "ALIAS", + "content": { + "type": "PATTERN", + "value": "LANGUAGE" + }, + "named": false, + "value": "LANGUAGE" + }, + { + "type": "SYMBOL", + "name": "identifier" + }, + { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "function_volatility" + }, + { + "type": "BLANK" + } + ] + } + ] + }, + "function_volatility": { + "type": "CHOICE", + "members": [ + { + "type": "ALIAS", + "content": { + "type": "PATTERN", + "value": "IMMUTABLE" + }, + "named": false, + "value": "IMMUTABLE" + }, + { + "type": "ALIAS", + "content": { + "type": "PATTERN", + "value": "STABLE" + }, + "named": false, + "value": "STABLE" + }, + { + "type": "ALIAS", + "content": { + "type": "PATTERN", + "value": "VOLATILE" + }, + "named": false, + "value": "VOLATILE" + } + ] + }, + "block": { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "dollar_quote" + }, + { + "type": "SYMBOL", + "name": "dollar_quote" + } + ] + }, + "dollar_quote": { + "type": "SEQ", + "members": [ + { + "type": "STRING", + "value": "$" + }, + { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "identifier" + }, + { + "type": "BLANK" + } + ] + }, + { + "type": "STRING", + "value": "$" + } + ] + }, + "function_signature": { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "identifier" + }, + { + "type": "SYMBOL", + "name": "function_parameters" + } + ] + }, + "function_parameters": { + "type": "SEQ", + "members": [ + { + "type": "STRING", + "value": "(" + }, + { + "type": "CHOICE", + "members": [ + { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "function_parameter" + }, + { + "type": "BLANK" + } + ] + }, + { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "function_parameter" + }, + { + "type": "REPEAT1", + "content": { + "type": "SEQ", + "members": [ + { + "type": "STRING", + "value": "," + }, + { + "type": "SYMBOL", + "name": "function_parameter" + } + ] + } + } + ] + } + ] + }, + { + "type": "STRING", + "value": ")" + } + ] + }, + "function_parameter": { + "type": "SEQ", + "members": [ + { + "type": "FIELD", + "name": "name", + "content": { + "type": "SYMBOL", + "name": "identifier" + } + }, + { + "type": "FIELD", + "name": "type", + "content": { + "type": "SYMBOL", + "name": "_type" + } + } + ] + }, + "or_replace": { + "type": "SEQ", + "members": [ + { + "type": "STRING", + "value": "OR" + }, + { + "type": "STRING", + "value": "REPLACE" + } + ] + }, + "_type": { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "identifier" + }, + { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "_type" + }, + { + "type": "STRING", + "value": "[" + }, + { + "type": "STRING", + "value": "]" + } + ] + } + ] + }, + "identifier": { + "type": "PATTERN", + "value": "[a-zA-Z0-9_]+[.a-zA-Z0-9_]*" + } + }, + "extras": [ + { + "type": "PATTERN", + "value": "\\s" + } + ], + "conflicts": [], + "precedences": [], + "externals": [], + "inline": [], + "supertypes": [] +} + diff --git a/src/node-types.json b/src/node-types.json new file mode 100644 index 0000000..156e798 --- /dev/null +++ b/src/node-types.json @@ -0,0 +1,228 @@ +[ + { + "type": "block", + "named": true, + "fields": {}, + "children": { + "multiple": true, + "required": true, + "types": [ + { + "type": "dollar_quote", + "named": true + } + ] + } + }, + { + "type": "create_function_statement", + "named": true, + "fields": {}, + "children": { + "multiple": true, + "required": true, + "types": [ + { + "type": "block", + "named": true + }, + { + "type": "function_signature", + "named": true + }, + { + "type": "function_volatility", + "named": true + }, + { + "type": "identifier", + "named": true + }, + { + "type": "or_replace", + "named": true + } + ] + } + }, + { + "type": "dollar_quote", + "named": true, + "fields": {}, + "children": { + "multiple": false, + "required": false, + "types": [ + { + "type": "identifier", + "named": true + } + ] + } + }, + { + "type": "function_parameter", + "named": true, + "fields": { + "name": { + "multiple": false, + "required": true, + "types": [ + { + "type": "identifier", + "named": true + } + ] + }, + "type": { + "multiple": true, + "required": true, + "types": [ + { + "type": "[", + "named": false + }, + { + "type": "]", + "named": false + }, + { + "type": "identifier", + "named": true + } + ] + } + } + }, + { + "type": "function_parameters", + "named": true, + "fields": {}, + "children": { + "multiple": true, + "required": false, + "types": [ + { + "type": "function_parameter", + "named": true + } + ] + } + }, + { + "type": "function_signature", + "named": true, + "fields": {}, + "children": { + "multiple": true, + "required": true, + "types": [ + { + "type": "function_parameters", + "named": true + }, + { + "type": "identifier", + "named": true + } + ] + } + }, + { + "type": "function_volatility", + "named": true, + "fields": {} + }, + { + "type": "or_replace", + "named": true, + "fields": {} + }, + { + "type": "source_file", + "named": true, + "fields": {}, + "children": { + "multiple": true, + "required": false, + "types": [ + { + "type": "create_function_statement", + "named": true + } + ] + } + }, + { + "type": "$", + "named": false + }, + { + "type": "(", + "named": false + }, + { + "type": ")", + "named": false + }, + { + "type": ",", + "named": false + }, + { + "type": ";", + "named": false + }, + { + "type": "AS", + "named": false + }, + { + "type": "CREATE", + "named": false + }, + { + "type": "FUNCTION", + "named": false + }, + { + "type": "IMMUTABLE", + "named": false + }, + { + "type": "LANGUAGE", + "named": false + }, + { + "type": "OR", + "named": false + }, + { + "type": "REPLACE", + "named": false + }, + { + "type": "RETURNS", + "named": false + }, + { + "type": "STABLE", + "named": false + }, + { + "type": "VOLATILE", + "named": false + }, + { + "type": "[", + "named": false + }, + { + "type": "]", + "named": false + }, + { + "type": "identifier", + "named": true + } +] \ No newline at end of file diff --git a/src/parser.c b/src/parser.c new file mode 100644 index 0000000..915cce9 --- /dev/null +++ b/src/parser.c @@ -0,0 +1,1097 @@ +#include + +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wmissing-field-initializers" +#endif + +#define LANGUAGE_VERSION 13 +#define STATE_COUNT 61 +#define LARGE_STATE_COUNT 2 +#define SYMBOL_COUNT 32 +#define ALIAS_COUNT 0 +#define TOKEN_COUNT 19 +#define EXTERNAL_TOKEN_COUNT 0 +#define FIELD_COUNT 2 +#define MAX_ALIAS_SEQUENCE_LENGTH 11 +#define PRODUCTION_ID_COUNT 2 + +enum { + anon_sym_SEMI = 1, + aux_sym_create_function_statement_token1 = 2, + aux_sym_create_function_statement_token2 = 3, + aux_sym_create_function_statement_token3 = 4, + aux_sym_create_function_statement_token4 = 5, + aux_sym_create_function_statement_token5 = 6, + aux_sym_function_volatility_token1 = 7, + aux_sym_function_volatility_token2 = 8, + aux_sym_function_volatility_token3 = 9, + anon_sym_DOLLAR = 10, + anon_sym_LPAREN = 11, + anon_sym_COMMA = 12, + anon_sym_RPAREN = 13, + anon_sym_OR = 14, + anon_sym_REPLACE = 15, + anon_sym_LBRACK = 16, + anon_sym_RBRACK = 17, + sym_identifier = 18, + sym_source_file = 19, + sym__statement = 20, + sym_create_function_statement = 21, + sym_function_volatility = 22, + sym_block = 23, + sym_dollar_quote = 24, + sym_function_signature = 25, + sym_function_parameters = 26, + sym_function_parameter = 27, + sym_or_replace = 28, + sym__type = 29, + aux_sym_source_file_repeat1 = 30, + aux_sym_function_parameters_repeat1 = 31, +}; + +static const char * const ts_symbol_names[] = { + [ts_builtin_sym_end] = "end", + [anon_sym_SEMI] = ";", + [aux_sym_create_function_statement_token1] = "CREATE", + [aux_sym_create_function_statement_token2] = "FUNCTION", + [aux_sym_create_function_statement_token3] = "RETURNS", + [aux_sym_create_function_statement_token4] = "AS", + [aux_sym_create_function_statement_token5] = "LANGUAGE", + [aux_sym_function_volatility_token1] = "IMMUTABLE", + [aux_sym_function_volatility_token2] = "STABLE", + [aux_sym_function_volatility_token3] = "VOLATILE", + [anon_sym_DOLLAR] = "$", + [anon_sym_LPAREN] = "(", + [anon_sym_COMMA] = ",", + [anon_sym_RPAREN] = ")", + [anon_sym_OR] = "OR", + [anon_sym_REPLACE] = "REPLACE", + [anon_sym_LBRACK] = "[", + [anon_sym_RBRACK] = "]", + [sym_identifier] = "identifier", + [sym_source_file] = "source_file", + [sym__statement] = "_statement", + [sym_create_function_statement] = "create_function_statement", + [sym_function_volatility] = "function_volatility", + [sym_block] = "block", + [sym_dollar_quote] = "dollar_quote", + [sym_function_signature] = "function_signature", + [sym_function_parameters] = "function_parameters", + [sym_function_parameter] = "function_parameter", + [sym_or_replace] = "or_replace", + [sym__type] = "_type", + [aux_sym_source_file_repeat1] = "source_file_repeat1", + [aux_sym_function_parameters_repeat1] = "function_parameters_repeat1", +}; + +static const TSSymbol ts_symbol_map[] = { + [ts_builtin_sym_end] = ts_builtin_sym_end, + [anon_sym_SEMI] = anon_sym_SEMI, + [aux_sym_create_function_statement_token1] = aux_sym_create_function_statement_token1, + [aux_sym_create_function_statement_token2] = aux_sym_create_function_statement_token2, + [aux_sym_create_function_statement_token3] = aux_sym_create_function_statement_token3, + [aux_sym_create_function_statement_token4] = aux_sym_create_function_statement_token4, + [aux_sym_create_function_statement_token5] = aux_sym_create_function_statement_token5, + [aux_sym_function_volatility_token1] = aux_sym_function_volatility_token1, + [aux_sym_function_volatility_token2] = aux_sym_function_volatility_token2, + [aux_sym_function_volatility_token3] = aux_sym_function_volatility_token3, + [anon_sym_DOLLAR] = anon_sym_DOLLAR, + [anon_sym_LPAREN] = anon_sym_LPAREN, + [anon_sym_COMMA] = anon_sym_COMMA, + [anon_sym_RPAREN] = anon_sym_RPAREN, + [anon_sym_OR] = anon_sym_OR, + [anon_sym_REPLACE] = anon_sym_REPLACE, + [anon_sym_LBRACK] = anon_sym_LBRACK, + [anon_sym_RBRACK] = anon_sym_RBRACK, + [sym_identifier] = sym_identifier, + [sym_source_file] = sym_source_file, + [sym__statement] = sym__statement, + [sym_create_function_statement] = sym_create_function_statement, + [sym_function_volatility] = sym_function_volatility, + [sym_block] = sym_block, + [sym_dollar_quote] = sym_dollar_quote, + [sym_function_signature] = sym_function_signature, + [sym_function_parameters] = sym_function_parameters, + [sym_function_parameter] = sym_function_parameter, + [sym_or_replace] = sym_or_replace, + [sym__type] = sym__type, + [aux_sym_source_file_repeat1] = aux_sym_source_file_repeat1, + [aux_sym_function_parameters_repeat1] = aux_sym_function_parameters_repeat1, +}; + +static const TSSymbolMetadata ts_symbol_metadata[] = { + [ts_builtin_sym_end] = { + .visible = false, + .named = true, + }, + [anon_sym_SEMI] = { + .visible = true, + .named = false, + }, + [aux_sym_create_function_statement_token1] = { + .visible = true, + .named = false, + }, + [aux_sym_create_function_statement_token2] = { + .visible = true, + .named = false, + }, + [aux_sym_create_function_statement_token3] = { + .visible = true, + .named = false, + }, + [aux_sym_create_function_statement_token4] = { + .visible = true, + .named = false, + }, + [aux_sym_create_function_statement_token5] = { + .visible = true, + .named = false, + }, + [aux_sym_function_volatility_token1] = { + .visible = true, + .named = false, + }, + [aux_sym_function_volatility_token2] = { + .visible = true, + .named = false, + }, + [aux_sym_function_volatility_token3] = { + .visible = true, + .named = false, + }, + [anon_sym_DOLLAR] = { + .visible = true, + .named = false, + }, + [anon_sym_LPAREN] = { + .visible = true, + .named = false, + }, + [anon_sym_COMMA] = { + .visible = true, + .named = false, + }, + [anon_sym_RPAREN] = { + .visible = true, + .named = false, + }, + [anon_sym_OR] = { + .visible = true, + .named = false, + }, + [anon_sym_REPLACE] = { + .visible = true, + .named = false, + }, + [anon_sym_LBRACK] = { + .visible = true, + .named = false, + }, + [anon_sym_RBRACK] = { + .visible = true, + .named = false, + }, + [sym_identifier] = { + .visible = true, + .named = true, + }, + [sym_source_file] = { + .visible = true, + .named = true, + }, + [sym__statement] = { + .visible = false, + .named = true, + }, + [sym_create_function_statement] = { + .visible = true, + .named = true, + }, + [sym_function_volatility] = { + .visible = true, + .named = true, + }, + [sym_block] = { + .visible = true, + .named = true, + }, + [sym_dollar_quote] = { + .visible = true, + .named = true, + }, + [sym_function_signature] = { + .visible = true, + .named = true, + }, + [sym_function_parameters] = { + .visible = true, + .named = true, + }, + [sym_function_parameter] = { + .visible = true, + .named = true, + }, + [sym_or_replace] = { + .visible = true, + .named = true, + }, + [sym__type] = { + .visible = false, + .named = true, + }, + [aux_sym_source_file_repeat1] = { + .visible = false, + .named = false, + }, + [aux_sym_function_parameters_repeat1] = { + .visible = false, + .named = false, + }, +}; + +enum { + field_name = 1, + field_type = 2, +}; + +static const char * const ts_field_names[] = { + [0] = NULL, + [field_name] = "name", + [field_type] = "type", +}; + +static const TSFieldMapSlice ts_field_map_slices[PRODUCTION_ID_COUNT] = { + [1] = {.index = 0, .length = 2}, +}; + +static const TSFieldMapEntry ts_field_map_entries[] = { + [0] = + {field_name, 0}, + {field_type, 1}, +}; + +static const TSSymbol ts_alias_sequences[PRODUCTION_ID_COUNT][MAX_ALIAS_SEQUENCE_LENGTH] = { + [0] = {0}, +}; + +static const uint16_t ts_non_terminal_alias_map[] = { + 0, +}; + +static bool ts_lex(TSLexer *lexer, TSStateId state) { + START_LEXER(); + eof = lexer->eof(lexer); + switch (state) { + case 0: + if (eof) ADVANCE(53); + if (lookahead == '$') ADVANCE(63); + if (lookahead == '(') ADVANCE(64); + if (lookahead == ')') ADVANCE(66); + if (lookahead == ',') ADVANCE(65); + if (lookahead == ';') ADVANCE(54); + if (lookahead == 'A') ADVANCE(42); + if (lookahead == 'C') ADVANCE(40); + if (lookahead == 'F') ADVANCE(49); + if (lookahead == 'I') ADVANCE(31); + if (lookahead == 'L') ADVANCE(4); + if (lookahead == 'O') ADVANCE(39); + if (lookahead == 'R') ADVANCE(13); + if (lookahead == 'S') ADVANCE(45); + if (lookahead == 'V') ADVANCE(36); + if (lookahead == '[') ADVANCE(69); + if (lookahead == ']') ADVANCE(70); + if (lookahead == '\t' || + lookahead == '\n' || + lookahead == '\r' || + lookahead == ' ') SKIP(0) + END_STATE(); + case 1: + if (lookahead == '$') ADVANCE(63); + if (lookahead == ')') ADVANCE(66); + if (lookahead == '\t' || + lookahead == '\n' || + lookahead == '\r' || + lookahead == ' ') SKIP(1) + if (('0' <= lookahead && lookahead <= '9') || + ('A' <= lookahead && lookahead <= 'Z') || + lookahead == '_' || + ('a' <= lookahead && lookahead <= 'z')) ADVANCE(71); + END_STATE(); + case 2: + if (lookahead == 'A') ADVANCE(9); + END_STATE(); + case 3: + if (lookahead == 'A') ADVANCE(47); + END_STATE(); + case 4: + if (lookahead == 'A') ADVANCE(33); + END_STATE(); + case 5: + if (lookahead == 'A') ADVANCE(12); + END_STATE(); + case 6: + if (lookahead == 'A') ADVANCE(22); + END_STATE(); + case 7: + if (lookahead == 'A') ADVANCE(46); + END_STATE(); + case 8: + if (lookahead == 'A') ADVANCE(10); + END_STATE(); + case 9: + if (lookahead == 'B') ADVANCE(26); + END_STATE(); + case 10: + if (lookahead == 'B') ADVANCE(29); + END_STATE(); + case 11: + if (lookahead == 'C') ADVANCE(44); + END_STATE(); + case 12: + if (lookahead == 'C') ADVANCE(16); + END_STATE(); + case 13: + if (lookahead == 'E') ADVANCE(38); + END_STATE(); + case 14: + if (lookahead == 'E') ADVANCE(55); + END_STATE(); + case 15: + if (lookahead == 'E') ADVANCE(61); + END_STATE(); + case 16: + if (lookahead == 'E') ADVANCE(68); + END_STATE(); + case 17: + if (lookahead == 'E') ADVANCE(59); + END_STATE(); + case 18: + if (lookahead == 'E') ADVANCE(62); + END_STATE(); + case 19: + if (lookahead == 'E') ADVANCE(60); + END_STATE(); + case 20: + if (lookahead == 'E') ADVANCE(3); + END_STATE(); + case 21: + if (lookahead == 'G') ADVANCE(51); + END_STATE(); + case 22: + if (lookahead == 'G') ADVANCE(17); + END_STATE(); + case 23: + if (lookahead == 'I') ADVANCE(37); + END_STATE(); + case 24: + if (lookahead == 'I') ADVANCE(28); + END_STATE(); + case 25: + if (lookahead == 'L') ADVANCE(7); + END_STATE(); + case 26: + if (lookahead == 'L') ADVANCE(15); + END_STATE(); + case 27: + if (lookahead == 'L') ADVANCE(5); + END_STATE(); + case 28: + if (lookahead == 'L') ADVANCE(18); + END_STATE(); + case 29: + if (lookahead == 'L') ADVANCE(19); + END_STATE(); + case 30: + if (lookahead == 'M') ADVANCE(52); + END_STATE(); + case 31: + if (lookahead == 'M') ADVANCE(30); + END_STATE(); + case 32: + if (lookahead == 'N') ADVANCE(11); + END_STATE(); + case 33: + if (lookahead == 'N') ADVANCE(21); + END_STATE(); + case 34: + if (lookahead == 'N') ADVANCE(56); + END_STATE(); + case 35: + if (lookahead == 'N') ADVANCE(43); + END_STATE(); + case 36: + if (lookahead == 'O') ADVANCE(25); + END_STATE(); + case 37: + if (lookahead == 'O') ADVANCE(34); + END_STATE(); + case 38: + if (lookahead == 'P') ADVANCE(27); + if (lookahead == 'T') ADVANCE(50); + END_STATE(); + case 39: + if (lookahead == 'R') ADVANCE(67); + END_STATE(); + case 40: + if (lookahead == 'R') ADVANCE(20); + END_STATE(); + case 41: + if (lookahead == 'R') ADVANCE(35); + END_STATE(); + case 42: + if (lookahead == 'S') ADVANCE(58); + END_STATE(); + case 43: + if (lookahead == 'S') ADVANCE(57); + END_STATE(); + case 44: + if (lookahead == 'T') ADVANCE(23); + END_STATE(); + case 45: + if (lookahead == 'T') ADVANCE(2); + END_STATE(); + case 46: + if (lookahead == 'T') ADVANCE(24); + END_STATE(); + case 47: + if (lookahead == 'T') ADVANCE(14); + END_STATE(); + case 48: + if (lookahead == 'T') ADVANCE(8); + END_STATE(); + case 49: + if (lookahead == 'U') ADVANCE(32); + END_STATE(); + case 50: + if (lookahead == 'U') ADVANCE(41); + END_STATE(); + case 51: + if (lookahead == 'U') ADVANCE(6); + END_STATE(); + case 52: + if (lookahead == 'U') ADVANCE(48); + END_STATE(); + case 53: + ACCEPT_TOKEN(ts_builtin_sym_end); + END_STATE(); + case 54: + ACCEPT_TOKEN(anon_sym_SEMI); + END_STATE(); + case 55: + ACCEPT_TOKEN(aux_sym_create_function_statement_token1); + END_STATE(); + case 56: + ACCEPT_TOKEN(aux_sym_create_function_statement_token2); + END_STATE(); + case 57: + ACCEPT_TOKEN(aux_sym_create_function_statement_token3); + END_STATE(); + case 58: + ACCEPT_TOKEN(aux_sym_create_function_statement_token4); + END_STATE(); + case 59: + ACCEPT_TOKEN(aux_sym_create_function_statement_token5); + END_STATE(); + case 60: + ACCEPT_TOKEN(aux_sym_function_volatility_token1); + END_STATE(); + case 61: + ACCEPT_TOKEN(aux_sym_function_volatility_token2); + END_STATE(); + case 62: + ACCEPT_TOKEN(aux_sym_function_volatility_token3); + END_STATE(); + case 63: + ACCEPT_TOKEN(anon_sym_DOLLAR); + END_STATE(); + case 64: + ACCEPT_TOKEN(anon_sym_LPAREN); + END_STATE(); + case 65: + ACCEPT_TOKEN(anon_sym_COMMA); + END_STATE(); + case 66: + ACCEPT_TOKEN(anon_sym_RPAREN); + END_STATE(); + case 67: + ACCEPT_TOKEN(anon_sym_OR); + END_STATE(); + case 68: + ACCEPT_TOKEN(anon_sym_REPLACE); + END_STATE(); + case 69: + ACCEPT_TOKEN(anon_sym_LBRACK); + END_STATE(); + case 70: + ACCEPT_TOKEN(anon_sym_RBRACK); + END_STATE(); + case 71: + ACCEPT_TOKEN(sym_identifier); + if (lookahead == '.') ADVANCE(72); + if (('0' <= lookahead && lookahead <= '9') || + ('A' <= lookahead && lookahead <= 'Z') || + lookahead == '_' || + ('a' <= lookahead && lookahead <= 'z')) ADVANCE(71); + END_STATE(); + case 72: + ACCEPT_TOKEN(sym_identifier); + if (lookahead == '.' || + ('0' <= lookahead && lookahead <= '9') || + ('A' <= lookahead && lookahead <= 'Z') || + lookahead == '_' || + ('a' <= lookahead && lookahead <= 'z')) ADVANCE(72); + END_STATE(); + default: + return false; + } +} + +static const TSLexMode ts_lex_modes[STATE_COUNT] = { + [0] = {.lex_state = 0}, + [1] = {.lex_state = 0}, + [2] = {.lex_state = 0}, + [3] = {.lex_state = 0}, + [4] = {.lex_state = 0}, + [5] = {.lex_state = 0}, + [6] = {.lex_state = 0}, + [7] = {.lex_state = 0}, + [8] = {.lex_state = 0}, + [9] = {.lex_state = 0}, + [10] = {.lex_state = 0}, + [11] = {.lex_state = 0}, + [12] = {.lex_state = 0}, + [13] = {.lex_state = 0}, + [14] = {.lex_state = 0}, + [15] = {.lex_state = 1}, + [16] = {.lex_state = 0}, + [17] = {.lex_state = 0}, + [18] = {.lex_state = 0}, + [19] = {.lex_state = 1}, + [20] = {.lex_state = 1}, + [21] = {.lex_state = 0}, + [22] = {.lex_state = 1}, + [23] = {.lex_state = 0}, + [24] = {.lex_state = 0}, + [25] = {.lex_state = 1}, + [26] = {.lex_state = 0}, + [27] = {.lex_state = 1}, + [28] = {.lex_state = 0}, + [29] = {.lex_state = 0}, + [30] = {.lex_state = 0}, + [31] = {.lex_state = 0}, + [32] = {.lex_state = 0}, + [33] = {.lex_state = 0}, + [34] = {.lex_state = 0}, + [35] = {.lex_state = 0}, + [36] = {.lex_state = 0}, + [37] = {.lex_state = 0}, + [38] = {.lex_state = 0}, + [39] = {.lex_state = 0}, + [40] = {.lex_state = 1}, + [41] = {.lex_state = 1}, + [42] = {.lex_state = 0}, + [43] = {.lex_state = 0}, + [44] = {.lex_state = 0}, + [45] = {.lex_state = 0}, + [46] = {.lex_state = 1}, + [47] = {.lex_state = 0}, + [48] = {.lex_state = 1}, + [49] = {.lex_state = 0}, + [50] = {.lex_state = 1}, + [51] = {.lex_state = 0}, + [52] = {.lex_state = 0}, + [53] = {.lex_state = 0}, + [54] = {.lex_state = 0}, + [55] = {.lex_state = 0}, + [56] = {.lex_state = 1}, + [57] = {.lex_state = 0}, + [58] = {.lex_state = 0}, + [59] = {.lex_state = 0}, + [60] = {.lex_state = 0}, +}; + +static const uint16_t ts_parse_table[LARGE_STATE_COUNT][SYMBOL_COUNT] = { + [0] = { + [ts_builtin_sym_end] = ACTIONS(1), + [anon_sym_SEMI] = ACTIONS(1), + [aux_sym_create_function_statement_token1] = ACTIONS(1), + [aux_sym_create_function_statement_token2] = ACTIONS(1), + [aux_sym_create_function_statement_token3] = ACTIONS(1), + [aux_sym_create_function_statement_token4] = ACTIONS(1), + [aux_sym_create_function_statement_token5] = ACTIONS(1), + [aux_sym_function_volatility_token1] = ACTIONS(1), + [aux_sym_function_volatility_token2] = ACTIONS(1), + [aux_sym_function_volatility_token3] = ACTIONS(1), + [anon_sym_DOLLAR] = ACTIONS(1), + [anon_sym_LPAREN] = ACTIONS(1), + [anon_sym_COMMA] = ACTIONS(1), + [anon_sym_RPAREN] = ACTIONS(1), + [anon_sym_OR] = ACTIONS(1), + [anon_sym_REPLACE] = ACTIONS(1), + [anon_sym_LBRACK] = ACTIONS(1), + [anon_sym_RBRACK] = ACTIONS(1), + }, + [1] = { + [sym_source_file] = STATE(45), + [sym__statement] = STATE(4), + [sym_create_function_statement] = STATE(59), + [aux_sym_source_file_repeat1] = STATE(4), + [ts_builtin_sym_end] = ACTIONS(3), + [aux_sym_create_function_statement_token1] = ACTIONS(5), + }, +}; + +static const uint16_t ts_small_parse_table[] = { + [0] = 3, + ACTIONS(7), 1, + anon_sym_SEMI, + STATE(60), 1, + sym_function_volatility, + ACTIONS(9), 3, + aux_sym_function_volatility_token1, + aux_sym_function_volatility_token2, + aux_sym_function_volatility_token3, + [12] = 3, + ACTIONS(11), 1, + anon_sym_SEMI, + STATE(58), 1, + sym_function_volatility, + ACTIONS(9), 3, + aux_sym_function_volatility_token1, + aux_sym_function_volatility_token2, + aux_sym_function_volatility_token3, + [24] = 4, + ACTIONS(5), 1, + aux_sym_create_function_statement_token1, + ACTIONS(13), 1, + ts_builtin_sym_end, + STATE(59), 1, + sym_create_function_statement, + STATE(7), 2, + sym__statement, + aux_sym_source_file_repeat1, + [38] = 3, + ACTIONS(15), 1, + anon_sym_SEMI, + STATE(57), 1, + sym_function_volatility, + ACTIONS(9), 3, + aux_sym_function_volatility_token1, + aux_sym_function_volatility_token2, + aux_sym_function_volatility_token3, + [50] = 3, + ACTIONS(17), 1, + anon_sym_SEMI, + STATE(52), 1, + sym_function_volatility, + ACTIONS(9), 3, + aux_sym_function_volatility_token1, + aux_sym_function_volatility_token2, + aux_sym_function_volatility_token3, + [62] = 4, + ACTIONS(19), 1, + ts_builtin_sym_end, + ACTIONS(21), 1, + aux_sym_create_function_statement_token1, + STATE(59), 1, + sym_create_function_statement, + STATE(7), 2, + sym__statement, + aux_sym_source_file_repeat1, + [76] = 2, + ACTIONS(26), 1, + anon_sym_LBRACK, + ACTIONS(24), 2, + anon_sym_COMMA, + anon_sym_RPAREN, + [84] = 3, + ACTIONS(28), 1, + aux_sym_create_function_statement_token2, + ACTIONS(30), 1, + anon_sym_OR, + STATE(37), 1, + sym_or_replace, + [94] = 1, + ACTIONS(32), 3, + anon_sym_COMMA, + anon_sym_RPAREN, + anon_sym_LBRACK, + [100] = 3, + ACTIONS(34), 1, + anon_sym_DOLLAR, + STATE(29), 1, + sym_dollar_quote, + STATE(53), 1, + sym_block, + [110] = 3, + ACTIONS(36), 1, + anon_sym_COMMA, + ACTIONS(39), 1, + anon_sym_RPAREN, + STATE(12), 1, + aux_sym_function_parameters_repeat1, + [120] = 3, + ACTIONS(34), 1, + anon_sym_DOLLAR, + STATE(29), 1, + sym_dollar_quote, + STATE(44), 1, + sym_block, + [130] = 3, + ACTIONS(41), 1, + anon_sym_COMMA, + ACTIONS(43), 1, + anon_sym_RPAREN, + STATE(12), 1, + aux_sym_function_parameters_repeat1, + [140] = 3, + ACTIONS(45), 1, + anon_sym_RPAREN, + ACTIONS(47), 1, + sym_identifier, + STATE(17), 1, + sym_function_parameter, + [150] = 3, + ACTIONS(34), 1, + anon_sym_DOLLAR, + STATE(29), 1, + sym_dollar_quote, + STATE(39), 1, + sym_block, + [160] = 3, + ACTIONS(41), 1, + anon_sym_COMMA, + ACTIONS(49), 1, + anon_sym_RPAREN, + STATE(14), 1, + aux_sym_function_parameters_repeat1, + [170] = 3, + ACTIONS(34), 1, + anon_sym_DOLLAR, + STATE(29), 1, + sym_dollar_quote, + STATE(47), 1, + sym_block, + [180] = 2, + ACTIONS(47), 1, + sym_identifier, + STATE(35), 1, + sym_function_parameter, + [187] = 2, + ACTIONS(51), 1, + sym_identifier, + STATE(23), 1, + sym_function_signature, + [194] = 1, + ACTIONS(53), 2, + aux_sym_create_function_statement_token3, + aux_sym_create_function_statement_token4, + [199] = 2, + ACTIONS(51), 1, + sym_identifier, + STATE(32), 1, + sym_function_signature, + [206] = 2, + ACTIONS(55), 1, + aux_sym_create_function_statement_token3, + ACTIONS(57), 1, + aux_sym_create_function_statement_token4, + [213] = 1, + ACTIONS(59), 2, + aux_sym_create_function_statement_token3, + aux_sym_create_function_statement_token4, + [218] = 2, + ACTIONS(61), 1, + sym_identifier, + STATE(8), 1, + sym__type, + [225] = 1, + ACTIONS(63), 2, + ts_builtin_sym_end, + aux_sym_create_function_statement_token1, + [230] = 2, + ACTIONS(65), 1, + anon_sym_DOLLAR, + ACTIONS(67), 1, + sym_identifier, + [237] = 2, + ACTIONS(69), 1, + anon_sym_LPAREN, + STATE(21), 1, + sym_function_parameters, + [244] = 2, + ACTIONS(34), 1, + anon_sym_DOLLAR, + STATE(49), 1, + sym_dollar_quote, + [251] = 1, + ACTIONS(71), 2, + aux_sym_create_function_statement_token5, + anon_sym_DOLLAR, + [256] = 1, + ACTIONS(73), 2, + aux_sym_create_function_statement_token3, + aux_sym_create_function_statement_token4, + [261] = 2, + ACTIONS(75), 1, + aux_sym_create_function_statement_token3, + ACTIONS(77), 1, + aux_sym_create_function_statement_token4, + [268] = 1, + ACTIONS(79), 2, + aux_sym_create_function_statement_token3, + aux_sym_create_function_statement_token4, + [273] = 1, + ACTIONS(81), 2, + aux_sym_create_function_statement_token5, + anon_sym_DOLLAR, + [278] = 1, + ACTIONS(39), 2, + anon_sym_COMMA, + anon_sym_RPAREN, + [283] = 1, + ACTIONS(83), 1, + anon_sym_RBRACK, + [287] = 1, + ACTIONS(85), 1, + aux_sym_create_function_statement_token2, + [291] = 1, + ACTIONS(87), 1, + aux_sym_create_function_statement_token4, + [295] = 1, + ACTIONS(89), 1, + aux_sym_create_function_statement_token5, + [299] = 1, + ACTIONS(91), 1, + sym_identifier, + [303] = 1, + ACTIONS(93), 1, + sym_identifier, + [307] = 1, + ACTIONS(95), 1, + anon_sym_DOLLAR, + [311] = 1, + ACTIONS(97), 1, + aux_sym_create_function_statement_token2, + [315] = 1, + ACTIONS(99), 1, + aux_sym_create_function_statement_token5, + [319] = 1, + ACTIONS(101), 1, + ts_builtin_sym_end, + [323] = 1, + ACTIONS(103), 1, + sym_identifier, + [327] = 1, + ACTIONS(105), 1, + aux_sym_create_function_statement_token5, + [331] = 1, + ACTIONS(107), 1, + sym_identifier, + [335] = 1, + ACTIONS(109), 1, + aux_sym_create_function_statement_token5, + [339] = 1, + ACTIONS(111), 1, + sym_identifier, + [343] = 1, + ACTIONS(113), 1, + anon_sym_SEMI, + [347] = 1, + ACTIONS(15), 1, + anon_sym_SEMI, + [351] = 1, + ACTIONS(115), 1, + aux_sym_create_function_statement_token5, + [355] = 1, + ACTIONS(117), 1, + anon_sym_REPLACE, + [359] = 1, + ACTIONS(119), 1, + aux_sym_create_function_statement_token4, + [363] = 1, + ACTIONS(121), 1, + sym_identifier, + [367] = 1, + ACTIONS(11), 1, + anon_sym_SEMI, + [371] = 1, + ACTIONS(7), 1, + anon_sym_SEMI, + [375] = 1, + ACTIONS(123), 1, + anon_sym_SEMI, + [379] = 1, + ACTIONS(125), 1, + anon_sym_SEMI, +}; + +static const uint32_t ts_small_parse_table_map[] = { + [SMALL_STATE(2)] = 0, + [SMALL_STATE(3)] = 12, + [SMALL_STATE(4)] = 24, + [SMALL_STATE(5)] = 38, + [SMALL_STATE(6)] = 50, + [SMALL_STATE(7)] = 62, + [SMALL_STATE(8)] = 76, + [SMALL_STATE(9)] = 84, + [SMALL_STATE(10)] = 94, + [SMALL_STATE(11)] = 100, + [SMALL_STATE(12)] = 110, + [SMALL_STATE(13)] = 120, + [SMALL_STATE(14)] = 130, + [SMALL_STATE(15)] = 140, + [SMALL_STATE(16)] = 150, + [SMALL_STATE(17)] = 160, + [SMALL_STATE(18)] = 170, + [SMALL_STATE(19)] = 180, + [SMALL_STATE(20)] = 187, + [SMALL_STATE(21)] = 194, + [SMALL_STATE(22)] = 199, + [SMALL_STATE(23)] = 206, + [SMALL_STATE(24)] = 213, + [SMALL_STATE(25)] = 218, + [SMALL_STATE(26)] = 225, + [SMALL_STATE(27)] = 230, + [SMALL_STATE(28)] = 237, + [SMALL_STATE(29)] = 244, + [SMALL_STATE(30)] = 251, + [SMALL_STATE(31)] = 256, + [SMALL_STATE(32)] = 261, + [SMALL_STATE(33)] = 268, + [SMALL_STATE(34)] = 273, + [SMALL_STATE(35)] = 278, + [SMALL_STATE(36)] = 283, + [SMALL_STATE(37)] = 287, + [SMALL_STATE(38)] = 291, + [SMALL_STATE(39)] = 295, + [SMALL_STATE(40)] = 299, + [SMALL_STATE(41)] = 303, + [SMALL_STATE(42)] = 307, + [SMALL_STATE(43)] = 311, + [SMALL_STATE(44)] = 315, + [SMALL_STATE(45)] = 319, + [SMALL_STATE(46)] = 323, + [SMALL_STATE(47)] = 327, + [SMALL_STATE(48)] = 331, + [SMALL_STATE(49)] = 335, + [SMALL_STATE(50)] = 339, + [SMALL_STATE(51)] = 343, + [SMALL_STATE(52)] = 347, + [SMALL_STATE(53)] = 351, + [SMALL_STATE(54)] = 355, + [SMALL_STATE(55)] = 359, + [SMALL_STATE(56)] = 363, + [SMALL_STATE(57)] = 367, + [SMALL_STATE(58)] = 371, + [SMALL_STATE(59)] = 375, + [SMALL_STATE(60)] = 379, +}; + +static const TSParseActionEntry ts_parse_actions[] = { + [0] = {.entry = {.count = 0, .reusable = false}}, + [1] = {.entry = {.count = 1, .reusable = false}}, RECOVER(), + [3] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_source_file, 0), + [5] = {.entry = {.count = 1, .reusable = true}}, SHIFT(9), + [7] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_create_function_statement, 10), + [9] = {.entry = {.count = 1, .reusable = true}}, SHIFT(51), + [11] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_create_function_statement, 9), + [13] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_source_file, 1), + [15] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_create_function_statement, 8), + [17] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_create_function_statement, 7), + [19] = {.entry = {.count = 1, .reusable = true}}, REDUCE(aux_sym_source_file_repeat1, 2), + [21] = {.entry = {.count = 2, .reusable = true}}, REDUCE(aux_sym_source_file_repeat1, 2), SHIFT_REPEAT(9), + [24] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_function_parameter, 2, .production_id = 1), + [26] = {.entry = {.count = 1, .reusable = true}}, SHIFT(36), + [28] = {.entry = {.count = 1, .reusable = true}}, SHIFT(22), + [30] = {.entry = {.count = 1, .reusable = true}}, SHIFT(54), + [32] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym__type, 3), + [34] = {.entry = {.count = 1, .reusable = true}}, SHIFT(27), + [36] = {.entry = {.count = 2, .reusable = true}}, REDUCE(aux_sym_function_parameters_repeat1, 2), SHIFT_REPEAT(19), + [39] = {.entry = {.count = 1, .reusable = true}}, REDUCE(aux_sym_function_parameters_repeat1, 2), + [41] = {.entry = {.count = 1, .reusable = true}}, SHIFT(19), + [43] = {.entry = {.count = 1, .reusable = true}}, SHIFT(33), + [45] = {.entry = {.count = 1, .reusable = true}}, SHIFT(24), + [47] = {.entry = {.count = 1, .reusable = true}}, SHIFT(25), + [49] = {.entry = {.count = 1, .reusable = true}}, SHIFT(31), + [51] = {.entry = {.count = 1, .reusable = true}}, SHIFT(28), + [53] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_function_signature, 2), + [55] = {.entry = {.count = 1, .reusable = true}}, SHIFT(46), + [57] = {.entry = {.count = 1, .reusable = true}}, SHIFT(16), + [59] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_function_parameters, 2), + [61] = {.entry = {.count = 1, .reusable = true}}, SHIFT(8), + [63] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym__statement, 2), + [65] = {.entry = {.count = 1, .reusable = true}}, SHIFT(34), + [67] = {.entry = {.count = 1, .reusable = true}}, SHIFT(42), + [69] = {.entry = {.count = 1, .reusable = true}}, SHIFT(15), + [71] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_dollar_quote, 3), + [73] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_function_parameters, 3), + [75] = {.entry = {.count = 1, .reusable = true}}, SHIFT(40), + [77] = {.entry = {.count = 1, .reusable = true}}, SHIFT(18), + [79] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_function_parameters, 4), + [81] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_dollar_quote, 2), + [83] = {.entry = {.count = 1, .reusable = true}}, SHIFT(10), + [85] = {.entry = {.count = 1, .reusable = true}}, SHIFT(20), + [87] = {.entry = {.count = 1, .reusable = true}}, SHIFT(11), + [89] = {.entry = {.count = 1, .reusable = true}}, SHIFT(48), + [91] = {.entry = {.count = 1, .reusable = true}}, SHIFT(55), + [93] = {.entry = {.count = 1, .reusable = true}}, SHIFT(6), + [95] = {.entry = {.count = 1, .reusable = true}}, SHIFT(30), + [97] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_or_replace, 2), + [99] = {.entry = {.count = 1, .reusable = true}}, SHIFT(50), + [101] = {.entry = {.count = 1, .reusable = true}}, ACCEPT_INPUT(), + [103] = {.entry = {.count = 1, .reusable = true}}, SHIFT(38), + [105] = {.entry = {.count = 1, .reusable = true}}, SHIFT(41), + [107] = {.entry = {.count = 1, .reusable = true}}, SHIFT(5), + [109] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_block, 2), + [111] = {.entry = {.count = 1, .reusable = true}}, SHIFT(3), + [113] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_function_volatility, 1), + [115] = {.entry = {.count = 1, .reusable = true}}, SHIFT(56), + [117] = {.entry = {.count = 1, .reusable = true}}, SHIFT(43), + [119] = {.entry = {.count = 1, .reusable = true}}, SHIFT(13), + [121] = {.entry = {.count = 1, .reusable = true}}, SHIFT(2), + [123] = {.entry = {.count = 1, .reusable = true}}, SHIFT(26), + [125] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_create_function_statement, 11), +}; + +#ifdef __cplusplus +extern "C" { +#endif +#ifdef _WIN32 +#define extern __declspec(dllexport) +#endif + +extern const TSLanguage *tree_sitter_plpgsql(void) { + static const TSLanguage language = { + .version = LANGUAGE_VERSION, + .symbol_count = SYMBOL_COUNT, + .alias_count = ALIAS_COUNT, + .token_count = TOKEN_COUNT, + .external_token_count = EXTERNAL_TOKEN_COUNT, + .state_count = STATE_COUNT, + .large_state_count = LARGE_STATE_COUNT, + .production_id_count = PRODUCTION_ID_COUNT, + .field_count = FIELD_COUNT, + .max_alias_sequence_length = MAX_ALIAS_SEQUENCE_LENGTH, + .parse_table = &ts_parse_table[0][0], + .small_parse_table = ts_small_parse_table, + .small_parse_table_map = ts_small_parse_table_map, + .parse_actions = ts_parse_actions, + .symbol_names = ts_symbol_names, + .field_names = ts_field_names, + .field_map_slices = ts_field_map_slices, + .field_map_entries = ts_field_map_entries, + .symbol_metadata = ts_symbol_metadata, + .public_symbol_map = ts_symbol_map, + .alias_map = ts_non_terminal_alias_map, + .alias_sequences = &ts_alias_sequences[0][0], + .lex_modes = ts_lex_modes, + .lex_fn = ts_lex, + }; + return &language; +} +#ifdef __cplusplus +} +#endif diff --git a/src/tree_sitter/parser.h b/src/tree_sitter/parser.h new file mode 100644 index 0000000..cbbc7b4 --- /dev/null +++ b/src/tree_sitter/parser.h @@ -0,0 +1,223 @@ +#ifndef TREE_SITTER_PARSER_H_ +#define TREE_SITTER_PARSER_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +#define ts_builtin_sym_error ((TSSymbol)-1) +#define ts_builtin_sym_end 0 +#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024 + +typedef uint16_t TSStateId; + +#ifndef TREE_SITTER_API_H_ +typedef uint16_t TSSymbol; +typedef uint16_t TSFieldId; +typedef struct TSLanguage TSLanguage; +#endif + +typedef struct { + TSFieldId field_id; + uint8_t child_index; + bool inherited; +} TSFieldMapEntry; + +typedef struct { + uint16_t index; + uint16_t length; +} TSFieldMapSlice; + +typedef struct { + bool visible; + bool named; + bool supertype; +} TSSymbolMetadata; + +typedef struct TSLexer TSLexer; + +struct TSLexer { + int32_t lookahead; + TSSymbol result_symbol; + void (*advance)(TSLexer *, bool); + void (*mark_end)(TSLexer *); + uint32_t (*get_column)(TSLexer *); + bool (*is_at_included_range_start)(const TSLexer *); + bool (*eof)(const TSLexer *); +}; + +typedef enum { + TSParseActionTypeShift, + TSParseActionTypeReduce, + TSParseActionTypeAccept, + TSParseActionTypeRecover, +} TSParseActionType; + +typedef union { + struct { + uint8_t type; + TSStateId state; + bool extra; + bool repetition; + } shift; + struct { + uint8_t type; + uint8_t child_count; + TSSymbol symbol; + int16_t dynamic_precedence; + uint16_t production_id; + } reduce; + uint8_t type; +} TSParseAction; + +typedef struct { + uint16_t lex_state; + uint16_t external_lex_state; +} TSLexMode; + +typedef union { + TSParseAction action; + struct { + uint8_t count; + bool reusable; + } entry; +} TSParseActionEntry; + +struct TSLanguage { + uint32_t version; + uint32_t symbol_count; + uint32_t alias_count; + uint32_t token_count; + uint32_t external_token_count; + uint32_t state_count; + uint32_t large_state_count; + uint32_t production_id_count; + uint32_t field_count; + uint16_t max_alias_sequence_length; + const uint16_t *parse_table; + const uint16_t *small_parse_table; + const uint32_t *small_parse_table_map; + const TSParseActionEntry *parse_actions; + const char * const *symbol_names; + const char * const *field_names; + const TSFieldMapSlice *field_map_slices; + const TSFieldMapEntry *field_map_entries; + const TSSymbolMetadata *symbol_metadata; + const TSSymbol *public_symbol_map; + const uint16_t *alias_map; + const TSSymbol *alias_sequences; + const TSLexMode *lex_modes; + bool (*lex_fn)(TSLexer *, TSStateId); + bool (*keyword_lex_fn)(TSLexer *, TSStateId); + TSSymbol keyword_capture_token; + struct { + const bool *states; + const TSSymbol *symbol_map; + void *(*create)(void); + void (*destroy)(void *); + bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist); + unsigned (*serialize)(void *, char *); + void (*deserialize)(void *, const char *, unsigned); + } external_scanner; +}; + +/* + * Lexer Macros + */ + +#define START_LEXER() \ + bool result = false; \ + bool skip = false; \ + bool eof = false; \ + int32_t lookahead; \ + goto start; \ + next_state: \ + lexer->advance(lexer, skip); \ + start: \ + skip = false; \ + lookahead = lexer->lookahead; + +#define ADVANCE(state_value) \ + { \ + state = state_value; \ + goto next_state; \ + } + +#define SKIP(state_value) \ + { \ + skip = true; \ + state = state_value; \ + goto next_state; \ + } + +#define ACCEPT_TOKEN(symbol_value) \ + result = true; \ + lexer->result_symbol = symbol_value; \ + lexer->mark_end(lexer); + +#define END_STATE() return result; + +/* + * Parse Table Macros + */ + +#define SMALL_STATE(id) id - LARGE_STATE_COUNT + +#define STATE(id) id + +#define ACTIONS(id) id + +#define SHIFT(state_value) \ + {{ \ + .shift = { \ + .type = TSParseActionTypeShift, \ + .state = state_value \ + } \ + }} + +#define SHIFT_REPEAT(state_value) \ + {{ \ + .shift = { \ + .type = TSParseActionTypeShift, \ + .state = state_value, \ + .repetition = true \ + } \ + }} + +#define SHIFT_EXTRA() \ + {{ \ + .shift = { \ + .type = TSParseActionTypeShift, \ + .extra = true \ + } \ + }} + +#define REDUCE(symbol_val, child_count_val, ...) \ + {{ \ + .reduce = { \ + .type = TSParseActionTypeReduce, \ + .symbol = symbol_val, \ + .child_count = child_count_val, \ + __VA_ARGS__ \ + }, \ + }} + +#define RECOVER() \ + {{ \ + .type = TSParseActionTypeRecover \ + }} + +#define ACCEPT_INPUT() \ + {{ \ + .type = TSParseActionTypeAccept \ + }} + +#ifdef __cplusplus +} +#endif + +#endif // TREE_SITTER_PARSER_H_