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

toml: rename ast.Node -> ast.Value (#11974)

This commit is contained in:
Larpon
2021-09-25 19:31:02 +02:00
committed by GitHub
parent 80c15607da
commit 13b2aa701c
6 changed files with 111 additions and 103 deletions

View File

@@ -22,7 +22,7 @@ mut:
peek_tok token.Token
skip_next bool
// The root map (map is called table in TOML world)
root_map map[string]ast.Node
root_map map[string]ast.Value
root_map_key string
// Array of Tables state
last_aot string
@@ -49,11 +49,11 @@ pub fn new_parser(config Config) Parser {
// init initializes the parser.
pub fn (mut p Parser) init() ? {
p.root_map = map[string]ast.Node{}
p.root_map = map[string]ast.Value{}
p.next() ?
}
// run_checker validates the parsed `ast.Node` nodes in the
// run_checker validates the parsed `ast.Value` nodes in the
// the generated AST.
fn (mut p Parser) run_checker() ? {
if p.config.run_checks {
@@ -120,9 +120,9 @@ fn (mut p Parser) expect(expected_token token.Kind) ? {
// If some segments of the key does not exist in the root table find_table will
// allocate a new map for each segment. This behavior is needed because you can
// reference maps by multiple keys "dotted" (separated by "." periods) in TOML documents.
pub fn (mut p Parser) find_table() ?&map[string]ast.Node {
pub fn (mut p Parser) find_table() ?&map[string]ast.Value {
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'locating "$p.root_map_key" in map ${ptr_str(p.root_map)}')
mut t := &map[string]ast.Node{}
mut t := &map[string]ast.Value{}
unsafe {
t = &p.root_map
}
@@ -144,13 +144,13 @@ pub fn (mut p Parser) sub_table_key(key string) (string, string) {
// If some segments of the key does not exist in the input map find_in_table will
// allocate a new map for the segment. This behavior is needed because you can
// reference maps by multiple keys "dotted" (separated by "." periods) in TOML documents.
pub fn (mut p Parser) find_sub_table(key string) ?&map[string]ast.Node {
pub fn (mut p Parser) find_sub_table(key string) ?&map[string]ast.Value {
mut ky := p.root_map_key + '.' + key
if p.root_map_key == '' {
ky = key
}
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'locating "$ky" in map ${ptr_str(p.root_map)}')
mut t := &map[string]ast.Node{}
mut t := &map[string]ast.Value{}
unsafe {
t = &p.root_map
}
@@ -165,12 +165,12 @@ pub fn (mut p Parser) find_sub_table(key string) ?&map[string]ast.Node {
// If some segments of the key does not exist in the input map find_in_table will
// allocate a new map for the segment. This behavior is needed because you can
// reference maps by multiple keys "dotted" (separated by "." periods) in TOML documents.
pub fn (mut p Parser) find_in_table(mut table map[string]ast.Node, key string) ?&map[string]ast.Node {
pub fn (mut p Parser) find_in_table(mut table map[string]ast.Value, key string) ?&map[string]ast.Value {
// NOTE This code is the result of much trial and error.
// I'm still not quite sure *exactly* why it works. All I can leave here is a hope
// that this kind of minefield someday will be easier in V :)
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'locating "$key" in map ${ptr_str(table)}')
mut t := &map[string]ast.Node{}
mut t := &map[string]ast.Value{}
unsafe {
t = &table
}
@@ -184,9 +184,9 @@ pub fn (mut p Parser) find_in_table(mut table map[string]ast.Node, key string) ?
' this should never happen. Key "$k" was checked before access')
}
{
if val is map[string]ast.Node {
if val is map[string]ast.Value {
// unsafe {
t = &(t[k] as map[string]ast.Node)
t = &(t[k] as map[string]ast.Value)
//}
} else {
return error(@MOD + '.' + @STRUCT + '.' + @FN + ' "$k" is not a map')
@@ -195,8 +195,8 @@ pub fn (mut p Parser) find_in_table(mut table map[string]ast.Node, key string) ?
} else {
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'no key "$k" found, allocating new map "$k" in map ${ptr_str(t)}"')
// unsafe {
t[k] = map[string]ast.Node{}
t = &(t[k] as map[string]ast.Node)
t[k] = map[string]ast.Value{}
t = &(t[k] as map[string]ast.Value)
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'allocated new map ${ptr_str(t)}"')
//}
}
@@ -221,7 +221,7 @@ pub fn (mut p Parser) sub_key() ?string {
return text
}
// root_table parses next tokens into the root map of `ast.Node`s.
// root_table parses next tokens into the root map of `ast.Value`s.
// The V `map` type is corresponding to a "table" in TOML.
pub fn (mut p Parser) root_table() ? {
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'parsing root table...')
@@ -306,9 +306,9 @@ fn (p Parser) excerpt() string {
return p.scanner.excerpt(p.tok.pos, 10)
}
// inline_table parses next tokens into a map of `ast.Node`s.
// inline_table parses next tokens into a map of `ast.Value`s.
// The V map type is corresponding to a "table" in TOML.
pub fn (mut p Parser) inline_table(mut tbl map[string]ast.Node) ? {
pub fn (mut p Parser) inline_table(mut tbl map[string]ast.Value) ? {
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'parsing inline table into ${ptr_str(tbl)}...')
for p.tok.kind != .eof {
@@ -371,8 +371,8 @@ pub fn (mut p Parser) inline_table(mut tbl map[string]ast.Node) ? {
}
}
// array_of_tables parses next tokens into an array of `ast.Node`s.
pub fn (mut p Parser) array_of_tables(mut table map[string]ast.Node) ? {
// array_of_tables parses next tokens into an array of `ast.Value`s.
pub fn (mut p Parser) array_of_tables(mut table map[string]ast.Value) ? {
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'parsing array of tables "$p.tok.kind" "$p.tok.lit"')
// NOTE this is starting to get ugly. TOML isn't simple at this point
p.check(.lsbr) ? // '[' bracket
@@ -396,8 +396,8 @@ pub fn (mut p Parser) array_of_tables(mut table map[string]ast.Node) ? {
' this should never happen. Key "$key_str" was checked before access')
}
{
if val is []ast.Node {
arr := &(table[key_str] as []ast.Node)
if val is []ast.Value {
arr := &(table[key_str] as []ast.Value)
arr << p.double_bracket_array() ?
table[key_str] = arr
} else {
@@ -413,8 +413,8 @@ pub fn (mut p Parser) array_of_tables(mut table map[string]ast.Node) ? {
p.last_aot_index = 0
}
// double_array_of_tables parses next tokens into an array of tables of arrays of `ast.Node`s...
pub fn (mut p Parser) double_array_of_tables(mut table map[string]ast.Node) ? {
// double_array_of_tables parses next tokens into an array of tables of arrays of `ast.Value`s...
pub fn (mut p Parser) double_array_of_tables(mut table map[string]ast.Value) ? {
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'parsing array of tables of arrays "$p.tok.kind" "$p.tok.lit"')
key := p.key() ?
@@ -443,16 +443,16 @@ pub fn (mut p Parser) double_array_of_tables(mut table map[string]ast.Node) ? {
unsafe {
// NOTE this is starting to get EVEN uglier. TOML is not at all simple at this point...
if p.last_aot != first {
table[first] = []ast.Node{}
table[first] = []ast.Value{}
p.last_aot = first
mut t_arr := &(table[p.last_aot] as []ast.Node)
t_arr << map[string]ast.Node{}
mut t_arr := &(table[p.last_aot] as []ast.Value)
t_arr << map[string]ast.Value{}
p.last_aot_index = 0
}
mut t_arr := &(table[p.last_aot] as []ast.Node)
mut t_arr := &(table[p.last_aot] as []ast.Value)
mut t_map := t_arr[p.last_aot_index]
mut t := &(t_map as map[string]ast.Node)
mut t := &(t_map as map[string]ast.Value)
if last in t.keys() {
if val := t[last] or {
@@ -460,8 +460,8 @@ pub fn (mut p Parser) double_array_of_tables(mut table map[string]ast.Node) ? {
' this should never happen. Key "$last" was checked before access')
}
{
if val is []ast.Node {
arr := &(val as []ast.Node)
if val is []ast.Value {
arr := &(val as []ast.Value)
arr << p.double_bracket_array() ?
t[last] = arr
} else {
@@ -475,11 +475,11 @@ pub fn (mut p Parser) double_array_of_tables(mut table map[string]ast.Node) ? {
}
}
// array parses next tokens into an array of `ast.Node`s.
pub fn (mut p Parser) double_bracket_array() ?[]ast.Node {
mut arr := []ast.Node{}
// array parses next tokens into an array of `ast.Value`s.
pub fn (mut p Parser) double_bracket_array() ?[]ast.Value {
mut arr := []ast.Value{}
for p.tok.kind in [.bare, .quoted, .boolean, .number] && p.peek_tok.kind == .assign {
mut tbl := map[string]ast.Node{}
mut tbl := map[string]ast.Value{}
key, val := p.key_value() ?
tbl[key.str()] = val
arr << tbl
@@ -488,17 +488,17 @@ pub fn (mut p Parser) double_bracket_array() ?[]ast.Node {
return arr
}
// array parses next tokens into an array of `ast.Node`s.
pub fn (mut p Parser) array() ?[]ast.Node {
// array parses next tokens into an array of `ast.Value`s.
pub fn (mut p Parser) array() ?[]ast.Value {
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'parsing array...')
mut arr := []ast.Node{}
mut arr := []ast.Value{}
p.expect(.lsbr) ? // '[' bracket
for p.tok.kind != .eof {
p.next() ?
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'parsing token "$p.tok.kind" "$p.tok.lit"')
match p.tok.kind {
.boolean {
arr << ast.Node(p.boolean() ?)
arr << ast.Value(p.boolean() ?)
}
.comma {
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'skipping comma array value seperator "$p.tok.lit"')
@@ -514,20 +514,20 @@ pub fn (mut p Parser) array() ?[]ast.Node {
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'skipping comment "$c.text"')
}
.lcbr {
mut t := map[string]ast.Node{}
mut t := map[string]ast.Value{}
p.inline_table(mut t) ?
ast.Node(t)
ast.Value(t)
}
.number {
val := p.number_or_date() ?
arr << val
}
.quoted {
arr << ast.Node(p.quoted())
arr << ast.Value(p.quoted())
}
.lsbr {
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'parsing array in array "$p.tok.kind" "$p.tok.lit"')
arr << ast.Node(p.array() ?)
arr << ast.Value(p.array() ?)
}
.rsbr {
break
@@ -603,9 +603,9 @@ pub fn (mut p Parser) key() ?ast.Key {
return key
}
// key_value parse and returns a pair `ast.Key` and `ast.Node` type.
// key_value parse and returns a pair `ast.Key` and `ast.Value` type.
// see also `key()` and `value()`
pub fn (mut p Parser) key_value() ?(ast.Key, ast.Node) {
pub fn (mut p Parser) key_value() ?(ast.Key, ast.Value) {
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'parsing key value pair...')
key := p.key() ?
p.next() ?
@@ -615,40 +615,40 @@ pub fn (mut p Parser) key_value() ?(ast.Key, ast.Node) {
return key, value
}
// value parse and returns an `ast.Node` type.
// value parse and returns an `ast.Value` type.
// values are the token(s) appearing after an assignment operator (=).
pub fn (mut p Parser) value() ?ast.Node {
pub fn (mut p Parser) value() ?ast.Value {
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'parsing value...')
// println('parsed comment "${p.tok.lit}"')
mut value := ast.Node(ast.Null{})
mut value := ast.Value(ast.Null{})
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'parsing token "$p.tok.kind" "$p.tok.lit"')
// mut value := ast.Node{}
// mut value := ast.Value{}
if p.tok.kind == .number {
number_or_date := p.number_or_date() ?
value = number_or_date
} else {
value = match p.tok.kind {
.quoted {
ast.Node(p.quoted())
ast.Value(p.quoted())
}
.boolean {
ast.Node(p.boolean() ?)
ast.Value(p.boolean() ?)
}
.lsbr {
ast.Node(p.array() ?)
ast.Value(p.array() ?)
}
.lcbr {
mut t := map[string]ast.Node{}
mut t := map[string]ast.Value{}
p.inline_table(mut t) ?
// table[key_str] = ast.Node(t)
ast.Node(t)
// table[key_str] = ast.Value(t)
ast.Value(t)
}
else {
error(@MOD + '.' + @STRUCT + '.' + @FN +
' value expected .boolean, .quoted, .lsbr, .lcbr or .number got "$p.tok.kind" "$p.tok.lit"')
ast.Node(ast.Null{}) // TODO workaround bug
ast.Value(ast.Null{}) // TODO workaround bug
}
}
}
@@ -656,25 +656,25 @@ pub fn (mut p Parser) value() ?ast.Node {
return value
}
// number_or_date parse and returns an `ast.Node` type as
// number_or_date parse and returns an `ast.Value` type as
// one of [`ast.Date`, `ast.Time`, `ast.DateTime`, `ast.Number`]
pub fn (mut p Parser) number_or_date() ?ast.Node {
pub fn (mut p Parser) number_or_date() ?ast.Value {
// Handle Date/Time
if p.peek_tok.kind == .minus || p.peek_tok.kind == .colon {
date_time_type := p.date_time() ?
match date_time_type {
ast.Date {
return ast.Node(date_time_type as ast.Date)
return ast.Value(date_time_type as ast.Date)
}
ast.Time {
return ast.Node(date_time_type as ast.Time)
return ast.Value(date_time_type as ast.Time)
}
ast.DateTime {
return ast.Node(date_time_type as ast.DateTime)
return ast.Value(date_time_type as ast.DateTime)
}
}
}
return ast.Node(p.number())
return ast.Value(p.number())
}
// bare parse and returns an `ast.Bare` type.