Compare commits

..

1 commit

Author SHA1 Message Date
Gered 2a50b3b0ce update rust bindings to use tree-sitter-language 2024-11-10 11:14:41 -05:00
13 changed files with 6487 additions and 6371 deletions

View file

@ -15,6 +15,9 @@ include = ["bindings/rust/*", "grammar.js", "queries/*", "src/*"]
path = "bindings/rust/lib.rs"
[dependencies]
tree-sitter-language = "0.1"
[dev-dependencies]
tree-sitter = ">=0.22"
[build-dependencies]

View file

@ -2,8 +2,8 @@
A Tree-sitter parser for CMake
==============================
This project provides a `cmake` parser. Its primary use case is to provide a `cmake`
parser for `nvim-treesitter`.
This project provides a `cmake` parser. Its primary use case is to provide a `cmake` parser for `nvim-treesitter`.
Parsed syntax
=============
@ -22,8 +22,10 @@ Parsed syntax
- Unquoted arguments
- Parentheses
- Variable references
- Variable refences
- Environment and cache variables
- Normal variables
- Generator expression

View file

@ -2,7 +2,7 @@ fn main() {
let src_dir = std::path::Path::new("src");
let mut c_config = cc::Build::new();
c_config.std("c11").include(src_dir);
c_config.include(&src_dir);
c_config
.flag_if_supported("-Wno-unused-parameter")
.flag_if_supported("-Wno-unused-but-set-variable")
@ -17,5 +17,6 @@ fn main() {
c_config.file(&scanner_path);
println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap());
c_config.compile("tree-sitter-cmake");
c_config.compile("parser");
println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap());
}

32
bindings/rust/lib.rs generated
View file

@ -4,12 +4,10 @@
//! tree-sitter [Parser][], and then use the parser to parse some code:
//!
//! ```
//! let code = r#"
//! "#;
//! let code = "";
//! let mut parser = tree_sitter::Parser::new();
//! parser.set_language(&tree_sitter_cmake::language()).expect("Error loading cmake grammar");
//! parser.set_language(&tree_sitter_cmake::LANGUAGE.into()).expect("Error loading cmake grammar");
//! let tree = parser.parse(code, None).unwrap();
//! assert!(!tree.root_node().has_error());
//! ```
//!
//! [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
@ -17,30 +15,28 @@
//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
//! [tree-sitter]: https://tree-sitter.github.io/
use tree_sitter::Language;
use tree_sitter_language::LanguageFn;
extern "C" {
fn tree_sitter_cmake() -> Language;
fn tree_sitter_cmake() -> *const ();
}
/// Get the tree-sitter [Language][] for this grammar.
/// The tree-sitter [`LanguageFn`][LanguageFn] for this grammar.
///
/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
pub fn language() -> Language {
unsafe { tree_sitter_cmake() }
}
/// [LanguageFn]: https://docs.rs/tree-sitter-language/*/tree_sitter_language/struct.LanguageFn.html
pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(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: &str = include_str!("../../src/node-types.json");
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: &str = include_str!("../../queries/highlights.scm");
// pub const INJECTIONS_QUERY: &str = include_str!("../../queries/injections.scm");
// pub const LOCALS_QUERY: &str = include_str!("../../queries/locals.scm");
// pub const TAGS_QUERY: &str = include_str!("../../queries/tags.scm");
// 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 {
@ -48,7 +44,7 @@ mod tests {
fn test_can_load_grammar() {
let mut parser = tree_sitter::Parser::new();
parser
.set_language(&super::language())
.expect("Error loading cmake grammar");
.set_language(&super::LANGUAGE.into())
.expect("Error loading cmake language");
}
}

View file

@ -1,4 +1,4 @@
const commands = [
commands = [
"if",
"elseif",
"else",

View file

@ -18,13 +18,14 @@
"optional": true
}
},
"files": [
"grammar.js",
"binding.gyp",
"prebuilds/**",
"bindings/node/*",
"queries/*",
"src/**"
"tree-sitter": [
{
"scope": "source.cmake",
"file-types": [
"cmake",
"CMakeLists.txt"
]
}
],
"devDependencies": {
"tree-sitter-cli": "^0.21.0",
@ -33,5 +34,13 @@
"scripts": {
"install": "node-gyp-build",
"prebuildify": "prebuildify --napi --strip"
}
}
},
"files": [
"grammar.js",
"binding.gyp",
"prebuilds/**",
"bindings/node/*",
"queries/*",
"src/**"
]
}

1
src/grammar.json generated
View file

@ -1,5 +1,4 @@
{
"$schema": "https://tree-sitter.github.io/tree-sitter/assets/schemas/grammar.schema.json",
"name": "cmake",
"rules": {
"source_file": {

1
src/node-types.json generated
View file

@ -568,7 +568,6 @@
{
"type": "source_file",
"named": true,
"root": true,
"fields": {},
"children": {
"multiple": true,

12592
src/parser.c generated

File diff suppressed because it is too large Load diff

View file

@ -12,10 +12,10 @@ extern "C" {
// Allow clients to override allocation functions
#ifdef TREE_SITTER_REUSE_ALLOCATOR
extern void *(*ts_current_malloc)(size_t size);
extern void *(*ts_current_calloc)(size_t count, size_t size);
extern void *(*ts_current_realloc)(void *ptr, size_t size);
extern void (*ts_current_free)(void *ptr);
extern void *(*ts_current_malloc)(size_t);
extern void *(*ts_current_calloc)(size_t, size_t);
extern void *(*ts_current_realloc)(void *, size_t);
extern void (*ts_current_free)(void *);
#ifndef ts_malloc
#define ts_malloc ts_current_malloc

View file

@ -47,7 +47,6 @@ struct TSLexer {
uint32_t (*get_column)(TSLexer *);
bool (*is_at_included_range_start)(const TSLexer *);
bool (*eof)(const TSLexer *);
void (*log)(const TSLexer *, const char *, ...);
};
typedef enum {

143
test/corpus/gen_exp.txt Normal file
View file

@ -0,0 +1,143 @@
=======================================
Unquoted generator expression [gen_exp]
=======================================
message($<>)
---
(source_file
(normal_command
(identifier)
(argument_list
(argument
(unquoted_argument
(gen_exp)
)
)
)
)
)
=====================================
Quoted generator expression [gen_exp]
=====================================
message("$<>")
---
(source_file
(normal_command
(identifier)
(argument_list
(argument
(quoted_argument
(quoted_element
(gen_exp)
)
)
)
)
)
)
=====================
No argument [gen_exp]
=====================
message($<ANGLE-R>)
---
(source_file
(normal_command
(identifier)
(argument_list
(argument
(unquoted_argument
(gen_exp
(argument
(unquoted_argument)
)
)
)
)
)
)
)
============================================
No argument with superfluous colon [gen_exp]
============================================
message($<ANGLE-R:>)
---
(source_file
(normal_command
(identifier)
(argument_list
(argument
(unquoted_argument
(gen_exp
(argument
(unquoted_argument)
)
)
)
)
)
)
)
======================
One argument [gen_exp]
======================
message($<BOOL:-NOTFOUND>)
---
(source_file
(normal_command
(identifier)
(argument_list
(argument
(unquoted_argument
(gen_exp
(argument
(unquoted_argument))
(argument
(unquoted_argument)
)
)
)
)
)
)
)
=======================
Two arguments [gen_exp]
=======================
message($<AND:TRUE,FALSE>)
---
(source_file
(normal_command
(identifier)
(argument_list
(argument
(unquoted_argument
(gen_exp
(argument
(unquoted_argument))
(argument
(unquoted_argument))
(argument
(unquoted_argument)
)
)
)
)
)
)
)

View file

@ -1,35 +0,0 @@
{
"grammars": [
{
"name": "cmake",
"camelcase": "Cmake",
"scope": "source.cmake",
"path": ".",
"file-types": [
"cmake",
"CMakeLists.txt"
]
}
],
"metadata": {
"version": "0.5.0",
"license": "MIT",
"description": "CMake grammar for tree-sitter",
"authors": [
{
"name": "Uy Ha"
}
],
"links": {
"repository": "https://github.com/tree-sitter/tree-sitter-cmake"
}
},
"bindings": {
"c": true,
"go": true,
"node": true,
"python": true,
"rust": true,
"swift": true
}
}