Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BC] Split parser into php_only parser and php parser (combined php/html) #180

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ repository = "https://github.com/tree-sitter/tree-sitter-php"
edition = "2018"

build = "bindings/rust/build.rs"
include = ["bindings/rust/*", "grammar.js", "queries/*", "src/*"]
include = [
"bindings/rust/*",
"tree-sitter-php/src/*",
"tree-sitter-php-only/src/*",
"tree-sitter-php/src/queries/*",
"tree-sitter-php-only/src/queries/*",
]

[lib]
path = "bindings/rust/lib.rs"
Expand Down
28 changes: 16 additions & 12 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,29 @@ import PackageDescription
let package = Package(
name: "TreeSitterPHP",
products: [
.library(name: "TreeSitterPHP", targets: ["TreeSitterPHP"]),
.library(name: "TreeSitterPHP", targets: ["TreeSitterPHP", "TreeSitterPHPOnly"]),
],
dependencies: [],
targets: [
.target(name: "TreeSitterPHP",
path: ".",
path: "tree-sitter-php",
exclude: [
"corpus",
"grammar.js",
],
sources: [
"src/parser.c",
],
resources: [
.copy("queries")
],
publicHeadersPath: "bindings/swift",
cSettings: [.headerSearchPath("src")])
.target(name: "TreeSitterPHPOnly",
path: "tree-sitter-php-only",
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",
Expand Down
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"
"tree-sitter-php/src",
"tree-sitter-php-only/src"
],
"sources": [
"src/parser.c",
"src/scanner.c",
"tree-sitter-php/src/parser.c",
"tree-sitter-php-only/src/parser.c",
"tree-sitter-php-only/src/scanner.c",
"bindings/node/binding.cc"
],
"cflags_c": [
Expand Down
12 changes: 9 additions & 3 deletions bindings/node/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using namespace v8;

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

namespace {

Expand All @@ -16,13 +17,18 @@ 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();
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);
Nan::Set(module, Nan::New("php").ToLocalChecked(), instance);

Local<Object> instance_only = constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(instance_only, 0, tree_sitter_php_only());
Nan::Set(instance_only, Nan::New("name").ToLocalChecked(), Nan::New("php_only").ToLocalChecked());
Nan::Set(module, Nan::New("php_only").ToLocalChecked(), instance_only);
}

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.nodeTypeInfo = require("../../tree-sitter-php/src/node-types.json");
module.exports.nodeTypeInfoOnly = require("../../tree-sitter-php-only/src/node-types.json");
} catch (_) {}
35 changes: 32 additions & 3 deletions bindings/rust/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
fn main() {
let src_dir = std::path::Path::new("src");
let src_dir = std::path::Path::new("tree-sitter-php/src");
let src_dir_only = std::path::Path::new("tree-sitter-php-only/src");

let mut c_config = cc::Build::new();
c_config.include(src_dir);
Expand All @@ -9,11 +10,39 @@ fn main() {
.flag_if_supported("-Wno-trigraphs");
let parser_path = src_dir.join("parser.c");
c_config.file(&parser_path);
c_config.compile("parser");
println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap());

let mut c_config = cc::Build::new();
c_config.include(src_dir_only);
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_only.join("parser.c");
c_config.file(&parser_path);
c_config.compile("parser_only");
println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap());

let mut c_config = cc::Build::new();
c_config.cpp(false);
c_config.include(src_dir);
c_config
.flag_if_supported("-Wno-unused-parameter")
.flag_if_supported("-Wno-unused-but-set-variable");
let scanner_path = src_dir.join("scanner.c");
c_config.file(&scanner_path);
c_config.compile("scanner");
println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap());

println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap());
c_config.compile("parser");
let mut c_config = cc::Build::new();
c_config.cpp(false);
c_config.include(src_dir_only);
c_config
.flag_if_supported("-Wno-unused-parameter")
.flag_if_supported("-Wno-unused-but-set-variable");
let scanner_path = src_dir_only.join("scanner.c");
c_config.file(&scanner_path);
c_config.compile("scanner_only");
println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap());
}
28 changes: 24 additions & 4 deletions bindings/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ 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.
Expand All @@ -28,17 +29,36 @@ pub fn language() -> Language {
unsafe { tree_sitter_php() }
}

/// Get the tree-sitter [Language][] for the php_only grammar.
///
/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
pub fn only_language() -> 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 NODE_TYPES: &'static str = include_str!("../../tree-sitter-php/src/node-types.json");

pub const NODE_TYPES_ONLY: &'static str = include_str!("../../tree-sitter-php-only/src/node-types.json");

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

pub const HIGHLIGHT_QUERY: &'static str = include_str!("../../queries/highlights.scm");
pub const INJECTIONS_QUERY: &'static str = include_str!("../../queries/injections.scm");
pub const HIGHLIGHT_QUERY_ONLY: &'static str = include_str!("../../tree-sitter-php-only/queries/highlights.scm");
pub const INJECTIONS_QUERY: &'static str = include_str!("../../tree-sitter-php/queries/injections.scm");
pub const INJECTIONS_QUERY_ONLY: &'static str = include_str!("../../tree-sitter-php-only/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 TAGS_QUERY_ONLY: &'static str = include_str!("../../tree-sitter-php-only/queries/tags.scm");

/// A parser that produces [`PHPTree`]s.
///
/// This is a convenience wrapper around [`language`] and [`only_language`].
pub struct PHPParser {
parser: Parser,
language: Language,
only_language: Language,
}

#[cfg(test)]
mod tests {
Expand Down
6 changes: 6 additions & 0 deletions bindings/swift/notes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
It would be more consistent to put Swift binding C headers here. But, that will not work because of two SPM limitations:

- Each target must have a unique directory
- C headers must be within that directory

Building both parsers as one target is possible, but would result in naming issue with the copied queries. If one day either of those limitations is removed, the per-parser bindings can be moved here.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
"shelljs": "^0.8.4"
},
"scripts": {
"build": "tree-sitter generate && node-gyp build",
"test": "tree-sitter test && node script/parse-examples.js"
"build": "(cd tree-sitter-php && tree-sitter generate --no-bindings) && (cd tree-sitter-php-only && tree-sitter generate --no-bindings) && node-gyp build",
"test": "(cd tree-sitter-php && tree-sitter test) && (cd tree-sitter-php-only && tree-sitter test) && node script/parse-examples.js"
},
"repository": {
"type": "git",
Expand All @@ -33,8 +33,7 @@
"scope": "source.php",
"file-types": [
"php"
],
"highlights": "queries/highlights.scm"
]
}
]
}
3 changes: 0 additions & 3 deletions queries/injections.scm

This file was deleted.

Loading