aboutsummaryrefslogtreecommitdiff
path: root/rakelib
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2026-11-13 12:01:17 +0100
committerEugen Wissner <belka@caraus.de>2026-11-13 13:15:30 +0100
commit7ab6e1021611cc6913ee4ab5faf1b31f928a65e9 (patch)
treeb6e650890682831503c73608ac7fd370d2847d8a /rakelib
parent0b5bd8114255e04dae27a7528a3111d6923e5fbe (diff)
downloadelna-7ab6e1021611cc6913ee4ab5faf1b31f928a65e9.tar.gz
Create parser stubs
Diffstat (limited to 'rakelib')
-rw-r--r--rakelib/ffi.rake63
-rw-r--r--rakelib/treesitter.rake23
2 files changed, 74 insertions, 12 deletions
diff --git a/rakelib/ffi.rake b/rakelib/ffi.rake
new file mode 100644
index 0000000..ad2832e
--- /dev/null
+++ b/rakelib/ffi.rake
@@ -0,0 +1,63 @@
+# 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 'ffi/clang'
+
+MUSL_BASE = Pathname.new "#{Dir.home}/Projects/gh/musl"
+MUSL_INCLUDES = ['include', 'arch/aarch64']
+ .map { |musl_include| "#{MUSL_BASE}/#{musl_include}" }
+ .freeze
+
+desc 'Generate bindings'
+task :ffi do
+ state = FFIState.new MUSL_INCLUDES
+
+ state.read_file "#{MUSL_BASE}/include/fenv.h"
+end
+
+# Collects binding.
+class FFIState
+ def initialize(includes)
+ @index = FFI::Clang::Index.new
+ @clang_args = includes.map { |include_path| "-I#{include_path}" }
+ end
+
+ def read_file(include_path)
+ tu = @index.parse_translation_unit include_path, @clang_args,
+ [], [:detailed_preprocessing_record]
+
+ # Verify there're now parse errors.
+ tu.diagnostics.each do |diag|
+ puts "[#{diag.severity}] #{diag.spelling} at #{diag.location.line}:#{diag.location.column}"
+ end
+
+ tu.cursor.visit_children do |cursor, _parent|
+ # Skip compiler internals.
+ next if cursor.location.file.nil?
+
+ read_definition cursor
+ end
+ end
+
+ private
+
+ def read_definition(cursor)
+ case cursor.kind
+ when :cursor_function
+ read_function cursor
+ when :cursor_struct
+ puts "Struct #{cursor.spelling}"
+ when :cursor_macro_definition
+ puts "Define #{cursor.spelling}"
+ when :cursor_inclusion_directive
+ # else
+ # puts "Current child #{cursor.kind}"
+ end
+ end
+
+ def read_function(cursor)
+ puts "Function #{cursor.spelling}"
+ end
+end
diff --git a/rakelib/treesitter.rake b/rakelib/treesitter.rake
index 8ccf48f..4c5494b 100644
--- a/rakelib/treesitter.rake
+++ b/rakelib/treesitter.rake
@@ -3,20 +3,19 @@
# 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
+TREE_SITTER_GRAMMAR = FileList[
+ TREE_SITTER_TREE / 'grammar.js',
+ TREE_SITTER_TREE / 'tree-sitter.json',
+ TREE_SITTER_TREE / 'queries/**/*.scm'
+]
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?
+ 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,
@@ -35,7 +34,7 @@ namespace :ts do
end
desc 'Synchronize tree-sitter tests with the compilable tests'
- task :sync do
+ task sync: :ts do
corpus = TREE_SITTER_TREE / 'test' / 'corpus'
rm_r corpus
mkdir_p corpus
@@ -56,10 +55,10 @@ namespace :ts do
end
desc 'Test tree-sitter'
- task :test do
+ 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: ['ts:generate', "#{TREE_SITTER_BUILD}/parser.#{TREE_SITTER_EXT}"]
+task ts: "#{TREE_SITTER_BUILD}/parser.#{TREE_SITTER_EXT}"