Skip to content

Commit 07af879

Browse files
committed
Refactor AST nodes into separate package
1 parent 1d156cb commit 07af879

File tree

15 files changed

+360
-259
lines changed

15 files changed

+360
-259
lines changed

bootstrap/ast_counter.go renamed to ast/counter.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
package bootstrap
1+
package ast
22

33
import (
44
"fmt"
5+
6+
"github.com/arr-ai/wbnf/bootstrap"
57
)
68

79
type counter struct {
@@ -12,6 +14,14 @@ func newCounter(lo, hi int) counter {
1214
return counter{lo: lo, hi: hi}
1315
}
1416

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+
1525
var (
1626
zeroOrOne = newCounter(0, 1)
1727
zeroOrMore = newCounter(0, 2)
@@ -39,7 +49,7 @@ func (c counter) union(d counter) counter {
3949

4050
type counters map[string]counter
4151

42-
func newCounters(t Term) counters {
52+
func newCounters(t bootstrap.Term) counters {
4353
result := counters{}
4454
result.termCountChildren(t, oneOne)
4555
return result
@@ -73,28 +83,28 @@ func (ctrs counters) union(o counters) {
7383
}
7484
}
7585

76-
func (ctrs counters) termCountChildren(term Term, parent counter) {
86+
func (ctrs counters) termCountChildren(term bootstrap.Term, parent counter) {
7787
switch t := term.(type) {
78-
case S, RE, REF:
88+
case bootstrap.S, bootstrap.RE, bootstrap.REF:
7989
ctrs.count("", parent)
80-
case Rule:
90+
case bootstrap.Rule:
8191
ctrs.count(string(t), parent)
82-
case Seq:
92+
case bootstrap.Seq:
8393
for _, child := range t {
8494
ctrs.termCountChildren(child, parent)
8595
}
86-
case Oneof:
96+
case bootstrap.Oneof:
8797
ds := counters{}
8898
for _, child := range t {
8999
ds.union(newCounters(child))
90100
}
91101
ctrs.mul(ds, parent)
92-
case Delim:
102+
case bootstrap.Delim:
93103
ctrs.termCountChildren(t.Term, parent.mul(oneOrMore))
94104
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:
98108
ctrs.count(t.Name, parent)
99109
default:
100110
panic(fmt.Errorf("unexpected term type: %v %[1]T", t))

0 commit comments

Comments
 (0)