1 package token 2 3 import "fmt" 4 5 // Pos describes an arbitrary source position 6 // including the file, line, and column location. 7 // A Position is valid if the line number is > 0. 8 type Pos struct { 9 Filename string // filename, if any 10 Offset int // offset, starting at 0 11 Line int // line number, starting at 1 12 Column int // column number, starting at 1 (character count) 13 } 14 15 // IsValid returns true if the position is valid. 16 func (p *Pos) IsValid() bool { return p.Line > 0 } 17 18 // String returns a string in one of several forms: 19 // 20 // file:line:column valid position with file name 21 // line:column valid position without file name 22 // file invalid position with file name 23 // - invalid position without file name 24 func (p Pos) String() string { 25 s := p.Filename 26 if p.IsValid() { 27 if s != "" { 28 s += ":" 29 } 30 s += fmt.Sprintf("%d:%d", p.Line, p.Column) 31 } 32 if s == "" { 33 s = "-" 34 } 35 return s 36 } 37 38 // Before reports whether the position p is before u. 39 func (p Pos) Before(u Pos) bool { 40 return u.Offset > p.Offset || u.Line > p.Line 41 } 42 43 // After reports whether the position p is after u. 44 func (p Pos) After(u Pos) bool { 45 return u.Offset < p.Offset || u.Line < p.Line 46 } 47