diff options
author | Adam Scarr <adam@vektah.net> | 2017-08-06 15:43:23 +1000 |
---|---|---|
committer | Adam Scarr <adam@vektah.net> | 2017-08-06 15:43:23 +1000 |
commit | 8b343d6360d0edc065b9b62ab5e708e907b45a92 (patch) | |
tree | d58b76b21e5a369e25e7c3807f5f466cbcd849ad /pointer.go | |
parent | 68cde88125e1f016c5706ca8d0b3db6ba06624a2 (diff) |
Clean up Pointer
Diffstat (limited to 'pointer.go')
-rw-r--r-- | pointer.go | 70 |
1 files changed, 3 insertions, 67 deletions
@@ -1,18 +1,5 @@ package parsec -import ( - "strings" - "unicode/utf8" -) - -const ( - EOF rune = -1 -) - -func Input(s string) Pointer { - return Pointer{s, 0} -} - type Pointer struct { input string pos int @@ -23,59 +10,8 @@ func (p Pointer) Advance(i int) Pointer { } func (p Pointer) Get() string { - return p.input[p.pos:] -} - -func (p Pointer) Remaining() int { - remaining := len(p.input) - p.pos - if remaining < 0 { - return 0 - } - return remaining -} - -func (p Pointer) Next() (rune, Pointer) { - if int(p.pos) >= len(p.input) { - return EOF, p - } - r, w := utf8.DecodeRuneInString(p.input[p.pos:]) - return r, p.Advance(w) -} - -func (p Pointer) HasPrefix(s string) bool { - return strings.HasPrefix(p.input[p.pos:], s) -} - -func (p Pointer) Accept(valid string) (string, Pointer) { - c, newP := p.Next() - if strings.ContainsRune(valid, c) { - return string(c), newP - } - return "", p -} - -func (p Pointer) AcceptRun(valid string) (string, Pointer) { - matched := 0 - for p.pos+matched < len(p.input) { - r, w := utf8.DecodeRuneInString(p.input[p.pos+matched:]) - if !strings.ContainsRune(valid, r) { - break - } - matched += w + if p.pos > len(p.input) { + return "" } - - return p.input[p.pos : p.pos+matched], p.Advance(matched) -} - -func (p Pointer) AcceptUntil(invalid string) (string, Pointer) { - matched := 0 - for p.pos+matched < len(p.input) { - r, w := utf8.DecodeRuneInString(p.input[p.pos+matched:]) - if strings.ContainsRune(invalid, r) { - break - } - matched += w - } - - return p.input[p.pos : p.pos+matched], p.Advance(matched) + return p.input[p.pos:] } |