Debian Codegraph @

file/file.go

raw symbols SCIP
1 // Package file encapsulates the file abstractions used by the ast & parser.
2 package file
3
4 import (
5 "fmt"
6 "strings"
7
8 "gopkg.in/sourcemap.v1"
9 )
10
11 // Idx is a compact encoding of a source position within a file set.
12 // It can be converted into a Position for a more convenient, but much
13 // larger, representation.
14 type Idx int
15
16 // Position describes an arbitrary source position
17 // including the filename, line, and column location.
18 type Position struct {
19 Filename string // The filename where the error occurred, if any
20 Offset int // The src offset
21 Line int // The line number, starting at 1
22 Column int // The column number, starting at 1 (The character count)
23 }
24
25 // A Position is valid if the line number is > 0.
26
27 func (p *Position) isValid() bool {
28 return p.Line > 0
29 }
30
31 // String returns a string in one of several forms:
32 //
33 // file:line:column A valid position with filename
34 // line:column A valid position without filename
35 // file An invalid position with filename
36 // - An invalid position without filename
37 func (p *Position) String() string {
38 str := p.Filename
39 if p.isValid() {
40 if str != "" {
41 str += ":"
42 }
43 str += fmt.Sprintf("%d:%d", p.Line, p.Column)
44 }
45 if str == "" {
46 str = "-"
47 }
48 return str
49 }
50
51 // A FileSet represents a set of source files.
52 type FileSet struct {
53 last *File
54 files []*File
55 }
56
57 // AddFile adds a new file with the given filename and src.
58 //
59 // This an internal method, but exported for cross-package use.
60 func (fs *FileSet) AddFile(filename, src string) int {
61 base := fs.nextBase()
62 file := &File{
63 name: filename,
64 src: src,
65 base: base,
66 }
67 fs.files = append(fs.files, file)
68 fs.last = file
69 return base
70 }
71
72 func (fs *FileSet) nextBase() int {
73 if fs.last == nil {
74 return 1
75 }
76 return fs.last.base + len(fs.last.src) + 1
77 }
78
79 // File returns the File at idx or nil if not found.
80 func (fs *FileSet) File(idx Idx) *File {
81 for _, file := range fs.files {
82 if idx <= Idx(file.base+len(file.src)) {
83 return file
84 }
85 }
86 return nil
87 }
88
89 // Position converts an Idx in the FileSet into a Position.
90 func (fs *FileSet) Position(idx Idx) *Position {
91 for _, file := range fs.files {
92 if idx <= Idx(file.base+len(file.src)) {
93 return file.Position(idx - Idx(file.base))
94 }
95 }
96
97 return nil
98 }
99
100 // File represents a file to parse.
101 type File struct {
102 sm *sourcemap.Consumer
103 name string
104 src string
105 base int
106 }
107
108 // NewFile returns a new file with the given filename, src and base.
109 func NewFile(filename, src string, base int) *File {
110 return &File{
111 name: filename,
112 src: src,
113 base: base,
114 }
115 }
116
117 // WithSourceMap sets the source map of fl.
118 func (fl *File) WithSourceMap(sm *sourcemap.Consumer) *File {
119 fl.sm = sm
120 return fl
121 }
122
123 // Name returns the name of fl.
124 func (fl *File) Name() string {
125 return fl.name
126 }
127
128 // Source returns the source of fl.
129 func (fl *File) Source() string {
130 return fl.src
131 }
132
133 // Base returns the base of fl.
134 func (fl *File) Base() int {
135 return fl.base
136 }
137
138 // Position returns the position at idx or nil if not valid.
139 func (fl *File) Position(idx Idx) *Position {
140 position := &Position{}
141
142 offset := int(idx) - fl.base
143
144 if offset >= len(fl.src) || offset < 0 {
145 return nil
146 }
147
148 src := fl.src[:offset]
149
150 position.Filename = fl.name
151 position.Offset = offset
152 position.Line = strings.Count(src, "\n") + 1
153
154 if index := strings.LastIndex(src, "\n"); index >= 0 {
155 position.Column = offset - index
156 } else {
157 position.Column = len(src) + 1
158 }
159
160 if fl.sm != nil {
161 if f, _, l, c, ok := fl.sm.Source(position.Line, position.Column); ok {
162 position.Filename, position.Line, position.Column = f, l, c
163 }
164 }
165
166 return position
167 }