...
1 package tokens
2
3 import (
4 "fmt"
5 "strings"
6 )
7
8 type Pos interface {
9 Pos() int
10 }
11
12
13
14
15
16 type Position struct {
17 Filename string
18 Offset int
19 Line int
20 Column int
21 }
22
23
24 func (pos *Position) IsValid() bool { return pos.Line > 0 }
25
26
27 func (pos *Position) Pos() int { return pos.Offset }
28
29
30
31
32
33
34
35
36
37
38 func (pos Position) String() string {
39 s := pos.Filename
40 if pos.IsValid() {
41 if s != "" {
42 s += ":"
43 }
44 s += fmt.Sprintf("%d", pos.Line)
45 if pos.Column != 0 {
46 s += fmt.Sprintf(":%d", pos.Column)
47 }
48 }
49 if s == "" {
50 s = "-"
51 }
52 return s
53 }
54
55 func ReadablePosition(pos int, input string) (int, int) {
56 before := input[:pos]
57 lines := strings.Split(before, "\n")
58 length := len(lines)
59 return length, len(lines[length-1]) + 1
60 }
61
View as plain text