treevee #
treevee
is a V implementation of a parser for the tree format
This is the WIP (work in progress) to provide v support for the tree format of the $mol frontend framework. Use it like this:
import koplenov.treevee
fn main() {
treeveee.Tree.from_string('simple_config') or { panic(err) }
}
Installing
- Using vpm:
v install koplenov.treevee
Usage
Select values:
tree := treevee.Tree.from_string_file('./tests/example_1.tree') or { panic(err) }
// integer
assert tree.value('port').int() == 8079
// string
assert tree.value('port').text() == '8079'
assert tree.value('database_root_password').text() == ''
assert tree.value('default_htaccess').text() == '.htaccess'
assert tree.value('server_name').text() == 'default server name'
Select nested values:
tree := treevee.Tree.from_string_file('./tests/example_2.tree') or { panic(err) }
assert tree.select('server', 'auth').kids[0].kids.len == 2
assert tree.value('server', 'auth', 'login').text() == 'root'
assert tree.value('server', 'auth', 'password').text() == 'qwerty'
Select arrays:
tree := treevee.Tree.from_string_file('./tests/example_3.tree') or { panic(err) }
// of strings
assert tree.value('user','hobby').array_of_strings() == ['kendo', 'dance', 'role play']
// of numbers
assert tree.value('user','loved_numbers').array_of_ints() == [7, 21, 42]
Other implementations
fn Span.begin #
fn Span.begin(uri string, source string) Span
Span.begin - Span for begin of unknown resource.
fn Span.end #
fn Span.end(uri string, source string) Span
Span.end - Makes new span for end of resource.
fn Span.entire #
fn Span.entire(uri string, source string) Span
Span.entire - Makes new span for entire resource.
fn Span.unknown #
fn Span.unknown() Span
Span.unknown - Span for begin of unknown resource.
fn Tree.from_string #
fn Tree.from_string(str string) !&Tree
Parses tree format from string.
fn Tree.from_string_file #
fn Tree.from_string_file(path string) !&Tree
fn Tree.from_string_with_uri #
fn Tree.from_string_with_uri(str string, uri string) !&Tree
fn Tree.list #
fn Tree.list(kids []&Tree, span Span) &Tree
fn Tree.struct #
fn Tree.struct(type string, kids []&Tree, span Span) &Tree
Makes struct node.
type TreePath #
type TreePath = string | int
struct Span #
struct Span {
pub:
uri string
source string
row int
col int
length int
}
fn (Span) span #
fn (span Span) span(row int, col int, length int) Span
fn (Span) to_string #
fn (span Span) to_string() string
fn (Span) str #
fn (span Span) str() string
struct Tree #
struct Tree {
pub:
// Type of structural node, `value` should be empty
type string
// Content of data node, `type` should be empty
value string
pub mut:
// Child nodes
kids []&Tree
span Span
}
fn (Tree) array_of_bool #
fn (tree &Tree) array_of_bool() []bool
array_of_bool - return bool array reptresentation of text node value.
fn (Tree) array_of_ints #
fn (tree &Tree) array_of_ints() []int
array_of_ints - return int array reptresentation of text node value.
fn (Tree) array_of_strings #
fn (tree &Tree) array_of_strings() []string
array_of_strings - return strings array reptresentation of text node value.
fn (Tree) bool #
fn (tree &Tree) bool() bool
bool - return bool reptresentation of text node value.
fn (Tree) int #
fn (tree &Tree) int() int
int - return int reptresentation of text node value.
fn (Tree) select #
fn (tree &Tree) select(path ...TreePath) &Tree
select - Query nodes by path.
fn (Tree) str #
fn (tree &Tree) str() string
str returns serialized tree node
fn (Tree) string #
fn (tree &Tree) string() string
string - return string reptresentation of text node value. (same as .text()
or .str()
)
fn (Tree) struct #
fn (tree Tree) struct(type string, kids []&Tree) &Tree
Makes new derived structural node.
fn (Tree) text #
fn (tree &Tree) text() string
Returns multiline text content.
fn (Tree) to_string #
fn (tree &Tree) to_string() string
Serializes to tree format.
fn (Tree) value #
fn (tree &Tree) value(path ...TreePath) &Tree
value - Select and unpack first value Tree node by path. Example node.value("port").int()
or node.value("config", "port").int()
If u need select node without take value, user select