-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontext.go
106 lines (93 loc) · 2.17 KB
/
context.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package denada
import "fmt"
type RuleContext struct {
This ElementList
parent *RuleContext
}
func NullContext() RuleContext {
return RuleContext{
This: ElementList{},
parent: nil,
}
}
func RootContext(elems ElementList) RuleContext {
return RuleContext{
This: elems,
parent: nil,
}
}
func ChildContext(elems ElementList, parent *RuleContext) RuleContext {
return RuleContext{
This: elems,
parent: parent,
}
}
func (c RuleContext) String() string {
names := []string{}
for _, e := range c.This {
rule, err := ParseRuleName(e.Description)
if err != nil {
names = append(names, "<non-rule>")
} else {
names = append(names, rule.Name)
}
}
if c.parent == nil {
return fmt.Sprintf("[%s]", names)
} else {
return fmt.Sprintf("[%s (%v)]", names, c.parent)
}
}
func (c RuleContext) Find(path ...string) (ret RuleContext, err error) {
/* If no path is provided, they must me this context */
if len(path) == 0 {
ret = c
return
}
/* Take the first element of the path and check against some "reserved" names */
head := path[0]
/* Are they looking in the current context? */
if head == "." {
return c.Find(path[1:]...)
}
/* Are they looking for the parent context? */
if head == ".." {
if c.parent == nil {
/* If we are at the root, we have no parent */
err = fmt.Errorf("Requested rule context at root level")
return
} else {
/* Otherwise, resume the search in our parent context */
return c.parent.Find(path[1:]...)
}
}
/* Are they looking for the root context? */
if head == "$root" {
if c.parent == nil {
/* If we are at the root, search this context */
return c.Find(path[1:]...)
} else {
/* If not, ask our parent for $root */
return c.parent.Find(path...)
}
}
/* Check to see if head matches any uniquely named child definitions */
result := ElementList{}
for _, d := range c.This {
if !d.IsDefinition() {
continue
}
rule, err := ParseRuleName(d.Description)
if err != nil {
continue
}
if rule.Name == head {
result = append(result, d.Contents...)
}
}
if result != nil {
return ChildContext(result, &c), nil
}
err = fmt.Errorf("Unable to find context %s", head)
return
}