homebrew-core/Formula/tree-sitter.rb

110 lines
3.5 KiB
Ruby

require "language/node"
class TreeSitter < Formula
desc "Parser generator tool and incremental parsing library"
homepage "https://tree-sitter.github.io/"
url "https://github.com/tree-sitter/tree-sitter/archive/v0.18.3.tar.gz"
sha256 "0803a19426aa98a06fd7fae817b72e7c3ce8684f3383870e2bd087a35d2b5612"
license "MIT"
head "https://github.com/tree-sitter/tree-sitter.git"
bottle do
sha256 cellar: :any, arm64_big_sur: "9c4e040fe7a53732521d8cb9e1676fd80ad3bed14ea390c24e495dd1b2d8467d"
sha256 cellar: :any, big_sur: "3b7c77795a3c46a8cdc8c4d5b4457b653ceb178d2f2da9b2a80dd49c34e6a54b"
sha256 cellar: :any, catalina: "55c0f022701ea6f6729e54e80049ca1c36e3c83db7c023ebeb14e40fc8e4cc1f"
sha256 cellar: :any, mojave: "919edfa4f479991fa1d8796ae9de999924a5c41463200acca30812199519c9b5"
end
depends_on "emscripten" => [:build, :test]
depends_on "node" => [:build, :test]
depends_on "rust" => :build
def install
system "make"
system "make", "install", "PREFIX=#{prefix}"
# NOTE: This step needs to be done *before* `cargo install`
cd "lib/binding_web" do
system "npm", "install", *Language::Node.local_npm_install_args
end
system "script/build-wasm"
cd "cli" do
system "cargo", "install", *std_cargo_args
end
# Install the wasm module into the prefix.
# NOTE: This step needs to be done *after* `cargo install`.
%w[tree-sitter.js tree-sitter-web.d.ts tree-sitter.wasm package.json].each do |file|
(lib/"binding_web").install "lib/binding_web/#{file}"
end
end
test do
# a trivial tree-sitter test
assert_equal "tree-sitter #{version}", shell_output("#{bin}/tree-sitter --version").strip
# test `tree-sitter generate`
(testpath/"grammar.js").write <<~EOS
module.exports = grammar({
name: 'YOUR_LANGUAGE_NAME',
rules: {
source_file: $ => 'hello'
}
});
EOS
system bin/"tree-sitter", "generate"
# test `tree-sitter parse`
(testpath/"test/corpus/hello.txt").write <<~EOS
hello
EOS
parse_result = shell_output("#{bin}/tree-sitter parse #{testpath}/test/corpus/hello.txt").strip
assert_equal("(source_file [0, 0] - [1, 0])", parse_result)
# test `tree-sitter test`
(testpath/"test"/"corpus"/"test_case.txt").write <<~EOS
=========
hello
=========
hello
---
(source_file)
EOS
system "#{bin}/tree-sitter", "test"
(testpath/"test_program.c").write <<~EOS
#include <string.h>
#include <tree_sitter/api.h>
int main(int argc, char* argv[]) {
TSParser *parser = ts_parser_new();
if (parser == NULL) {
return 1;
}
// Because we have no language libraries installed, we cannot
// actually parse a string successfully. But, we can verify
// that it can at least be attempted.
const char *source_code = "empty";
TSTree *tree = ts_parser_parse_string(
parser,
NULL,
source_code,
strlen(source_code)
);
if (tree == NULL) {
printf("tree creation failed");
}
ts_tree_delete(tree);
ts_parser_delete(parser);
return 0;
}
EOS
system ENV.cc, "test_program.c", "-L#{lib}", "-ltree-sitter", "-o", "test_program"
assert_equal "tree creation failed", shell_output("./test_program")
# test `tree-sitter build-wasm`
ENV.delete "CPATH"
system bin/"tree-sitter", "build-wasm"
end
end