Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
e344b5973a | |||
aab77ddc6c | |||
1bb8ab5e9b | |||
19e0a61345 | |||
5897251193 | |||
9d4da8ef95 | |||
31f5aa6e52 | |||
1c9a3962ec |
@ -6,4 +6,5 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
ErrNothingToUnread = adverr.NewErrTmpl("ErrNothingToUnread", "Unreading failed because there wasn't any Read yet")
|
ErrNothingToUnread = adverr.NewErrTmpl("ErrNothingToUnread", "Unreading failed because there wasn't any Read yet")
|
||||||
|
ErrNoMatchFound = adverr.NewErrTmpl("ErrNoMatchFound", "no match found")
|
||||||
)
|
)
|
||||||
|
@ -10,7 +10,7 @@ func (p *Position) Advance(rn rune) {
|
|||||||
p.Index++
|
p.Index++
|
||||||
if rn == '\n' {
|
if rn == '\n' {
|
||||||
p.Line++
|
p.Line++
|
||||||
p.Column = 0
|
p.Column = 1
|
||||||
} else {
|
} else {
|
||||||
p.Column++
|
p.Column++
|
||||||
}
|
}
|
||||||
|
91
reader.go
91
reader.go
@ -14,14 +14,18 @@ type Reader struct {
|
|||||||
pos *Position
|
pos *Position
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewReader(r io.Reader) *Reader {
|
func New(r io.Reader) *Reader {
|
||||||
return &Reader{
|
return &Reader{
|
||||||
buf: ds.NewArrayStack[posRune](),
|
buf: ds.NewArrayStack[posRune](),
|
||||||
src: bufio.NewReader(r),
|
src: bufio.NewReader(r),
|
||||||
pos: &Position{},
|
pos: &Position{Index: 0, Line: 1, Column: 1},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewFromString(str string) *Reader {
|
||||||
|
return New(strings.NewReader(str))
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Reader) psrn(rn rune) posRune {
|
func (r *Reader) psrn(rn rune) posRune {
|
||||||
return posRune{
|
return posRune{
|
||||||
Rune: rn,
|
Rune: rn,
|
||||||
@ -29,8 +33,8 @@ func (r *Reader) psrn(rn rune) posRune {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reader) Pos() (index, line, column int) {
|
func (r *Reader) Pos() Position {
|
||||||
return r.pos.Index, r.pos.Line, r.pos.Column
|
return *r.pos
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rune returns the next rune in r
|
// Rune returns the next rune in r
|
||||||
@ -43,6 +47,10 @@ func (r *Reader) Rune() (rune, error) {
|
|||||||
return rn, err
|
return rn, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Reader) Buffered() int {
|
||||||
|
return r.src.Buffered()
|
||||||
|
}
|
||||||
|
|
||||||
// PeekRune returns the next rune in r without advancing reader position.
|
// PeekRune returns the next rune in r without advancing reader position.
|
||||||
// The next read will return the same rune again.
|
// The next read will return the same rune again.
|
||||||
func (r *Reader) PeekRune() (rune, error) {
|
func (r *Reader) PeekRune() (rune, error) {
|
||||||
@ -149,12 +157,6 @@ func (r *Reader) PeekStringWhile(f ...RuneFunc) (string, error) {
|
|||||||
return str, nil
|
return str, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SkipStringWhile acts as StringWhile but discards the string
|
|
||||||
func (r *Reader) SkipStringWhile(f ...RuneFunc) error {
|
|
||||||
_, err := r.StringWhile(f...)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// StringUntil reads runes and calls all functions for each one.
|
// StringUntil reads runes and calls all functions for each one.
|
||||||
// It returns all runes as a string for which all functions returned true.
|
// It returns all runes as a string for which all functions returned true.
|
||||||
// It stops when any function returns false or an error occured.
|
// It stops when any function returns false or an error occured.
|
||||||
@ -177,11 +179,76 @@ func (r *Reader) PeekStringUntil(f ...RuneFunc) (string, error) {
|
|||||||
return str, nil
|
return str, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SkipStringUntil acts as StringUntil but discards the string
|
// SkipUntil acts as StringUntil but discards the string
|
||||||
func (r *Reader) SkipStringUntil(f ...RuneFunc) error {
|
// The rune for which that function returned false will be unread.
|
||||||
|
func (r *Reader) SkipUntil(f ...RuneFunc) error {
|
||||||
_, err := r.StringUntil(f...)
|
_, err := r.StringUntil(f...)
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
return r.UnreadRune()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SkipWhile acts as StringWhile but discards the string.
|
||||||
|
// The rune for which that function returned false will be unread.
|
||||||
|
func (r *Reader) SkipWhile(f ...RuneFunc) error {
|
||||||
|
_, err := r.StringWhile(f...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return r.UnreadRune()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExpectRune returns true if any function returns true for the next rune read from r
|
||||||
|
func (r *Reader) ExpectRune(f ...RuneFunc) (bool, error) {
|
||||||
|
rn, err := r.Rune()
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return findFirstTrue(rn, f), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExpectString calls ExpectRune for each rune in str successively.
|
||||||
|
// If the expected string was not found, all read runes will be unread
|
||||||
|
func (r *Reader) ExpectString(str string) (bool, error) {
|
||||||
|
read := 0
|
||||||
|
for _, rn := range str {
|
||||||
|
ok, err := r.ExpectRune(Is(rn))
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
read++
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
if err := r.UnreadRunes(read); err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExpectOneOfString calls ExpectString for each string successively
|
||||||
|
// and returns the string which first matched.
|
||||||
|
// The returned string will not be unread.
|
||||||
|
// If no string matches, ErrNoMatchFound is returned
|
||||||
|
func (r *Reader) ExpectOneOfString(str ...string) (string, error) {
|
||||||
|
for _, s := range str {
|
||||||
|
ok, err := r.ExpectString(s)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if ok {
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", ErrNoMatchFound
|
||||||
|
}
|
||||||
|
|
||||||
// Commit clears the internal buffer and therefore removes all data which were already read.
|
// Commit clears the internal buffer and therefore removes all data which were already read.
|
||||||
// After calling Commit any unreads will return ErrNothingToUnread until another read occured.
|
// After calling Commit any unreads will return ErrNothingToUnread until another read occured.
|
||||||
|
@ -7,12 +7,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestPos(t *testing.T) {
|
func TestPos(t *testing.T) {
|
||||||
r := NewReader(strings.NewReader("hello world\nsecond line"))
|
r := New(strings.NewReader("hello world\nsecond line"))
|
||||||
|
|
||||||
unread := false
|
unread := false
|
||||||
for rn, err := r.Rune(); err == nil; rn, err = r.Rune() {
|
for rn, err := r.Rune(); err == nil; rn, err = r.Rune() {
|
||||||
index, line, col := r.Pos()
|
pos := r.Pos()
|
||||||
fmt.Println(string(rn), index, line, col)
|
fmt.Println(string(rn), pos.Index, pos.Line, pos.Column)
|
||||||
|
|
||||||
if !unread && rn == '\n' {
|
if !unread && rn == '\n' {
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
@ -24,7 +24,7 @@ func TestPos(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEOF(t *testing.T) {
|
func TestEOF(t *testing.T) {
|
||||||
r := NewReader(strings.NewReader("hello world\nasddsa"))
|
r := New(strings.NewReader("hello world\nasddsa"))
|
||||||
|
|
||||||
var line string
|
var line string
|
||||||
var err error
|
var err error
|
||||||
|
@ -12,8 +12,12 @@ func IsSpace(rn rune) bool {
|
|||||||
return rn == ' '
|
return rn == ' '
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IsTab(rn rune) bool {
|
||||||
|
return rn == '\t'
|
||||||
|
}
|
||||||
|
|
||||||
func IsWhitespace(rn rune) bool {
|
func IsWhitespace(rn rune) bool {
|
||||||
return IsSpace(rn) || IsNewLine(rn)
|
return IsSpace(rn) || IsTab(rn) || IsNewLine(rn)
|
||||||
}
|
}
|
||||||
|
|
||||||
func And(f ...RuneFunc) RuneFunc {
|
func And(f ...RuneFunc) RuneFunc {
|
||||||
|
Reference in New Issue
Block a user