-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseVariable.go
43 lines (34 loc) · 1.41 KB
/
ParseVariable.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
// =================================================================
//
// Copyright (C) 2019 Spatial Current, Inc. - All Rights Reserved
// Released as open source under the MIT License. See LICENSE file.
//
// =================================================================
package dfl
import (
"strings"
"unicode"
"github.com/pkg/errors"
)
// ParseVariable parses a Variable Node from an input string
// If parameter "in" is gramatically a child node, then return the parent node.
// All variable references must start with an "$" character and have no spaces.
// For example, $amenity, $shop, $population, etc.
// Given those rules the remainder, if any, if simply parsed from the input strings
// Examples:
// node, err := ParseAttribute("$amenities := [bar, restaurant]")
func ParseVariable(in string, remainder string) (Node, string, error) {
if len(remainder) == 0 || (len(remainder) >= 2 && remainder[0] == ':' && remainder[1] != '=') {
return &Variable{Name: strings.TrimLeftFunc(in, unicode.IsSpace)[1:]}, remainder, nil
}
left := &Variable{Name: strings.TrimLeftFunc(in, unicode.IsSpace)[1:]}
root, remainder, err := Parse(remainder)
if err != nil {
return root, remainder, errors.Wrap(err, "error parsing remainder < "+remainder+" >")
}
err = AttachLeft(root, left)
if err != nil {
return root, remainder, errors.Wrap(err, "error attaching left for variable "+in)
}
return root, remainder, nil
}