create_function

This commit is contained in:
Christian De la Hoz 2021-08-21 19:15:42 +02:00
commit 78f290713f
20 changed files with 2464 additions and 0 deletions

1
.envrc Normal file
View file

@ -0,0 +1 @@
eval "$(lorri direnv)"

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
node_modules

26
Cargo.toml Normal file
View file

@ -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"

14
Makefile Normal file
View file

@ -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

19
binding.gyp Normal file
View file

@ -0,0 +1,19 @@
{
"targets": [
{
"target_name": "tree_sitter_YOUR_LANGUAGE_NAME_binding",
"include_dirs": [
"<!(node -e \"require('nan')\")",
"src"
],
"sources": [
"bindings/node/binding.cc",
"src/parser.c",
# If your language uses an external scanner, add it here.
],
"cflags_c": [
"-std=c99",
]
}
]
}

28
bindings/node/binding.cc Normal file
View file

@ -0,0 +1,28 @@
#include "tree_sitter/parser.h"
#include <node.h>
#include "nan.h"
using namespace v8;
extern "C" TSLanguage * tree_sitter_YOUR_LANGUAGE_NAME();
namespace {
NAN_METHOD(New) {}
void Init(Local<Object> exports, Local<Object> module) {
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
tpl->SetClassName(Nan::New("Language").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
Local<Function> constructor = Nan::GetFunction(tpl).ToLocalChecked();
Local<Object> 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

19
bindings/node/index.js Normal file
View file

@ -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 (_) {}

40
bindings/rust/build.rs Normal file
View file

@ -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());
*/
}

52
bindings/rust/lib.rs Normal file
View file

@ -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");
}
}

View file

@ -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)))

View file

@ -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))

View file

@ -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)))

83
grammar.js Normal file
View file

@ -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_]*/,
}
});

18
package-lock.json generated Normal file
View file

@ -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=="
}
}
}

15
package.json Normal file
View file

@ -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"
}
}

13
shell.nix Normal file
View file

@ -0,0 +1,13 @@
let
pkgs = import <nixpkgs> { };
# 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";
}

347
src/grammar.json Normal file
View file

@ -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": []
}

228
src/node-types.json Normal file
View file

@ -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
}
]

1097
src/parser.c Normal file

File diff suppressed because it is too large Load diff

223
src/tree_sitter/parser.h Normal file
View file

@ -0,0 +1,223 @@
#ifndef TREE_SITTER_PARSER_H_
#define TREE_SITTER_PARSER_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#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_