aboutsummaryrefslogtreecommitdiff
path: root/rakelib/treesitter.rake
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2026-10-05 00:26:05 +0200
committerEugen Wissner <belka@caraus.de>2026-10-05 01:00:50 +0200
commita9f6cfb6a8b6d38057faca067c015da1f72cf62d (patch)
treedb4e740db52df8be62bda5d2101603910107b7d4 /rakelib/treesitter.rake
parent5b7f983b3912a8d0fbeaf22f7fe0e2c5f1e0ff01 (diff)
downloadelna-a9f6cfb6a8b6d38057faca067c015da1f72cf62d.tar.gz
Add a treesitter grammar
Diffstat (limited to 'rakelib/treesitter.rake')
-rw-r--r--rakelib/treesitter.rake65
1 files changed, 65 insertions, 0 deletions
diff --git a/rakelib/treesitter.rake b/rakelib/treesitter.rake
new file mode 100644
index 0000000..7e48865
--- /dev/null
+++ b/rakelib/treesitter.rake
@@ -0,0 +1,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'
+
+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.so" => "#{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'
+task ts: "#{TREE_SITTER_BUILD}/parser.so"