2021-09-24 21:13:52 +03:00
|
|
|
module walker
|
|
|
|
|
|
|
|
import toml.ast
|
|
|
|
|
2021-11-20 20:48:44 +03:00
|
|
|
// Visitor defines a visit method which is invoked by the walker on each Value node it encounters.
|
2021-09-24 21:13:52 +03:00
|
|
|
pub interface Visitor {
|
2022-10-16 09:28:57 +03:00
|
|
|
visit(value &ast.Value) !
|
2021-09-24 21:13:52 +03:00
|
|
|
}
|
|
|
|
|
2021-11-20 20:48:44 +03:00
|
|
|
// Modifier defines a modify method which is invoked by the walker on each Value node it encounters.
|
|
|
|
pub interface Modifier {
|
2022-10-16 09:28:57 +03:00
|
|
|
modify(mut value ast.Value) !
|
2021-11-20 20:48:44 +03:00
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +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
|
|
|
|
}
|
|
|
|
|
2022-11-22 20:45:12 +03:00
|
|
|
// visit calls the inspector callback on the specified Value node.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (i &Inspector) visit(value &ast.Value) ! {
|
2021-09-25 20:31:02 +03:00
|
|
|
i.inspector_callback(value, i.data) or { return err }
|
2021-09-24 21:13:52 +03:00
|
|
|
}
|
|
|
|
|
2021-09-25 20:31:02 +03:00
|
|
|
// inspect traverses and checks the AST Value node on a depth-first order and based on the data given
|
2022-10-16 09:28:57 +03:00
|
|
|
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
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn walk(visitor Visitor, value &ast.Value) ! {
|
2021-09-25 20:31:02 +03:00
|
|
|
if value is map[string]ast.Value {
|
|
|
|
value_map := value as map[string]ast.Value
|
|
|
|
for _, val in value_map {
|
2022-10-16 09:28:57 +03:00
|
|
|
walk(visitor, &val)!
|
2021-09-24 21:13:52 +03:00
|
|
|
}
|
2021-11-20 20:48:44 +03:00
|
|
|
}
|
|
|
|
if value is []ast.Value {
|
|
|
|
value_array := value as []ast.Value
|
|
|
|
for val in value_array {
|
2022-10-16 09:28:57 +03:00
|
|
|
walk(visitor, &val)!
|
2021-11-20 20:48:44 +03:00
|
|
|
}
|
2021-09-24 21:13:52 +03:00
|
|
|
} else {
|
2022-10-16 09:28:57 +03:00
|
|
|
visitor.visit(value)!
|
2021-09-24 21:13:52 +03:00
|
|
|
}
|
|
|
|
}
|
2021-11-20 20:48:44 +03:00
|
|
|
|
|
|
|
// walk_and_modify traverses the AST using the given modifier and lets the visitor
|
|
|
|
// modify the contents.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn walk_and_modify(modifier Modifier, mut value ast.Value) ! {
|
2021-11-20 20:48:44 +03:00
|
|
|
if value is map[string]ast.Value {
|
|
|
|
mut value_map := value as map[string]ast.Value
|
|
|
|
for _, mut val in value_map {
|
2022-10-16 09:28:57 +03:00
|
|
|
walk_and_modify(modifier, mut &val)!
|
2021-11-20 20:48:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if value is []ast.Value {
|
|
|
|
mut value_array := value as []ast.Value
|
|
|
|
for mut val in value_array {
|
2022-10-16 09:28:57 +03:00
|
|
|
walk_and_modify(modifier, mut &val)!
|
2021-11-20 20:48:44 +03:00
|
|
|
}
|
|
|
|
} else {
|
2022-10-16 09:28:57 +03:00
|
|
|
modifier.modify(mut value)!
|
2021-11-20 20:48:44 +03:00
|
|
|
}
|
|
|
|
}
|