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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
package participle
import (
"bytes"
"fmt"
"strings"
"github.com/alecthomas/participle/lexer"
)
type stringerVisitor struct {
bytes.Buffer
seen map[node]bool
}
func stringern(n node, depth int) string {
v := &stringerVisitor{seen: map[node]bool{}}
v.visit(n, depth)
return v.String()
}
func stringer(n node) string {
return stringern(n, 1)
}
func (s *stringerVisitor) visit(n node, depth int) { // nolint: gocognit
if s.seen[n] || depth <= 0 {
fmt.Fprintf(s, "...")
return
}
s.seen[n] = true
switch n := n.(type) {
case *disjunction:
for i, c := range n.nodes {
if i > 0 {
fmt.Fprint(s, " | ")
}
s.visit(c, depth)
}
case *strct:
s.visit(n.expr, depth)
case *sequence:
c := n
for i := 0; c != nil && depth-i > 0; c, i = c.next, i+1 {
if c != n {
fmt.Fprint(s, " ")
}
s.visit(c.node, depth-i)
}
case *parseable:
fmt.Fprintf(s, "<%s>", strings.ToLower(n.t.Name()))
case *capture:
if _, ok := n.node.(*parseable); ok {
fmt.Fprintf(s, "<%s>", strings.ToLower(n.field.Name))
} else {
if n.node == nil {
fmt.Fprintf(s, "<%s>", strings.ToLower(n.field.Name))
} else {
s.visit(n.node, depth)
}
}
case *reference:
fmt.Fprintf(s, "<%s>", strings.ToLower(n.identifier))
case *optional:
composite := compositeNode(map[node]bool{}, n, true)
if composite {
fmt.Fprint(s, "(")
}
s.visit(n.node, depth)
if composite {
fmt.Fprint(s, ")")
}
fmt.Fprint(s, "?")
case *repetition:
composite := compositeNode(map[node]bool{}, n, true)
if composite {
fmt.Fprint(s, "(")
}
s.visit(n.node, depth)
if composite {
fmt.Fprint(s, ")")
}
fmt.Fprint(s, "*")
case *negation:
fmt.Fprintf(s, "!")
composite := compositeNode(map[node]bool{}, n, true)
if composite {
fmt.Fprint(s, "(")
}
s.visit(n.node, depth)
if composite {
fmt.Fprint(s, ")")
}
case *literal:
fmt.Fprintf(s, "%q", n.s)
if n.t != lexer.EOF && n.s == "" {
fmt.Fprintf(s, ":%s", n.tt)
}
case *group:
composite := (n.mode != groupMatchOnce) && compositeNode(map[node]bool{}, n, true)
if composite {
fmt.Fprint(s, "(")
}
if child, ok := n.expr.(*group); ok && child.mode == groupMatchOnce {
s.visit(child.expr, depth)
} else if child, ok := n.expr.(*capture); ok {
if grandchild, ok := child.node.(*group); ok && grandchild.mode == groupMatchOnce {
s.visit(grandchild.expr, depth)
} else {
s.visit(n.expr, depth)
}
} else {
s.visit(n.expr, depth)
}
if composite {
fmt.Fprint(s, ")")
}
switch n.mode {
case groupMatchNonEmpty:
fmt.Fprintf(s, "!")
case groupMatchZeroOrOne:
fmt.Fprintf(s, "?")
case groupMatchZeroOrMore:
fmt.Fprintf(s, "*")
case groupMatchOneOrMore:
fmt.Fprintf(s, "+")
}
default:
panic("unsupported")
}
}
func compositeNode(seen map[node]bool, n node, strctAsComposite bool) bool {
if n == nil || seen[n] {
return false
}
seen[n] = true
switch n := n.(type) {
case *sequence:
return n.next != nil
case *disjunction:
for _, c := range n.nodes {
if compositeNode(seen, c, strctAsComposite) {
return true
}
}
return false
case *reference, *literal, *parseable:
return false
case *negation:
return compositeNode(seen, n.node, strctAsComposite)
case *strct:
return strctAsComposite && compositeNode(seen, n.expr, strctAsComposite)
case *capture:
return compositeNode(seen, n.node, strctAsComposite)
case *optional:
return compositeNode(seen, n.node, strctAsComposite)
case *repetition:
return compositeNode(seen, n.node, strctAsComposite)
case *group:
return compositeNode(seen, n.expr, strctAsComposite)
default:
panic("unsupported")
}
}
|