-
-
Notifications
You must be signed in to change notification settings - Fork 187
/
trace.go
57 lines (52 loc) · 1.2 KB
/
trace.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
package participle
import (
"fmt"
"io"
"reflect"
"strings"
)
// Trace the parse to "w".
func Trace(w io.Writer) Option {
return func(p *Parser) error {
p.trace = w
return nil
}
}
type trace struct {
w io.Writer
indent int
node
}
func (t *trace) Parse(ctx *parseContext, parent reflect.Value) ([]reflect.Value, error) {
tok, _ := ctx.Peek(0)
fmt.Fprintf(t.w, "%s%q %s\n", strings.Repeat(" ", t.indent), tok, t.node.GoString())
return t.node.Parse(ctx, parent)
}
func injectTrace(w io.Writer, indent int, n node) node {
out := &trace{w, indent, n}
switch n := n.(type) {
case *disjunction:
for i, child := range n.nodes {
n.nodes[i] = injectTrace(w, indent+2, child)
}
case *strct:
n.expr = injectTrace(w, indent+2, n.expr)
case *sequence:
n.node = injectTrace(w, indent+2, n.node)
// injectTrace(w, indent, n.next)
case *parseable:
case *capture:
n.node = injectTrace(w, indent+2, n.node)
case *reference:
case *optional:
n.node = injectTrace(w, indent+2, n.node)
case *repetition:
n.node = injectTrace(w, indent+2, n.node)
case *negation:
n.node = injectTrace(w, indent+2, n.node)
case *literal:
case *group:
n.expr = injectTrace(w, indent+2, n.expr)
}
return out
}