Skip to content

Commit

Permalink
Split parser into php/php_only dialects with common files
Browse files Browse the repository at this point in the history
  • Loading branch information
calebdw committed Jan 4, 2024
1 parent 594b8ba commit 657546d
Show file tree
Hide file tree
Showing 48 changed files with 147,336 additions and 1,737 deletions.
16 changes: 15 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,18 @@ node_modules
build
package-lock.json
/target/
.build/
.build/
bindings/c/*.pc
bindings/c/*.h
*.a
*.o
*.so

# These files would be generated by 'tree-sitter generate' with the default
# settings. We don't want them because there's already a copy at the root.
php/Cargo.toml
php/binding.gyp
php/bindings
php_only/Cargo.toml
php_only/binding.gyp
php_only/bindings
10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ edition = "2021"
license = "MIT"

build = "bindings/rust/build.rs"
include = ["bindings/rust/*", "grammar.js", "queries/*", "src/*"]
include = [
"common",
"bindings/rust",
"php/grammar.js",
"php/src",
"php_only/grammar.js",
"php_only/src",
"queries",
]

[lib]
path = "bindings/rust/lib.rs"
Expand Down
11 changes: 5 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
VERSION := 0.19.0

# Repository
SRC_DIR := src

PARSER_REPO_URL := $(shell git -C $(SRC_DIR) remote get-url origin )
PARSER_REPO_URL := $(shell git -C $(PWD) remote get-url origin )

ifeq (, $(PARSER_NAME))
PARSER_NAME := $(shell basename $(PARSER_REPO_URL))
PARSER_NAME := $(subst tree-sitter-,,$(PARSER_NAME))
PARSER_NAME := $(subst .git,,$(PARSER_NAME))
endif

SRC_DIR := $(PARSER_NAME)/src

ifeq (, $(PARSER_URL))
PARSER_URL := $(subst :,/,$(PARSER_REPO_URL))
PARSER_URL := $(subst git@,https://,$(PARSER_URL))
Expand All @@ -29,7 +28,7 @@ PCLIBDIR ?= $(LIBDIR)/pkgconfig
CPPSRC := $(wildcard $(SRC_DIR)/*.cc)

ifeq (, $(CPPSRC))
ADDITIONALLIBS :=
ADDITIONALLIBS :=
else
ADDITIONALLIBS := -lc++
endif
Expand Down Expand Up @@ -71,7 +70,7 @@ endif
ifneq (,$(filter $(shell uname),FreeBSD NetBSD DragonFly))
PCLIBDIR := $(PREFIX)/libdata/pkgconfig
endif

all: libtree-sitter-$(PARSER_NAME).a libtree-sitter-$(PARSER_NAME).$(SOEXTVER) bindings/c/$(PARSER_NAME).h bindings/c/tree-sitter-$(PARSER_NAME).pc

libtree-sitter-$(PARSER_NAME).a: $(OBJ)
Expand Down
20 changes: 5 additions & 15 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,17 @@ let package = Package(
.target(name: "TreeSitterPHP",
path: ".",
exclude: [
"binding.gyp",
"bindings",
"Cargo.toml",
"corpus",
"grammar.js",
"LICENSE",
"Makefile",
"package.json",
"README.md",
"script",
"src/grammar.json",
"src/node-types.json",
],
sources: [
"src/parser.c",
"src/scanner.c",
"php/src/parser.c",
"php/src/scanner.c",
"php_only/src/parser.c",
"php_only/src/scanner.c",
],
resources: [
.copy("queries")
],
publicHeadersPath: "bindings/swift",
cSettings: [.headerSearchPath("src")])
cSettings: [.headerSearchPath("php/src")])
]
)
8 changes: 5 additions & 3 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
"target_name": "tree_sitter_php_binding",
"include_dirs": [
"<!(node -e \"require('nan')\")",
"src"
"php/src"
],
"sources": [
"src/parser.c",
"src/scanner.c",
"php/src/parser.c",
"php/src/scanner.c",
"php_only/src/parser.c",
"php_only/src/scanner.c",
"bindings/node/binding.cc"
],
"cflags_c": [
Expand Down
31 changes: 20 additions & 11 deletions bindings/node/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,33 @@
using namespace v8;

extern "C" TSLanguage * tree_sitter_php();
extern "C" TSLanguage * tree_sitter_php_only();

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_php());

Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("php").ToLocalChecked());
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
Local<FunctionTemplate> php_tpl = Nan::New<FunctionTemplate>(New);
php_tpl->SetClassName(Nan::New("Language").ToLocalChecked());
php_tpl->InstanceTemplate()->SetInternalFieldCount(1);
Local<Function> php_constructor = Nan::GetFunction(php_tpl).ToLocalChecked();
Local<Object> php_instance = php_constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(php_instance, 0, tree_sitter_php());
Nan::Set(php_instance, Nan::New("name").ToLocalChecked(), Nan::New("php").ToLocalChecked());

Local<FunctionTemplate> php_only_tpl = Nan::New<FunctionTemplate>(New);
php_only_tpl->SetClassName(Nan::New("Language").ToLocalChecked());
php_only_tpl->InstanceTemplate()->SetInternalFieldCount(1);
Local<Function> php_only_constructor = Nan::GetFunction(php_only_tpl).ToLocalChecked();
Local<Object> php_only_instance = php_only_constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(php_only_instance, 0, tree_sitter_php_only());
Nan::Set(php_only_instance, Nan::New("name").ToLocalChecked(), Nan::New("php_only").ToLocalChecked());

Nan::Set(exports, Nan::New("php").ToLocalChecked(), php_instance);
Nan::Set(exports, Nan::New("php_only").ToLocalChecked(), php_only_instance);
}

NODE_MODULE(tree_sitter_php_binding, Init)

} // namespace
} // namespace
3 changes: 2 additions & 1 deletion bindings/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ try {
}

try {
module.exports.nodeTypeInfo = require("../../src/node-types.json");
module.exports.php.nodeTypeInfo = require("../../php/src/node-types.json");
module.exports.php_only.nodeTypeInfo = require("../../php_only/src/node-types.json");
} catch (_) {}
1 change: 1 addition & 0 deletions bindings/node/php.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./index').php;
1 change: 1 addition & 0 deletions bindings/node/php_only.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./index').php_only;
28 changes: 19 additions & 9 deletions bindings/rust/build.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
fn main() {
let src_dir = std::path::Path::new("src");
let root_dir = std::path::Path::new(".");
let php_dir = root_dir.join("php").join("src");
let php_only_dir = root_dir.join("php_only").join("src");

let mut c_config = cc::Build::new();
c_config.include(src_dir);
c_config.include(&php_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);

let scanner_path = src_dir.join("scanner.c");
c_config.file(&scanner_path);
println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap());
for path in &[
php_dir.join("parser.c"),
php_dir.join("scanner.c"),
php_only_dir.join("parser.c"),
php_only_dir.join("scanner.c"),
] {
c_config.file(&path);
println!("cargo:rerun-if-changed={}", path.to_str().unwrap());
}

println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap());
c_config.compile("parser");
println!(
"cargo:rerun-if-changed={}",
root_dir.join("common").join("scanner.h").to_str().unwrap()
);

c_config.compile("parser-scanner");
}
13 changes: 11 additions & 2 deletions bindings/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,28 @@ use tree_sitter::Language;

extern "C" {
fn tree_sitter_php() -> Language;
fn tree_sitter_php_only() -> Language;
}

/// Get the tree-sitter [Language][] for this grammar.
///
/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
pub fn language() -> Language {
pub fn language_php() -> Language {
unsafe { tree_sitter_php() }
}

/// Get the tree-sitter [Language][] for this grammar.
///
/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
pub fn language_php_only() -> Language {
unsafe { tree_sitter_php_only() }
}

/// 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");
pub const PHP_NODE_TYPES: &'static str = include_str!("../../php/src/node-types.json");
pub const PHP_ONLY_NODE_TYPES: &'static str = include_str!("../../php_only/src/node-types.json");

// Uncomment these to include any queries that this grammar contains

Expand Down
1 change: 1 addition & 0 deletions bindings/swift/TreeSitterPHP/php.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern "C" {
#endif

extern TSLanguage *tree_sitter_php();
extern TSLanguage *tree_sitter_php_only();

#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit 657546d

Please sign in to comment.