v.ast.walker: pass nodes by reference
parent
9ffbda3833
commit
a4c9aeab14
|
@ -4,10 +4,10 @@ import v.ast
|
||||||
|
|
||||||
// Visitor defines a visit method which is invoked by the walker in each node it encounters.
|
// Visitor defines a visit method which is invoked by the walker in each node it encounters.
|
||||||
pub interface Visitor {
|
pub interface Visitor {
|
||||||
visit(node ast.Node) ?
|
visit(node &ast.Node) ?
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type InspectorFn = fn (node ast.Node, data voidptr) bool
|
pub type InspectorFn = fn (node &ast.Node, data voidptr) bool
|
||||||
|
|
||||||
struct Inspector {
|
struct Inspector {
|
||||||
inspector_callback InspectorFn
|
inspector_callback InspectorFn
|
||||||
|
@ -15,7 +15,7 @@ mut:
|
||||||
data voidptr
|
data voidptr
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (i &Inspector) visit(node ast.Node) ? {
|
pub fn (i &Inspector) visit(node &ast.Node) ? {
|
||||||
if i.inspector_callback(node, i.data) {
|
if i.inspector_callback(node, i.data) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -23,12 +23,12 @@ pub fn (i &Inspector) visit(node ast.Node) ? {
|
||||||
}
|
}
|
||||||
|
|
||||||
// inspect traverses and checks the AST node on a depth-first order and based on the data given
|
// inspect traverses and checks the AST node on a depth-first order and based on the data given
|
||||||
pub fn inspect(node ast.Node, data voidptr, inspector_callback InspectorFn) {
|
pub fn inspect(node &ast.Node, data voidptr, inspector_callback InspectorFn) {
|
||||||
walk(Inspector{inspector_callback, data}, node)
|
walk(Inspector{inspector_callback, data}, node)
|
||||||
}
|
}
|
||||||
|
|
||||||
// walk traverses the AST using the given visitor
|
// walk traverses the AST using the given visitor
|
||||||
pub fn walk(visitor Visitor, node ast.Node) {
|
pub fn walk(visitor Visitor, node &ast.Node) {
|
||||||
visitor.visit(node) or { return }
|
visitor.visit(node) or { return }
|
||||||
children := node.children()
|
children := node.children()
|
||||||
for child_node in children {
|
for child_node in children {
|
||||||
|
|
|
@ -15,7 +15,7 @@ mut:
|
||||||
node ast.Node
|
node ast.Node
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut n NodeByOffset) visit(node ast.Node) ? {
|
fn (mut n NodeByOffset) visit(node &ast.Node) ? {
|
||||||
node_pos := node.position()
|
node_pos := node.position()
|
||||||
if n.pos >= node_pos.pos && n.pos <= node_pos.pos + node_pos.len && node !is ast.File {
|
if n.pos >= node_pos.pos && n.pos <= node_pos.pos + node_pos.len && node !is ast.File {
|
||||||
n.node = node
|
n.node = node
|
||||||
|
@ -46,7 +46,7 @@ fn test_inspect() {
|
||||||
module main
|
module main
|
||||||
'
|
'
|
||||||
file := parse_text(source)
|
file := parse_text(source)
|
||||||
walker.inspect(file, voidptr(0), fn (node ast.Node, data voidptr) bool {
|
walker.inspect(file, voidptr(0), fn (node &ast.Node, data voidptr) bool {
|
||||||
// Second visit must be ast.Stmt
|
// Second visit must be ast.Stmt
|
||||||
if node is ast.Stmt {
|
if node is ast.Stmt {
|
||||||
if node !is ast.Module {
|
if node !is ast.Module {
|
||||||
|
|
Loading…
Reference in New Issue