position.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // Copyright 2010 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. // Package token is variant of the stdlib package token with types FileSet and
  5. // Token removed.
  6. package token // import "modernc.org/token"
  7. import (
  8. "fmt"
  9. "sort"
  10. )
  11. // -----------------------------------------------------------------------------
  12. // Positions
  13. // Position describes an arbitrary source position
  14. // including the file, line, and column location.
  15. // A Position is valid if the line number is > 0.
  16. type Position struct {
  17. Filename string // filename, if any
  18. Offset int // offset, starting at 0
  19. Line int // line number, starting at 1
  20. Column int // column number, starting at 1 (byte count)
  21. }
  22. // IsValid reports whether the position is valid.
  23. func (pos *Position) IsValid() bool { return pos.Line > 0 }
  24. // String returns a string in one of several forms:
  25. //
  26. // file:line:column valid position with file name
  27. // file:line valid position with file name but no column (column == 0)
  28. // line:column valid position without file name
  29. // line valid position without file name and no column (column == 0)
  30. // file invalid position with file name
  31. // - invalid position without file name
  32. func (pos Position) String() string {
  33. s := pos.Filename
  34. if pos.IsValid() {
  35. if s != "" {
  36. s += ":"
  37. }
  38. s += fmt.Sprintf("%d", pos.Line)
  39. if pos.Column != 0 {
  40. s += fmt.Sprintf(":%d", pos.Column)
  41. }
  42. }
  43. if s == "" {
  44. s = "-"
  45. }
  46. return s
  47. }
  48. // Pos is a compact encoding of a source position within a file set.
  49. // It can be converted into a Position for a more convenient, but much
  50. // larger, representation.
  51. //
  52. // The Pos value for a given file is a number in the range [base, base+size],
  53. // where base and size are specified when a file is added to the file set.
  54. // The difference between a Pos value and the corresponding file base
  55. // corresponds to the byte offset of that position (represented by the Pos value)
  56. // from the beginning of the file. Thus, the file base offset is the Pos value
  57. // representing the first byte in the file.
  58. //
  59. // To create the Pos value for a specific source offset (measured in bytes),
  60. // first add the respective file to the current file set using FileSet.AddFile
  61. // and then call File.Pos(offset) for that file. Given a Pos value p
  62. // for a specific file set fset, the corresponding Position value is
  63. // obtained by calling fset.Position(p).
  64. //
  65. // Pos values can be compared directly with the usual comparison operators:
  66. // If two Pos values p and q are in the same file, comparing p and q is
  67. // equivalent to comparing the respective source file offsets. If p and q
  68. // are in different files, p < q is true if the file implied by p was added
  69. // to the respective file set before the file implied by q.
  70. type Pos int
  71. // The zero value for Pos is NoPos; there is no file and line information
  72. // associated with it, and NoPos.IsValid() is false. NoPos is always
  73. // smaller than any other Pos value. The corresponding Position value
  74. // for NoPos is the zero value for Position.
  75. const NoPos Pos = 0
  76. // IsValid reports whether the position is valid.
  77. func (p Pos) IsValid() bool {
  78. return p != NoPos
  79. }
  80. // -----------------------------------------------------------------------------
  81. // File
  82. // A File is a handle for a file belonging to a FileSet.
  83. // A File has a name, size, and line offset table.
  84. type File struct {
  85. name string // file name as provided to AddFile
  86. base int // Pos value range for this file is [base...base+size]
  87. size int // file size as provided to AddFile
  88. lines []int // lines contains the offset of the first character for each line (the first entry is always 0)
  89. infos []lineInfo
  90. }
  91. // NewFile returns a new file with a given filename and file size.
  92. //
  93. func NewFile(filename string, size int) *File {
  94. return &File{name: filename, base: 1, size: size, lines: []int{0}}
  95. }
  96. // Name returns the file name of file f as registered with AddFile.
  97. func (f *File) Name() string {
  98. return f.name
  99. }
  100. // Base returns the base offset of file f as registered with AddFile.
  101. func (f *File) Base() int {
  102. return f.base
  103. }
  104. // Size returns the size of file f as registered with AddFile.
  105. func (f *File) Size() int {
  106. return f.size
  107. }
  108. // LineCount returns the number of lines in file f.
  109. func (f *File) LineCount() int {
  110. n := len(f.lines)
  111. return n
  112. }
  113. // AddLine adds the line offset for a new line.
  114. // The line offset must be larger than the offset for the previous line
  115. // and smaller than the file size; otherwise the line offset is ignored.
  116. func (f *File) AddLine(offset int) {
  117. if i := len(f.lines); (i == 0 || f.lines[i-1] < offset) && offset < f.size {
  118. f.lines = append(f.lines, offset)
  119. }
  120. }
  121. // MergeLine merges a line with the following line. It is akin to replacing
  122. // the newline character at the end of the line with a space (to not change the
  123. // remaining offsets). To obtain the line number, consult e.g. Position.Line.
  124. // MergeLine will panic if given an invalid line number.
  125. func (f *File) MergeLine(line int) {
  126. if line < 1 {
  127. panic(fmt.Sprintf("invalid line number %d (should be >= 1)", line))
  128. }
  129. if line >= len(f.lines) {
  130. panic(fmt.Sprintf("invalid line number %d (should be < %d)", line, len(f.lines)))
  131. }
  132. // To merge the line numbered <line> with the line numbered <line+1>,
  133. // we need to remove the entry in lines corresponding to the line
  134. // numbered <line+1>. The entry in lines corresponding to the line
  135. // numbered <line+1> is located at index <line>, since indices in lines
  136. // are 0-based and line numbers are 1-based.
  137. copy(f.lines[line:], f.lines[line+1:])
  138. f.lines = f.lines[:len(f.lines)-1]
  139. }
  140. // SetLines sets the line offsets for a file and reports whether it succeeded.
  141. // The line offsets are the offsets of the first character of each line;
  142. // for instance for the content "ab\nc\n" the line offsets are {0, 3}.
  143. // An empty file has an empty line offset table.
  144. // Each line offset must be larger than the offset for the previous line
  145. // and smaller than the file size; otherwise SetLines fails and returns
  146. // false.
  147. // Callers must not mutate the provided slice after SetLines returns.
  148. func (f *File) SetLines(lines []int) bool {
  149. // verify validity of lines table
  150. size := f.size
  151. for i, offset := range lines {
  152. if i > 0 && offset <= lines[i-1] || size <= offset {
  153. return false
  154. }
  155. }
  156. // set lines table
  157. f.lines = lines
  158. return true
  159. }
  160. // SetLinesForContent sets the line offsets for the given file content.
  161. // It ignores position-altering //line comments.
  162. func (f *File) SetLinesForContent(content []byte) {
  163. var lines []int
  164. line := 0
  165. for offset, b := range content {
  166. if line >= 0 {
  167. lines = append(lines, line)
  168. }
  169. line = -1
  170. if b == '\n' {
  171. line = offset + 1
  172. }
  173. }
  174. // set lines table
  175. f.lines = lines
  176. }
  177. // LineStart returns the Pos value of the start of the specified line.
  178. // It ignores any alternative positions set using AddLineColumnInfo.
  179. // LineStart panics if the 1-based line number is invalid.
  180. func (f *File) LineStart(line int) Pos {
  181. if line < 1 {
  182. panic(fmt.Sprintf("invalid line number %d (should be >= 1)", line))
  183. }
  184. if line > len(f.lines) {
  185. panic(fmt.Sprintf("invalid line number %d (should be < %d)", line, len(f.lines)))
  186. }
  187. return Pos(f.base + f.lines[line-1])
  188. }
  189. // A lineInfo object describes alternative file, line, and column
  190. // number information (such as provided via a //line directive)
  191. // for a given file offset.
  192. type lineInfo struct {
  193. // fields are exported to make them accessible to gob
  194. Offset int
  195. Filename string
  196. Line, Column int
  197. }
  198. // AddLineInfo is like AddLineColumnInfo with a column = 1 argument.
  199. // It is here for backward-compatibility for code prior to Go 1.11.
  200. func (f *File) AddLineInfo(offset int, filename string, line int) {
  201. f.AddLineColumnInfo(offset, filename, line, 1)
  202. }
  203. // AddLineColumnInfo adds alternative file, line, and column number
  204. // information for a given file offset. The offset must be larger
  205. // than the offset for the previously added alternative line info
  206. // and smaller than the file size; otherwise the information is
  207. // ignored.
  208. //
  209. // AddLineColumnInfo is typically used to register alternative position
  210. // information for line directives such as //line filename:line:column.
  211. func (f *File) AddLineColumnInfo(offset int, filename string, line, column int) {
  212. if i := len(f.infos); i == 0 || f.infos[i-1].Offset < offset && offset < f.size {
  213. f.infos = append(f.infos, lineInfo{offset, filename, line, column})
  214. }
  215. }
  216. // Pos returns the Pos value for the given file offset;
  217. // the offset must be <= f.Size().
  218. // f.Pos(f.Offset(p)) == p.
  219. func (f *File) Pos(offset int) Pos {
  220. if offset > f.size {
  221. panic(fmt.Sprintf("invalid file offset %d (should be <= %d)", offset, f.size))
  222. }
  223. return Pos(f.base + offset)
  224. }
  225. // Offset returns the offset for the given file position p;
  226. // p must be a valid Pos value in that file.
  227. // f.Offset(f.Pos(offset)) == offset.
  228. func (f *File) Offset(p Pos) int {
  229. if int(p) < f.base || int(p) > f.base+f.size {
  230. panic(fmt.Sprintf("invalid Pos value %d (should be in [%d, %d])", p, f.base, f.base+f.size))
  231. }
  232. return int(p) - f.base
  233. }
  234. // Line returns the line number for the given file position p;
  235. // p must be a Pos value in that file or NoPos.
  236. func (f *File) Line(p Pos) int {
  237. return f.Position(p).Line
  238. }
  239. func searchLineInfos(a []lineInfo, x int) int {
  240. return sort.Search(len(a), func(i int) bool { return a[i].Offset > x }) - 1
  241. }
  242. // unpack returns the filename and line and column number for a file offset.
  243. // If adjusted is set, unpack will return the filename and line information
  244. // possibly adjusted by //line comments; otherwise those comments are ignored.
  245. func (f *File) unpack(offset int, adjusted bool) (filename string, line, column int) {
  246. filename = f.name
  247. if i := searchInts(f.lines, offset); i >= 0 {
  248. line, column = i+1, offset-f.lines[i]+1
  249. }
  250. if adjusted && len(f.infos) > 0 {
  251. // few files have extra line infos
  252. if i := searchLineInfos(f.infos, offset); i >= 0 {
  253. alt := &f.infos[i]
  254. filename = alt.Filename
  255. if i := searchInts(f.lines, alt.Offset); i >= 0 {
  256. // i+1 is the line at which the alternative position was recorded
  257. d := line - (i + 1) // line distance from alternative position base
  258. line = alt.Line + d
  259. if alt.Column == 0 {
  260. // alternative column is unknown => relative column is unknown
  261. // (the current specification for line directives requires
  262. // this to apply until the next PosBase/line directive,
  263. // not just until the new newline)
  264. column = 0
  265. } else if d == 0 {
  266. // the alternative position base is on the current line
  267. // => column is relative to alternative column
  268. column = alt.Column + (offset - alt.Offset)
  269. }
  270. }
  271. }
  272. }
  273. return
  274. }
  275. func (f *File) position(p Pos, adjusted bool) (pos Position) {
  276. offset := int(p) - f.base
  277. pos.Offset = offset
  278. pos.Filename, pos.Line, pos.Column = f.unpack(offset, adjusted)
  279. return
  280. }
  281. // PositionFor returns the Position value for the given file position p.
  282. // If adjusted is set, the position may be adjusted by position-altering
  283. // //line comments; otherwise those comments are ignored.
  284. // p must be a Pos value in f or NoPos.
  285. func (f *File) PositionFor(p Pos, adjusted bool) (pos Position) {
  286. if p != NoPos {
  287. if int(p) < f.base || int(p) > f.base+f.size {
  288. panic(fmt.Sprintf("invalid Pos value %d (should be in [%d, %d])", p, f.base, f.base+f.size))
  289. }
  290. pos = f.position(p, adjusted)
  291. }
  292. return
  293. }
  294. // Position returns the Position value for the given file position p.
  295. // Calling f.Position(p) is equivalent to calling f.PositionFor(p, true).
  296. func (f *File) Position(p Pos) (pos Position) {
  297. return f.PositionFor(p, true)
  298. }
  299. // -----------------------------------------------------------------------------
  300. // Helper functions
  301. func searchInts(a []int, x int) int {
  302. // This function body is a manually inlined version of:
  303. //
  304. // return sort.Search(len(a), func(i int) bool { return a[i] > x }) - 1
  305. //
  306. // With better compiler optimizations, this may not be needed in the
  307. // future, but at the moment this change improves the go/printer
  308. // benchmark performance by ~30%. This has a direct impact on the
  309. // speed of gofmt and thus seems worthwhile (2011-04-29).
  310. // TODO(gri): Remove this when compilers have caught up.
  311. i, j := 0, len(a)
  312. for i < j {
  313. h := int(uint(i+j) >> 1) // avoid overflow when computing h
  314. // i ≤ h < j
  315. if a[h] <= x {
  316. i = h + 1
  317. } else {
  318. j = h
  319. }
  320. }
  321. return i - 1
  322. }