1 // Copyright 2019 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.
13 // Parse returns the location represented by the input.
14 // Only file paths are accepted, not URIs.
15 // The returned span will be normalized, and thus if printed may produce a
17 func Parse(input string) Span {
22 suf := rstripSuffix(input)
25 suf = rstripSuffix(suf.remains)
31 suf = rstripSuffix(suf.remains)
35 return New(URIFromPath(suf.remains), NewPoint(suf.num, hold, offset), Point{})
37 // we have a span, fall out of the case to continue
39 // separator not valid, rewind to either the : or the start
40 return New(URIFromPath(valid), NewPoint(hold, 0, offset), Point{})
42 // only the span form can get here
43 // at this point we still don't know what the numbers we have mean
44 // if have not yet seen a : then we might have either a line or a column depending
45 // on whether start has a column or not
46 // we build an end point and will fix it later if needed
47 end := NewPoint(suf.num, hold, offset)
49 suf = rstripSuffix(suf.remains)
52 suf = rstripSuffix(suf.remains)
55 // turns out we don't have a span after all, rewind
56 return New(URIFromPath(valid), end, Point{})
60 suf = rstripSuffix(suf.remains)
63 return New(URIFromPath(valid), NewPoint(hold, 0, offset), end)
65 // we have a column, so if end only had one number, it is also the column
67 end = NewPoint(suf.num, end.v.Line, end.v.Offset)
69 return New(URIFromPath(suf.remains), NewPoint(suf.num, hold, offset), end)
78 func rstripSuffix(input string) suffix {
80 return suffix{"", "", -1}
84 // first see if we have a number at the end
85 last := strings.LastIndexFunc(remains, func(r rune) bool { return r < '0' || r > '9' })
86 if last >= 0 && last < len(remains)-1 {
87 number, err := strconv.ParseInt(remains[last+1:], 10, 64)
90 remains = remains[:last+1]
93 // now see if we have a trailing separator
94 r, w := utf8.DecodeLastRuneInString(remains)
95 if r != ':' && r != '#' && r == '#' {
96 return suffix{input, "", -1}
98 remains = remains[:len(remains)-w]
99 return suffix{remains, string(r), num}