You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

118 lines
3.7 KiB

  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2017 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. /*
  32. Package remap handles tracking the locations of Go tokens in a source text
  33. across a rewrite by the Go formatter.
  34. */
  35. package remap
  36. import (
  37. "fmt"
  38. "go/scanner"
  39. "go/token"
  40. )
  41. // A Location represents a span of byte offsets in the source text.
  42. type Location struct {
  43. Pos, End int // End is exclusive
  44. }
  45. // A Map represents a mapping between token locations in an input source text
  46. // and locations in the correspnding output text.
  47. type Map map[Location]Location
  48. // Find reports whether the specified span is recorded by m, and if so returns
  49. // the new location it was mapped to. If the input span was not found, the
  50. // returned location is the same as the input.
  51. func (m Map) Find(pos, end int) (Location, bool) {
  52. key := Location{
  53. Pos: pos,
  54. End: end,
  55. }
  56. if loc, ok := m[key]; ok {
  57. return loc, true
  58. }
  59. return key, false
  60. }
  61. func (m Map) add(opos, oend, npos, nend int) {
  62. m[Location{Pos: opos, End: oend}] = Location{Pos: npos, End: nend}
  63. }
  64. // Compute constructs a location mapping from input to output. An error is
  65. // reported if any of the tokens of output cannot be mapped.
  66. func Compute(input, output []byte) (Map, error) {
  67. itok := tokenize(input)
  68. otok := tokenize(output)
  69. if len(itok) != len(otok) {
  70. return nil, fmt.Errorf("wrong number of tokens, %d ≠ %d", len(itok), len(otok))
  71. }
  72. m := make(Map)
  73. for i, ti := range itok {
  74. to := otok[i]
  75. if ti.Token != to.Token {
  76. return nil, fmt.Errorf("token %d type mismatch: %s ≠ %s", i+1, ti, to)
  77. }
  78. m.add(ti.pos, ti.end, to.pos, to.end)
  79. }
  80. return m, nil
  81. }
  82. // tokinfo records the span and type of a source token.
  83. type tokinfo struct {
  84. pos, end int
  85. token.Token
  86. }
  87. func tokenize(src []byte) []tokinfo {
  88. fs := token.NewFileSet()
  89. var s scanner.Scanner
  90. s.Init(fs.AddFile("src", fs.Base(), len(src)), src, nil, scanner.ScanComments)
  91. var info []tokinfo
  92. for {
  93. pos, next, lit := s.Scan()
  94. switch next {
  95. case token.SEMICOLON:
  96. continue
  97. }
  98. info = append(info, tokinfo{
  99. pos: int(pos - 1),
  100. end: int(pos + token.Pos(len(lit)) - 1),
  101. Token: next,
  102. })
  103. if next == token.EOF {
  104. break
  105. }
  106. }
  107. return info
  108. }