From 48ad7f125be68a37b566417cfa1b97aafa253341 Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Sat, 16 Mar 2024 15:52:22 +0100 Subject: [PATCH] further simplify and decouple, split grammar and binding --- .gitignore | 1 + analyzer/parser/parser.v | 3 +-- {tree_sitter_v/bindings/v => tree_sitter}/bindings.h | 0 {tree_sitter_v => tree_sitter}/bindings.v | 11 +++++------ tree_sitter/examples/cursor.v | 7 +++---- tree_sitter/examples/simple.v | 7 +++---- tree_sitter/examples/with_old_tree.v | 7 +++---- tree_sitter/generate_types.vsh | 6 +++--- tree_sitter/simple_test.v | 7 +++---- .../tools/project-checker.v | 5 ++--- tree_sitter/tree_sitter.c.v | 6 +++--- tree_sitter/v.mod | 7 ------- tree_sitter_v/v.mod | 7 ------- 13 files changed, 27 insertions(+), 47 deletions(-) rename {tree_sitter_v/bindings/v => tree_sitter}/bindings.h (100%) rename {tree_sitter_v => tree_sitter}/bindings.v (67%) rename {tree_sitter_v => tree_sitter}/tools/project-checker.v (96%) delete mode 100644 tree_sitter/v.mod delete mode 100644 tree_sitter_v/v.mod diff --git a/.gitignore b/.gitignore index 0da793cf..994ea6fd 100644 --- a/.gitignore +++ b/.gitignore @@ -32,4 +32,5 @@ cache # generated executables on unix: install +generate_types cmd/v-analyzer/v-analyzer diff --git a/analyzer/parser/parser.v b/analyzer/parser/parser.v index a2469f64..a6c6d1c9 100644 --- a/analyzer/parser/parser.v +++ b/analyzer/parser/parser.v @@ -1,6 +1,5 @@ module parser -import tree_sitter_v import tree_sitter as ts import os @@ -97,7 +96,7 @@ pub fn parse_code(code string) ParseResult { // } pub fn parse_code_with_tree(code string, old_tree &ts.Tree[ts.NodeType]) ParseResult { mut parser := ts.new_parser[ts.NodeType](ts.type_factory) - parser.set_language(tree_sitter_v.language) + parser.set_language(ts.language) raw_tree := if isnil(old_tree) { unsafe { nil } } else { old_tree.raw_tree } tree := parser.parse_string(source: code, tree: raw_tree) return ParseResult{ diff --git a/tree_sitter_v/bindings/v/bindings.h b/tree_sitter/bindings.h similarity index 100% rename from tree_sitter_v/bindings/v/bindings.h rename to tree_sitter/bindings.h diff --git a/tree_sitter_v/bindings.v b/tree_sitter/bindings.v similarity index 67% rename from tree_sitter_v/bindings.v rename to tree_sitter/bindings.v index 71ea6f72..a015f036 100644 --- a/tree_sitter_v/bindings.v +++ b/tree_sitter/bindings.v @@ -1,16 +1,15 @@ -module tree_sitter_v +module tree_sitter import x.json2 import os -import tree_sitter -#flag -I @VMODROOT/bindings/v -#flag -I @VMODROOT/src -#flag @VMODROOT/src/parser.c +#flag -I @VMODROOT/tree_sitter +#flag -I @VMODROOT/tree_sitter_v/src +#flag @VMODROOT/tree_sitter_v/src/parser.c #include "bindings.h" -fn C.tree_sitter_v() &tree_sitter.TSLanguage +fn C.tree_sitter_v() &TSLanguage pub const language = unsafe { C.tree_sitter_v() } diff --git a/tree_sitter/examples/cursor.v b/tree_sitter/examples/cursor.v index dbf80065..8b1607c5 100644 --- a/tree_sitter/examples/cursor.v +++ b/tree_sitter/examples/cursor.v @@ -1,11 +1,10 @@ module main -import v_tree_sitter.tree_sitter -import tree_sitter_v +import tree_sitter as ts fn main() { - mut p := tree_sitter.new_parser[v_tree_sitter.NodeType](v_tree_sitter.type_factory) - p.set_language(tree_sitter_v.language) + mut p := ts.new_parser[ts.NodeType](ts.type_factory) + p.set_language(ts.language) code := ' fn foo() int { diff --git a/tree_sitter/examples/simple.v b/tree_sitter/examples/simple.v index fe861ae5..fa6401a5 100644 --- a/tree_sitter/examples/simple.v +++ b/tree_sitter/examples/simple.v @@ -1,11 +1,10 @@ module main -import v_tree_sitter.tree_sitter -import tree_sitter_v +import tree_sitter as ts fn main() { - mut p := tree_sitter.new_parser[tree_sitter.NodeType](tree_sitter_v.type_factory) - p.set_language(tree_sitter_v.language) + mut p := ts.new_parser[ts.NodeType](ts.type_factory) + p.set_language(ts.language) code := 'fn main() {}' tree := p.parse_string(source: code) diff --git a/tree_sitter/examples/with_old_tree.v b/tree_sitter/examples/with_old_tree.v index bb53f704..ecf70adf 100644 --- a/tree_sitter/examples/with_old_tree.v +++ b/tree_sitter/examples/with_old_tree.v @@ -1,12 +1,11 @@ module main -import v_tree_sitter.tree_sitter -import tree_sitter_v import time +import tree_sitter as ts fn main() { - mut p := tree_sitter.new_parser[v_tree_sitter.NodeType](v_tree_sitter.type_factory) - p.set_language(tree_sitter_v.language) + mut p := ts.new_parser[ts.NodeType](ts.type_factory) + p.set_language(ts.language) code := ' fn foo() int { diff --git a/tree_sitter/generate_types.vsh b/tree_sitter/generate_types.vsh index dc796aa1..6a3ac345 100644 --- a/tree_sitter/generate_types.vsh +++ b/tree_sitter/generate_types.vsh @@ -58,7 +58,7 @@ fn (typ TSNodeType) is_anon() bool { } cur_dir := dir(@FILE) -node_types_json := read_file(join_path(vmodules_dir(), 'tree_sitter_v', 'src', 'node-types.json'))! +node_types_json := read_file(join_path(@VMODROOT, 'tree_sitter_v', 'src', 'node-types.json'))! node_types := json.decode([]TSNodeType, node_types_json)! node_type_enum_name := 'NodeType' super_type_enum_name := 'SuperType' @@ -70,7 +70,7 @@ mut supertype_node_groups := map[string][]string{} sb.writeln('// This is an AUTO-GENERATED file. DO NOT EDIT this file directly! See `generate_types.vsh`') -sb.writeln('module tree_sitter_v') +sb.writeln('module tree_sitter') sb.writeln('\n') sb.writeln('import arrays { merge }') @@ -170,7 +170,7 @@ sb.writeln('\n') sb.writeln('pub struct ${node_type_factory_sym_name} {}') sb.writeln('\n') sb.writeln('pub fn (nf ${node_type_factory_sym_name}) get_type(type_name string) ${node_type_enum_name} {') -sb.writeln(' return v_tree_sitter.node_type_name_to_enum[type_name] or { NodeType.unknown }') +sb.writeln(' return tree_sitter.node_type_name_to_enum[type_name] or { NodeType.unknown }') sb.writeln('}') sb.writeln('\n') sb.writeln('const node_type_name_to_enum = {') diff --git a/tree_sitter/simple_test.v b/tree_sitter/simple_test.v index 46c50265..b2981080 100644 --- a/tree_sitter/simple_test.v +++ b/tree_sitter/simple_test.v @@ -1,9 +1,8 @@ -import tree_sitter -import tree_sitter_v +import tree_sitter as ts fn test_simple() { - mut p := tree_sitter.new_parser[tree_sitter.NodeType](tree_sitter.type_factory) - p.set_language(tree_sitter_v.language) + mut p := ts.new_parser[ts.NodeType](ts.type_factory) + p.set_language(ts.language) code := 'fn main() {}' tree := p.parse_string(source: code) diff --git a/tree_sitter_v/tools/project-checker.v b/tree_sitter/tools/project-checker.v similarity index 96% rename from tree_sitter_v/tools/project-checker.v rename to tree_sitter/tools/project-checker.v index 61afad6f..669f4c45 100644 --- a/tree_sitter_v/tools/project-checker.v +++ b/tree_sitter/tools/project-checker.v @@ -6,8 +6,7 @@ import sync import os import time import analyzer.parser -import v_tree_sitter.tree_sitter -import tree_sitter_v as v +import tree_sitter as ts fn main() { mut checker := Checker{ @@ -16,7 +15,7 @@ fn main() { checker.check() } -pub type AstNode = tree_sitter.Node[v.NodeType] +pub type AstNode = ts.Node[ts.NodeType] struct ErrorInfo { path string diff --git a/tree_sitter/tree_sitter.c.v b/tree_sitter/tree_sitter.c.v index e9debe82..d691144f 100644 --- a/tree_sitter/tree_sitter.c.v +++ b/tree_sitter/tree_sitter.c.v @@ -7,9 +7,9 @@ module tree_sitter // See the header file "./lib/api.h" for comments for functions and structures. // We directly build "lib.c" rather using the static library. -#flag -I @VMODROOT/core/lib/include -#flag -I @VMODROOT/core/lib/src -#flag @VMODROOT/core/lib/src/lib.c +#flag -I @VMODROOT/tree_sitter/core/lib/include +#flag -I @VMODROOT/tree_sitter/core/lib/src +#flag @VMODROOT/tree_sitter/core/lib/src/lib.c #include "tree_sitter/api.h" pub enum TSVInputEncoding { diff --git a/tree_sitter/v.mod b/tree_sitter/v.mod deleted file mode 100644 index 95ae87ac..00000000 --- a/tree_sitter/v.mod +++ /dev/null @@ -1,7 +0,0 @@ -Module { - name: 'v_tree_sitter' - description: 'V bindings for tree-sitter' - version: '0.1.1' - license: 'MIT' - dependencies: [] -} diff --git a/tree_sitter_v/v.mod b/tree_sitter_v/v.mod deleted file mode 100644 index da73a071..00000000 --- a/tree_sitter_v/v.mod +++ /dev/null @@ -1,7 +0,0 @@ -Module { - name: 'tree_sitter_v' - description: 'Tree sitter grammar for V' - version: '0.0.4-beta.1' - license: 'MIT' - dependencies: [] -}