30 lines
695 B
Go
30 lines
695 B
Go
package parsekit
|
|
|
|
import "fmt"
|
|
|
|
// Cursor represents the position of the input cursor in various ways.
|
|
type Cursor struct {
|
|
Byte int // The cursor offset in bytes
|
|
Rune int // The cursor offset in UTF8 runes
|
|
Column int // The column at which the cursor is (0-indexed)
|
|
Line int // The line at which the cursor is (0-indexed)
|
|
}
|
|
|
|
func (c *Cursor) String() string {
|
|
return fmt.Sprintf("line %d, column %d", c.Line+1, c.Column+1)
|
|
}
|
|
|
|
// move updates the position of the cursor, based on the provided input string.
|
|
func (c *Cursor) move(input string) {
|
|
c.Byte += len(input)
|
|
for _, r := range input {
|
|
c.Rune++
|
|
if r == '\n' {
|
|
c.Column = 0
|
|
c.Line++
|
|
} else {
|
|
c.Column++
|
|
}
|
|
}
|
|
}
|