1 // Go support for Protocol Buffers - Google's data interchange format
3 // Copyright 2017 The Go Authors. All rights reserved.
4 // https://github.com/golang/protobuf
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
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
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.
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.
33 Package remap handles tracking the locations of Go tokens in a source text
34 across a rewrite by the Go formatter.
44 // A Location represents a span of byte offsets in the source text.
45 type Location struct {
46 Pos, End int // End is exclusive
49 // A Map represents a mapping between token locations in an input source text
50 // and locations in the correspnding output text.
51 type Map map[Location]Location
53 // Find reports whether the specified span is recorded by m, and if so returns
54 // the new location it was mapped to. If the input span was not found, the
55 // returned location is the same as the input.
56 func (m Map) Find(pos, end int) (Location, bool) {
61 if loc, ok := m[key]; ok {
67 func (m Map) add(opos, oend, npos, nend int) {
68 m[Location{Pos: opos, End: oend}] = Location{Pos: npos, End: nend}
71 // Compute constructs a location mapping from input to output. An error is
72 // reported if any of the tokens of output cannot be mapped.
73 func Compute(input, output []byte) (Map, error) {
74 itok := tokenize(input)
75 otok := tokenize(output)
76 if len(itok) != len(otok) {
77 return nil, fmt.Errorf("wrong number of tokens, %d ≠ %d", len(itok), len(otok))
80 for i, ti := range itok {
82 if ti.Token != to.Token {
83 return nil, fmt.Errorf("token %d type mismatch: %s ≠ %s", i+1, ti, to)
85 m.add(ti.pos, ti.end, to.pos, to.end)
90 // tokinfo records the span and type of a source token.
96 func tokenize(src []byte) []tokinfo {
97 fs := token.NewFileSet()
99 s.Init(fs.AddFile("src", fs.Base(), len(src)), src, nil, scanner.ScanComments)
102 pos, next, lit := s.Scan()
104 case token.SEMICOLON:
107 info = append(info, tokinfo{
109 end: int(pos + token.Pos(len(lit)) - 1),
112 if next == token.EOF {