Skip to content

Commit a68a319

Browse files
committed
Python: Also use tree-sitter 0.22.6
1 parent 67dbcb4 commit a68a319

36 files changed

+1669
-99
lines changed

python/extractor/tsg-python/Cargo.Bazel.lock

Lines changed: 221 additions & 79 deletions
Large diffs are not rendered by default.

python/extractor/tsg-python/Cargo.lock

Lines changed: 35 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/extractor/tsg-python/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
name = "tsg-python"
55
version = "0.1.0"
66
authors = ["Taus Brock-Nannestad <tausbn@github.com>"]
7-
edition = "2018"
7+
edition = "2021"
88

99
# When changing/updating these, the `Cargo.Bazel.lock` file has to be regenerated.
1010
# Run `CARGO_BAZEL_REPIN=true CARGO_BAZEL_REPIN_ONLY=py_deps ./tools/bazel sync --only=py_deps`
@@ -20,7 +20,7 @@ anyhow = "1.0"
2020
regex = "1"
2121
smallvec = { version="1.6", features=["union"] }
2222
thiserror = "1.0"
23-
tree-sitter = "0.20.4"
23+
tree-sitter = ">= 0.22.6"
2424
tree-sitter-graph = "0.7.0"
2525
tsp = {path = "tsp"}
2626
clap = "2.32"

python/extractor/tsg-python/Makefile

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
VERSION := 0.0.1
2+
3+
LANGUAGE_NAME := tree-sitter-tsg_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+
TS ?= tree-sitter
19+
20+
# ABI versioning
21+
SONAME_MAJOR := $(word 1,$(subst ., ,$(VERSION)))
22+
SONAME_MINOR := $(word 2,$(subst ., ,$(VERSION)))
23+
24+
# install directory layout
25+
PREFIX ?= /usr/local
26+
INCLUDEDIR ?= $(PREFIX)/include
27+
LIBDIR ?= $(PREFIX)/lib
28+
PCLIBDIR ?= $(LIBDIR)/pkgconfig
29+
30+
# source/object files
31+
PARSER := $(SRC_DIR)/parser.c
32+
EXTRAS := $(filter-out $(PARSER),$(wildcard $(SRC_DIR)/*.c))
33+
OBJS := $(patsubst %.c,%.o,$(PARSER) $(EXTRAS))
34+
35+
# flags
36+
ARFLAGS ?= rcs
37+
override CFLAGS += -I$(SRC_DIR) -std=c11 -fPIC
38+
39+
# OS-specific bits
40+
ifeq ($(OS),Windows_NT)
41+
$(error "Windows is not supported")
42+
else ifeq ($(shell uname),Darwin)
43+
SOEXT = dylib
44+
SOEXTVER_MAJOR = $(SONAME_MAJOR).dylib
45+
SOEXTVER = $(SONAME_MAJOR).$(SONAME_MINOR).dylib
46+
LINKSHARED := $(LINKSHARED)-dynamiclib -Wl,
47+
ifneq ($(ADDITIONAL_LIBS),)
48+
LINKSHARED := $(LINKSHARED)$(ADDITIONAL_LIBS),
49+
endif
50+
LINKSHARED := $(LINKSHARED)-install_name,$(LIBDIR)/lib$(LANGUAGE_NAME).$(SONAME_MAJOR).dylib,-rpath,@executable_path/../Frameworks
51+
else
52+
SOEXT = so
53+
SOEXTVER_MAJOR = so.$(SONAME_MAJOR)
54+
SOEXTVER = so.$(SONAME_MAJOR).$(SONAME_MINOR)
55+
LINKSHARED := $(LINKSHARED)-shared -Wl,
56+
ifneq ($(ADDITIONAL_LIBS),)
57+
LINKSHARED := $(LINKSHARED)$(ADDITIONAL_LIBS)
58+
endif
59+
LINKSHARED := $(LINKSHARED)-soname,lib$(LANGUAGE_NAME).so.$(SONAME_MAJOR)
60+
endif
61+
ifneq ($(filter $(shell uname),FreeBSD NetBSD DragonFly),)
62+
PCLIBDIR := $(PREFIX)/libdata/pkgconfig
63+
endif
64+
65+
all: lib$(LANGUAGE_NAME).a lib$(LANGUAGE_NAME).$(SOEXT) $(LANGUAGE_NAME).pc
66+
67+
lib$(LANGUAGE_NAME).a: $(OBJS)
68+
$(AR) $(ARFLAGS) $@ $^
69+
70+
lib$(LANGUAGE_NAME).$(SOEXT): $(OBJS)
71+
$(CC) $(LDFLAGS) $(LINKSHARED) $^ $(LDLIBS) -o $@
72+
ifneq ($(STRIP),)
73+
$(STRIP) $@
74+
endif
75+
76+
$(LANGUAGE_NAME).pc: bindings/c/$(LANGUAGE_NAME).pc.in
77+
sed -e 's|@URL@|$(PARSER_URL)|' \
78+
-e 's|@VERSION@|$(VERSION)|' \
79+
-e 's|@LIBDIR@|$(LIBDIR)|' \
80+
-e 's|@INCLUDEDIR@|$(INCLUDEDIR)|' \
81+
-e 's|@REQUIRES@|$(REQUIRES)|' \
82+
-e 's|@ADDITIONAL_LIBS@|$(ADDITIONAL_LIBS)|' \
83+
-e 's|=$(PREFIX)|=$${prefix}|' \
84+
-e 's|@PREFIX@|$(PREFIX)|' $< > $@
85+
86+
$(PARSER): $(SRC_DIR)/grammar.json
87+
$(TS) generate --no-bindings $^
88+
89+
install: all
90+
install -d '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter '$(DESTDIR)$(PCLIBDIR)' '$(DESTDIR)$(LIBDIR)'
91+
install -m644 bindings/c/$(LANGUAGE_NAME).h '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter/$(LANGUAGE_NAME).h
92+
install -m644 $(LANGUAGE_NAME).pc '$(DESTDIR)$(PCLIBDIR)'/$(LANGUAGE_NAME).pc
93+
install -m644 lib$(LANGUAGE_NAME).a '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).a
94+
install -m755 lib$(LANGUAGE_NAME).$(SOEXT) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER)
95+
ln -sf lib$(LANGUAGE_NAME).$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER_MAJOR)
96+
ln -sf lib$(LANGUAGE_NAME).$(SOEXTVER_MAJOR) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXT)
97+
98+
uninstall:
99+
$(RM) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).a \
100+
'$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER) \
101+
'$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER_MAJOR) \
102+
'$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXT) \
103+
'$(DESTDIR)$(INCLUDEDIR)'/tree_sitter/$(LANGUAGE_NAME).h \
104+
'$(DESTDIR)$(PCLIBDIR)'/$(LANGUAGE_NAME).pc
105+
106+
clean:
107+
$(RM) $(OBJS) $(LANGUAGE_NAME).pc lib$(LANGUAGE_NAME).a lib$(LANGUAGE_NAME).$(SOEXT)
108+
109+
test:
110+
$(TS) test
111+
112+
.PHONY: all install uninstall clean test
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// swift-tools-version:5.3
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "TreeSitterTsgPython",
6+
products: [
7+
.library(name: "TreeSitterTsgPython", targets: ["TreeSitterTsgPython"]),
8+
],
9+
dependencies: [],
10+
targets: [
11+
.target(name: "TreeSitterTsgPython",
12+
path: ".",
13+
exclude: [
14+
"Cargo.toml",
15+
"Makefile",
16+
"binding.gyp",
17+
"bindings/c",
18+
"bindings/go",
19+
"bindings/node",
20+
"bindings/python",
21+
"bindings/rust",
22+
"prebuilds",
23+
"grammar.js",
24+
"package.json",
25+
"package-lock.json",
26+
"pyproject.toml",
27+
"setup.py",
28+
"test",
29+
"examples",
30+
".editorconfig",
31+
".github",
32+
".gitignore",
33+
".gitattributes",
34+
".gitmodules",
35+
],
36+
sources: [
37+
"src/parser.c",
38+
// NOTE: if your language has an external scanner, add it here.
39+
],
40+
resources: [
41+
.copy("queries")
42+
],
43+
publicHeadersPath: "bindings/swift",
44+
cSettings: [.headerSearchPath("src")])
45+
],
46+
cLanguageStandard: .c11
47+
)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "tree_sitter_tsg_python_binding",
5+
"dependencies": [
6+
"<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except",
7+
],
8+
"include_dirs": [
9+
"src",
10+
],
11+
"sources": [
12+
"bindings/node/binding.cc",
13+
"src/parser.c",
14+
# NOTE: if your language has an external scanner, add it here.
15+
],
16+
"conditions": [
17+
["OS!='win'", {
18+
"cflags_c": [
19+
"-std=c11",
20+
],
21+
}, { # OS == "win"
22+
"cflags_c": [
23+
"/std:c11",
24+
"/utf-8",
25+
],
26+
}],
27+
],
28+
}
29+
]
30+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef TREE_SITTER_TSG_PYTHON_H_
2+
#define TREE_SITTER_TSG_PYTHON_H_
3+
4+
typedef struct TSLanguage TSLanguage;
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
const TSLanguage *tree_sitter_tsg_python(void);
11+
12+
#ifdef __cplusplus
13+
}
14+
#endif
15+
16+
#endif // TREE_SITTER_TSG_PYTHON_H_
Lines changed: 11 additions & 0 deletions
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-tsg_python
6+
Description: TsgPython grammar for tree-sitter
7+
URL: @URL@
8+
Version: @VERSION@
9+
Requires: @REQUIRES@
10+
Libs: -L${libdir} @ADDITIONAL_LIBS@ -ltree-sitter-tsg_python
11+
Cflags: -I${includedir}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package tree_sitter_tsg_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_tsg_python())
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package tree_sitter_tsg_python_test
2+
3+
import (
4+
"testing"
5+
6+
tree_sitter "github.com/smacker/go-tree-sitter"
7+
"github.com/tree-sitter/tree-sitter-tsg_python"
8+
)
9+
10+
func TestCanLoadGrammar(t *testing.T) {
11+
language := tree_sitter.NewLanguage(tree_sitter_tsg_python.Language())
12+
if language == nil {
13+
t.Errorf("Error loading TsgPython grammar")
14+
}
15+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/tree-sitter/tree-sitter-tsg_python
2+
3+
go 1.22
4+
5+
require github.com/smacker/go-tree-sitter v0.0.0-20230720070738-0d0a9f78d8f8

0 commit comments

Comments
 (0)