This repository has been archived by the owner on Mar 8, 2020. It is now read-only.
/
xpathdescription.go
126 lines (111 loc) · 2.71 KB
/
xpathdescription.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package ann
import (
"bytes"
"fmt"
"strings"
)
// xPathDescription is a folder (as in "analyzer of recursive data
// structures combining the information in its nodes", not as in
// "directory"). It traverses Rules in pre-order, generating the
// XPath-like description of each rule and a human readable description
// of their associated actions.
type xPathDescription struct {
current path
descriptions []string
}
// A path represents how to get from the root of a rule to one of its
// nodes. We push nodes as we go deeper into the tree and pop them back
// when we climb towards its root.
type path []*Rule
func (p *path) push(rule *Rule) {
*p = append(*p, rule)
}
func (p *path) pop() {
(*p)[len(*p)-1] = nil
*p = (*p)[:len(*p)-1]
}
// Returns the path in a format similar to XPath.
func (p *path) String() string {
var buf bytes.Buffer
for _, r := range *p {
buf.WriteRune('/')
fmt.Fprintf(&buf, "%s::*", r.axis)
for _, p := range r.predicates {
fmt.Fprintf(&buf, "[%s]", p)
}
}
return buf.String()
}
// Calculates the description for all the nodes in the rule.
func (f *xPathDescription) fold(r *Rule) {
if len(r.actions) == 0 && len(r.rules) == 0 {
return
}
(&f.current).push(r)
defer f.current.pop()
if len(r.actions) != 0 {
s := fmt.Sprintf("| %s | %s |",
markdownEscape(f.current.String()),
markdownEscape(joinActions(r.actions, ", ")))
f.descriptions = append(f.descriptions, abbreviate(s))
}
for _, child := range r.rules {
f.fold(child)
}
}
func joinActions(as []Action, sep string) string {
var buf bytes.Buffer
_sep := ""
for _, e := range as {
fmt.Fprintf(&buf, "%s%s", _sep, e)
_sep = sep
}
return buf.String()
}
// Idempotent.
func abbreviate(s string) string {
// Replace the On(Any).Something at the begining with root
if !strings.HasPrefix(s, `| /self::\*\[\*\] | `) {
s = strings.Replace(s, `/self::\*\[\*\]`, "", 1)
}
// replace descendant:: with //
s = strings.Replace(s, `/descendant::\*`, `//\*`, -1) // no limit
// replace child:: with /
s = strings.Replace(s, "/child::", "/", -1) // no limit
return s
}
func markdownEscape(s string) string {
var buf bytes.Buffer
for _, r := range s {
if mustEscape(r) {
buf.WriteRune('\\')
}
buf.WriteRune(r)
}
return buf.String()
}
func mustEscape(r rune) bool {
return r == '\\' ||
r == '|' ||
r == '*' ||
r == '_' ||
r == '{' ||
r == '}' ||
r == '[' ||
r == ']' ||
r == '(' ||
r == ')' ||
r == '#' ||
r == '+' ||
r == '-' ||
r == '.' ||
r == '!'
}
// Returns a string with all the description separated by a newline.
func (f *xPathDescription) String() string {
var buf bytes.Buffer
for _, e := range f.descriptions {
fmt.Fprintf(&buf, "%s\n", e)
}
return buf.String()
}