59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
package tokenize
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.makaay.nl/mauricem/go-parsekit/read"
|
|
)
|
|
|
|
// Input provides input-related functionality for the tokenize API,
|
|
// which is not specifically bound to a specific read mode (byte, rune).
|
|
type Input struct {
|
|
api *API
|
|
reader *read.Buffer // the buffered input reader
|
|
}
|
|
|
|
// Cursor returns a string that describes the current read cursor position.
|
|
func (i Input) Cursor() string {
|
|
column, line := 0, 0
|
|
for _, f := range i.api.stackFrames[:i.api.stackLevel+1] {
|
|
if f.line > 0 {
|
|
column = f.column
|
|
line += f.line
|
|
} else {
|
|
column += f.column
|
|
}
|
|
}
|
|
if line == 0 && column == 0 {
|
|
return fmt.Sprintf("start of file")
|
|
}
|
|
return fmt.Sprintf("line %d, column %d", line+1, column+1)
|
|
}
|
|
|
|
func (i Input) Reset() {
|
|
f := i.api.stackFrame
|
|
if f.offsetLocal > 0 {
|
|
f.column = 0
|
|
f.line = 0
|
|
f.offset -= f.offsetLocal
|
|
f.offsetLocal = 0
|
|
}
|
|
}
|
|
|
|
// Flush flushes input data from the read buffer up to the current
|
|
// read cursor position of the tokenizer.
|
|
//
|
|
// Note: in most cases, you won't have to call this method yourself.
|
|
// Parsekit will call this method at points where it knows it is a
|
|
// safe thing to do.
|
|
func (i Input) Flush() bool {
|
|
f := i.api.stackFrame
|
|
if f.offset > 0 {
|
|
i.reader.Flush(f.offset)
|
|
f.offset = 0
|
|
f.offsetLocal = 0
|
|
return true
|
|
}
|
|
return false
|
|
}
|