Skip to content

Commit ece95d4

Browse files
committed
feat: improve bindings
1 parent 94b477e commit ece95d4

20 files changed

+715
-37
lines changed

.editorconfig

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
trim_trailing_whitespace = true
8+
9+
[*.{json,toml,yml,gyp}]
10+
indent_style = space
11+
indent_size = 2
12+
13+
[*.js]
14+
indent_style = space
15+
indent_size = 2
16+
17+
[*.rs]
18+
indent_style = space
19+
indent_size = 4
20+
21+
[*.{c,cc,h}]
22+
indent_style = space
23+
indent_size = 4
24+
25+
[*.{py,pyi}]
26+
indent_style = space
27+
indent_size = 4
28+
29+
[*.swift]
30+
indent_style = space
31+
indent_size = 4
32+
33+
[*.go]
34+
indent_style = tab
35+
indent_size = 8
36+
37+
[Makefile]
38+
indent_style = tab
39+
indent_size = 8

Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ name = "tree-sitter-python"
33
description = "Python grammar for tree-sitter"
44
version = "0.20.4"
55
authors = [
6-
"Max Brunsfeld <[email protected]>",
7-
"Douglas Creager <[email protected]>",
6+
"Max Brunsfeld <[email protected]>",
7+
"Douglas Creager <[email protected]>",
88
]
99
license = "MIT"
1010
readme = "bindings/rust/README.md"
@@ -21,7 +21,7 @@ include = ["bindings/rust/*", "grammar.js", "queries/*", "src/*"]
2121
path = "bindings/rust/lib.rs"
2222

2323
[dependencies]
24-
tree-sitter = "~0.20.10"
24+
tree-sitter = "0.21.0"
2525

2626
[build-dependencies]
27-
cc = "~1.0"
27+
cc = "1.0.88"

Makefile

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
VERSION := 0.0.1
2+
3+
LANGUAGE_NAME := tree-sitter-python
4+
5+
# repository
6+
SRC_DIR := src
7+
8+
PARSER_REPO_URL := $(shell git -C $(SRC_DIR) remote get-url origin 2>/dev/null)
9+
10+
ifeq ($(PARSER_URL),)
11+
PARSER_URL := $(subst .git,,$(PARSER_REPO_URL))
12+
ifeq ($(shell echo $(PARSER_URL) | grep '^[a-z][-+.0-9a-z]*://'),)
13+
PARSER_URL := $(subst :,/,$(PARSER_URL))
14+
PARSER_URL := $(subst git@,https://,$(PARSER_URL))
15+
endif
16+
endif
17+
18+
# ABI versioning
19+
SONAME_MAJOR := $(word 1,$(subst ., ,$(VERSION)))
20+
SONAME_MINOR := $(word 2,$(subst ., ,$(VERSION)))
21+
22+
# install directory layout
23+
PREFIX ?= /usr/local
24+
INCLUDEDIR ?= $(PREFIX)/include
25+
LIBDIR ?= $(PREFIX)/lib
26+
PCLIBDIR ?= $(LIBDIR)/pkgconfig
27+
28+
# object files
29+
OBJS := $(patsubst %.c,%.o,$(wildcard $(SRC_DIR)/*.c))
30+
31+
# flags
32+
ARFLAGS := rcs
33+
override CFLAGS += -I$(SRC_DIR) -std=c11
34+
35+
# OS-specific bits
36+
ifeq ($(shell uname),Darwin)
37+
SOEXT = dylib
38+
SOEXTVER_MAJOR = $(SONAME_MAJOR).dylib
39+
SOEXTVER = $(SONAME_MAJOR).$(SONAME_MINOR).dylib
40+
LINKSHARED := $(LINKSHARED)-dynamiclib -Wl,
41+
ifneq ($(ADDITIONAL_LIBS),)
42+
LINKSHARED := $(LINKSHARED)$(ADDITIONAL_LIBS),
43+
endif
44+
LINKSHARED := $(LINKSHARED)-install_name,$(LIBDIR)/lib$(LANGUAGE_NAME).$(SONAME_MAJOR).dylib,-rpath,@executable_path/../Frameworks
45+
else ifneq ($(filter $(shell uname),Linux FreeBSD NetBSD DragonFly),)
46+
SOEXT = so
47+
SOEXTVER_MAJOR = so.$(SONAME_MAJOR)
48+
SOEXTVER = so.$(SONAME_MAJOR).$(SONAME_MINOR)
49+
LINKSHARED := $(LINKSHARED)-shared -Wl,
50+
ifneq ($(ADDITIONAL_LIBS),)
51+
LINKSHARED := $(LINKSHARED)$(ADDITIONAL_LIBS)
52+
endif
53+
LINKSHARED := $(LINKSHARED)-soname,lib$(LANGUAGE_NAME).so.$(SONAME_MAJOR)
54+
else ifeq ($(OS),Windows_NT)
55+
$(error "Windows is not supported")
56+
endif
57+
ifneq ($(filter $(shell uname),FreeBSD NetBSD DragonFly),)
58+
PCLIBDIR := $(PREFIX)/libdata/pkgconfig
59+
endif
60+
61+
all: lib$(LANGUAGE_NAME).a lib$(LANGUAGE_NAME).$(SOEXT) $(LANGUAGE_NAME).pc
62+
63+
$(SRC_DIR)/%.o: $(SRC_DIR)/%.c
64+
$(CC) -c $^ -o $@
65+
66+
lib$(LANGUAGE_NAME).a: $(OBJS)
67+
$(AR) $(ARFLAGS) $@ $^
68+
69+
lib$(LANGUAGE_NAME).$(SOEXT): $(OBJS)
70+
$(CC) -fPIC $(LDFLAGS) $(LINKSHARED) $^ $(LDLIBS) -o $@
71+
72+
$(LANGUAGE_NAME).pc:
73+
sed > $@ bindings/c/$(LANGUAGE_NAME).pc.in \
74+
-e 's|@URL@|$(PARSER_URL)|' \
75+
-e 's|@VERSION@|$(VERSION)|' \
76+
-e 's|@LIBDIR@|$(LIBDIR)|;' \
77+
-e 's|@INCLUDEDIR@|$(INCLUDEDIR)|;' \
78+
-e 's|=$(PREFIX)|=$${prefix}|' \
79+
-e 's|@PREFIX@|$(PREFIX)|' \
80+
-e 's|@REQUIRES@|$(REQUIRES)|' \
81+
-e 's|@ADDITIONAL_LIBS@|$(ADDITIONAL_LIBS)|'
82+
83+
install: all
84+
install -Dm644 bindings/c/$(LANGUAGE_NAME).h '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter/$(LANGUAGE_NAME).h
85+
install -Dm644 $(LANGUAGE_NAME).pc '$(DESTDIR)$(PCLIBDIR)'/$(LANGUAGE_NAME).pc
86+
install -Dm755 lib$(LANGUAGE_NAME).a '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).a
87+
install -Dm755 lib$(LANGUAGE_NAME).$(SOEXT) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER)
88+
ln -sf lib$(LANGUAGE_NAME).$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER_MAJOR)
89+
ln -sf lib$(LANGUAGE_NAME).$(SOEXTVER_MAJOR) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXT)
90+
91+
clean:
92+
$(RM) $(OBJS) $(LANGUAGE_NAME).pc lib$(LANGUAGE_NAME).a lib$(LANGUAGE_NAME).$(SOEXT)
93+
94+
.PHONY: all install clean

binding.gyp

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44
"target_name": "tree_sitter_python_binding",
55
"include_dirs": [
66
"<!(node -e \"require('nan')\")",
7-
"src"
7+
"src",
88
],
99
"sources": [
1010
"bindings/node/binding.cc",
1111
"src/parser.c",
12-
"src/scanner.c"
12+
"src/scanner.c",
1313
],
1414
"cflags_c": [
15-
"-std=c99",
15+
"-std=c11",
16+
],
17+
"cflags_cc": [
18+
"-Wno-cast-function-type",
1619
]
1720
}
1821
]

bindings/c/tree-sitter-python.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef TREE_SITTER_PYTHON_H_
2+
#define TREE_SITTER_PYTHON_H_
3+
4+
typedef struct TSLanguage TSLanguage;
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
extern const TSLanguage *tree_sitter_python(void);
11+
12+
#ifdef __cplusplus
13+
}
14+
#endif
15+
16+
#endif // TREE_SITTER_PYTHON_H_

bindings/c/tree-sitter-python.pc.in

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
prefix=@PREFIX@
2+
libdir=@LIBDIR@
3+
includedir=@INCLUDEDIR@
4+
5+
Name: tree-sitter-python
6+
Description: python grammar for tree-sitter
7+
URL: @URL@
8+
Version: @VERSION@
9+
Requires: @REQUIRES@
10+
Libs: -L${libdir} @ADDITIONAL_LIBS@ -ltree-sitter-python
11+
Cflags: -I${includedir}

bindings/go/binding.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package tree_sitter_python
2+
3+
// #cgo CFLAGS: -std=c11 -fPIC
4+
// #include "../../src/parser.c"
5+
// // NOTE: if your language has an external scanner, add it here.
6+
import "C"
7+
8+
import "unsafe"
9+
10+
// Get the tree-sitter Language for this grammar.
11+
func Language() unsafe.Pointer {
12+
return unsafe.Pointer(C.tree_sitter_python())
13+
}

bindings/go/binding_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package tree_sitter_python_test
2+
3+
import (
4+
"testing"
5+
6+
tree_sitter "github.com/smacker/go-tree-sitter"
7+
"github.com/tree-sitter/tree-sitter-python"
8+
)
9+
10+
func TestCanLoadGrammar(t *testing.T) {
11+
language := tree_sitter.NewLanguage(tree_sitter_python.Language())
12+
if language == nil {
13+
t.Errorf("Error loading Python grammar")
14+
}
15+
}

bindings/go/go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/tree-sitter/tree-sitter-python
2+
3+
go 1.22
4+
5+
require github.com/smacker/go-tree-sitter v0.0.0-20230720070738-0d0a9f78d8f8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from ._tree_sitter_python import lib as _lib, ffi as _ffi
2+
3+
def language():
4+
"""Get the tree-sitter language for this grammar."""
5+
return int(_ffi.cast("uintptr_t", _lib.tree_sitter_python()))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"Python grammar for tree-sitter"
2+
3+
from ._binding import language
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def language() -> int: ...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <Python.h>
2+
3+
typedef struct TSLanguage TSLanguage;
4+
5+
extern const TSLanguage *tree_sitter_python(void);
6+
7+
static PyObject* _binding_language(PyObject *self, PyObject *args) {
8+
return PyLong_FromVoidPtr((void *)tree_sitter_python());
9+
}
10+
11+
static PyMethodDef methods[] = {
12+
{"language", _binding_language, METH_NOARGS,
13+
"Get the tree-sitter language for this grammar."},
14+
{NULL, NULL, 0, NULL}
15+
};
16+
17+
static struct PyModuleDef module = {
18+
.m_base = PyModuleDef_HEAD_INIT,
19+
.m_name = "_binding",
20+
.m_doc = NULL,
21+
.m_size = -1,
22+
.m_methods = methods
23+
};
24+
25+
PyMODINIT_FUNC PyInit__binding(void) {
26+
return PyModule_Create(&module);
27+
}

bindings/python/tree_sitter_python/py.typed

Whitespace-only changes.

bindings/rust/build.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@ fn main() {
22
let src_dir = std::path::Path::new("src");
33

44
let mut c_config = cc::Build::new();
5+
c_config.flag_if_supported("-Wno-unused-value");
56
c_config.include(src_dir);
6-
c_config
7-
.flag_if_supported("-Wno-unused-parameter")
8-
.flag_if_supported("-Wno-unused-but-set-variable")
9-
.flag_if_supported("-Wno-trigraphs");
107
let parser_path = src_dir.join("parser.c");
118
c_config.file(&parser_path);
129

1310
let scanner_path = src_dir.join("scanner.c");
1411
c_config.file(&scanner_path);
1512
println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap());
1613

17-
c_config.compile("parser");
14+
c_config.compile("tree-sitter-python");
1815
println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap());
1916
}

bindings/rust/lib.rs

+11-24
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
1-
// -*- coding: utf-8 -*-
2-
// ------------------------------------------------------------------------------------------------
3-
// Copyright © 2020, tree-sitter-python authors.
4-
// See the LICENSE file in this repo for license details.
5-
// ------------------------------------------------------------------------------------------------
6-
7-
//! This crate provides a Python grammar for the [tree-sitter][] parsing library.
1+
//! This crate provides Python language support for the [tree-sitter][] parsing library.
82
//!
9-
//! Typically, you will use the [language][language func] function to add this grammar to a
3+
//! Typically, you will use the [language][language func] function to add this language to a
104
//! tree-sitter [Parser][], and then use the parser to parse some code:
115
//!
126
//! ```
13-
//! use tree_sitter::Parser;
14-
//!
157
//! let code = r#"
168
//! def double(x):
179
//! return x * 2
1810
//! "#;
19-
//! let mut parser = Parser::new();
20-
//! parser.set_language(tree_sitter_python::language()).expect("Error loading Python grammar");
21-
//! let parsed = parser.parse(code, None);
22-
//! # let parsed = parsed.unwrap();
23-
//! # let root = parsed.root_node();
24-
//! # assert!(!root.has_error());
11+
//! let mut parser = tree_sitter::Parser::new();
12+
//! parser.set_language(&tree_sitter_python::language()).expect("Error loading Python grammar");
13+
//! let tree = parser.parse(code, None).unwrap();
14+
//! assert!(!tree.root_node().has_error());
2515
//! ```
2616
//!
2717
//! [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
@@ -35,7 +25,7 @@ extern "C" {
3525
fn tree_sitter_python() -> Language;
3626
}
3727

38-
/// Returns the tree-sitter [Language][] for this grammar.
28+
/// Get the tree-sitter [Language][] for this grammar.
3929
///
4030
/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
4131
pub fn language() -> Language {
@@ -45,24 +35,21 @@ pub fn language() -> Language {
4535
/// The source of the Python tree-sitter grammar description.
4636
pub const GRAMMAR: &str = include_str!("../../grammar.js");
4737

48-
/// The syntax highlighting query for this language.
49-
pub const HIGHLIGHT_QUERY: &str = include_str!("../../queries/highlights.scm");
50-
5138
/// The content of the [`node-types.json`][] file for this grammar.
5239
///
5340
/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
5441
pub const NODE_TYPES: &str = include_str!("../../src/node-types.json");
5542

56-
/// The symbol tagging query for this language.
57-
pub const TAGGING_QUERY: &str = include_str!("../../queries/tags.scm");
43+
pub const HIGHLIGHTS_QUERY: &str = include_str!("../../queries/highlights.scm");
44+
pub const TAGS_QUERY: &str = include_str!("../../queries/tags.scm");
5845

5946
#[cfg(test)]
6047
mod tests {
6148
#[test]
62-
fn can_load_grammar() {
49+
fn test_can_load_grammar() {
6350
let mut parser = tree_sitter::Parser::new();
6451
parser
65-
.set_language(super::language())
52+
.set_language(&super::language())
6653
.expect("Error loading Python grammar");
6754
}
6855
}

0 commit comments

Comments
 (0)