-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptimize-ast.go
171 lines (165 loc) · 3.58 KB
/
optimize-ast.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
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
package yarex
import "fmt"
func optimizeAst(re Ast) Ast {
re = optimizeAstFlattenSeqAndAlt(re)
re = optimizeAstUnwrapSingletonSeqAndAlt(re)
return re
}
// optimizeAstFlattenSeqAndAlt flattens nested AstSeqs and AstAlts
func optimizeAstFlattenSeqAndAlt(re Ast) Ast {
switch v := re.(type) {
case *AstSeq:
out := make([]Ast, 0, len(v.seq))
for _, r := range v.seq {
r = optimizeAstFlattenSeqAndAlt(r)
if as, ok := r.(*AstSeq); ok {
out = append(out, as.seq...)
} else {
out = append(out, r)
}
}
return &AstSeq{out}
case *AstAlt:
out := make([]Ast, 0, len(v.opts))
for _, r := range v.opts {
r = optimizeAstFlattenSeqAndAlt(r)
if aa, ok := r.(*AstAlt); ok {
out = append(out, aa.opts...)
} else {
out = append(out, r)
}
}
return &AstAlt{out}
case *AstRepeat:
out := *v
out.re = optimizeAstFlattenSeqAndAlt(v.re)
return &out
case *AstCap:
out := *v
out.re = optimizeAstFlattenSeqAndAlt(v.re)
return &out
case AstLit, AstNotNewline, AstAssertBegin, AstAssertEnd, AstBackRef, AstCharClass:
return v
default:
panic(fmt.Errorf("IMPLEMENT optimizeAstFlattenSeqAndAlt for %T", re))
}
}
// optimizeAstUnwrapSingletonSeqAndAlt joins adjacent literals, and unwrap seqs and alts
// containing a single re as much as possible
func optimizeAstUnwrapSingletonSeqAndAlt(re Ast) Ast {
switch v := re.(type) {
case *AstSeq:
out := make([]Ast, 0, len(v.seq))
var acc *string = nil
for _, r := range v.seq {
r = optimizeAstUnwrapSingletonSeqAndAlt(r)
if lit, ok := r.(AstLit); ok {
if acc == nil {
s := string(lit)
acc = &s
} else {
*acc = *acc + string(lit)
}
} else {
if acc != nil {
out = append(out, AstLit(*acc))
acc = nil
}
out = append(out, r)
}
}
if acc != nil {
out = append(out, AstLit(*acc))
}
switch len(out) {
case 0:
return AstLit("")
case 1:
return out[0]
}
return &AstSeq{out}
case *AstAlt:
out := make([]Ast, len(v.opts), len(v.opts))
for i, r := range v.opts {
out[i] = optimizeAstUnwrapSingletonSeqAndAlt(r)
}
switch len(out) {
case 0:
return AstLit("")
case 1:
return out[0]
}
return &AstAlt{out}
case *AstRepeat:
out := *v
out.re = optimizeAstUnwrapSingletonSeqAndAlt(v.re)
return &out
case *AstCap:
out := *v
out.re = optimizeAstUnwrapSingletonSeqAndAlt(v.re)
return &out
default:
return v
}
}
func canOnlyMatchAtBegining(re Ast) bool {
switch v := re.(type) {
case AstAssertBegin:
return true
case *AstSeq:
if len(v.seq) == 0 {
return false
}
return canOnlyMatchAtBegining(v.seq[0])
case *AstAlt:
if len(v.opts) == 0 {
return false
}
for _, r := range v.opts {
if !canOnlyMatchAtBegining(r) {
return false
}
}
return true
case *AstRepeat:
if v.min == 0 {
return false
}
return canOnlyMatchAtBegining(v.re)
case *AstCap:
return canOnlyMatchAtBegining(v.re)
default:
return false
}
}
func minRequiredLengthOfAst(re Ast) int {
switch v := re.(type) {
case AstAssertBegin, AstAssertEnd, AstBackRef:
return 0
case AstNotNewline, AstCharClass:
return 1
case AstLit:
return len(string(v))
case *AstSeq:
acc := 0
for _, r := range v.seq {
acc += minRequiredLengthOfAst(r)
}
return acc
case *AstAlt:
min := minRequiredLengthOfAst(v.opts[0])
for _, r := range v.opts[1:] {
m := minRequiredLengthOfAst(r)
if m < min {
min = m
}
}
return min
case *AstRepeat:
return minRequiredLengthOfAst(v.re) * v.min
case *AstCap:
return minRequiredLengthOfAst(v.re)
default:
panic(fmt.Errorf("IMPLEMENT optimizeAstFlattenSeqAndAlt for %T", re))
}
}