1
- package bootstrap
1
+ package ast
2
2
3
3
import (
4
4
"fmt"
5
+
6
+ "github.com/arr-ai/wbnf/bootstrap"
5
7
)
6
8
7
9
type counter struct {
@@ -12,6 +14,14 @@ func newCounter(lo, hi int) counter {
12
14
return counter {lo : lo , hi : hi }
13
15
}
14
16
17
+ func newCounterFromQuant (q bootstrap.Quant ) counter {
18
+ max := q .Max
19
+ if max == 0 {
20
+ max = 2
21
+ }
22
+ return newCounter (q .Min , max )
23
+ }
24
+
15
25
var (
16
26
zeroOrOne = newCounter (0 , 1 )
17
27
zeroOrMore = newCounter (0 , 2 )
@@ -39,7 +49,7 @@ func (c counter) union(d counter) counter {
39
49
40
50
type counters map [string ]counter
41
51
42
- func newCounters (t Term ) counters {
52
+ func newCounters (t bootstrap. Term ) counters {
43
53
result := counters {}
44
54
result .termCountChildren (t , oneOne )
45
55
return result
@@ -73,28 +83,28 @@ func (ctrs counters) union(o counters) {
73
83
}
74
84
}
75
85
76
- func (ctrs counters ) termCountChildren (term Term , parent counter ) {
86
+ func (ctrs counters ) termCountChildren (term bootstrap. Term , parent counter ) {
77
87
switch t := term .(type ) {
78
- case S , RE , REF :
88
+ case bootstrap. S , bootstrap. RE , bootstrap. REF :
79
89
ctrs .count ("" , parent )
80
- case Rule :
90
+ case bootstrap. Rule :
81
91
ctrs .count (string (t ), parent )
82
- case Seq :
92
+ case bootstrap. Seq :
83
93
for _ , child := range t {
84
94
ctrs .termCountChildren (child , parent )
85
95
}
86
- case Oneof :
96
+ case bootstrap. Oneof :
87
97
ds := counters {}
88
98
for _ , child := range t {
89
99
ds .union (newCounters (child ))
90
100
}
91
101
ctrs .mul (ds , parent )
92
- case Delim :
102
+ case bootstrap. Delim :
93
103
ctrs .termCountChildren (t .Term , parent .mul (oneOrMore ))
94
104
ctrs .termCountChildren (t .Sep , parent .mul (zeroOrMore ))
95
- case Quant :
96
- ctrs .termCountChildren (t .Term , parent .mul (t . counter ( )))
97
- case Named :
105
+ case bootstrap. Quant :
106
+ ctrs .termCountChildren (t .Term , parent .mul (newCounterFromQuant ( t )))
107
+ case bootstrap. Named :
98
108
ctrs .count (t .Name , parent )
99
109
default :
100
110
panic (fmt .Errorf ("unexpected term type: %v %[1]T" , t ))
0 commit comments