diff --git a/.gitignore b/.gitignore index 3450c4b..62e6d54 100644 --- a/.gitignore +++ b/.gitignore @@ -116,10 +116,6 @@ dist .pnp.* # Tree sitter generated files -bindings/** -src/** -binding.gyp -Cargo.toml parser.exp parser.lib parser.obj diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..63360e6 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "tree-sitter-CMake" +description = "CMake grammar for the tree-sitter parsing library" +version = "0.0.1" +keywords = ["incremental", "parsing", "CMake"] +categories = ["parsing", "text-editors"] +repository = "https://github.com/tree-sitter/tree-sitter-javascript" +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.17" + +[build-dependencies] +cc = "1.0" diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..e69de29 diff --git a/binding.gyp b/binding.gyp new file mode 100644 index 0000000..a2beb8a --- /dev/null +++ b/binding.gyp @@ -0,0 +1,19 @@ +{ + "targets": [ + { + "target_name": "tree_sitter_CMake_binding", + "include_dirs": [ + " +#include "nan.h" + +using namespace v8; + +extern "C" TSLanguage * tree_sitter_CMake(); + +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_CMake()); + + Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("CMake").ToLocalChecked()); + Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance); +} + +NODE_MODULE(tree_sitter_CMake_binding, Init) + +} // namespace diff --git a/bindings/node/index.js b/bindings/node/index.js new file mode 100644 index 0000000..cd60779 --- /dev/null +++ b/bindings/node/index.js @@ -0,0 +1,19 @@ +try { + module.exports = require("../../build/Release/tree_sitter_CMake_binding"); +} catch (error1) { + if (error1.code !== 'MODULE_NOT_FOUND') { + throw error1; + } + try { + module.exports = require("../../build/Debug/tree_sitter_CMake_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..9e794b2 --- /dev/null +++ b/bindings/rust/lib.rs @@ -0,0 +1,52 @@ +//! This crate provides CMake 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_CMake::language()).expect("Error loading CMake 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_CMake() -> 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_CMake() } +} + +/// 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 CMake language"); + } +} diff --git a/src/grammar.json b/src/grammar.json new file mode 100644 index 0000000..c1329bf --- /dev/null +++ b/src/grammar.json @@ -0,0 +1,160 @@ +{ + "name": "CMake", + "rules": { + "source_file": { + "type": "REPEAT", + "content": { + "type": "SYMBOL", + "name": "command_invocation" + } + }, + "line_ending": { + "type": "SYMBOL", + "name": "newline" + }, + "space": { + "type": "PATTERN", + "value": "[ \\t]+" + }, + "newline": { + "type": "PATTERN", + "value": "\\n" + }, + "identifier": { + "type": "PATTERN", + "value": "[A-Za-z_][A-Za-z0-9_]*" + }, + "unquoted_argument": { + "type": "PATTERN", + "value": "[^ ()#\\\"\\\\]+" + }, + "argument": { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "unquoted_argument" + } + ] + }, + "seperation": { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "space" + }, + { + "type": "SYMBOL", + "name": "line_ending" + } + ] + }, + "arguments": { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "argument" + }, + { + "type": "REPEAT", + "content": { + "type": "SYMBOL", + "name": "_seperated_arguments" + } + } + ] + }, + "_seperated_arguments": { + "type": "PREC_LEFT", + "value": 1, + "content": { + "type": "SEQ", + "members": [ + { + "type": "REPEAT1", + "content": { + "type": "SYMBOL", + "name": "seperation" + } + }, + { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "argument" + }, + { + "type": "BLANK" + } + ] + } + ] + } + }, + "command_invocation": { + "type": "SEQ", + "members": [ + { + "type": "REPEAT", + "content": { + "type": "SYMBOL", + "name": "space" + } + }, + { + "type": "SYMBOL", + "name": "identifier" + }, + { + "type": "REPEAT", + "content": { + "type": "SYMBOL", + "name": "space" + } + }, + { + "type": "STRING", + "value": "(" + }, + { + "type": "REPEAT", + "content": { + "type": "SYMBOL", + "name": "seperation" + } + }, + { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "arguments" + }, + { + "type": "BLANK" + } + ] + }, + { + "type": "STRING", + "value": ")" + } + ] + } + }, + "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..2a3e28c --- /dev/null +++ b/src/node-types.json @@ -0,0 +1,136 @@ +[ + { + "type": "argument", + "named": true, + "fields": {}, + "children": { + "multiple": false, + "required": true, + "types": [ + { + "type": "unquoted_argument", + "named": true + } + ] + } + }, + { + "type": "arguments", + "named": true, + "fields": {}, + "children": { + "multiple": true, + "required": true, + "types": [ + { + "type": "argument", + "named": true + }, + { + "type": "seperation", + "named": true + } + ] + } + }, + { + "type": "command_invocation", + "named": true, + "fields": {}, + "children": { + "multiple": true, + "required": true, + "types": [ + { + "type": "arguments", + "named": true + }, + { + "type": "identifier", + "named": true + }, + { + "type": "seperation", + "named": true + }, + { + "type": "space", + "named": true + } + ] + } + }, + { + "type": "line_ending", + "named": true, + "fields": {}, + "children": { + "multiple": false, + "required": true, + "types": [ + { + "type": "newline", + "named": true + } + ] + } + }, + { + "type": "seperation", + "named": true, + "fields": {}, + "children": { + "multiple": false, + "required": true, + "types": [ + { + "type": "line_ending", + "named": true + }, + { + "type": "space", + "named": true + } + ] + } + }, + { + "type": "source_file", + "named": true, + "fields": {}, + "children": { + "multiple": true, + "required": false, + "types": [ + { + "type": "command_invocation", + "named": true + } + ] + } + }, + { + "type": "(", + "named": false + }, + { + "type": ")", + "named": false + }, + { + "type": "identifier", + "named": true + }, + { + "type": "newline", + "named": true + }, + { + "type": "space", + "named": true + }, + { + "type": "unquoted_argument", + "named": true + } +] \ No newline at end of file diff --git a/src/parser.c b/src/parser.c new file mode 100644 index 0000000..64e5b2c --- /dev/null +++ b/src/parser.c @@ -0,0 +1,782 @@ +#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 35 +#define LARGE_STATE_COUNT 2 +#define SYMBOL_COUNT 18 +#define ALIAS_COUNT 0 +#define TOKEN_COUNT 7 +#define EXTERNAL_TOKEN_COUNT 0 +#define FIELD_COUNT 0 +#define MAX_ALIAS_SEQUENCE_LENGTH 7 +#define PRODUCTION_ID_COUNT 1 + +enum { + sym_space = 1, + sym_newline = 2, + sym_identifier = 3, + sym_unquoted_argument = 4, + anon_sym_LPAREN = 5, + anon_sym_RPAREN = 6, + sym_source_file = 7, + sym_line_ending = 8, + sym_argument = 9, + sym_seperation = 10, + sym_arguments = 11, + sym__seperated_arguments = 12, + sym_command_invocation = 13, + aux_sym_source_file_repeat1 = 14, + aux_sym_arguments_repeat1 = 15, + aux_sym__seperated_arguments_repeat1 = 16, + aux_sym_command_invocation_repeat1 = 17, +}; + +static const char *ts_symbol_names[] = { + [ts_builtin_sym_end] = "end", + [sym_space] = "space", + [sym_newline] = "newline", + [sym_identifier] = "identifier", + [sym_unquoted_argument] = "unquoted_argument", + [anon_sym_LPAREN] = "(", + [anon_sym_RPAREN] = ")", + [sym_source_file] = "source_file", + [sym_line_ending] = "line_ending", + [sym_argument] = "argument", + [sym_seperation] = "seperation", + [sym_arguments] = "arguments", + [sym__seperated_arguments] = "_seperated_arguments", + [sym_command_invocation] = "command_invocation", + [aux_sym_source_file_repeat1] = "source_file_repeat1", + [aux_sym_arguments_repeat1] = "arguments_repeat1", + [aux_sym__seperated_arguments_repeat1] = "_seperated_arguments_repeat1", + [aux_sym_command_invocation_repeat1] = "command_invocation_repeat1", +}; + +static TSSymbol ts_symbol_map[] = { + [ts_builtin_sym_end] = ts_builtin_sym_end, + [sym_space] = sym_space, + [sym_newline] = sym_newline, + [sym_identifier] = sym_identifier, + [sym_unquoted_argument] = sym_unquoted_argument, + [anon_sym_LPAREN] = anon_sym_LPAREN, + [anon_sym_RPAREN] = anon_sym_RPAREN, + [sym_source_file] = sym_source_file, + [sym_line_ending] = sym_line_ending, + [sym_argument] = sym_argument, + [sym_seperation] = sym_seperation, + [sym_arguments] = sym_arguments, + [sym__seperated_arguments] = sym__seperated_arguments, + [sym_command_invocation] = sym_command_invocation, + [aux_sym_source_file_repeat1] = aux_sym_source_file_repeat1, + [aux_sym_arguments_repeat1] = aux_sym_arguments_repeat1, + [aux_sym__seperated_arguments_repeat1] = aux_sym__seperated_arguments_repeat1, + [aux_sym_command_invocation_repeat1] = aux_sym_command_invocation_repeat1, +}; + +static const TSSymbolMetadata ts_symbol_metadata[] = { + [ts_builtin_sym_end] = { + .visible = false, + .named = true, + }, + [sym_space] = { + .visible = true, + .named = true, + }, + [sym_newline] = { + .visible = true, + .named = true, + }, + [sym_identifier] = { + .visible = true, + .named = true, + }, + [sym_unquoted_argument] = { + .visible = true, + .named = true, + }, + [anon_sym_LPAREN] = { + .visible = true, + .named = false, + }, + [anon_sym_RPAREN] = { + .visible = true, + .named = false, + }, + [sym_source_file] = { + .visible = true, + .named = true, + }, + [sym_line_ending] = { + .visible = true, + .named = true, + }, + [sym_argument] = { + .visible = true, + .named = true, + }, + [sym_seperation] = { + .visible = true, + .named = true, + }, + [sym_arguments] = { + .visible = true, + .named = true, + }, + [sym__seperated_arguments] = { + .visible = false, + .named = true, + }, + [sym_command_invocation] = { + .visible = true, + .named = true, + }, + [aux_sym_source_file_repeat1] = { + .visible = false, + .named = false, + }, + [aux_sym_arguments_repeat1] = { + .visible = false, + .named = false, + }, + [aux_sym__seperated_arguments_repeat1] = { + .visible = false, + .named = false, + }, + [aux_sym_command_invocation_repeat1] = { + .visible = false, + .named = false, + }, +}; + +static TSSymbol ts_alias_sequences[PRODUCTION_ID_COUNT][MAX_ALIAS_SEQUENCE_LENGTH] = { + [0] = {0}, +}; + +static 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(4); + if (lookahead == '(') ADVANCE(13); + if (lookahead == ')') ADVANCE(14); + if (lookahead == '\t' || + lookahead == ' ') ADVANCE(7); + if (lookahead == '\n' || + lookahead == '\r') SKIP(0) + if (('A' <= lookahead && lookahead <= 'Z') || + lookahead == '_' || + ('a' <= lookahead && lookahead <= 'z')) ADVANCE(10); + END_STATE(); + case 1: + if (lookahead == '\t') ADVANCE(5); + if (lookahead == '\n') ADVANCE(8); + if (lookahead == '\r') ADVANCE(11); + if (lookahead == ' ') ADVANCE(5); + if (lookahead == ')') ADVANCE(14); + if (lookahead != 0 && + lookahead != '"' && + lookahead != '#' && + lookahead != '(' && + lookahead != '\\') ADVANCE(12); + END_STATE(); + case 2: + if (lookahead == '\n') ADVANCE(9); + if (lookahead == '\r') SKIP(2) + if (lookahead == ')') ADVANCE(14); + if (lookahead == '\t' || + lookahead == ' ') ADVANCE(6); + END_STATE(); + case 3: + if (lookahead == ')') ADVANCE(14); + if (lookahead == '\t' || + lookahead == '\n' || + lookahead == '\r' || + lookahead == ' ') SKIP(3) + END_STATE(); + case 4: + ACCEPT_TOKEN(ts_builtin_sym_end); + END_STATE(); + case 5: + ACCEPT_TOKEN(sym_space); + if (lookahead == '\t') ADVANCE(5); + if (lookahead == '\n') ADVANCE(8); + if (lookahead == '\r') ADVANCE(11); + if (lookahead == ' ') ADVANCE(5); + END_STATE(); + case 6: + ACCEPT_TOKEN(sym_space); + if (lookahead == '\n') ADVANCE(9); + if (lookahead == '\t' || + lookahead == ' ') ADVANCE(6); + END_STATE(); + case 7: + ACCEPT_TOKEN(sym_space); + if (lookahead == '\t' || + lookahead == ' ') ADVANCE(7); + END_STATE(); + case 8: + ACCEPT_TOKEN(sym_newline); + if (lookahead == '\t') ADVANCE(5); + if (lookahead == '\n') ADVANCE(8); + if (lookahead == '\r') ADVANCE(11); + if (lookahead == ' ') ADVANCE(5); + END_STATE(); + case 9: + ACCEPT_TOKEN(sym_newline); + if (lookahead == '\n') ADVANCE(9); + if (lookahead == '\t' || + lookahead == ' ') ADVANCE(6); + END_STATE(); + case 10: + ACCEPT_TOKEN(sym_identifier); + if (('0' <= lookahead && lookahead <= '9') || + ('A' <= lookahead && lookahead <= 'Z') || + lookahead == '_' || + ('a' <= lookahead && lookahead <= 'z')) ADVANCE(10); + END_STATE(); + case 11: + ACCEPT_TOKEN(sym_unquoted_argument); + if (lookahead == '\t') ADVANCE(5); + if (lookahead == '\n') ADVANCE(8); + if (lookahead == '\r') ADVANCE(11); + if (lookahead == ' ') ADVANCE(5); + if (lookahead != 0 && + lookahead != '"' && + lookahead != '#' && + lookahead != '(' && + lookahead != ')' && + lookahead != '\\') ADVANCE(12); + END_STATE(); + case 12: + ACCEPT_TOKEN(sym_unquoted_argument); + if (lookahead != 0 && + lookahead != ' ' && + lookahead != '"' && + lookahead != '#' && + lookahead != '(' && + lookahead != ')' && + lookahead != '\\') ADVANCE(12); + END_STATE(); + case 13: + ACCEPT_TOKEN(anon_sym_LPAREN); + END_STATE(); + case 14: + ACCEPT_TOKEN(anon_sym_RPAREN); + END_STATE(); + default: + return false; + } +} + +static TSLexMode ts_lex_modes[STATE_COUNT] = { + [0] = {.lex_state = 0}, + [1] = {.lex_state = 0}, + [2] = {.lex_state = 1}, + [3] = {.lex_state = 1}, + [4] = {.lex_state = 1}, + [5] = {.lex_state = 1}, + [6] = {.lex_state = 1}, + [7] = {.lex_state = 1}, + [8] = {.lex_state = 2}, + [9] = {.lex_state = 2}, + [10] = {.lex_state = 1}, + [11] = {.lex_state = 2}, + [12] = {.lex_state = 1}, + [13] = {.lex_state = 0}, + [14] = {.lex_state = 0}, + [15] = {.lex_state = 1}, + [16] = {.lex_state = 0}, + [17] = {.lex_state = 1}, + [18] = {.lex_state = 0}, + [19] = {.lex_state = 0}, + [20] = {.lex_state = 0}, + [21] = {.lex_state = 0}, + [22] = {.lex_state = 0}, + [23] = {.lex_state = 2}, + [24] = {.lex_state = 0}, + [25] = {.lex_state = 0}, + [26] = {.lex_state = 2}, + [27] = {.lex_state = 0}, + [28] = {.lex_state = 0}, + [29] = {.lex_state = 0}, + [30] = {.lex_state = 3}, + [31] = {.lex_state = 3}, + [32] = {.lex_state = 3}, + [33] = {.lex_state = 0}, + [34] = {.lex_state = 3}, +}; + +static uint16_t ts_parse_table[LARGE_STATE_COUNT][SYMBOL_COUNT] = { + [0] = { + [ts_builtin_sym_end] = ACTIONS(1), + [sym_space] = ACTIONS(1), + [sym_identifier] = ACTIONS(1), + [anon_sym_LPAREN] = ACTIONS(1), + [anon_sym_RPAREN] = ACTIONS(1), + }, + [1] = { + [sym_source_file] = STATE(33), + [sym_command_invocation] = STATE(14), + [aux_sym_source_file_repeat1] = STATE(14), + [aux_sym_command_invocation_repeat1] = STATE(25), + [ts_builtin_sym_end] = ACTIONS(3), + [sym_space] = ACTIONS(5), + [sym_identifier] = ACTIONS(7), + }, +}; + +static uint16_t ts_small_parse_table[] = { + [0] = 8, + ACTIONS(9), 1, + sym_space, + ACTIONS(11), 1, + sym_newline, + ACTIONS(13), 1, + sym_unquoted_argument, + ACTIONS(15), 1, + anon_sym_RPAREN, + STATE(8), 1, + sym_argument, + STATE(17), 1, + sym_line_ending, + STATE(34), 1, + sym_arguments, + STATE(12), 2, + sym_seperation, + aux_sym__seperated_arguments_repeat1, + [26] = 8, + ACTIONS(9), 1, + sym_space, + ACTIONS(11), 1, + sym_newline, + ACTIONS(13), 1, + sym_unquoted_argument, + ACTIONS(17), 1, + anon_sym_RPAREN, + STATE(8), 1, + sym_argument, + STATE(17), 1, + sym_line_ending, + STATE(32), 1, + sym_arguments, + STATE(2), 2, + sym_seperation, + aux_sym__seperated_arguments_repeat1, + [52] = 8, + ACTIONS(9), 1, + sym_space, + ACTIONS(11), 1, + sym_newline, + ACTIONS(13), 1, + sym_unquoted_argument, + ACTIONS(17), 1, + anon_sym_RPAREN, + STATE(8), 1, + sym_argument, + STATE(17), 1, + sym_line_ending, + STATE(32), 1, + sym_arguments, + STATE(12), 2, + sym_seperation, + aux_sym__seperated_arguments_repeat1, + [78] = 8, + ACTIONS(9), 1, + sym_space, + ACTIONS(11), 1, + sym_newline, + ACTIONS(13), 1, + sym_unquoted_argument, + ACTIONS(19), 1, + anon_sym_RPAREN, + STATE(8), 1, + sym_argument, + STATE(17), 1, + sym_line_ending, + STATE(31), 1, + sym_arguments, + STATE(7), 2, + sym_seperation, + aux_sym__seperated_arguments_repeat1, + [104] = 8, + ACTIONS(9), 1, + sym_space, + ACTIONS(11), 1, + sym_newline, + ACTIONS(13), 1, + sym_unquoted_argument, + ACTIONS(21), 1, + anon_sym_RPAREN, + STATE(8), 1, + sym_argument, + STATE(17), 1, + sym_line_ending, + STATE(30), 1, + sym_arguments, + STATE(4), 2, + sym_seperation, + aux_sym__seperated_arguments_repeat1, + [130] = 8, + ACTIONS(9), 1, + sym_space, + ACTIONS(11), 1, + sym_newline, + ACTIONS(13), 1, + sym_unquoted_argument, + ACTIONS(21), 1, + anon_sym_RPAREN, + STATE(8), 1, + sym_argument, + STATE(17), 1, + sym_line_ending, + STATE(30), 1, + sym_arguments, + STATE(12), 2, + sym_seperation, + aux_sym__seperated_arguments_repeat1, + [156] = 6, + ACTIONS(9), 1, + sym_space, + ACTIONS(11), 1, + sym_newline, + ACTIONS(23), 1, + anon_sym_RPAREN, + STATE(17), 1, + sym_line_ending, + STATE(10), 2, + sym_seperation, + aux_sym__seperated_arguments_repeat1, + STATE(11), 2, + sym__seperated_arguments, + aux_sym_arguments_repeat1, + [177] = 6, + ACTIONS(25), 1, + sym_space, + ACTIONS(28), 1, + sym_newline, + ACTIONS(31), 1, + anon_sym_RPAREN, + STATE(17), 1, + sym_line_ending, + STATE(9), 2, + sym__seperated_arguments, + aux_sym_arguments_repeat1, + STATE(10), 2, + sym_seperation, + aux_sym__seperated_arguments_repeat1, + [198] = 5, + ACTIONS(13), 1, + sym_unquoted_argument, + STATE(17), 1, + sym_line_ending, + STATE(26), 1, + sym_argument, + STATE(12), 2, + sym_seperation, + aux_sym__seperated_arguments_repeat1, + ACTIONS(33), 3, + sym_space, + sym_newline, + anon_sym_RPAREN, + [217] = 6, + ACTIONS(9), 1, + sym_space, + ACTIONS(11), 1, + sym_newline, + ACTIONS(35), 1, + anon_sym_RPAREN, + STATE(17), 1, + sym_line_ending, + STATE(9), 2, + sym__seperated_arguments, + aux_sym_arguments_repeat1, + STATE(10), 2, + sym_seperation, + aux_sym__seperated_arguments_repeat1, + [238] = 5, + ACTIONS(37), 1, + sym_space, + ACTIONS(40), 1, + sym_newline, + STATE(17), 1, + sym_line_ending, + ACTIONS(43), 2, + sym_unquoted_argument, + anon_sym_RPAREN, + STATE(12), 2, + sym_seperation, + aux_sym__seperated_arguments_repeat1, + [256] = 5, + ACTIONS(45), 1, + ts_builtin_sym_end, + ACTIONS(47), 1, + sym_space, + ACTIONS(50), 1, + sym_identifier, + STATE(25), 1, + aux_sym_command_invocation_repeat1, + STATE(13), 2, + sym_command_invocation, + aux_sym_source_file_repeat1, + [273] = 5, + ACTIONS(5), 1, + sym_space, + ACTIONS(7), 1, + sym_identifier, + ACTIONS(53), 1, + ts_builtin_sym_end, + STATE(25), 1, + aux_sym_command_invocation_repeat1, + STATE(13), 2, + sym_command_invocation, + aux_sym_source_file_repeat1, + [290] = 1, + ACTIONS(55), 4, + sym_space, + sym_newline, + sym_unquoted_argument, + anon_sym_RPAREN, + [297] = 3, + ACTIONS(57), 1, + sym_space, + STATE(16), 1, + aux_sym_command_invocation_repeat1, + ACTIONS(60), 2, + sym_identifier, + anon_sym_LPAREN, + [308] = 1, + ACTIONS(62), 4, + sym_space, + sym_newline, + sym_unquoted_argument, + anon_sym_RPAREN, + [315] = 3, + ACTIONS(64), 1, + sym_space, + ACTIONS(66), 1, + anon_sym_LPAREN, + STATE(20), 1, + aux_sym_command_invocation_repeat1, + [325] = 3, + ACTIONS(68), 1, + sym_space, + ACTIONS(70), 1, + anon_sym_LPAREN, + STATE(24), 1, + aux_sym_command_invocation_repeat1, + [335] = 3, + ACTIONS(72), 1, + sym_space, + ACTIONS(74), 1, + anon_sym_LPAREN, + STATE(16), 1, + aux_sym_command_invocation_repeat1, + [345] = 2, + ACTIONS(78), 1, + sym_identifier, + ACTIONS(76), 2, + ts_builtin_sym_end, + sym_space, + [353] = 2, + ACTIONS(82), 1, + sym_identifier, + ACTIONS(80), 2, + ts_builtin_sym_end, + sym_space, + [361] = 1, + ACTIONS(84), 3, + sym_space, + sym_newline, + anon_sym_RPAREN, + [367] = 3, + ACTIONS(66), 1, + anon_sym_LPAREN, + ACTIONS(72), 1, + sym_space, + STATE(16), 1, + aux_sym_command_invocation_repeat1, + [377] = 3, + ACTIONS(72), 1, + sym_space, + ACTIONS(86), 1, + sym_identifier, + STATE(16), 1, + aux_sym_command_invocation_repeat1, + [387] = 1, + ACTIONS(88), 3, + sym_space, + sym_newline, + anon_sym_RPAREN, + [393] = 2, + ACTIONS(92), 1, + sym_identifier, + ACTIONS(90), 2, + ts_builtin_sym_end, + sym_space, + [401] = 2, + ACTIONS(96), 1, + sym_identifier, + ACTIONS(94), 2, + ts_builtin_sym_end, + sym_space, + [409] = 2, + ACTIONS(100), 1, + sym_identifier, + ACTIONS(98), 2, + ts_builtin_sym_end, + sym_space, + [417] = 1, + ACTIONS(102), 1, + anon_sym_RPAREN, + [421] = 1, + ACTIONS(104), 1, + anon_sym_RPAREN, + [425] = 1, + ACTIONS(106), 1, + anon_sym_RPAREN, + [429] = 1, + ACTIONS(108), 1, + ts_builtin_sym_end, + [433] = 1, + ACTIONS(110), 1, + anon_sym_RPAREN, +}; + +static uint32_t ts_small_parse_table_map[] = { + [SMALL_STATE(2)] = 0, + [SMALL_STATE(3)] = 26, + [SMALL_STATE(4)] = 52, + [SMALL_STATE(5)] = 78, + [SMALL_STATE(6)] = 104, + [SMALL_STATE(7)] = 130, + [SMALL_STATE(8)] = 156, + [SMALL_STATE(9)] = 177, + [SMALL_STATE(10)] = 198, + [SMALL_STATE(11)] = 217, + [SMALL_STATE(12)] = 238, + [SMALL_STATE(13)] = 256, + [SMALL_STATE(14)] = 273, + [SMALL_STATE(15)] = 290, + [SMALL_STATE(16)] = 297, + [SMALL_STATE(17)] = 308, + [SMALL_STATE(18)] = 315, + [SMALL_STATE(19)] = 325, + [SMALL_STATE(20)] = 335, + [SMALL_STATE(21)] = 345, + [SMALL_STATE(22)] = 353, + [SMALL_STATE(23)] = 361, + [SMALL_STATE(24)] = 367, + [SMALL_STATE(25)] = 377, + [SMALL_STATE(26)] = 387, + [SMALL_STATE(27)] = 393, + [SMALL_STATE(28)] = 401, + [SMALL_STATE(29)] = 409, + [SMALL_STATE(30)] = 417, + [SMALL_STATE(31)] = 421, + [SMALL_STATE(32)] = 425, + [SMALL_STATE(33)] = 429, + [SMALL_STATE(34)] = 433, +}; + +static 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(25), + [7] = {.entry = {.count = 1, .reusable = false}}, SHIFT(19), + [9] = {.entry = {.count = 1, .reusable = false}}, SHIFT(17), + [11] = {.entry = {.count = 1, .reusable = false}}, SHIFT(15), + [13] = {.entry = {.count = 1, .reusable = false}}, SHIFT(23), + [15] = {.entry = {.count = 1, .reusable = false}}, SHIFT(28), + [17] = {.entry = {.count = 1, .reusable = false}}, SHIFT(27), + [19] = {.entry = {.count = 1, .reusable = false}}, SHIFT(21), + [21] = {.entry = {.count = 1, .reusable = false}}, SHIFT(22), + [23] = {.entry = {.count = 1, .reusable = false}}, REDUCE(sym_arguments, 1), + [25] = {.entry = {.count = 2, .reusable = false}}, REDUCE(aux_sym_arguments_repeat1, 2), SHIFT_REPEAT(17), + [28] = {.entry = {.count = 2, .reusable = false}}, REDUCE(aux_sym_arguments_repeat1, 2), SHIFT_REPEAT(15), + [31] = {.entry = {.count = 1, .reusable = false}}, REDUCE(aux_sym_arguments_repeat1, 2), + [33] = {.entry = {.count = 1, .reusable = false}}, REDUCE(sym__seperated_arguments, 1), + [35] = {.entry = {.count = 1, .reusable = false}}, REDUCE(sym_arguments, 2), + [37] = {.entry = {.count = 2, .reusable = false}}, REDUCE(aux_sym__seperated_arguments_repeat1, 2), SHIFT_REPEAT(17), + [40] = {.entry = {.count = 2, .reusable = false}}, REDUCE(aux_sym__seperated_arguments_repeat1, 2), SHIFT_REPEAT(15), + [43] = {.entry = {.count = 1, .reusable = false}}, REDUCE(aux_sym__seperated_arguments_repeat1, 2), + [45] = {.entry = {.count = 1, .reusable = true}}, REDUCE(aux_sym_source_file_repeat1, 2), + [47] = {.entry = {.count = 2, .reusable = true}}, REDUCE(aux_sym_source_file_repeat1, 2), SHIFT_REPEAT(25), + [50] = {.entry = {.count = 2, .reusable = false}}, REDUCE(aux_sym_source_file_repeat1, 2), SHIFT_REPEAT(19), + [53] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_source_file, 1), + [55] = {.entry = {.count = 1, .reusable = false}}, REDUCE(sym_line_ending, 1), + [57] = {.entry = {.count = 2, .reusable = true}}, REDUCE(aux_sym_command_invocation_repeat1, 2), SHIFT_REPEAT(16), + [60] = {.entry = {.count = 1, .reusable = false}}, REDUCE(aux_sym_command_invocation_repeat1, 2), + [62] = {.entry = {.count = 1, .reusable = false}}, REDUCE(sym_seperation, 1), + [64] = {.entry = {.count = 1, .reusable = true}}, SHIFT(20), + [66] = {.entry = {.count = 1, .reusable = false}}, SHIFT(6), + [68] = {.entry = {.count = 1, .reusable = true}}, SHIFT(24), + [70] = {.entry = {.count = 1, .reusable = false}}, SHIFT(5), + [72] = {.entry = {.count = 1, .reusable = true}}, SHIFT(16), + [74] = {.entry = {.count = 1, .reusable = false}}, SHIFT(3), + [76] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_command_invocation, 3), + [78] = {.entry = {.count = 1, .reusable = false}}, REDUCE(sym_command_invocation, 3), + [80] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_command_invocation, 4), + [82] = {.entry = {.count = 1, .reusable = false}}, REDUCE(sym_command_invocation, 4), + [84] = {.entry = {.count = 1, .reusable = false}}, REDUCE(sym_argument, 1), + [86] = {.entry = {.count = 1, .reusable = false}}, SHIFT(18), + [88] = {.entry = {.count = 1, .reusable = false}}, REDUCE(sym__seperated_arguments, 2), + [90] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_command_invocation, 5), + [92] = {.entry = {.count = 1, .reusable = false}}, REDUCE(sym_command_invocation, 5), + [94] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_command_invocation, 6), + [96] = {.entry = {.count = 1, .reusable = false}}, REDUCE(sym_command_invocation, 6), + [98] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_command_invocation, 7), + [100] = {.entry = {.count = 1, .reusable = false}}, REDUCE(sym_command_invocation, 7), + [102] = {.entry = {.count = 1, .reusable = true}}, SHIFT(27), + [104] = {.entry = {.count = 1, .reusable = true}}, SHIFT(22), + [106] = {.entry = {.count = 1, .reusable = true}}, SHIFT(28), + [108] = {.entry = {.count = 1, .reusable = true}}, ACCEPT_INPUT(), + [110] = {.entry = {.count = 1, .reusable = true}}, SHIFT(29), +}; + +#ifdef __cplusplus +extern "C" { +#endif +#ifdef _WIN32 +#define extern __declspec(dllexport) +#endif + +extern const TSLanguage *tree_sitter_CMake(void) { + static 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 = (const uint16_t *)ts_parse_table, + .small_parse_table = (const uint16_t *)ts_small_parse_table, + .small_parse_table_map = (const uint32_t *)ts_small_parse_table_map, + .parse_actions = ts_parse_actions, + .symbol_names = ts_symbol_names, + .symbol_metadata = ts_symbol_metadata, + .public_symbol_map = ts_symbol_map, + .alias_map = ts_non_terminal_alias_map, + .alias_sequences = (const TSSymbol *)ts_alias_sequences, + .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..a3a87bd --- /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 **symbol_names; + const char **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_