initial checking

This commit is contained in:
Peter Hart 2022-09-02 23:36:25 -04:00
commit 51def200ab
17 changed files with 2591 additions and 0 deletions

13
.gitignore vendored Normal file
View File

@ -0,0 +1,13 @@
Cargo.lock
node_modules
build
*.log
package-lock.json
target
*.a
*.dylib
*.so
*.o
bindings/c/*.h
bindings/c/tree-sitter-*.pc
.build/

26
Cargo.toml Normal file
View File

@ -0,0 +1,26 @@
[package]
name = "tree-sitter-Groovy"
description = "Groovy grammar for the tree-sitter parsing library"
version = "0.0.1"
keywords = ["incremental", "parsing", "Groovy"]
categories = ["parsing", "text-editors"]
repository = "https://github.com/tree-sitter/tree-sitter-Groovy"
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.3"
[build-dependencies]
cc = "1.0"

19
binding.gyp Normal file
View File

@ -0,0 +1,19 @@
{
"targets": [
{
"target_name": "tree_sitter_Groovy_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_Groovy();
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_Groovy());
Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("Groovy").ToLocalChecked());
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
}
NODE_MODULE(tree_sitter_Groovy_binding, Init)
} // namespace

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

@ -0,0 +1,19 @@
try {
module.exports = require("../../build/Release/tree_sitter_Groovy_binding");
} catch (error1) {
if (error1.code !== 'MODULE_NOT_FOUND') {
throw error1;
}
try {
module.exports = require("../../build/Debug/tree_sitter_Groovy_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 Groovy 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_Groovy::language()).expect("Error loading Groovy 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_Groovy() -> 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_Groovy() }
}
/// 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 Groovy language");
}
}

63
grammar.js Normal file
View File

@ -0,0 +1,63 @@
module.exports = grammar({
name: 'Groovy',
rules: {
// TODO: add the actual grammar rules
source_file: $ => seq(
optional($._shebang_comment),
optional(seq($.package_definition, repeat($._nl_semicolon))),
repeat(
choice(
seq(
$.import_statement,
repeat1($._nl_semicolon)
),
seq(
$.script_part,
repeat1($._nl_semicolon)
)
)
),
optional($.script_part)
),
import_statement: $ => seq(
'import',
optional('static'),
field('name',
seq(
$.identifier,
repeat(seq('.', $.identifier)),
optional(seq('.', '*')
)
)
)
),
_shebang_comment: $ => token.immediate(/#!.*\n/),
package_definition: $ => seq(
'package',
field('name', seq(
$.identifier,
repeat(seq('.', $.identifier))))
),
script_part: $ => choice(
$.statement,
$.method_declaration
),
expression: $ => choice(
$.path_expression
),
path_expression: $ => seq(
repeat(
seq($.identifier, '.')
),
$.identifier
),
identifier: $ => /[A-Za-z_][A-Za-z_0-9]*/,
statement: $ => choice(
$.expression
),
method_declaration: $ => seq('def', field('name', $.identifier)),
_nl_semicolon: $ => choice('\n', ';'),
word: $ => $.identifier
}
});

17
package.json Normal file
View File

@ -0,0 +1,17 @@
{
"name": "tree-sitter-groovy",
"version": "0.0.1",
"description": "Groovy tree-sitter grammar",
"main": "bindings/node",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Peter Hart",
"license": "ISC",
"dependencies": {
"nan": "^2.16.0"
},
"devDependencies": {
"tree-sitter-cli": "^0.20.7"
}
}

320
src/grammar.json Normal file
View File

@ -0,0 +1,320 @@
{
"name": "Groovy",
"rules": {
"source_file": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_shebang_comment"
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "package_definition"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_nl_semicolon"
}
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "import_statement"
},
{
"type": "REPEAT1",
"content": {
"type": "SYMBOL",
"name": "_nl_semicolon"
}
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "script_part"
},
{
"type": "REPEAT1",
"content": {
"type": "SYMBOL",
"name": "_nl_semicolon"
}
}
]
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "script_part"
},
{
"type": "BLANK"
}
]
}
]
},
"import_statement": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "import"
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "static"
},
{
"type": "BLANK"
}
]
},
{
"type": "FIELD",
"name": "name",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "."
},
{
"type": "SYMBOL",
"name": "identifier"
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "."
},
{
"type": "STRING",
"value": "*"
}
]
},
{
"type": "BLANK"
}
]
}
]
}
}
]
},
"_shebang_comment": {
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "PATTERN",
"value": "#!.*\\n"
}
},
"package_definition": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "package"
},
{
"type": "FIELD",
"name": "name",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "."
},
{
"type": "SYMBOL",
"name": "identifier"
}
]
}
}
]
}
}
]
},
"script_part": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "statement"
},
{
"type": "SYMBOL",
"name": "method_declaration"
}
]
},
"expression": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "path_expression"
}
]
},
"path_expression": {
"type": "SEQ",
"members": [
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "STRING",
"value": "."
}
]
}
},
{
"type": "SYMBOL",
"name": "identifier"
}
]
},
"identifier": {
"type": "PATTERN",
"value": "[A-Za-z_][A-Za-z_0-9]*"
},
"statement": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "expression"
}
]
},
"method_declaration": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "def"
},
{
"type": "FIELD",
"name": "name",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
}
]
},
"_nl_semicolon": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "\n"
},
{
"type": "STRING",
"value": ";"
}
]
},
"word": {
"type": "SYMBOL",
"name": "identifier"
}
},
"extras": [
{
"type": "PATTERN",
"value": "\\s"
}
],
"conflicts": [],
"precedences": [],
"externals": [],
"inline": [],
"supertypes": []
}

185
src/node-types.json Normal file
View File

@ -0,0 +1,185 @@
[
{
"type": "expression",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "path_expression",
"named": true
}
]
}
},
{
"type": "import_statement",
"named": true,
"fields": {
"name": {
"multiple": true,
"required": true,
"types": [
{
"type": "*",
"named": false
},
{
"type": ".",
"named": false
},
{
"type": "identifier",
"named": true
}
]
}
}
},
{
"type": "method_declaration",
"named": true,
"fields": {
"name": {
"multiple": false,
"required": true,
"types": [
{
"type": "identifier",
"named": true
}
]
}
}
},
{
"type": "package_definition",
"named": true,
"fields": {
"name": {
"multiple": true,
"required": true,
"types": [
{
"type": ".",
"named": false
},
{
"type": "identifier",
"named": true
}
]
}
}
},
{
"type": "path_expression",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "identifier",
"named": true
}
]
}
},
{
"type": "script_part",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "method_declaration",
"named": true
},
{
"type": "statement",
"named": true
}
]
}
},
{
"type": "source_file",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "import_statement",
"named": true
},
{
"type": "package_definition",
"named": true
},
{
"type": "script_part",
"named": true
}
]
}
},
{
"type": "statement",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "expression",
"named": true
}
]
}
},
{
"type": "\n",
"named": false
},
{
"type": "*",
"named": false
},
{
"type": ".",
"named": false
},
{
"type": ";",
"named": false
},
{
"type": "def",
"named": false
},
{
"type": "identifier",
"named": true
},
{
"type": "import",
"named": false
},
{
"type": "package",
"named": false
},
{
"type": "static",
"named": false
}
]

1395
src/parser.c Normal file

File diff suppressed because it is too large Load Diff

224
src/tree_sitter/parser.h Normal file
View File

@ -0,0 +1,224 @@
#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;
const TSStateId *primary_state_ids;
};
/*
* 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_

View File

@ -0,0 +1,47 @@
================================================================================
single identifier import
================================================================================
import wibble
--------------------------------------------------------------------------------
(source_file
(import_statement
(identifier)))
================================================================================
multiple identifier import
================================================================================
import wobble.qq
--------------------------------------------------------------------------------
(source_file
(import_statement
(identifier)
(identifier)))
================================================================================
wildcard import
================================================================================
import flobble.fnurfle.*
--------------------------------------------------------------------------------
(source_file
(import_statement
(identifier)
(identifier)))
================================================================================
multiple imports semicolon separated
================================================================================
import qq.ss; import flobble.*;
--------------------------------------------------------------------------------
(source_file
(import_statement
(identifier)
(identifier))
(import_statement
(identifier)))

View File

@ -0,0 +1,20 @@
================================================================================
Simple package definition
================================================================================
package flob
--------------------------------------------------------------------------------
(source_file
(package_definition
(identifier)))
================================================================================
Dotted package definition
================================================================================
package flob.wibble
--------------------------------------------------------------------------------
(source_file
(package_definition
(identifier)
(identifier)))

33
test/corpus/shebang.txt Normal file
View File

@ -0,0 +1,33 @@
================================================================================
Shebang ignored at beginning of first line
================================================================================
#!/usr/bin/env groovy
hello
--------------------------------------------------------------------------------
(source_file
(script_part
(statement
(expression
(path_expression
(identifier))))))
================================================================================
Shebang not ignored after first character
================================================================================
#!/usr/bin/env groovy
--------------------------------------------------------------------------------
(source_file
(ERROR
(UNEXPECTED '#')
(identifier)
(UNEXPECTED '/')
(identifier)
(UNEXPECTED '/')
(identifier))
(script_part
(statement
(expression
(path_expression
(identifier))))))

View File

@ -0,0 +1,90 @@
================================================================================
Empty File
================================================================================
--------------------------------------------------------------------------------
(source_file)
================================================================================
statement - single identifier
================================================================================
hello
--------------------------------------------------------------------------------
(source_file
(script_part
(statement
(expression
(path_expression
(identifier))))))
================================================================================
statement - Simple method definition
================================================================================
def hello
--------------------------------------------------------------------------------
(source_file
(script_part
(method_declaration
(identifier))))
================================================================================
statement - Multiple statements same line
================================================================================
hello;goodbye;def flunk
--------------------------------------------------------------------------------
(source_file
(script_part
(statement
(expression
(path_expression
(identifier)))))
(script_part
(statement
(expression
(path_expression
(identifier)))))
(script_part
(method_declaration
(identifier))))
================================================================================
statement - Multiple Statements, Multiple Lines
================================================================================
hello
def flunk
goodbye;
--------------------------------------------------------------------------------
(source_file
(script_part
(statement
(expression
(path_expression
(identifier)))))
(script_part
(method_declaration
(identifier)))
(script_part
(statement
(expression
(path_expression
(identifier))))))
================================================================================
statement - path expression multiple parts
================================================================================
wibble.wobble.q
--------------------------------------------------------------------------------
(source_file
(script_part
(statement
(expression
(path_expression
(identifier)
(identifier)
(identifier))))))