Skip to content

Commit 8bcce31

Browse files
committed
Rename bootstrap to wbnf
1 parent 07af879 commit 8bcce31

26 files changed

+73
-74
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
1313
dist/
14-
wbnf
1514

1615
# Dependency directories (remove the comment below to include it)
1716
# vendor/

ast/counter.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package ast
33
import (
44
"fmt"
55

6-
"github.com/arr-ai/wbnf/bootstrap"
6+
"github.com/arr-ai/wbnf/wbnf"
77
)
88

99
type counter struct {
@@ -14,7 +14,7 @@ func newCounter(lo, hi int) counter {
1414
return counter{lo: lo, hi: hi}
1515
}
1616

17-
func newCounterFromQuant(q bootstrap.Quant) counter {
17+
func newCounterFromQuant(q wbnf.Quant) counter {
1818
max := q.Max
1919
if max == 0 {
2020
max = 2
@@ -49,7 +49,7 @@ func (c counter) union(d counter) counter {
4949

5050
type counters map[string]counter
5151

52-
func newCounters(t bootstrap.Term) counters {
52+
func newCounters(t wbnf.Term) counters {
5353
result := counters{}
5454
result.termCountChildren(t, oneOne)
5555
return result
@@ -83,28 +83,28 @@ func (ctrs counters) union(o counters) {
8383
}
8484
}
8585

86-
func (ctrs counters) termCountChildren(term bootstrap.Term, parent counter) {
86+
func (ctrs counters) termCountChildren(term wbnf.Term, parent counter) {
8787
switch t := term.(type) {
88-
case bootstrap.S, bootstrap.RE, bootstrap.REF:
88+
case wbnf.S, wbnf.RE, wbnf.REF:
8989
ctrs.count("", parent)
90-
case bootstrap.Rule:
90+
case wbnf.Rule:
9191
ctrs.count(string(t), parent)
92-
case bootstrap.Seq:
92+
case wbnf.Seq:
9393
for _, child := range t {
9494
ctrs.termCountChildren(child, parent)
9595
}
96-
case bootstrap.Oneof:
96+
case wbnf.Oneof:
9797
ds := counters{}
9898
for _, child := range t {
9999
ds.union(newCounters(child))
100100
}
101101
ctrs.mul(ds, parent)
102-
case bootstrap.Delim:
102+
case wbnf.Delim:
103103
ctrs.termCountChildren(t.Term, parent.mul(oneOrMore))
104104
ctrs.termCountChildren(t.Sep, parent.mul(zeroOrMore))
105-
case bootstrap.Quant:
105+
case wbnf.Quant:
106106
ctrs.termCountChildren(t.Term, parent.mul(newCounterFromQuant(t)))
107-
case bootstrap.Named:
107+
case wbnf.Named:
108108
ctrs.count(t.Name, parent)
109109
default:
110110
panic(fmt.Errorf("unexpected term type: %v %[1]T", t))

ast/node.go

+25-25
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"strconv"
88
"strings"
99

10-
"github.com/arr-ai/wbnf/bootstrap"
10+
"github.com/arr-ai/wbnf/wbnf"
1111
"github.com/arr-ai/wbnf/errors"
1212
"github.com/arr-ai/wbnf/parser"
1313
)
@@ -19,18 +19,18 @@ const (
1919
quantTag = "?"
2020
)
2121

22-
func ParserNodeToNode(g bootstrap.Grammar, v interface{}) Branch {
23-
rule := bootstrap.NodeRule(v)
22+
func ParserNodeToNode(g wbnf.Grammar, v interface{}) Branch {
23+
rule := wbnf.NodeRule(v)
2424
term := g[rule]
2525
result := Branch{}
2626
result.one("@rule", Extra{extra: rule})
2727
result.fromTerm(g, term, newCounters(term), v)
2828
return result
2929
}
3030

31-
func NodeToParserNode(g bootstrap.Grammar, branch Branch) interface{} {
31+
func NodeToParserNode(g wbnf.Grammar, branch Branch) interface{} {
3232
branch = branch.clone().(Branch)
33-
rule := branch.pullOne("@rule").(Extra).extra.(bootstrap.Rule)
33+
rule := branch.pullOne("@rule").(Extra).extra.(wbnf.Rule)
3434
term := g[rule]
3535
ctrs := newCounters(term)
3636
return relabelNode(string(rule), branch.toTerm(g, term, ctrs))
@@ -162,43 +162,43 @@ func (n Branch) singular() Children {
162162
return nil
163163
}
164164

165-
func (n Branch) fromTerm(g bootstrap.Grammar, term bootstrap.Term, ctrs counters, v interface{}) {
165+
func (n Branch) fromTerm(g wbnf.Grammar, term wbnf.Term, ctrs counters, v interface{}) {
166166
switch t := term.(type) {
167-
case bootstrap.S, bootstrap.RE, bootstrap.REF:
167+
case wbnf.S, wbnf.RE, wbnf.REF:
168168
n.add("", 0, Leaf(v.(parser.Scanner)), ctrs[""], nil)
169-
case bootstrap.Rule:
169+
case wbnf.Rule:
170170
term := g[t]
171171
ctrs2 := newCounters(term)
172172
b := Branch{}
173173
b.fromTerm(g, term, ctrs2, v)
174174
unleveled, level := unlevel(string(t))
175175
n.add(unleveled, level, b, ctrs[string(t)], ctrs2)
176-
case bootstrap.Seq:
176+
case wbnf.Seq:
177177
node := v.(parser.Node)
178178
for i, child := range node.Children {
179179
n.fromTerm(g, t[i], ctrs, child)
180180
}
181-
case bootstrap.Oneof:
181+
case wbnf.Oneof:
182182
node := v.(parser.Node)
183183
n.many("@choice", Extra{extra: node.Extra.(int)})
184184
n.fromTerm(g, t[node.Extra.(int)], ctrs, node.Children[0])
185-
case bootstrap.Delim:
185+
case wbnf.Delim:
186186
node := v.(parser.Node)
187-
if node.Extra.(bootstrap.Associativity) != bootstrap.NonAssociative {
187+
if node.Extra.(wbnf.Associativity) != wbnf.NonAssociative {
188188
panic(errors.Unfinished)
189189
}
190190
L, R := t.LRTerms(node)
191-
terms := [2]bootstrap.Term{L, t.Sep}
191+
terms := [2]wbnf.Term{L, t.Sep}
192192
for i, child := range node.Children {
193193
n.fromTerm(g, terms[i%2], ctrs, child)
194194
terms[0] = R
195195
}
196-
case bootstrap.Quant:
196+
case wbnf.Quant:
197197
node := v.(parser.Node)
198198
for _, child := range node.Children {
199199
n.fromTerm(g, t.Term, ctrs, child)
200200
}
201-
case bootstrap.Named:
201+
case wbnf.Named:
202202
b := Branch{}
203203
ctrs2 := newCounters(t.Term)
204204
b.fromTerm(g, t.Term, ctrs2, v)
@@ -271,23 +271,23 @@ func (n Branch) pullMany(name string) Node {
271271
return nil
272272
}
273273

274-
func (n Branch) toTerm(g bootstrap.Grammar, term bootstrap.Term, ctrs counters) (out interface{}) {
274+
func (n Branch) toTerm(g wbnf.Grammar, term wbnf.Term, ctrs counters) (out interface{}) {
275275
// defer enterf("%T %[1]v %v", term, ctrs).exitf("%v", &out)
276276
switch t := term.(type) {
277-
case bootstrap.S, bootstrap.RE:
277+
case wbnf.S, wbnf.RE:
278278
if node := n.pull("", 0, ctrs[""], nil); node != nil {
279279
return parser.Scanner(node.(Leaf))
280280
}
281281
return nil
282-
case bootstrap.Rule:
282+
case wbnf.Rule:
283283
term := g[t]
284284
ctrs2 := newCounters(term)
285285
unleveled, level := unlevel(string(t))
286286
if b := n.pull(unleveled, level, ctrs[string(t)], ctrs2); b != nil {
287287
return relabelNode(string(t), b.(Branch).toTerm(g, term, ctrs2))
288288
}
289289
return nil
290-
case bootstrap.Seq:
290+
case wbnf.Seq:
291291
result := parser.Node{Tag: seqTag}
292292
for _, child := range t {
293293
if node := n.toTerm(g, child, ctrs); node != nil {
@@ -297,19 +297,19 @@ func (n Branch) toTerm(g bootstrap.Grammar, term bootstrap.Term, ctrs counters)
297297
}
298298
}
299299
return result
300-
case bootstrap.Oneof:
300+
case wbnf.Oneof:
301301
extra := n.pullMany("@choice").(Extra).extra.(int)
302302
return parser.Node{
303303
Tag: oneofTag,
304304
Extra: extra,
305305
Children: []interface{}{n.toTerm(g, t[extra], ctrs)},
306306
}
307-
case bootstrap.Delim:
307+
case wbnf.Delim:
308308
v := parser.Node{
309309
Tag: delimTag,
310-
Extra: bootstrap.NonAssociative,
310+
Extra: wbnf.NonAssociative,
311311
}
312-
terms := [2]bootstrap.Term{t.Term, t.Sep}
312+
terms := [2]wbnf.Term{t.Term, t.Sep}
313313
i := 0
314314
for ; ; i++ {
315315
if child := n.toTerm(g, terms[i%2], ctrs); child != nil {
@@ -322,7 +322,7 @@ func (n Branch) toTerm(g bootstrap.Grammar, term bootstrap.Term, ctrs counters)
322322
panic(errors.Inconceivable)
323323
}
324324
return v
325-
case bootstrap.Quant:
325+
case wbnf.Quant:
326326
result := parser.Node{Tag: quantTag}
327327
for {
328328
if v := n.toTerm(g, t.Term, ctrs); v != nil {
@@ -335,7 +335,7 @@ func (n Branch) toTerm(g bootstrap.Grammar, term bootstrap.Term, ctrs counters)
335335
panic(errors.Inconceivable)
336336
}
337337
return result
338-
case bootstrap.Named:
338+
case wbnf.Named:
339339
ctrs2 := newCounters(t.Term)
340340
if b := n.pull(t.Name, 0, ctrs[t.Name], ctrs2); b != nil {
341341
return relabelNode(t.Name, b.(Branch).toTerm(g, t.Term, ctrs2))

ast/node_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ import (
44
"fmt"
55
"testing"
66

7-
"github.com/arr-ai/wbnf/bootstrap"
7+
"github.com/arr-ai/wbnf/wbnf"
88
"github.com/arr-ai/wbnf/parser"
99
"github.com/stretchr/testify/assert"
1010
)
1111

1212
func TestParserNodeToNode(t *testing.T) {
13-
p := bootstrap.Core()
14-
v := p.MustParse(bootstrap.GrammarRule, parser.NewScanner(`expr -> @:op="+" ^ @:op="*" ^ /{\d+};`)).(parser.Node)
13+
p := wbnf.Core()
14+
v := p.MustParse(wbnf.GrammarRule, parser.NewScanner(`expr -> @:op="+" ^ @:op="*" ^ /{\d+};`)).(parser.Node)
1515
g := p.Grammar()
1616
n := ParserNodeToNode(g, v)
1717
u := NodeToParserNode(g, n).(parser.Node)
1818
parser.AssertEqualNodes(t, v, u)
1919

20-
p = bootstrap.NewFromNode(v).Compile()
21-
v = p.MustParse(bootstrap.Rule("expr"), parser.NewScanner(`1+2*3`)).(parser.Node)
20+
p = wbnf.NewFromNode(v).Compile()
21+
v = p.MustParse(wbnf.Rule("expr"), parser.NewScanner(`1+2*3`)).(parser.Node)
2222
g = p.Grammar()
2323
n = ParserNodeToNode(g, v)
2424
u = NodeToParserNode(g, n).(parser.Node)
@@ -28,15 +28,15 @@ func TestParserNodeToNode(t *testing.T) {
2828
func TestTinyXmlGrammar(t *testing.T) {
2929
t.Parallel()
3030

31-
v, err := bootstrap.Core().Parse(bootstrap.GrammarRule, parser.NewScanner(`
31+
v, err := wbnf.Core().Parse(wbnf.GrammarRule, parser.NewScanner(`
3232
xml -> s "<" s NAME attr* s ">" xml* "</" s NAME s ">" | CDATA=/{[^<]+};
3333
attr -> s NAME s "=" s value=/{"[^"]*"};
3434
NAME -> /{[A-Za-z_:][-A-Za-z0-9._:]*};
3535
s -> /{\s*};
3636
`))
3737
assert.NoError(t, err)
3838

39-
xmlParser := bootstrap.NewFromNode(v.(parser.Node)).Compile()
39+
xmlParser := wbnf.NewFromNode(v.(parser.Node)).Compile()
4040

4141
src := parser.NewScanner(`<a x="1">hello <b>world!</b></a>`)
4242
orig := *src
@@ -49,14 +49,14 @@ func TestTinyXmlGrammar(t *testing.T) {
4949
return Leaf(*orig.Slice(offset, end))
5050
}
5151

52-
xml, err := xmlParser.Parse(bootstrap.Rule("xml"), src)
52+
xml, err := xmlParser.Parse(wbnf.Rule("xml"), src)
5353
assert.NoError(t, err)
5454

5555
ast := ParserNodeToNode(xmlParser.Grammar(), xml)
5656

5757
assert.EqualValues(t,
5858
Branch{
59-
"@rule": One{Extra{bootstrap.Rule("xml")}},
59+
"@rule": One{Extra{wbnf.Rule("xml")}},
6060
"@choice": Many{Extra{0}},
6161
"": Many{s(0, `<`), s(8, `>`), s(28, `</`), s(31, `>`)},
6262
"s": Many{s(0, ``), s(1, ``), s(8, ``), s(30, ``), s(31, ``)},

cmd/codegen.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"io/ioutil"
77
"strings"
88

9-
"github.com/arr-ai/wbnf/bootstrap"
9+
"github.com/arr-ai/wbnf/wbnf"
1010
"github.com/arr-ai/wbnf/parser"
1111
"github.com/urfave/cli"
1212
)
@@ -49,9 +49,9 @@ func codegen(c *cli.Context) error {
4949
}
5050

5151
scanner := parser.NewScanner(string(buf))
52-
g := bootstrap.Core()
52+
g := wbnf.Core()
5353

54-
tree, err := g.Parse(bootstrap.RootRule, scanner)
54+
tree, err := g.Parse(wbnf.RootRule, scanner)
5555
if err != nil {
5656
return err
5757
}
@@ -60,21 +60,21 @@ func codegen(c *cli.Context) error {
6060
return err
6161
}
6262

63-
ast := bootstrap.NewFromNode2(tree.(parser.Node))
63+
ast := wbnf.NewFromNode2(tree.(parser.Node))
6464
if ast == nil {
6565
return nil
6666
} /*
6767
text := ast.Dump()
6868
69-
newg := bootstrap.NewFromNode(tree.(parser.Node)).Compile()
69+
newg := wbnf.NewFromNode(tree.(parser.Node)).Compile()
7070
7171
scanner = parser.NewScanner(text)
72-
_, err := newg.Parse(bootstrap.RootRule, scanner)
72+
_, err := newg.Parse(wbnf.RootRule, scanner)
7373
if err != nil {
7474
return err
7575
}
7676
*/
77-
fmt.Print(bootstrap.Codegen(ast))
77+
fmt.Print(wbnf.Codegen(ast))
7878

7979
return nil
8080
}

cmd/test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"os"
77

88
"github.com/arr-ai/wbnf/ast"
9-
"github.com/arr-ai/wbnf/bootstrap"
9+
"github.com/arr-ai/wbnf/wbnf"
1010
"github.com/arr-ai/wbnf/parser"
1111

1212
"github.com/urfave/cli"
@@ -44,12 +44,12 @@ var testCommand = cli.Command{
4444
},
4545
}
4646

47-
func loadTestGrammar() bootstrap.Parsers {
47+
func loadTestGrammar() wbnf.Parsers {
4848
text, err := ioutil.ReadFile(inGrammarFile)
4949
if err != nil {
5050
panic(err)
5151
}
52-
return bootstrap.MustCompile(string(text))
52+
return wbnf.MustCompile(string(text))
5353
}
5454

5555
func test(c *cli.Context) error {
@@ -72,7 +72,7 @@ func test(c *cli.Context) error {
7272
}
7373

7474
g := loadTestGrammar()
75-
tree, err := g.Parse(bootstrap.Rule(startingRule), parser.NewScanner(input))
75+
tree, err := g.Parse(wbnf.Rule(startingRule), parser.NewScanner(input))
7676
if err != nil {
7777
return err
7878
}

bootstrap/compile.go renamed to wbnf/compile.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package bootstrap
1+
package wbnf
22

33
import (
44
"fmt"
55
"regexp"
66
"strconv"
77
"strings"
88

9-
"github.com/arr-ai/wbnf/bootstrap/internal"
9+
"github.com/arr-ai/wbnf/wbnf/internal"
1010
"github.com/arr-ai/wbnf/errors"
1111

1212
"github.com/arr-ai/wbnf/parser"

bootstrap/copygrammar.sh renamed to wbnf/copygrammar.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
out=wbnfgrammar.go
44
echo Generating $out
55
cat > $out <<EOF
6-
package bootstrap
6+
package wbnf
77
88
var grammarGrammarSrc = unfakeBackquote(\`
99
$(sed 's/`/‵/g' ../examples/wbnf.txt)

bootstrap/diff.go renamed to wbnf/diff.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package bootstrap
1+
package wbnf
22

33
import (
44
"fmt"

0 commit comments

Comments
 (0)