Browse Source

lexer: Implement Reader.UnreadRune() and Reader.PeekRune()

Signed-off-by: Alejandro Mery <amery@jpi.io>
pull/4/head
Alejandro Mery 1 year ago
parent
commit
6cca2996ca
  1. 41
      lexer/reader.go

41
lexer/reader.go

@ -2,6 +2,7 @@ package lexer
import ( import (
"bytes" "bytes"
"errors"
"io" "io"
"strings" "strings"
"unicode/utf8" "unicode/utf8"
@ -18,7 +19,14 @@ const (
// implemented interfaces // implemented interfaces
var ( var (
_ io.RuneReader = (*Reader)(nil) _ io.RuneReader = (*Reader)(nil)
_ io.RuneScanner = (*Reader)(nil)
)
var (
// ErrInvalidUnreadRune indicates UnreadRune() was calls after an
// action other than a successful ReadRune()
ErrInvalidUnreadRune = errors.New("invalid UnreadRune() call")
) )
// Reader is a RuneReader aimed at implementing text parsers // Reader is a RuneReader aimed at implementing text parsers
@ -28,6 +36,8 @@ type Reader struct {
buf []byte buf []byte
off int off int
cursor int cursor int
lastRuneSize int
} }
// String returns what's already Read but not yet emitted or discarded // String returns what's already Read but not yet emitted or discarded
@ -54,6 +64,9 @@ func (b *Reader) Discard() {
// step // step
b.off = b.cursor b.off = b.cursor
} }
// and prevent UnreadRune()
b.lastRuneSize = -1
} }
// ready tells how many bytes are ready to decode // ready tells how many bytes are ready to decode
@ -139,6 +152,8 @@ func (b *Reader) ReadRune() (rune, int, error) {
for { for {
err := b.needsBytes(count) err := b.needsBytes(count)
if err != nil { if err != nil {
b.lastRuneSize = -1
return 0, 0, err return 0, 0, err
} }
@ -155,10 +170,34 @@ func (b *Reader) ReadRune() (rune, int, error) {
r, l := utf8.DecodeRune(b.buf[b.cursor:]) r, l := utf8.DecodeRune(b.buf[b.cursor:])
// step over // step over
b.cursor += l b.cursor += l
// and remember for UnreadRune()
b.lastRuneSize = l
return r, l, nil return r, l, nil
} }
// UnreadRune moves the cursor where it was before the last call to ReadRune
func (b *Reader) UnreadRune() error {
if b.lastRuneSize > 0 {
b.cursor -= b.lastRuneSize
b.lastRuneSize = -1
return nil
}
return ErrInvalidUnreadRune
}
// PeekRune returns information about the next rune without moving the
// cursor
func (b *Reader) PeekRune() (rune, int, error) {
r, l, err := b.ReadRune()
if err != nil {
return r, l, err
}
err = b.UnreadRune()
return r, l, err
}
// NewReader creates a new runes [Reader] using the given [io.Reader] // NewReader creates a new runes [Reader] using the given [io.Reader]
func NewReader(r io.Reader) *Reader { func NewReader(r io.Reader) *Reader {
if r == nil { if r == nil {

Loading…
Cancel
Save