2022-04-19 14:35:10 +02:00
|
|
|
package bufr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.tordarus.net/Tordarus/dstruct"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Reader struct {
|
2022-05-23 12:59:35 +02:00
|
|
|
buf *dstruct.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
|
|
|
}
|
|
|
|
|
|
|
|
func NewReader(r io.Reader) *Reader {
|
|
|
|
return &Reader{
|
2022-05-23 12:59:35 +02:00
|
|
|
buf: new(dstruct.Stack[posRune]),
|
2022-04-19 14:35:10 +02:00
|
|
|
src: bufio.NewReader(r),
|
2022-05-23 12:59:35 +02:00
|
|
|
pos: &Position{},
|
2022-04-19 14:35:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-23 12:59:35 +02:00
|
|
|
func (r *Reader) psrn(rn rune) posRune {
|
|
|
|
return posRune{
|
|
|
|
Rune: rn,
|
|
|
|
Pos: *r.pos,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) Pos() (index, line, column int) {
|
|
|
|
return r.pos.Index, r.pos.Line, r.pos.Column
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
// StringWhile reads runes and calls f for each one.
|
|
|
|
// It returns all runes as a string for which f returned true.
|
|
|
|
// It stops when f returns false or an error occured.
|
|
|
|
// The rune for which f returned false will not be unread.
|
|
|
|
func (r *Reader) StringWhile(f func(rn rune) bool) (string, error) {
|
|
|
|
s := new(strings.Builder)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
for rn, err := r.Rune(); err == nil && f(rn); rn, err = r.Rune() {
|
|
|
|
s.WriteRune(rn)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.String(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// StringUntil is a shorthand for r.StringWhile(func(rn rune) bool { return !f(rn) })
|
|
|
|
func (r *Reader) StringUntil(f func(rn rune) bool) (string, error) {
|
|
|
|
return r.StringWhile(func(rn rune) bool { return !f(rn) })
|
|
|
|
}
|