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

toml: add value decoding (#12521)

This commit is contained in:
Larpon
2021-11-20 18:48:44 +01:00
committed by GitHub
parent 4b9e8e243c
commit f1dd0e3355
8 changed files with 206 additions and 64 deletions

View File

@@ -2,11 +2,16 @@ module walker
import toml.ast
// Visitor defines a visit method which is invoked by the walker in each Value node it encounters.
// Visitor defines a visit method which is invoked by the walker on each Value node it encounters.
pub interface Visitor {
visit(value &ast.Value) ?
}
// Modifier defines a modify method which is invoked by the walker on each Value node it encounters.
pub interface Modifier {
modify(mut value ast.Value) ?
}
pub type InspectorFn = fn (value &ast.Value, data voidptr) ?
struct Inspector {
@@ -31,7 +36,32 @@ pub fn walk(visitor Visitor, value &ast.Value) ? {
for _, val in value_map {
walk(visitor, &val) ?
}
}
if value is []ast.Value {
value_array := value as []ast.Value
for val in value_array {
walk(visitor, &val) ?
}
} else {
visitor.visit(value) ?
}
}
// walk_and_modify traverses the AST using the given modifier and lets the visitor
// modify the contents.
pub fn walk_and_modify(modifier Modifier, mut value ast.Value) ? {
if value is map[string]ast.Value {
mut value_map := value as map[string]ast.Value
for _, mut val in value_map {
walk_and_modify(modifier, mut &val) ?
}
}
if value is []ast.Value {
mut value_array := value as []ast.Value
for mut val in value_array {
walk_and_modify(modifier, mut &val) ?
}
} else {
modifier.modify(mut value) ?
}
}