1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/toml/ast/walker/walker.v

38 lines
947 B
V
Raw Normal View History

2021-09-24 21:13:52 +03:00
module walker
import toml.ast
// Visitor defines a visit method which is invoked by the walker in each Value node it encounters.
2021-09-24 21:13:52 +03:00
pub interface Visitor {
visit(value &ast.Value) ?
2021-09-24 21:13:52 +03:00
}
pub type InspectorFn = fn (value &ast.Value, data voidptr) ?
2021-09-24 21:13:52 +03:00
struct Inspector {
inspector_callback InspectorFn
mut:
data voidptr
}
pub fn (i &Inspector) visit(value &ast.Value) ? {
i.inspector_callback(value, i.data) or { return err }
2021-09-24 21:13:52 +03:00
}
// inspect traverses and checks the AST Value node on a depth-first order and based on the data given
pub fn inspect(value &ast.Value, data voidptr, inspector_callback InspectorFn) ? {
walk(Inspector{inspector_callback, data}, value) ?
2021-09-24 21:13:52 +03:00
}
// walk traverses the AST using the given visitor
pub fn walk(visitor Visitor, value &ast.Value) ? {
if value is map[string]ast.Value {
value_map := value as map[string]ast.Value
for _, val in value_map {
walk(visitor, &val) ?
2021-09-24 21:13:52 +03:00
}
} else {
visitor.visit(value) ?
2021-09-24 21:13:52 +03:00
}
}