aboutsummaryrefslogtreecommitdiff
path: root/rakelib/treesitter.rake
blob: 8ccf48f7dae7932474c21de9b7024d9f2fbc0163 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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

require 'rake/clean'

TREE_SITTER_BUILD = Pathname.new 'build/tree-sitter'
TREE_SITTER_TREE = Pathname.new 'tools/tree-sitter'
TREE_SITTER_EXT = RUBY_PLATFORM =~ /darwin/ ? 'dylib' : 'so'

CLEAN.include TREE_SITTER_BUILD

namespace :ts do
  directory TREE_SITTER_BUILD

  desc 'Generate a parser'
  task :generate do
    cp_r TREE_SITTER_TREE, TREE_SITTER_BUILD unless TREE_SITTER_BUILD.directory?

    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 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 do
    sh 'tree-sitter', 'test', '--grammar-path', TREE_SITTER_BUILD.to_path
  end
end

desc 'Build a tree-sitter parser'
task ts: ['ts:generate', "#{TREE_SITTER_BUILD}/parser.#{TREE_SITTER_EXT}"]