bufr/reader.go

254 lines
5.7 KiB
Go
Raw Normal View History

2022-04-19 14:35:10 +02:00
package bufr
import (
"bufio"
"io"
"strings"
2023-01-20 22:16:46 +01:00
"git.milar.in/milarin/ds"
2022-04-19 14:35:10 +02:00
)
type Reader struct {
2023-01-20 22:16:46 +01:00
buf ds.Stack[posRune]
2022-04-19 14:35:10 +02:00
src *bufio.Reader
2022-05-23 12:59:35 +02:00
pos *Position
2022-04-19 14:35:10 +02:00
}
2023-01-21 00:18:38 +01:00
func New(r io.Reader) *Reader {
2022-04-19 14:35:10 +02:00
return &Reader{
2023-01-20 22:16:46 +01:00
buf: ds.NewArrayStack[posRune](),
2022-04-19 14:35:10 +02:00
src: bufio.NewReader(r),
2023-03-18 23:10:22 +01:00
pos: &Position{Index: 0, Line: 1, Column: 1},
2022-04-19 14:35:10 +02:00
}
}
2023-01-21 00:18:38 +01:00
func NewFromString(str string) *Reader {
return New(strings.NewReader(str))
}
2022-05-23 12:59:35 +02:00
func (r *Reader) psrn(rn rune) posRune {
return posRune{
Rune: rn,
Pos: *r.pos,
}
}
2023-06-30 18:08:34 +02:00
func (r *Reader) Pos() Position {
return *r.pos
2022-05-23 12:59:35 +02:00
}
2022-04-19 14:35:10 +02:00
// Rune returns the next rune in r
func (r *Reader) Rune() (rune, error) {
rn, _, err := r.src.ReadRune()
if err == nil {
2022-05-23 12:59:35 +02:00
r.buf.Push(r.psrn(rn))
r.pos.Advance(rn)
2022-04-19 14:35:10 +02:00
}
return rn, err
}
2023-01-20 22:16:46 +01:00
// PeekRune returns the next rune in r without advancing reader position.
// The next read will return the same rune again.
func (r *Reader) PeekRune() (rune, error) {
rn, err := r.Rune()
if err != nil {
return 0, err
}
if err := r.UnreadRune(); err != nil {
return 0, err
}
return rn, nil
}
2023-01-20 22:33:44 +01:00
// String returns the next len runes in r as a string.
// If an error occurs, both the already read string and the error will be returned
func (r *Reader) String(len int) (string, error) {
str := ""
for i := 0; i < len; i++ {
rn, err := r.Rune()
if err != nil {
return str, err
}
str += string(rn)
}
return str, nil
}
2022-04-19 14:35:10 +02:00
// UnreadRune unreads the last rune.
// The next read will include the unread rune.
// It returns ErrNothingToUnread if there wasn't any read yet
func (r *Reader) UnreadRune() error {
if r.buf.Empty() {
return ErrNothingToUnread.New()
}
if err := r.src.UnreadRune(); err == nil {
2022-05-23 12:59:35 +02:00
rn := r.buf.Pop()
*r.pos = rn.Pos
2022-04-19 14:35:10 +02:00
} else {
2022-05-23 12:59:35 +02:00
rn := r.buf.Pop()
*r.pos = rn.Pos
r.src = prependRune(rn.Rune, r.src)
2022-04-19 14:35:10 +02:00
}
return nil
}
2022-04-19 19:58:38 +02:00
// UnreadString calls UnreadRune for each rune in str + one addtional rune for the separator rune
// The actual runes in str are irrelevant.
2022-04-19 14:35:10 +02:00
// Only the rune count of str determines the amount of UnreadRune calls.
// The first error occured will be returned immediately.
func (r *Reader) UnreadString(str string) error {
for range str {
err := r.UnreadRune()
if err != nil {
return err
}
}
2022-04-19 19:58:38 +02:00
return r.UnreadRune()
2022-04-19 14:35:10 +02:00
}
2023-01-20 22:33:44 +01:00
// UnreadRunes calls UnreadRune n times
func (r *Reader) UnreadRunes(n int) error {
for i := 0; i < n; i++ {
err := r.UnreadRune()
if err != nil {
return err
}
}
return nil
}
2023-01-20 23:59:36 +01:00
// StringWhile reads runes and calls all functions for each one.
// It returns all runes as a string for which any function returned true.
// It stops when all functions returned false or an error occured.
// The rune for which that function returned false will not be unread.
func (r *Reader) StringWhile(f ...RuneFunc) (string, error) {
2022-04-19 14:35:10 +02:00
s := new(strings.Builder)
2022-05-23 14:04:39 +02:00
var rn rune
2022-04-19 14:35:10 +02:00
var err error
2023-01-20 23:59:36 +01:00
for rn, err = r.Rune(); err == nil && findFirstTrue(rn, f); rn, err = r.Rune() {
2022-04-19 14:35:10 +02:00
s.WriteRune(rn)
}
return s.String(), err
}
2023-01-20 22:16:46 +01:00
// PeekStringWhile acts as StringWhile but does not advance reader position
2023-01-20 23:59:36 +01:00
func (r *Reader) PeekStringWhile(f ...RuneFunc) (string, error) {
str, err := r.StringWhile(f...)
2023-01-20 22:16:46 +01:00
if err != nil {
return "", err
}
if err := r.UnreadString(str); err != nil {
return "", err
}
return str, nil
}
2023-01-20 23:59:36 +01:00
// StringUntil reads runes and calls all functions for each one.
// It returns all runes as a string for which all functions returned true.
// It stops when any function returns false or an error occured.
// The rune for which that function returned false will not be unread.
func (r *Reader) StringUntil(f ...RuneFunc) (string, error) {
return r.StringWhile(func(rn rune) bool { return !findFirstTrue(rn, f) })
2022-04-19 14:35:10 +02:00
}
2023-01-20 22:16:46 +01:00
// PeekStringUntil acts as StringUntil but does not advance reader position
2023-01-20 23:59:36 +01:00
func (r *Reader) PeekStringUntil(f ...RuneFunc) (string, error) {
str, err := r.StringUntil(f...)
2023-01-20 22:16:46 +01:00
if err != nil {
return "", err
}
if err := r.UnreadString(str); err != nil {
return "", err
}
return str, nil
}
2023-01-21 00:18:38 +01:00
// SkipUntil acts as StringUntil but discards the string
2023-01-21 00:37:14 +01:00
// The rune for which that function returned false will be unread.
2023-01-21 00:18:38 +01:00
func (r *Reader) SkipUntil(f ...RuneFunc) error {
2023-01-20 23:59:36 +01:00
_, err := r.StringUntil(f...)
2023-01-21 00:37:14 +01:00
if err != nil {
return err
}
return r.UnreadRune()
2023-01-20 23:59:36 +01:00
}
2023-01-21 00:37:14 +01:00
// SkipWhile acts as StringWhile but discards the string.
// The rune for which that function returned false will be unread.
2023-01-21 00:18:38 +01:00
func (r *Reader) SkipWhile(f ...RuneFunc) error {
_, err := r.StringWhile(f...)
2023-01-21 00:37:14 +01:00
if err != nil {
return err
}
return r.UnreadRune()
2023-01-21 00:18:38 +01:00
}
// 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
}
2023-01-20 22:16:46 +01:00
// 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.
func (r *Reader) Commit() {
r.buf.Clear()
}