homebrew-core/Formula/tree-sitter.rb

112 lines
3.8 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.20.4.tar.gz"
sha256 "979ad0b36eb90975baf0c65d155d106276cac08afb1c2fe0ad54d4b7d498ce39"
license "MIT"
head "https://github.com/tree-sitter/tree-sitter.git", branch: "master"
bottle do
sha256 cellar: :any, arm64_monterey: "5dd02d0ee49c9fcc300d1fd5c3eb001acbbeb3ad7aca8c9d9ae7e480f181e31c"
sha256 cellar: :any, arm64_big_sur: "07ada407cc0dcbbecc7a74e186a41daf5b6e69a0689b6f49b15e50c47e12ebc2"
sha256 cellar: :any, monterey: "c92942aa105c4cace674a429eed93b940fa079536b18d8fcfd722346a257095f"
sha256 cellar: :any, big_sur: "a4f79e68a590f163703217d98eebdfe9af4fa3f002e23347a03e34f332b2efd0"
sha256 cellar: :any, catalina: "9ad214206cea385d1c2b99239d071d58f1c6e345e44fa63a051b0d9dafffd944"
sha256 cellar: :any_skip_relocation, x86_64_linux: "c4aff3b0e216a90fa1fa82dffe6eb1ba7e8f904407fc605e089ddb99a5e5ff62"
end
depends_on "emscripten" => [:build, :test]
depends_on "node" => [:build, :test]
depends_on "rust" => :build
def install
system "make", "AMALGAMATED=1"
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", "--abi=latest"
# 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