-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseArray.go
47 lines (38 loc) · 1.37 KB
/
ParseArray.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// =================================================================
//
// Copyright (C) 2019 Spatial Current, Inc. - All Rights Reserved
// Released as open source under the MIT License. See LICENSE file.
//
// =================================================================
package dfl
import (
"github.com/pkg/errors"
)
// ParseArray parses an array of nodes.
// If parameter "in" is gramatically a child node, then return the parent node.
// DFL arrays can include Attribute or Literal Nodes.
// As all attribute references must start with an "@" character, parantheses are optional for literals except if a comma exists.
// Below are some example inputs
//
// [bank, bureau_de_change, atm]
// [1, 2, @target]
// [Taco, Tacos, Burrito, Burritos, "Mexican Food", @example]
func ParseArray(in string, remainder string) (Node, string, error) {
nodes, err := ParseList(in)
if err != nil {
return &Array{}, remainder, errors.Wrap(err, "error parsing array "+in)
}
if len(remainder) == 0 || (len(remainder) >= 2 && remainder[0] == ':' && remainder[1] != '=') {
return &Array{Nodes: nodes}, remainder, nil
}
left := &Array{Nodes: nodes}
root, remainder, err := Parse(remainder)
if err != nil {
return root, remainder, err
}
err = AttachLeft(root, left)
if err != nil {
return root, remainder, errors.Wrap(err, "error attaching left for "+in)
}
return root, remainder, nil
}