Skip to content

Commit

Permalink
feat: update bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
amaanq committed Mar 6, 2023
1 parent 2209b21 commit 6ad7fcf
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 45 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "tree-sitter-bass"
description = "Bass grammar for the tree-sitter parsing library"
version = "0.0.1"
readme = "bindings/rust/README.md"
keywords = ["incremental", "parsing", "bass"]
categories = ["parsing", "text-editors"]
repository = "https://github.com/vito/tree-sitter-bass"
Expand Down
12 changes: 7 additions & 5 deletions bindings/node/binding.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "nan.h"
#include "tree_sitter/parser.h"
#include <node.h>
#include "nan.h"

using namespace v8;

extern "C" TSLanguage * tree_sitter_bass();
extern "C" TSLanguage *tree_sitter_bass();

namespace {

Expand All @@ -16,13 +16,15 @@ void Init(Local<Object> exports, Local<Object> module) {
tpl->InstanceTemplate()->SetInternalFieldCount(1);

Local<Function> constructor = Nan::GetFunction(tpl).ToLocalChecked();
Local<Object> instance = constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Local<Object> instance =
constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(instance, 0, tree_sitter_bass());

Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("bass").ToLocalChecked());
Nan::Set(instance, Nan::New("name").ToLocalChecked(),
Nan::New("bass").ToLocalChecked());
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
}

NODE_MODULE(tree_sitter_bass_binding, Init)

} // namespace
} // namespace
8 changes: 4 additions & 4 deletions bindings/node/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
try {
module.exports = require("../../build/Release/tree_sitter_bass_binding");
module.exports = require('../../build/Release/tree_sitter_bass_binding');
} catch (error1) {
if (error1.code !== 'MODULE_NOT_FOUND') {
throw error1;
}
try {
module.exports = require("../../build/Debug/tree_sitter_bass_binding");
module.exports = require('../../build/Debug/tree_sitter_bass_binding');
} catch (error2) {
if (error2.code !== 'MODULE_NOT_FOUND') {
throw error2;
}
throw error1
throw error1;
}
}

try {
module.exports.nodeTypeInfo = require("../../src/node-types.json");
module.exports.nodeTypeInfo = require('../../src/node-types.json');
} catch (_) {}
40 changes: 40 additions & 0 deletions bindings/rust/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# tree-sitter-bass

This crate provides a Bass grammar for the [tree-sitter][] parsing library. To
use this crate, add it to the `[dependencies]` section of your `Cargo.toml`
file. (Note that you will probably also need to depend on the
[`tree-sitter`][tree-sitter crate] crate to use the parsed result in any useful
way.)

```toml
[dependencies]
tree-sitter = "~0.20.3"
tree-sitter-bass = "0.0.1"
```

Typically, you will use the [language][language func] function to add this
grammar to a tree-sitter [Parser][], and then use the parser to parse some code:

```rust
let code = r#"
(provide [git-submodules]
(defn git-submodules [src]
(map (fn [[_ path]] (string->dir path))
(-> ($ git config --file src/.gitmodules --get-regexp path)
(with-image (linux/alpine/git))
(read :unix-table)
take-all))))
"#;
let mut parser = Parser::new();
parser.set_language(tree_sitter_bass::language()).expect("Error loading Bass grammar");
let parsed = parser.parse(code, None);
```

If you have any questions, please reach out to us in the [tree-sitter
discussions] page.

[language func]: https://docs.rs/tree-sitter-bass/*/tree_sitter_bass/fn.language.html
[parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
[tree-sitter]: https://tree-sitter.github.io/
[tree-sitter crate]: https://crates.io/crates/tree-sitter
[tree-sitter discussions]: https://github.com/tree-sitter/tree-sitter/discussions
27 changes: 1 addition & 26 deletions bindings/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,14 @@ fn main() {
let src_dir = std::path::Path::new("src");

let mut c_config = cc::Build::new();
c_config.include(&src_dir);
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());
*/
}
27 changes: 17 additions & 10 deletions bindings/rust/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// ------------------------------------------------------------------------------------------------
// Copyright © 2023 Alex Suraci <suraci.alex@gmail.com>, Amaan Qureshi <amaanq12@gmail.com>,
// See the LICENSE file in this repo for license details.
// ------------------------------------------------------------------------------------------------

//! This crate provides Bass language support for the [tree-sitter][] parsing library.
//!
//! Typically, you will use the [language][language func] function to add this language to a
Expand All @@ -6,7 +11,7 @@
//! ```
//! let code = "";
//! let mut parser = tree_sitter::Parser::new();
//! parser.set_language(tree_sitter_bass::language()).expect("Error loading bass grammar");
//! parser.set_language(tree_sitter_bass::language()).expect("Error loading Bass grammar");
//! let tree = parser.parse(code, None).unwrap();
//! ```
//!
Expand All @@ -28,17 +33,19 @@ pub fn language() -> Language {
unsafe { tree_sitter_bass() }
}

/// The source of the Rust tree-sitter grammar description.
pub const GRAMMAR: &str = include_str!("../../grammar.js");

/// The syntax highlighting query for this language.
pub const HIGHLIGHTS_QUERY: &str = include_str!("../../queries/highlights.scm");

/// The injection query for this language.
pub const INJECTIONS_QUERY: &str = include_str!("../../queries/nvim/injections.scm");

/// 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");
pub const NODE_TYPES: &str = include_str!("../../src/node-types.json");

#[cfg(test)]
mod tests {
Expand All @@ -47,6 +54,6 @@ mod tests {
let mut parser = tree_sitter::Parser::new();
parser
.set_language(super::language())
.expect("Error loading Bass language");
.expect("Error loading Bass grammar");
}
}

0 comments on commit 6ad7fcf

Please sign in to comment.