1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 func parseOctothorpDecimal(s string) int {
15 if s != "" && s[0] == '#' {
16 if s, err := strconv.ParseInt(s[1:], 10, 32); err == nil {
23 func parsePos(pos string) (filename string, startOffset, endOffset int, err error) {
25 err = fmt.Errorf("no source position specified")
29 colon := strings.LastIndex(pos, ":")
31 err = fmt.Errorf("bad position syntax %q", pos)
34 filename, offset := pos[:colon], pos[colon+1:]
37 if hyphen := strings.Index(offset, ","); hyphen < 0 {
39 startOffset = parseOctothorpDecimal(offset)
40 endOffset = startOffset
42 // e.g. "foo.go:#123,#456"
43 startOffset = parseOctothorpDecimal(offset[:hyphen])
44 endOffset = parseOctothorpDecimal(offset[hyphen+1:])
46 if startOffset < 0 || endOffset < 0 {
47 err = fmt.Errorf("invalid offset %q in query position", offset)
53 func fileOffsetToPos(file *token.File, startOffset, endOffset int) (start, end token.Pos, err error) {
54 // Range check [start..end], inclusive of both end-points.
56 if 0 <= startOffset && startOffset <= file.Size() {
57 start = file.Pos(int(startOffset))
59 err = fmt.Errorf("start position is beyond end of file")
63 if 0 <= endOffset && endOffset <= file.Size() {
64 end = file.Pos(int(endOffset))
66 err = fmt.Errorf("end position is beyond end of file")