| 1 | // Copyright 2022 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. |
| 4 | |
| 5 | // Package safetoken provides wrappers around methods in go/token, |
| 6 | // that return errors rather than panicking. |
| 7 | // |
| 8 | // It also provides a central place for workarounds in the underlying |
| 9 | // packages. The use of this package's functions instead of methods of |
| 10 | // token.File (such as Offset, Position, and PositionFor) is mandatory |
| 11 | // throughout the gopls codebase and enforced by a static check. |
| 12 | package safetoken |
| 13 | |
| 14 | import ( |
| 15 | "fmt" |
| 16 | "go/token" |
| 17 | ) |
| 18 | |
| 19 | // Offset returns f.Offset(pos), but first checks that the file |
| 20 | // contains the pos. |
| 21 | // |
| 22 | // The definition of "contains" here differs from that of token.File |
| 23 | // in order to work around a bug in the parser (issue #57490): during |
| 24 | // error recovery, the parser may create syntax nodes whose computed |
| 25 | // End position is 1 byte beyond EOF, which would cause |
| 26 | // token.File.Offset to panic. The workaround is that this function |
| 27 | // accepts a Pos that is exactly 1 byte beyond EOF and maps it to the |
| 28 | // EOF offset. |
| 29 | func Offset(f *token.File, pos token.Pos) (int, error) { |
| 30 | if !inRange(f, pos) { |
| 31 | // Accept a Pos that is 1 byte beyond EOF, |
| 32 | // and map it to the EOF offset. |
| 33 | // (Workaround for #57490.) |
| 34 | if int(pos) == f.Base()+f.Size()+1 { |
| 35 | return f.Size(), nil |
| 36 | } |
| 37 | |
| 38 | return -1, fmt.Errorf("pos %d is not in range [%d:%d] of file %s", |
| 39 | pos, f.Base(), f.Base()+f.Size(), f.Name()) |
| 40 | } |
| 41 | return int(pos) - f.Base(), nil |
| 42 | } |
| 43 | |
| 44 | // Offsets returns Offset(start) and Offset(end). |
| 45 | // It returns an error if either failed, or if start > end. |
| 46 | func Offsets(f *token.File, start, end token.Pos) (int, int, error) { |
| 47 | startOffset, err := Offset(f, start) |
| 48 | if err != nil { |
| 49 | return 0, 0, fmt.Errorf("start: %v", err) |
| 50 | } |
| 51 | endOffset, err := Offset(f, end) |
| 52 | if err != nil { |
| 53 | return 0, 0, fmt.Errorf("end: %v", err) |
| 54 | } |
| 55 | if start > end { |
| 56 | return 0, 0, fmt.Errorf("start (offset %d) > end (offset %d)", start, end) |
| 57 | } |
| 58 | return startOffset, endOffset, nil |
| 59 | } |
| 60 | |
| 61 | // Pos returns f.Pos(offset), but first checks that the offset is |
| 62 | // non-negative and not larger than the size of the file. |
| 63 | func Pos(f *token.File, offset int) (token.Pos, error) { |
| 64 | if !(0 <= offset && offset <= f.Size()) { |
| 65 | return token.NoPos, fmt.Errorf("offset %d is not in range for file %s of size %d", offset, f.Name(), f.Size()) |
| 66 | } |
| 67 | return token.Pos(f.Base() + offset), nil |
| 68 | } |
| 69 | |
| 70 | // inRange reports whether file f contains position pos, |
| 71 | // according to the invariants of token.File. |
| 72 | // |
| 73 | // This function is not public because of the ambiguity it would |
| 74 | // create w.r.t. the definition of "contains". Use Offset instead. |
| 75 | func inRange(f *token.File, pos token.Pos) bool { |
| 76 | return token.Pos(f.Base()) <= pos && pos <= token.Pos(f.Base()+f.Size()) |
| 77 | } |
| 78 | |
| 79 | // Position returns the Position for the pos value in the given file. |
| 80 | // |
| 81 | // p must be NoPos, a valid Pos in the range of f, or exactly 1 byte |
| 82 | // beyond the end of f. (See [Offset] for explanation.) |
| 83 | // Any other value causes a panic. |
| 84 | // |
| 85 | // Line directives (//line comments) are ignored. |
| 86 | func Position(f *token.File, pos token.Pos) token.Position { |
| 87 | // Work around issue #57490. |
| 88 | if int(pos) == f.Base()+f.Size()+1 { |
| 89 | pos-- |
| 90 | } |
| 91 | |
| 92 | // TODO(adonovan): centralize the workaround for |
| 93 | // golang/go#41029 (newline at EOF) here too. |
| 94 | |
| 95 | return f.PositionFor(pos, false) |
| 96 | } |
| 97 | |
| 98 | // Line returns the line number for the given offset in the given file. |
| 99 | func Line(f *token.File, pos token.Pos) int { |
| 100 | return Position(f, pos).Line |
| 101 | } |
| 102 | |
| 103 | // StartPosition converts a start Pos in the FileSet into a Position. |
| 104 | // |
| 105 | // Call this function only if start represents the start of a token or |
| 106 | // parse tree, such as the result of Node.Pos(). If start is the end of |
| 107 | // an interval, such as Node.End(), call EndPosition instead, as it |
| 108 | // may need the correction described at [Position]. |
| 109 | func StartPosition(fset *token.FileSet, start token.Pos) (_ token.Position) { |
| 110 | if f := fset.File(start); f != nil { |
| 111 | return Position(f, start) |
| 112 | } |
| 113 | return |
| 114 | } |
| 115 | |
| 116 | // EndPosition converts an end Pos in the FileSet into a Position. |
| 117 | // |
| 118 | // Call this function only if pos represents the end of |
| 119 | // a non-empty interval, such as the result of Node.End(). |
| 120 | func EndPosition(fset *token.FileSet, end token.Pos) (_ token.Position) { |
| 121 | if f := fset.File(end); f != nil && int(end) > f.Base() { |
| 122 | return Position(f, end) |
| 123 | } |
| 124 | |
| 125 | // Work around issue #57490. |
| 126 | if f := fset.File(end - 1); f != nil { |
| 127 | return Position(f, end) |
| 128 | } |
| 129 | |
| 130 | return |
| 131 | } |