Add tk.A2-A9 support

This commit is contained in:
rwxrob 2022-03-04 18:04:56 -05:00
parent 20e3267a74
commit 0bbdd11dc0
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77
3 changed files with 56 additions and 8 deletions

View File

@ -278,13 +278,36 @@ func (s *R) Expect(expr any) (*Cur, error) {
switch v := expr.(type) {
case rune: // ------------------------------------------------------
if v != tk.ANY && s.Cur.Rune != v {
if s.Cur.Rune != v {
err := s.ErrorExpected(v)
return nil, err
}
s.Scan()
return s.Last, nil
case tk.Token: // --------------------------------------------------
switch v {
case tk.ANY: // A, A1
s.Scan()
case tk.A2:
s.ScanN(2)
case tk.A3:
s.ScanN(3)
case tk.A4:
s.ScanN(4)
case tk.A5:
s.ScanN(5)
case tk.A6:
s.ScanN(6)
case tk.A7:
s.ScanN(7)
case tk.A8:
s.ScanN(8)
case tk.A9:
s.ScanN(9)
}
return s.Last, nil
case string: // ----------------------------------------------------
if v == "" {
return s.Mark(), nil
@ -450,18 +473,17 @@ func (s *R) Expect(expr any) (*Cur, error) {
for n := 0; n < v.N; n++ {
s.Scan()
}
m := s.Mark()
s.Scan()
return m, nil
return s.Last, nil
// see rune for A2-9
case z.R: // ----------------------------------------------------
if !(v.First <= s.Cur.Rune && s.Cur.Rune <= v.Last) {
err := s.ErrorExpected(v)
return nil, err
}
m := s.Mark()
s.Scan()
return m, nil
return s.Last, nil
case z.C: // ------------------------------------------------------
return s.expcount(v.N, v.This)

View File

@ -387,13 +387,25 @@ func ExampleExpect_seq_Not_Fail() {
}
func ExampleExpect_token_ANY() {
s, _ := scan.New("some thing")
s, _ := scan.New("some thing wonderful")
c, _ := s.Expect(tk.ANY)
c.Print() // same as `s` or s.Scan()
s.Print() // advances
c, _ = s.Expect(tk.A)
c.Print() // same as `o` or s.Scan()
s.Print() // advances
c, _ = s.Expect(tk.A1)
// we'll skip tk.A2 - tk.A8
s.Print()
c, _ = s.Expect(tk.A9)
s.Print() // should advance 9 to pos 13
// Output:
// U+0073 's' 1,1-1 (1-1)
// U+006F 'o' 1,2-2 (2-2)
// U+006F 'o' 1,2-2 (2-2)
// U+006D 'm' 1,3-3 (3-3)
// U+0065 'e' 1,4-4 (4-4)
// U+006F 'o' 1,13-13 (13-13)
}
func ExampleExpect_any_Success() {

View File

@ -3,6 +3,8 @@ Package tk contains tokens that when detected in the Expect expression stream ca
*/
package tk
type Token uint32
const (
// EOD is a special value that is returned when the end of data is
@ -11,9 +13,21 @@ const (
// Unicode (currently) ends at \U+FFFD we are safe to use the largest
// possible valid rune value. Passing EOD directly to Expect always
// stops the scan where it is.
EOD rune = 1<<31 - (iota + 1) // max int32
EOD rune = 1<<31 - 1 // max int32
// ANY represents any possible single rune similar to question mark (?)
// when globing and dot (.) in most regular expression syntaxes.
ANY
ANY Token = iota + 1
A2
A3
A4
A5
A6
A7
A8
A9
// aliases
A = ANY
A1 = ANY
)