Skip to content
Snippets Groups Projects
Commit 6d0cddf6 authored by Leo Unoki's avatar Leo Unoki
Browse files

feat(tree-sitter): add tree sitter bindings

parent 09afc3ff
No related branches found
No related tags found
Loading
This commit is part of merge request !3238. Comments created here will be created in the context of that merge request.
......@@ -128,6 +128,7 @@
shellHook = ''
# This is a hack to work around the hack used in the dune files
export OPAM_SWITCH_PREFIX="${ligo.OPAM_SWITCH_PREFIX}";
export LIGO_TREE_SITTER="${ligo.LIGO_TREE_SITTER}";
'';
};
......
This diff is collapsed.
open Ctypes
open Foreign
(* ////////// Types ////////// *)
let ts_symbol = uint16_t
let ts_state_id = uint16_t
let ts_field_id = uint16_t
(* ////////// struct TSPoint ////////// *)
type ts_point
let ts_point: ts_point structure typ = structure "TSPoint"
let row = field ts_point "row" uint
let column = field ts_point "column" uint
let () = seal ts_point
(* ////////// struct TSRange ////////// *)
type ts_range
let ts_range: ts_range structure typ = structure "TSRange"
let start_point = field ts_range "start_point" ts_point
let endpoint = field ts_range "endpoint" ts_point
let start_byte = field ts_range "start_byte" uint32_t
let end_byte = field ts_range "end_byte" uint32_t
let () = seal ts_range
(* ////////// struct TSLanguage ////////// *)
type ts_language
let ts_language: ts_language structure typ = structure "TSLanguage"
(* ////////// struct TSTree ////////// *)
type ts_tree
let ts_tree: ts_tree structure typ = structure "TSTree"
(* ////////// struct TSParser ////////// *)
type ts_parser
let ts_parser: ts_parser structure typ = structure "TSParser"
(* ////////// struct TSNode ////////// *)
type ts_node
let ts_node: ts_tree structure typ = structure "TSNode"
(* CTypes doesn't suport an array so instead we declare 4 uint32 *)
let c1 = field ts_node "" (uint32_t)
let c2 = field ts_node "" (uint32_t)
let c3 = field ts_node "" (uint32_t)
let c4 = field ts_node "" (uint32_t)
let id = field ts_node "id" (ptr void)
let tree = field ts_node "tree" (ptr ts_tree)
let () = seal ts_node
(* FFI *)
let ts_parser_new = foreign "ts_parser_new" (void @-> returning (ptr ts_parser))
let ts_parser_delete = foreign "ts_parser_delete" (ptr ts_parser @-> returning void)
let ts_parser_set_language = foreign "ts_parser_set_language" (ptr ts_parser @-> ptr ts_language @-> returning bool)
let ts_parser_parse_string = foreign "ts_parser_parse_string" (ptr ts_parser @-> ptr ts_tree @-> string @-> uint32_t @-> returning (ptr ts_tree))
let ts_tree_delete = foreign "ts_tree_delete" (ptr ts_tree @-> returning void)
let ts_tree_root_node = foreign "ts_tree_root_node" (ptr ts_tree @-> returning (ts_node))
let ts_node_named_child = foreign "ts_node_named_child" (ts_node @-> uint32_t @-> returning ts_node)
let ts_node_type = foreign "ts_node_type" (ts_node @-> returning string)
let ts_node_child_count = foreign "ts_node_child_count" (ts_node @-> returning uint32_t)
let ts_node_string = foreign "ts_node_string" (ts_node @-> returning string)
\ No newline at end of file
(library
(name tree_sitter)
(libraries ctypes ctypes-foreign)
(modules bindings))
(executable
(name example)
(libraries ctypes ctypes-foreign tree_sitter tree_sitter_typescript)
(modules example)
(link_flags
:standard
-cclib
-ltree-sitter
-cclib
%{env:LIGO_TREE_SITTER=}/parser))
open Tree_sitter.Bindings
open Tree_sitter_typescript.Bindings
open Ctypes
open Unsigned
(* export DYLD_LIBRARY_PATH=$(pwd)/tree_sitter:$(pwd)/tree_sitter_typescript && dune build && dune exec tree_sitter/example.exe *)
let parse_typescript_string source_code =
let parser = ts_parser_new () in
let language = tree_sitter_typescript () in
let _ = ts_parser_set_language parser language in
let null_tree = from_voidp ts_tree null in
let tree = ts_parser_parse_string parser null_tree source_code (UInt32.of_int @@ String.length source_code) in ts_parser_delete parser; tree
let print_node node =
let str = ts_node_string node in (Printf.printf "\n\n TypeScript CST : %s\n\n" str)
let () =
let code = "[1, null]" in
let tree = parse_typescript_string code in
(* extract the node *)
let root_node = ts_tree_root_node tree in
let expr_stmt_node = ts_node_named_child root_node (UInt32.of_int 0) in
let array_node = ts_node_named_child expr_stmt_node (UInt32.of_int 0) in
let number_node = ts_node_named_child array_node (UInt32.of_int 0) in
let null_node = ts_node_named_child array_node (UInt32.of_int 1) in
(* get the node type *)
assert ((ts_node_type root_node) = "program");
assert ((ts_node_type expr_stmt_node) = "expression_statement");
assert ((ts_node_type array_node) = "array");
assert ((ts_node_type number_node) = "number");
assert ((ts_node_type null_node) = "null");
(* get the child count *)
assert ((ts_node_child_count root_node) = (UInt32.of_int 1));
assert ((ts_node_child_count array_node) = (UInt32.of_int 5));
assert ((ts_node_child_count number_node) = (UInt32.of_int 0));
print_node root_node; ts_tree_delete tree ;
\ No newline at end of file
#include "../tree_sitter/api.h"
// Declare the `tree_sitter_typescript` function, which is
// implemented by the `tree_sitter_typescript` library.
const TSLanguage *tree_sitter_typescript(void);
\ No newline at end of file
open Foreign
open Ctypes
open Tree_sitter.Bindings
let tree_sitter_typescript = foreign "tree_sitter_typescript" (void @-> returning (ptr ts_language))
\ No newline at end of file
(library
(name tree_sitter_typescript)
(libraries ctypes ctypes-foreign tree_sitter)
(modules bindings)) ; Ensure that the module name matches the file name
......@@ -3,6 +3,8 @@
lib,
pkgs,
tezos-ligo,
tree-sitter,
tree-sitter-grammars
}: let
inherit (pkgs) darwin ocamlPackages python3Packages coq_8_13 tezos-rust-libs;
in
......@@ -13,6 +15,7 @@ in
src = ./..;
OPAM_SWITCH_PREFIX = "${tezos-rust-libs}";
LIGO_TREE_SITTER = "${tree-sitter-grammars.tree-sitter-typescript}";
postPatch = ''
mkdir -p vendors/tezos-ligo
......@@ -26,6 +29,8 @@ in
crunch
odoc
python3Packages.jsonschema
tree-sitter
tree-sitter-grammars.tree-sitter-typescript
];
propagatedBuildInputs =
......@@ -97,6 +102,8 @@ in
alcotest # with-test
ppx_expect # with-test
ppx_inline_test # with-test
ctypes
ctypes-foreign
]
++ lib.optionals stdenv.isDarwin [
darwin.apple_sdk.frameworks.Security
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment