Skip to content

Commit

Permalink
feat: update bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
amaanq committed Jul 6, 2023
1 parent 9abae25 commit 6a371c9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 25 deletions.
24 changes: 12 additions & 12 deletions bindings/node/binding.cc
Expand Up @@ -11,18 +11,18 @@ 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);
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)
Expand Down
13 changes: 11 additions & 2 deletions bindings/rust/README.md
Expand Up @@ -16,14 +16,23 @@ 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#"
let cairo_0_code = r#"
func uint256_signed_lt{range_check_ptr}(a: Uint256, b: Uint256) -> (res: felt) {
let (a, _) = uint256_add(a, cast((low=0, high=2 ** 127), Uint256));
}
"#;
let mut parser = Parser::new();
parser.set_language(tree_sitter_cairo::language()).expect("Error loading Cairo grammar");
let parsed = parser.parse(code, None);
let cairo_0_parsed = parser.parse(code, None);

let cairo_1_code = r#"
impl ArrayIndex<T> of IndexView<Array<T>, usize, @T> {
fn index(self: @Array<T>, index: usize) -> @T {
array_at(self, index).unbox()
}
}
"#;
let cairo_1_parsed = parser.parse(code, None);
```

If you have any questions, please reach out to us in the [tree-sitter
Expand Down
26 changes: 19 additions & 7 deletions bindings/rust/lib.rs
Expand Up @@ -28,18 +28,30 @@ pub fn language() -> Language {
unsafe { tree_sitter_cairo() }
}

/// The source of the Cairo tree-sitter grammar description (though you would also need
/// cairo_0/cairo_1.js).
pub const GRAMMAR: &str = include_str!("../../grammar.js");

/// The folds query for this language.
pub const FOLDS_QUERY: &str = include_str!("../../queries/folds.scm");

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

/// The indents query for this language.
pub const INDENTS_QUERY: &str = include_str!("../../queries/indents.scm");

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

/// The symbol tagging query for this language.
pub const LOCALS_QUERY: &str = include_str!("../../queries/locals.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: &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]
Expand Down
8 changes: 4 additions & 4 deletions bindings/swift/cairo.h
@@ -1,16 +1,16 @@
#ifndef TREE_SITTER_SWIFT_H_
#define TREE_SITTER_SWIFT_H_
#ifndef TREE_SITTER_CAIRO_H_
#define TREE_SITTER_CAIRO_H_

typedef struct TSLanguage TSLanguage;

#ifdef __cplusplus
extern "C" {
#endif

extern TSLanguage *tree_sitter_swift();
extern TSLanguage *tree_sitter_cairo();

#ifdef __cplusplus
}
#endif

#endif // TREE_SITTER_SWIFT_H_
#endif // TREE_SITTER_CAIRO_H_

0 comments on commit 6a371c9

Please sign in to comment.