Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Mar 18, 2024
1 parent 2659aaa commit 89a7ca1
Show file tree
Hide file tree
Showing 23 changed files with 82 additions and 83 deletions.
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[submodule "v_tree_sitter/core"]
path = tree_sitter/core
[submodule "tree_sitter_v_api/core"]
path = tree_sitter_v_api/core
url = https://github.com/tree-sitter/tree-sitter.git
branch = master
10 changes: 5 additions & 5 deletions analyzer/parser/parser.v
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module parser

import tree_sitter as ts
import tree_sitter_v_api as api
import os

// ParseResult represents the result of a parsing operation.
pub struct ParseResult {
pub:
tree &ts.Tree[ts.NodeType] = unsafe { nil } // Resulting tree or nil if the source could not be parsed.
tree &api.Tree[api.NodeType] = unsafe { nil } // Resulting tree or nil if the source could not be parsed.
source_text string // Source code.
pub mut:
path string // Path of the file that was parsed.
Expand Down Expand Up @@ -94,9 +94,9 @@ pub fn parse_code(code string) ParseResult {
// res2 = parser.parse_code_with_tree(code2, res.tree)
// println(res2.tree
// }
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(ts.language)
pub fn parse_code_with_tree(code string, old_tree &api.Tree[api.NodeType]) ParseResult {
mut parser := api.new_parser[api.NodeType](api.type_factory)
parser.set_language(api.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
4 changes: 2 additions & 2 deletions analyzer/psi/AstNode.v
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module psi

import tree_sitter as ts
import tree_sitter_v_api as api

pub fn (node AstNode) parent_of_type(typ ts.NodeType) ?AstNode {
pub fn (node AstNode) parent_of_type(typ api.NodeType) ?AstNode {
mut res := node
for {
res = res.parent()?
Expand Down
28 changes: 14 additions & 14 deletions analyzer/psi/PsiElement.v
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
module psi

import tree_sitter as ts
import tree_sitter_v_api as api

pub type ID = int

pub type AstNode = ts.Node[ts.NodeType]
pub type AstNode = api.Node[api.NodeType]

pub interface PsiElement {
node AstNode // base node from Tree Sitter
containing_file &PsiFile // file where the element is located
stub_id StubId
get_stub() ?&StubBase
stub_list() &StubList
element_type() ts.NodeType
element_type() api.NodeType
node() AstNode // return base node from Tree Sitter
containing_file() &PsiFile // return file where the element is located
is_equal(other PsiElement) bool // return true if the element is equal to the other element
Expand All @@ -31,21 +31,21 @@ pub interface PsiElement {
parent_nth(depth int) ?PsiElement
// parent_of_type returns the parent node with the specified type.
// If no such node exists, none is returned.
parent_of_type(typ ts.NodeType) ?PsiElement
parent_of_type(typ api.NodeType) ?PsiElement
// parent_of_any_type returns the parent node with one of the specified types.
// If no such node exists, none is returned.
parent_of_any_type(types ...ts.NodeType) ?PsiElement
parent_of_any_type(types ...api.NodeType) ?PsiElement
// inside returns true if the node is inside a node with the specified type.
inside(typ ts.NodeType) bool
inside(typ api.NodeType) bool
// is_parent_of returns true if the passed node is a child of the given node.
is_parent_of(element PsiElement) bool
// sibling_of_type_backward returns the previous node at the same nesting level with the specified type.
// If no such node exists, none is returned.
sibling_of_type_backward(typ ts.NodeType) ?PsiElement
sibling_of_type_backward(typ api.NodeType) ?PsiElement
// parent_of_type_or_self returns the parent node with the specified type, or the
// node itself if its type matches the specified one.
// If no such node exists, none is returned.
parent_of_type_or_self(typ ts.NodeType) ?PsiElement
parent_of_type_or_self(typ api.NodeType) ?PsiElement
// children returns all child nodes.
children() []PsiElement
// named_children returns child nodes except unknown nodes.
Expand Down Expand Up @@ -73,27 +73,27 @@ pub interface PsiElement {
prev_sibling() ?PsiElement
// prev_sibling_of_type returns the previous node at the same nesting level with the specified type.
// If no such node exists, none is returned.
prev_sibling_of_type(typ ts.NodeType) ?PsiElement
prev_sibling_of_type(typ api.NodeType) ?PsiElement
// prev_sibling_or_stub returns the previous node at the same nesting level or stub.
// If the node is the first child node or stub, none is returned.
prev_sibling_or_stub() ?PsiElement
// find_child_by_type returns the first child node with the specified type.
// If no such node is found, none is returned.
find_child_by_type(typ ts.NodeType) ?PsiElement
find_child_by_type(typ api.NodeType) ?PsiElement
// has_child_of_type returns true if the node has a child with the specified type.
has_child_of_type(typ ts.NodeType) bool
has_child_of_type(typ api.NodeType) bool
// find_child_by_type_or_stub returns the first child node with the specified type or stub.
// If no such node is found, none is returned.
find_child_by_type_or_stub(typ ts.NodeType) ?PsiElement
find_child_by_type_or_stub(typ api.NodeType) ?PsiElement
// find_child_by_name returns the first child node with the specified name.
// If no such node is found, none is returned.
find_child_by_name(name string) ?PsiElement
// find_children_by_type returns all child nodes with the specified type.
// If no such nodes are found, an empty array is returned.
find_children_by_type(typ ts.NodeType) []PsiElement
find_children_by_type(typ api.NodeType) []PsiElement
// find_children_by_type_or_stub returns all child nodes with the specified type or stub.
// If no such nodes are found, an empty array is returned.
find_children_by_type_or_stub(typ ts.NodeType) []PsiElement
find_children_by_type_or_stub(typ api.NodeType) []PsiElement
// get_text returns the text of the node.
get_text() string
// text_matches returns true if the text of the node matches the specified value.
Expand Down
28 changes: 14 additions & 14 deletions analyzer/psi/PsiElementImpl.v
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module psi

import tree_sitter as ts
import tree_sitter_v_api as api

pub struct PsiElementImpl {
pub:
Expand Down Expand Up @@ -48,7 +48,7 @@ pub fn (n &PsiElementImpl) stub_list() &StubList {
return n.stubs_list
}

pub fn (n &PsiElementImpl) element_type() ts.NodeType {
pub fn (n &PsiElementImpl) element_type() api.NodeType {
if stub := n.get_stub() {
return stub.element_type()
}
Expand Down Expand Up @@ -138,7 +138,7 @@ pub fn (n &PsiElementImpl) parent_nth(depth int) ?PsiElement {
return create_element(parent, n.containing_file)
}

pub fn (n &PsiElementImpl) parent_of_type(typ ts.NodeType) ?PsiElement {
pub fn (n &PsiElementImpl) parent_of_type(typ api.NodeType) ?PsiElement {
mut res := PsiElement(n)
for {
res = res.parent()?
Expand All @@ -150,7 +150,7 @@ pub fn (n &PsiElementImpl) parent_of_type(typ ts.NodeType) ?PsiElement {
return none
}

pub fn (n &PsiElementImpl) parent_of_any_type(types ...ts.NodeType) ?PsiElement {
pub fn (n &PsiElementImpl) parent_of_any_type(types ...api.NodeType) ?PsiElement {
mut res := PsiElement(n)
for {
res = res.parent()?
Expand All @@ -163,7 +163,7 @@ pub fn (n &PsiElementImpl) parent_of_any_type(types ...ts.NodeType) ?PsiElement
return none
}

pub fn (n &PsiElementImpl) inside(typ ts.NodeType) bool {
pub fn (n &PsiElementImpl) inside(typ api.NodeType) bool {
mut res := PsiElement(n)
for {
res = res.parent() or { return false }
Expand Down Expand Up @@ -196,7 +196,7 @@ pub fn (n &PsiElementImpl) is_parent_of(element PsiElement) bool {
return false
}

pub fn (n &PsiElementImpl) sibling_of_type_backward(typ ts.NodeType) ?PsiElement {
pub fn (n &PsiElementImpl) sibling_of_type_backward(typ api.NodeType) ?PsiElement {
mut res := PsiElement(n)
for {
res = res.prev_sibling_or_stub()?
Expand All @@ -208,7 +208,7 @@ pub fn (n &PsiElementImpl) sibling_of_type_backward(typ ts.NodeType) ?PsiElement
return none
}

pub fn (n &PsiElementImpl) parent_of_type_or_self(typ ts.NodeType) ?PsiElement {
pub fn (n &PsiElementImpl) parent_of_type_or_self(typ api.NodeType) ?PsiElement {
if n.node.type_name == typ {
return create_element(n.node, n.containing_file)
}
Expand Down Expand Up @@ -311,7 +311,7 @@ pub fn (n &PsiElementImpl) prev_sibling() ?PsiElement {
return create_element(sibling, n.containing_file)
}

pub fn (n &PsiElementImpl) prev_sibling_of_type(typ ts.NodeType) ?PsiElement {
pub fn (n &PsiElementImpl) prev_sibling_of_type(typ api.NodeType) ?PsiElement {
mut res := PsiElement(n)
for {
res = res.prev_sibling_or_stub()?
Expand All @@ -335,12 +335,12 @@ pub fn (n &PsiElementImpl) prev_sibling_or_stub() ?PsiElement {
return n.prev_sibling()
}

pub fn (n &PsiElementImpl) find_child_by_type(typ ts.NodeType) ?PsiElement {
pub fn (n &PsiElementImpl) find_child_by_type(typ api.NodeType) ?PsiElement {
ast_node := n.node.first_node_by_type(typ)?
return create_element(ast_node, n.containing_file)
}

pub fn (n &PsiElementImpl) has_child_of_type(typ ts.NodeType) bool {
pub fn (n &PsiElementImpl) has_child_of_type(typ api.NodeType) bool {
if stub := n.get_stub() {
return stub.has_child_of_type(node_type_to_stub_type(typ))
}
Expand All @@ -352,7 +352,7 @@ pub fn (n &PsiElementImpl) has_child_of_type(typ ts.NodeType) bool {
return false
}

pub fn (n &PsiElementImpl) find_child_by_type_or_stub(typ ts.NodeType) ?PsiElement {
pub fn (n &PsiElementImpl) find_child_by_type_or_stub(typ api.NodeType) ?PsiElement {
if stub := n.get_stub() {
child := stub.get_child_by_type(node_type_to_stub_type(typ))?
return child.get_psi()
Expand All @@ -367,7 +367,7 @@ pub fn (n &PsiElementImpl) find_child_by_name(name string) ?PsiElement {
return create_element(ast_node, n.containing_file)
}

pub fn (n &PsiElementImpl) find_children_by_type(typ ts.NodeType) []PsiElement {
pub fn (n &PsiElementImpl) find_children_by_type(typ api.NodeType) []PsiElement {
mut result := []PsiElement{}
mut child := n.node.first_child() or { return [] }
for {
Expand All @@ -379,7 +379,7 @@ pub fn (n &PsiElementImpl) find_children_by_type(typ ts.NodeType) []PsiElement {
return result
}

pub fn (n &PsiElementImpl) find_children_by_type_or_stub(typ ts.NodeType) []PsiElement {
pub fn (n &PsiElementImpl) find_children_by_type_or_stub(typ api.NodeType) []PsiElement {
if stub := n.get_stub() {
return stub.get_children_by_type(node_type_to_stub_type(typ)).get_psi()
}
Expand All @@ -395,7 +395,7 @@ pub fn (n &PsiElementImpl) find_children_by_type_or_stub(typ ts.NodeType) []PsiE
return result
}

pub fn (n &PsiElementImpl) find_last_child_by_type(typ ts.NodeType) ?PsiElement {
pub fn (n &PsiElementImpl) find_last_child_by_type(typ api.NodeType) ?PsiElement {
ast_node := n.node.last_node_by_type(typ)?
return create_element(ast_node, n.containing_file)
}
Expand Down
6 changes: 3 additions & 3 deletions analyzer/psi/PsiFile.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ import lsp
import time
import utils
import loglib
import tree_sitter as ts
import analyzer.parser
import tree_sitter_v_api as api

@[heap]
pub struct PsiFile {
pub:
path string
stub_list &StubList = unsafe { nil }
pub mut:
tree &ts.Tree[ts.NodeType] = unsafe { nil }
tree &api.Tree[api.NodeType] = unsafe { nil }
source_text string
root PsiElement
}

pub fn new_psi_file(path string, tree &ts.Tree[ts.NodeType], source_text string) &PsiFile {
pub fn new_psi_file(path string, tree &api.Tree[api.NodeType], source_text string) &PsiFile {
mut file := &PsiFile{
path: path
tree: unsafe { tree }
Expand Down
4 changes: 2 additions & 2 deletions analyzer/psi/PsiNamedElement.v
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module psi

import tree_sitter as ts
import tree_sitter_v_api as api

pub interface PsiNamedElement {
parent_of_type(typ ts.NodeType) ?PsiElement
parent_of_type(typ api.NodeType) ?PsiElement
identifier_text_range() TextRange
identifier() ?PsiElement
name() string
Expand Down
4 changes: 2 additions & 2 deletions analyzer/psi/StubBase.v
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module psi

import tree_sitter as ts
import tree_sitter_v_api as api

pub type StubId = int

Expand Down Expand Up @@ -74,7 +74,7 @@ pub fn (s &StubBase) stub_type() StubType {
return s.stub_type
}

pub fn (s &StubBase) element_type() ts.NodeType {
pub fn (s &StubBase) element_type() api.NodeType {
return match s.stub_type {
.root { .unknown }
.function_declaration { .function_declaration }
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 as ts
import tree_sitter_v_api as api

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 ts.NodeType) StubType {
pub fn node_type_to_stub_type(typ api.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,11 +1,11 @@
module psi

import tree_sitter as ts
import tree_sitter_v_api as api

struct TreeWalker {
mut:
already_visited_children bool
cursor ts.TreeCursor[ts.NodeType] @[required]
cursor api.TreeCursor[api.NodeType] @[required]
}

pub fn (mut tw TreeWalker) next() ?AstNode {
Expand Down
1 change: 0 additions & 1 deletion tree_sitter/core
Submodule core deleted from 30fd71
File renamed without changes.
4 changes: 2 additions & 2 deletions tree_sitter/bindings.v → tree_sitter_v_api/bindings.v
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module tree_sitter
module tree_sitter_v_api

import x.json2
import os

#flag -I @VMODROOT/tree_sitter
#flag -I @VMODROOT/tree_sitter_v_api
#flag -I @VMODROOT/tree_sitter_v/src
#flag @VMODROOT/tree_sitter_v/src/parser.c

Expand Down
1 change: 1 addition & 0 deletions tree_sitter_v_api/core
Submodule core added at d569d0
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module main

import tree_sitter as ts
import tree_sitter_v_api as api

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

code := '
fn foo() int {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module main

import tree_sitter as ts
import tree_sitter_v_api as api

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

code := 'fn main() {}'
tree := p.parse_string(source: code)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module main

import time
import tree_sitter as ts
import tree_sitter_v_api as api

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

code := '
fn foo() int {
Expand Down
Loading

0 comments on commit 89a7ca1

Please sign in to comment.