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.
14 // Parse returns the location represented by the input.
15 // Only file paths are accepted, not URIs.
16 // The returned span will be normalized, and thus if printed may produce a
18 func Parse(input string) Span {
19 return ParseInDir(input, ".")
22 // ParseInDir is like Parse, but interprets paths relative to wd.
23 func ParseInDir(input, wd string) Span {
24 uri := func(path string) URI {
25 if !filepath.IsAbs(path) {
26 path = filepath.Join(wd, path)
28 return URIFromPath(path)
34 suf := rstripSuffix(input)
37 suf = rstripSuffix(suf.remains)
43 suf = rstripSuffix(suf.remains)
47 return New(uri(suf.remains), NewPoint(suf.num, hold, offset), Point{})
49 // we have a span, fall out of the case to continue
51 // separator not valid, rewind to either the : or the start
52 return New(uri(valid), NewPoint(hold, 0, offset), Point{})
54 // only the span form can get here
55 // at this point we still don't know what the numbers we have mean
56 // if have not yet seen a : then we might have either a line or a column depending
57 // on whether start has a column or not
58 // we build an end point and will fix it later if needed
59 end := NewPoint(suf.num, hold, offset)
61 suf = rstripSuffix(suf.remains)
64 suf = rstripSuffix(suf.remains)
67 // turns out we don't have a span after all, rewind
68 return New(uri(valid), end, Point{})
72 suf = rstripSuffix(suf.remains)
75 return New(uri(valid), NewPoint(hold, 0, offset), end)
77 // we have a column, so if end only had one number, it is also the column
79 end = NewPoint(suf.num, end.v.Line, end.v.Offset)
81 return New(uri(suf.remains), NewPoint(suf.num, hold, offset), end)
90 func rstripSuffix(input string) suffix {
92 return suffix{"", "", -1}
96 // first see if we have a number at the end
97 last := strings.LastIndexFunc(remains, func(r rune) bool { return r < '0' || r > '9' })
98 if last >= 0 && last < len(remains)-1 {
99 number, err := strconv.ParseInt(remains[last+1:], 10, 64)
102 remains = remains[:last+1]
105 // now see if we have a trailing separator
106 r, w := utf8.DecodeLastRuneInString(remains)
107 if r != ':' && r != '#' && r == '#' {
108 return suffix{input, "", -1}
110 remains = remains[:len(remains)-w]
111 return suffix{remains, string(r), num}