-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposition.go
41 lines (33 loc) · 820 Bytes
/
position.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package simplexer
import (
"fmt"
"strings"
)
// Position in the file.
type Position struct {
Line int
Column int
}
// Convert to string.
func (p Position) String() string {
return fmt.Sprintf("[line:%d, column:%d]", p.Line, p.Column)
}
// Position.Before will check p is before than x.
func (p Position) Before(x Position) bool {
return p.Line < x.Line || (p.Line == x.Line && p.Column < x.Column)
}
// Position.After will check p is after than x.
func (p Position) After(x Position) bool {
return p.Line > x.Line || (p.Line == x.Line && p.Column > x.Column)
}
func shiftPos(p Position, s string) Position {
lines := strings.Split(s, "\n")
lineShift := len(lines) - 1
if lineShift == 0 {
p.Column += len(lines[0])
} else {
p.Column = len(lines[len(lines)-1])
}
p.Line += lineShift
return p
}