-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexport_test.go
103 lines (95 loc) · 2.19 KB
/
export_test.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
package yarex
import (
"fmt"
"strings"
)
// Export private functions only for test
var (
Parse = parse
OptimizeAst = optimizeAst
AstMatch = astMatch
)
// MustCompileOp is identical to MustCompile, but ignores compiled version of regexp
// and returns OpTree version.
func MustCompileOp(ptn string) *Regexp {
ast, err := parse(ptn)
if err != nil {
panic(err)
}
ast = optimizeAst(ast)
op := opCompile(ast)
return &Regexp{ptn, opExecer{op}}
}
func IsOpMatcher(r *Regexp) bool {
_, ok := r.exe.(opExecer)
return ok
}
func IsCompiledMatcher(r *Regexp) bool {
_, ok := r.exe.(*compiledExecer)
return ok
}
func DumpAst(re Ast) string {
var buf strings.Builder
dumpAux(re, 0, &buf)
return buf.String()
}
func indent(n int) string {
return strings.Repeat(" ", n)
}
func dumpAux(re Ast, n int, buf *strings.Builder) {
switch v := re.(type) {
case AstLit:
fmt.Fprintf(buf, "%sLit{%q}", indent(n), string(v))
return
case *AstSeq:
if len(v.seq) == 0 {
fmt.Fprintf(buf, "%sSeq{ }", indent(n))
return
}
fmt.Fprintf(buf, "%sSeq{\n", indent(n))
for _, r := range v.seq {
dumpAux(r, n+1, buf)
buf.WriteRune('\n')
}
fmt.Fprintf(buf, "%s}", indent(n))
return
case *AstAlt:
if len(v.opts) == 0 {
fmt.Fprintf(buf, "%sAlt{ }", indent(n))
return
}
fmt.Fprintf(buf, "%sAlt{\n", indent(n))
for _, r := range v.opts {
dumpAux(r, n+1, buf)
buf.WriteRune('\n')
}
fmt.Fprintf(buf, "%s}", indent(n))
return
case AstNotNewline:
fmt.Fprintf(buf, "%sNotNewLine", indent(n))
return
case *AstRepeat:
fmt.Fprintf(buf, "%sRepeat(min=%d,max=%d){\n", indent(n), v.min, v.max)
dumpAux(v.re, n+1, buf)
fmt.Fprintf(buf, "\n%s}", indent(n))
return
case *AstCap:
fmt.Fprintf(buf, "%sCapture(index=%d){\n", indent(n), v.index)
dumpAux(v.re, n+1, buf)
fmt.Fprintf(buf, "\n%s}", indent(n))
return
case AstBackRef:
fmt.Fprintf(buf, "%sBackRef(index=%d)", indent(n), int(v))
return
case AstAssertBegin:
fmt.Fprintf(buf, "%sAssertBegin", indent(n))
return
case AstAssertEnd:
fmt.Fprintf(buf, "%sAssertEnd", indent(n))
return
case AstCharClass:
fmt.Fprintf(buf, "%sCharClass%s", indent(n), v)
return
}
panic(fmt.Errorf("IMPLEMENT DUMP for %T", re))
}