Convert "is" to "z" throughout

pull/53/head
rwxrob 2 years ago
parent 47ee497b0d
commit 605bb28021
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77

@ -4,15 +4,15 @@
/*
Package scan implements a non-linear, rune-centric, buffered data,
extendable scanner that includes its own high-level parsing expression
grammar (BPEGN) comprised of Go slices and structs used as expressions
from the "is" subpackage making parser generation (by hand or code)
trivial from any structured meta languages such as PEGN, PEG, EBNF,
ABNF, etc. Most will use the scanner to create parsers quickly where
a regular expression will not suffice. See the "is" and "tk" packages
for a growing number of common, centrally maintain expressions for your
parsing pleasure. Also see the "mark" (BonzaiMark) subpackage for
a working examples of the scanner in action, which is used by the
included Bonzai help.Cmd command and others.
grammar (BPEGN) comprised of Go constants, slices, and structs used as
expressions from the z ("is") subpackage making parser generation (by
hand or code) trivial from any structured meta languages such as PEGN,
PEG, EBNF, ABNF, etc. Most will use the scanner to create parsers
quickly where a regular expression will not suffice. See the z ("is")
and tk packages for a growing number of common, centrally maintain
expressions for your parsing pleasure. Also see the mark (BonzaiMark)
subpackage for a working examples of the scanner in action, which is
used by the included Bonzai help.Cmd command and others.
*/
package scan
@ -22,7 +22,7 @@ import (
"io"
"unicode/utf8"
is "github.com/rwxrob/bonzai/scan/is"
z "github.com/rwxrob/bonzai/scan/is"
"github.com/rwxrob/bonzai/scan/tk"
"github.com/rwxrob/bonzai/util"
)
@ -255,8 +255,8 @@ func (s *R) LookSlice(beg *Cur, end *Cur) string {
// naturally when putting together a parser quickly and without the
// cognitive overhead of switching between grammars. The BPEGN
// expressions passed to Expect are 100% Go. For full documentation of
// BPEGN expressions see the "is" package and the source of this Expect
// method.
// BPEGN expressions see the z ("is") package and the source of this
// Expect method.
//
// Warning: While it is nothing for most developers to be concerned
// with, the Expect method does do a fair amount of functional recursion
@ -266,7 +266,7 @@ func (s *R) LookSlice(beg *Cur, end *Cur) string {
// own benchmarking and perhaps start with BPEGN until they can create
// more optimized parsers when and if necessary. Most will discover
// other more substantial bottlenecks. The Bonzai project places
// priority is on speed and quality of developer delivery over run-time
// priority on speed and quality of developer delivery over run-time
// performance. Delivery time is far more costly than the minimal gains
// in run-time performance. "Premature optimization is the root of all
// evil," as they say.
@ -299,7 +299,7 @@ func (s *R) Expect(expr any) (*Cur, error) {
}
return s.Last, nil
case is.X: // -----------------------------------------------------
case z.X: // -----------------------------------------------------
var err error
b := s.Mark()
m := s.Mark()
@ -312,7 +312,7 @@ func (s *R) Expect(expr any) (*Cur, error) {
}
return m, nil
case is.O: // -------------------------------------------------------
case z.O: // -------------------------------------------------------
for _, i := range v {
m, _ := s.Expect(i)
if m != nil {
@ -320,7 +320,7 @@ func (s *R) Expect(expr any) (*Cur, error) {
}
}
case is.Toi: // -----------------------------------------------------
case z.Toi: // -----------------------------------------------------
back := s.Mark()
for s.Cur.Rune != tk.EOD {
for _, i := range v {
@ -334,7 +334,7 @@ func (s *R) Expect(expr any) (*Cur, error) {
s.Jump(back)
return nil, s.ErrorExpected(v)
case is.To: // -----------------------------------------------------
case z.To: // -----------------------------------------------------
m := s.Mark()
b4 := s.Mark()
for s.Cur.Rune != tk.EOD {
@ -352,7 +352,7 @@ func (s *R) Expect(expr any) (*Cur, error) {
s.Jump(m)
return nil, s.ErrorExpected(v)
case is.It: // ----------------------------------------------------
case z.It: // ----------------------------------------------------
var m *Cur
b := s.Mark()
for _, i := range v {
@ -367,7 +367,7 @@ func (s *R) Expect(expr any) (*Cur, error) {
s.Jump(b)
return b, nil
case is.Not: // ----------------------------------------------------
case z.Not: // ----------------------------------------------------
m := s.Mark()
for _, i := range v {
if c, _ := s.Expect(i); c != nil {
@ -378,7 +378,7 @@ func (s *R) Expect(expr any) (*Cur, error) {
}
return m, nil
case is.In: // -----------------------------------------------------
case z.In: // -----------------------------------------------------
var m *Cur
for _, i := range v {
var err error
@ -394,7 +394,7 @@ func (s *R) Expect(expr any) (*Cur, error) {
}
return m, nil
case is.MMx: // ----------------------------------------------------
case z.MMx: // ----------------------------------------------------
c := 0
last := s.Mark()
var err error
@ -418,16 +418,16 @@ func (s *R) Expect(expr any) (*Cur, error) {
}
return end, nil
case is.Mn1: // ----------------------------------------------------
case z.Mn1: // ----------------------------------------------------
m := s.Mark()
c, err := s.Expect(is.Min{1, v.This})
c, err := s.Expect(z.Min{1, v.This})
if err != nil {
s.Jump(m)
return nil, s.ErrorExpected(v)
}
return c, nil
case is.Min: // ----------------------------------------------------
case z.Min: // ----------------------------------------------------
c := 0
last := s.Mark()
var err error
@ -446,16 +446,16 @@ func (s *R) Expect(expr any) (*Cur, error) {
}
return last, nil
case is.C: // ------------------------------------------------------
case z.C: // ------------------------------------------------------
b := s.Mark()
m, err := s.Expect(is.MMx{v.N, v.N, v.This})
m, err := s.Expect(z.MMx{v.N, v.N, v.This})
if err != nil {
s.Jump(b)
return nil, s.ErrorExpected(v)
}
return m, nil
case is.Any: // ----------------------------------------------------
case z.Any: // ----------------------------------------------------
for n := 0; n < v.N; n++ {
s.Scan()
}
@ -463,7 +463,7 @@ func (s *R) Expect(expr any) (*Cur, error) {
s.Scan()
return m, nil
case is.Rng: // ----------------------------------------------------
case z.Rng: // ----------------------------------------------------
if !(v.First <= s.Cur.Rune && s.Cur.Rune <= v.Last) {
err := s.ErrorExpected(v)
return nil, err
@ -511,47 +511,47 @@ func (s *R) ErrorExpected(this any, args ...any) error {
switch v := this.(type) {
case rune: // otherwise will use uint32
msg = fmt.Sprintf(`expected rune %q`, v)
case is.It:
case z.It:
if len(v) > 1 {
msg = fmt.Sprintf(`expected one of %q`, v)
} else {
msg = fmt.Sprintf(`expected %q`, v[0])
}
case is.Not:
case z.Not:
msg = fmt.Sprintf(`unexpected %q`, args[0])
case is.In:
case z.In:
str := `expected one of %q`
msg = fmt.Sprintf(str, v)
case is.X:
case z.X:
//str := `expected %q in sequence %q at %v beginning`
//msg = fmt.Sprintf(str, args[0], v, args[1])
str := `expected %q in sequence`
msg = fmt.Sprintf(str, v)
case is.O:
case z.O:
str := `expected an optional %v`
msg = fmt.Sprintf(str, v)
case is.Mn1:
case z.Mn1:
str := `expected one or more %q`
msg = fmt.Sprintf(str, v.This)
case is.Min:
case z.Min:
str := `expected min %v of %q`
msg = fmt.Sprintf(str, v.Min, v.This)
case is.MMx:
case z.MMx:
str := `expected min %v, max %v of %q`
msg = fmt.Sprintf(str, v.Min, v.Max, v.This)
case is.C:
case z.C:
str := `expected exactly %v of %q`
msg = fmt.Sprintf(str, v.N, v.This)
case is.Rng:
case z.Rng:
str := `expected range [%v-%v]`
msg = fmt.Sprintf(str, string(v.First), string(v.Last))
case is.Toi:
case z.Toi:
str := `%q not found`
if len(v) > 1 {
str = `none of %q found`
}
msg = fmt.Sprintf(str, v)
case is.To:
case z.To:
msg = fmt.Sprintf(`none of %q found`, v)
default:
msg = fmt.Sprintf(`expected %T %q`, v, v)

@ -7,7 +7,7 @@ import (
"strings"
"github.com/rwxrob/bonzai/scan"
is "github.com/rwxrob/bonzai/scan/is"
z "github.com/rwxrob/bonzai/scan/is"
"github.com/rwxrob/bonzai/scan/tk"
)
@ -206,7 +206,7 @@ func ExampleExpect_string() {
func ExampleExpect_compound_Expr_String() {
s, _ := scan.New("some thing")
c, _ := s.Expect(is.X{"some ", "thin"})
c, _ := s.Expect(z.X{"some ", "thin"})
c.Print()
s.Print()
// Output:
@ -216,7 +216,7 @@ func ExampleExpect_compound_Expr_String() {
func ExampleExpect_compound_Expr_Rune() {
s, _ := scan.New("some thing")
c, _ := s.Expect(is.X{"some", ' ', "thin"})
c, _ := s.Expect(z.X{"some", ' ', "thin"})
c.Print()
s.Print()
// Output:
@ -226,7 +226,7 @@ func ExampleExpect_compound_Expr_Rune() {
func ExampleExpect_it_Success() {
s, _ := scan.New("some thing")
c, _ := s.Expect(is.It{"some"})
c, _ := s.Expect(z.It{"some"})
c.Print() // even though true, not moved
s.Print() // scanner also not moved
// Output:
@ -236,7 +236,7 @@ func ExampleExpect_it_Success() {
func ExampleExpect_it_Success_Middle() {
s, _ := scan.New("some thing")
c, _ := s.Expect(is.X{"some", is.It{' '}})
c, _ := s.Expect(z.X{"some", z.It{' '}})
c.Print() // advanced up to (but not including) ' '
s.Print() // scanner also not moved
// Output:
@ -246,7 +246,7 @@ func ExampleExpect_it_Success_Middle() {
func ExampleExpect_it_Fail() {
s, _ := scan.New("some thing")
_, err := s.Expect(is.X{"some", is.It{"thing"}})
_, err := s.Expect(z.X{"some", z.It{"thing"}})
fmt.Println(err)
s.Print() // but scanner did get "some" so advanced
// Output:
@ -256,7 +256,7 @@ func ExampleExpect_it_Fail() {
func ExampleExpect_it_Fail_X() {
s, _ := scan.New("some thing")
_, err := s.Expect(is.X{"some", is.It{"thing"}})
_, err := s.Expect(z.X{"some", z.It{"thing"}})
fmt.Println(err)
s.Print() // but scanner did get "some" so advanced
// Output:
@ -266,7 +266,7 @@ func ExampleExpect_it_Fail_X() {
func ExampleExpect_not_Success() {
s, _ := scan.New("some thing")
c, _ := s.Expect(is.Not{"foo"})
c, _ := s.Expect(z.Not{"foo"})
c.Print() // not advanced, but also not <nil>
s.Print() // not advanced at all
// Output:
@ -276,7 +276,7 @@ func ExampleExpect_not_Success() {
func ExampleExpect_not_Fail() {
s, _ := scan.New("some thing")
_, err := s.Expect(is.Not{"some"})
_, err := s.Expect(z.Not{"some"})
fmt.Println(err)
s.Print() // not advanced at all
// Output:
@ -286,7 +286,7 @@ func ExampleExpect_not_Fail() {
func ExampleExpect_not_X_Fail() {
s, _ := scan.New("some thing wonderful")
_, err := s.Expect(is.X{is.Not{'s'}, 'o'})
_, err := s.Expect(z.X{z.Not{'s'}, 'o'})
fmt.Println(err) // sees the s so fails
s.Print() // not advanced
// Output:
@ -296,7 +296,7 @@ func ExampleExpect_not_X_Fail() {
func ExampleExpect_not_X_Success() {
s, _ := scan.New("some thing wonderful")
c, _ := s.Expect(is.X{is.Not{`n`}, is.In{`som`}})
c, _ := s.Expect(z.X{z.Not{`n`}, z.In{`som`}})
c.Print() // pointing to last in match 'm'
s.Print() // advanced to next after match 'e'
// Output:
@ -306,7 +306,7 @@ func ExampleExpect_not_X_Success() {
func ExampleExpect_to_Success_Mid() {
s, _ := scan.New("some thing wonderful")
c, _ := s.Expect(is.To{"wo"})
c, _ := s.Expect(z.To{"wo"})
c.Print() // "wo" not inc, same as "some thing ", so ' '
s.Print() // advances to 'w'
// Output:
@ -317,10 +317,10 @@ func ExampleExpect_to_Success_Mid() {
func ExampleExpect_in() {
s, _ := scan.New("some thing")
s.Scan()
c, _ := s.Expect(is.In{'O', 'o', "ome"})
c, _ := s.Expect(z.In{'O', 'o', "ome"})
c.Print()
s.Print()
_, err := s.Expect(is.In{'x', 'y', "zee"})
_, err := s.Expect(z.In{'x', 'y', "zee"})
fmt.Println(err)
// Output:
// U+006F 'o' 1,2-2 (2-2)
@ -331,12 +331,12 @@ func ExampleExpect_in() {
func ExampleExpect_avoid_Not_with_In() {
s, _ := scan.New("some thing")
s.Snap()
c, _ := s.Expect(is.In{is.Not{'s'}, is.Rng{'a', 'z'}})
c, _ := s.Expect(z.In{z.Not{'s'}, z.Rng{'a', 'z'}})
c.Print() // unexpected success
s.Print() // advanced to 'o'
s.Back()
// use is.X instead
_, err := s.Expect(is.X{is.Not{'s'}, is.Rng{'a', 'z'}})
// use z.X instead
_, err := s.Expect(z.X{z.Not{'s'}, z.Rng{'a', 'z'}})
fmt.Println(err)
s.Print() // not advanced
// Output:
@ -348,7 +348,7 @@ func ExampleExpect_avoid_Not_with_In() {
func ExampleExpect_seq_Success() {
s, _ := scan.New("some thing")
c, _ := s.Expect(is.X{"some", ' ', "thin"})
c, _ := s.Expect(z.X{"some", ' ', "thin"})
c.Print() // same as "some thin", points at 'n'
s.Print() // advanced to 'g'
// Output:
@ -358,7 +358,7 @@ func ExampleExpect_seq_Success() {
func ExampleExpect_seq_Fail() {
s, _ := scan.New("some thing")
_, err := s.Expect(is.X{"some", "thin"})
_, err := s.Expect(z.X{"some", "thin"})
fmt.Println(err)
s.Print() // not advanced at all
// Output:
@ -368,7 +368,7 @@ func ExampleExpect_seq_Fail() {
func ExampleExpect_seq_Not_Success() {
s, _ := scan.New("some thing")
c, _ := s.Expect(is.X{"some", ` `, is.Not{`T`}, "thin"})
c, _ := s.Expect(z.X{"some", ` `, z.Not{`T`}, "thin"})
c.Print() // same as "some thin"
s.Print() // advanced to next after ('g')
// Output:
@ -378,7 +378,7 @@ func ExampleExpect_seq_Not_Success() {
func ExampleExpect_seq_Not_Fail() {
s, _ := scan.New("some Thing")
_, err := s.Expect(is.X{"some", ' ', is.Not{`T`}, "ignored"})
_, err := s.Expect(z.X{"some", ' ', z.Not{`T`}, "ignored"})
fmt.Println(err)
s.Print() // not advanced at all
// Output:
@ -398,7 +398,7 @@ func ExampleExpect_token_ANY() {
func ExampleExpect_any_Success() {
s, _ := scan.New("some thing")
c, _ := s.Expect(is.Any{5})
c, _ := s.Expect(z.Any{5})
c.Print() // same as "some "
s.Print() // advanced to next after ('t')
// Output:
@ -408,7 +408,7 @@ func ExampleExpect_any_Success() {
func ExampleExpect_o_Optional_Success() {
s, _ := scan.New("some thing")
//c, _ := s.Expect(is.O{"thing", "some"})
//c, _ := s.Expect(z.O{"thing", "some"})
c, _ := s.Expect("some")
c.Print() // same as "some", points to 'e'
s.Print() // advances to space ' '
@ -421,11 +421,11 @@ func ExampleExpect_mn1() {
s, _ := scan.New("sommme thing")
start := s.Mark()
s.ScanN(2)
c, _ := s.Expect(is.Mn1{'m'}) // goggles up all three
c, _ := s.Expect(z.Mn1{'m'}) // goggles up all three
c.Print()
s.Print()
s.Jump(start)
c, _ = s.Expect(is.Mn1{'s'}) // yep, just one
c, _ = s.Expect(z.Mn1{'s'}) // yep, just one
c.Print()
s.Print()
// Output:
@ -437,7 +437,7 @@ func ExampleExpect_mn1() {
func ExampleExpect_min() {
s, _ := scan.New("sssoommme thing")
c, _ := s.Expect(is.Min{2, 's'})
c, _ := s.Expect(z.Min{2, 's'})
c.Print() // needs 2, but will consume all three to last 's'
s.Print() // advances to next after ('o')
// Output:
@ -450,12 +450,12 @@ func ExampleExpect_mMx() {
s.Snap()
s.ScanN(2)
s.Print()
s.Expect(is.MMx{1, 3, 'm'}) // goggles up all three
s.Expect(z.MMx{1, 3, 'm'}) // goggles up all three
s.Print()
s.Back()
s.Expect(is.MMx{1, 3, 's'}) // yep, at least one
s.Expect(z.MMx{1, 3, 's'}) // yep, at least one
s.Print()
_, err := s.Expect(is.MMx{1, 3, 'X'}) // nope
_, err := s.Expect(z.MMx{1, 3, 'X'}) // nope
fmt.Println(err)
// Output:
// U+006D 'm' 1,3-3 (3-3)
@ -469,12 +469,12 @@ func ExampleExpect_c() {
s.Snap()
s.ScanN(2)
s.Print()
s.Expect(is.C{3, 'm'}) // goggles up all three
s.Expect(z.C{3, 'm'}) // goggles up all three
s.Print()
s.Back()
s.Expect(is.C{1, 's'}) // yes, but silly since 's' is easier
s.Expect(z.C{1, 's'}) // yes, but silly since 's' is easier
s.Print()
_, err := s.Expect(is.C{3, 'X'}) // nope
_, err := s.Expect(z.C{3, 'X'}) // nope
fmt.Println(err)
// Output:
// U+006D 'm' 1,3-3 (3-3)
@ -486,7 +486,7 @@ func ExampleExpect_c() {
func ExampleExpect_rng() {
s, _ := scan.New("some thing")
s.Scan()
c1, _ := s.Expect(is.Rng{'l', 'p'})
c1, _ := s.Expect(z.Rng{'l', 'p'})
c1.Print()
s.Print()
// Output:
@ -531,7 +531,7 @@ func ExampleExpect_hook() {
func ExampleExpect_to_Success() {
s, _ := scan.New("some thing")
c, _ := s.Expect(is.To{'e'})
c, _ := s.Expect(z.To{'e'})
c.Print() // same as "som", points to 'm'
s.Print() // scanned next after ('e')
// Output:
@ -541,7 +541,7 @@ func ExampleExpect_to_Success() {
func ExampleExpect_toi() {
s, _ := scan.New("some thing")
c, _ := s.Expect(is.Toi{'e'})
c, _ := s.Expect(z.Toi{'e'})
c.Print() // same as "some", points to 'e'
s.Print() // scanned next after (' ')
// Output:

Loading…
Cancel
Save