# This Source Code Form is subject to the terms of the Mozilla Public License, # v. 2.0. If a copy of the MPL was not distributed with this file, You can # obtain one at https://mozilla.org/MPL/2.0/. -} # frozen_string_literal: true TREE_SITTER_BUILD = Pathname.new 'build/tree-sitter' TREE_SITTER_TREE = Pathname.new 'tools/tree-sitter' TREE_SITTER_EXT = RUBY_PLATFORM =~ /darwin/ ? 'dylib' : 'so' TREE_SITTER_GRAMMAR = FileList[ TREE_SITTER_TREE / 'grammar.js', TREE_SITTER_TREE / 'tree-sitter.json', TREE_SITTER_TREE / 'queries/**/*.scm' ] namespace :ts do file "#{TREE_SITTER_BUILD}/src/parser.c" => TREE_SITTER_GRAMMAR do rm_rf TREE_SITTER_BUILD cp_r TREE_SITTER_TREE, TREE_SITTER_BUILD sh 'tree-sitter', 'generate', '--output', (TREE_SITTER_BUILD / 'src').to_path, (TREE_SITTER_TREE / 'grammar.js').to_path build_test = TREE_SITTER_BUILD / 'test' build_corpus = build_test / 'corpus' rm_r build_corpus File.symlink (TREE_SITTER_TREE / 'test' / 'corpus').relative_path_from(build_test), build_corpus end file "#{TREE_SITTER_BUILD}/parser.#{TREE_SITTER_EXT}" => "#{TREE_SITTER_BUILD}/src/parser.c" do |t| sh 'build/host/install/bin/gcc', '-O2', '-Wall', '-fPIC', '-shared', t.source, '-o', t.name end desc 'Synchronize tree-sitter tests with the compilable tests' task sync: :ts do corpus = TREE_SITTER_TREE / 'test' / 'corpus' rm_r corpus mkdir_p corpus FileList['testsuite/compilable/*.elna'].map do |test| target = corpus / Pathname.new(test).basename.sub_ext('.txt').basename File.open target, 'w' do |output| output.write "==================\n" output.write "#{File.basename(test, '.elna')}\n" output.write "==================\n\n" output.write File.read(test) output.write "\n---\n\n" end end sh 'tree-sitter', 'test', '--update', '--grammar-path', TREE_SITTER_BUILD.to_path end desc 'Test tree-sitter' task test: :ts do sh 'tree-sitter', 'test', '--grammar-path', TREE_SITTER_BUILD.to_path end end desc 'Build a tree-sitter parser' task ts: "#{TREE_SITTER_BUILD}/parser.#{TREE_SITTER_EXT}"