Skip to content

Commit

Permalink
wip: decouple
Browse files Browse the repository at this point in the history
move binding p1: node type gen
  • Loading branch information
ttytm committed Mar 16, 2024
1 parent 59d104a commit 4aa5288
Show file tree
Hide file tree
Showing 15 changed files with 28 additions and 27 deletions.
5 changes: 3 additions & 2 deletions analyzer/parser/parser.v
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module parser

import tree_sitter_v as v
import v_tree_sitter as v
import tree_sitter_v
import v_tree_sitter.tree_sitter
import os

Expand Down Expand Up @@ -97,7 +98,7 @@ pub fn parse_code(code string) ParseResult {
// }
pub fn parse_code_with_tree(code string, old_tree &tree_sitter.Tree[v.NodeType]) ParseResult {
mut parser := tree_sitter.new_parser[v.NodeType](v.type_factory)
parser.set_language(v.language)
parser.set_language(tree_sitter_v.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{
Expand Down
2 changes: 1 addition & 1 deletion analyzer/psi/AstNode.v
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module psi

import tree_sitter_v as v
import v_tree_sitter as v

pub fn (node AstNode) parent_of_type(typ v.NodeType) ?AstNode {
mut res := node
Expand Down
2 changes: 1 addition & 1 deletion analyzer/psi/PsiElement.v
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module psi

import v_tree_sitter.tree_sitter
import tree_sitter_v as v
import v_tree_sitter as v

pub type ID = int

Expand Down
2 changes: 1 addition & 1 deletion analyzer/psi/PsiElementImpl.v
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module psi

import tree_sitter_v as v
import v_tree_sitter as v

pub struct PsiElementImpl {
pub:
Expand Down
2 changes: 1 addition & 1 deletion analyzer/psi/PsiFile.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import lsp
import time
import utils
import loglib
import tree_sitter_v as v
import v_tree_sitter as v
import analyzer.parser
import v_tree_sitter.tree_sitter

Expand Down
2 changes: 1 addition & 1 deletion analyzer/psi/PsiNamedElement.v
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module psi

import tree_sitter_v as v
import v_tree_sitter as v

pub interface PsiNamedElement {
parent_of_type(typ v.NodeType) ?PsiElement
Expand Down
2 changes: 1 addition & 1 deletion analyzer/psi/StubBase.v
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module psi

import tree_sitter_v as v
import v_tree_sitter as v

pub type StubId = int

Expand Down
4 changes: 2 additions & 2 deletions analyzer/psi/StubbedElementTypeImpl.v
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module psi

import utils
import tree_sitter_v
import v_tree_sitter

pub enum StubType as u8 {
root
Expand Down Expand Up @@ -60,7 +60,7 @@ pub enum StubType as u8 {
embedded_definition
}

pub fn node_type_to_stub_type(typ tree_sitter_v.NodeType) StubType {
pub fn node_type_to_stub_type(typ v_tree_sitter.NodeType) StubType {
return match typ {
.function_declaration { .function_declaration }
.receiver { .receiver }
Expand Down
4 changes: 2 additions & 2 deletions analyzer/psi/TreeWalker.v
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module psi

import v_tree_sitter.tree_sitter
import tree_sitter_v
import v_tree_sitter

struct TreeWalker {
mut:
already_visited_children bool
cursor tree_sitter.TreeCursor[tree_sitter_v.NodeType] @[required]
cursor tree_sitter.TreeCursor[v_tree_sitter.NodeType] @[required]
}

pub fn (mut tw TreeWalker) next() ?AstNode {
Expand Down
2 changes: 1 addition & 1 deletion v_tree_sitter/examples/cursor.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import v_tree_sitter.tree_sitter
import tree_sitter_v

fn main() {
mut p := tree_sitter.new_parser[tree_sitter_v.NodeType](tree_sitter_v.type_factory)
mut p := tree_sitter.new_parser[v_tree_sitter.NodeType](v_tree_sitter.type_factory)
p.set_language(tree_sitter_v.language)

code := '
Expand Down
2 changes: 1 addition & 1 deletion v_tree_sitter/examples/simple.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import v_tree_sitter.tree_sitter
import tree_sitter_v

fn main() {
mut p := tree_sitter.new_parser[tree_sitter_v.NodeType](tree_sitter_v.type_factory)
mut p := tree_sitter.new_parser[tree_sitter.NodeType](tree_sitter_v.type_factory)
p.set_language(tree_sitter_v.language)

code := 'fn main() {}'
Expand Down
2 changes: 1 addition & 1 deletion v_tree_sitter/examples/with_old_tree.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import tree_sitter_v
import time

fn main() {
mut p := tree_sitter.new_parser[tree_sitter_v.NodeType](tree_sitter_v.type_factory)
mut p := tree_sitter.new_parser[v_tree_sitter.NodeType](v_tree_sitter.type_factory)
p.set_language(tree_sitter_v.language)

code := '
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn (typ TSNodeType) is_anon() bool {
}

cur_dir := dir(@FILE)
node_types_json := read_file(join_path(cur_dir, 'src', 'node-types.json'))!
node_types_json := read_file(join_path(vmodules_dir(), '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'
Expand Down Expand Up @@ -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 tree_sitter_v.node_type_name_to_enum[type_name] or { NodeType.unknown }')
sb.writeln(' return v_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 = {')
Expand Down
16 changes: 8 additions & 8 deletions tree_sitter_v/node_types.v → v_tree_sitter/node_types.v
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This is an AUTO-GENERATED file. DO NOT EDIT this file directly! See `generate_types.vsh`
module tree_sitter_v
module v_tree_sitter

import arrays { merge }

Expand Down Expand Up @@ -259,11 +259,11 @@ const supertype__top_level_declaration_nodes = [
]

pub fn (typ NodeType) group() SuperType {
return if typ in tree_sitter_v.supertype__top_level_declaration_nodes {
return if typ in v_tree_sitter.supertype__top_level_declaration_nodes {
SuperType.top_level_declaration
} else if typ in tree_sitter_v.supertype__expression_nodes {
} else if typ in v_tree_sitter.supertype__expression_nodes {
SuperType.expression
} else if typ in tree_sitter_v.supertype__statement_nodes {
} else if typ in v_tree_sitter.supertype__statement_nodes {
SuperType.statement
} else {
SuperType.unknown
Expand Down Expand Up @@ -303,23 +303,23 @@ const literal_node_types = [
]

pub fn (typ NodeType) is_declaration() bool {
return typ in tree_sitter_v.declaration_node_types
return typ in v_tree_sitter.declaration_node_types
}

pub fn (typ NodeType) is_identifier() bool {
return typ in tree_sitter_v.identifier_node_types
return typ in v_tree_sitter.identifier_node_types
}

pub fn (typ NodeType) is_literal() bool {
return typ in tree_sitter_v.literal_node_types
return typ in v_tree_sitter.literal_node_types
}

pub const type_factory = &VNodeTypeFactory{}

pub struct VNodeTypeFactory {}

pub fn (nf VNodeTypeFactory) get_type(type_name string) NodeType {
return tree_sitter_v.node_type_name_to_enum[type_name] or { NodeType.unknown }
return v_tree_sitter.node_type_name_to_enum[type_name] or { NodeType.unknown }
}

const node_type_name_to_enum = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module tests
module v_tree_sitter

import v_tree_sitter.tree_sitter
import tree_sitter_v

fn test_simple() {
mut p := tree_sitter.new_parser[tree_sitter_v.NodeType](tree_sitter_v.type_factory)
mut p := tree_sitter.new_parser[NodeType](type_factory)
p.set_language(tree_sitter_v.language)

code := 'fn main() {}'
Expand Down

0 comments on commit 4aa5288

Please sign in to comment.