5 Commits

Author SHA1 Message Date
1bb8ab5e9b fixed IsWhitespace function consideres tabs as whitespace 2023-06-30 19:16:25 +02:00
19e0a61345 Pos() returns a Position object 2023-06-30 18:08:34 +02:00
5897251193 fixed column indexing 2023-03-18 23:10:22 +01:00
9d4da8ef95 fixed line indexing 2023-03-18 23:08:33 +01:00
31f5aa6e52 unread rune on skip methods 2023-01-21 00:37:14 +01:00
4 changed files with 22 additions and 10 deletions

View File

@ -10,7 +10,7 @@ func (p *Position) Advance(rn rune) {
p.Index++
if rn == '\n' {
p.Line++
p.Column = 0
p.Column = 1
} else {
p.Column++
}

View File

@ -18,7 +18,7 @@ func New(r io.Reader) *Reader {
return &Reader{
buf: ds.NewArrayStack[posRune](),
src: bufio.NewReader(r),
pos: &Position{},
pos: &Position{Index: 0, Line: 1, Column: 1},
}
}
@ -33,8 +33,8 @@ func (r *Reader) psrn(rn rune) posRune {
}
}
func (r *Reader) Pos() (index, line, column int) {
return r.pos.Index, r.pos.Line, r.pos.Column
func (r *Reader) Pos() Position {
return *r.pos
}
// Rune returns the next rune in r
@ -176,16 +176,24 @@ func (r *Reader) PeekStringUntil(f ...RuneFunc) (string, error) {
}
// SkipUntil acts as StringUntil but discards the string
// The rune for which that function returned false will be unread.
func (r *Reader) SkipUntil(f ...RuneFunc) error {
_, err := r.StringUntil(f...)
if err != nil {
return err
}
return r.UnreadRune()
}
// SkipWhile acts as StringWhile but discards the string
// 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) {

View File

@ -11,8 +11,8 @@ func TestPos(t *testing.T) {
unread := false
for rn, err := r.Rune(); err == nil; rn, err = r.Rune() {
index, line, col := r.Pos()
fmt.Println(string(rn), index, line, col)
pos := r.Pos()
fmt.Println(string(rn), pos.Index, pos.Line, pos.Column)
if !unread && rn == '\n' {
for i := 0; i < 5; i++ {

View File

@ -12,8 +12,12 @@ func IsSpace(rn rune) bool {
return rn == ' '
}
func IsTab(rn rune) bool {
return rn == '\t'
}
func IsWhitespace(rn rune) bool {
return IsSpace(rn) || IsNewLine(rn)
return IsSpace(rn) || IsTab(rn) || IsNewLine(rn)
}
func And(f ...RuneFunc) RuneFunc {