File: file.go

package info (click to toggle)
golang-github-dop251-goja 0.0~git20250630.0.58d95d8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,264 kB
  • sloc: javascript: 454; perl: 184; makefile: 6; sh: 1
file content (236 lines) | stat: -rw-r--r-- 5,146 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Package file encapsulates the file abstractions used by the ast & parser.
package file

import (
	"fmt"
	"net/url"
	"path"
	"path/filepath"
	"sort"
	"strings"
	"sync"

	"github.com/go-sourcemap/sourcemap"
)

// Idx is a compact encoding of a source position within a file set.
// It can be converted into a Position for a more convenient, but much
// larger, representation.
type Idx int

// Position describes an arbitrary source position
// including the filename, line, and column location.
type Position struct {
	Filename string // The filename where the error occurred, if any
	Line     int    // The line number, starting at 1
	Column   int    // The column number, starting at 1 (The character count)

}

// A Position is valid if the line number is > 0.

func (self *Position) isValid() bool {
	return self.Line > 0
}

// String returns a string in one of several forms:
//
//	file:line:column    A valid position with filename
//	line:column         A valid position without filename
//	file                An invalid position with filename
//	-                   An invalid position without filename
func (self Position) String() string {
	str := self.Filename
	if self.isValid() {
		if str != "" {
			str += ":"
		}
		str += fmt.Sprintf("%d:%d", self.Line, self.Column)
	}
	if str == "" {
		str = "-"
	}
	return str
}

// FileSet

// A FileSet represents a set of source files.
type FileSet struct {
	files []*File
	last  *File
}

// AddFile adds a new file with the given filename and src.
//
// This an internal method, but exported for cross-package use.
func (self *FileSet) AddFile(filename, src string) int {
	base := self.nextBase()
	file := &File{
		name: filename,
		src:  src,
		base: base,
	}
	self.files = append(self.files, file)
	self.last = file
	return base
}

func (self *FileSet) nextBase() int {
	if self.last == nil {
		return 1
	}
	return self.last.base + len(self.last.src) + 1
}

func (self *FileSet) File(idx Idx) *File {
	for _, file := range self.files {
		if idx <= Idx(file.base+len(file.src)) {
			return file
		}
	}
	return nil
}

// Position converts an Idx in the FileSet into a Position.
func (self *FileSet) Position(idx Idx) Position {
	for _, file := range self.files {
		if idx <= Idx(file.base+len(file.src)) {
			return file.Position(int(idx) - file.base)
		}
	}
	return Position{}
}

type File struct {
	mu                sync.Mutex
	name              string
	src               string
	base              int // This will always be 1 or greater
	sourceMap         *sourcemap.Consumer
	lineOffsets       []int
	lastScannedOffset int
}

func NewFile(filename, src string, base int) *File {
	return &File{
		name: filename,
		src:  src,
		base: base,
	}
}

func (fl *File) Name() string {
	return fl.name
}

func (fl *File) Source() string {
	return fl.src
}

func (fl *File) Base() int {
	return fl.base
}

func (fl *File) SetSourceMap(m *sourcemap.Consumer) {
	fl.sourceMap = m
}

func (fl *File) Position(offset int) Position {
	var line int
	var lineOffsets []int
	fl.mu.Lock()
	if offset > fl.lastScannedOffset {
		line = fl.scanTo(offset)
		lineOffsets = fl.lineOffsets
		fl.mu.Unlock()
	} else {
		lineOffsets = fl.lineOffsets
		fl.mu.Unlock()
		line = sort.Search(len(lineOffsets), func(x int) bool { return lineOffsets[x] > offset }) - 1
	}

	var lineStart int
	if line >= 0 {
		lineStart = lineOffsets[line]
	}

	row := line + 2
	col := offset - lineStart + 1

	if fl.sourceMap != nil {
		if source, _, row, col, ok := fl.sourceMap.Source(row, col); ok {
			sourceUrlStr := source
			sourceURL := ResolveSourcemapURL(fl.Name(), source)
			if sourceURL != nil {
				sourceUrlStr = sourceURL.String()
			}

			return Position{
				Filename: sourceUrlStr,
				Line:     row,
				Column:   col,
			}
		}
	}

	return Position{
		Filename: fl.name,
		Line:     row,
		Column:   col,
	}
}

func ResolveSourcemapURL(basename, source string) *url.URL {
	// if the url is absolute(has scheme) there is nothing to do
	smURL, err := url.Parse(filepath.ToSlash(strings.TrimSpace(source)))
	if err == nil && !smURL.IsAbs() {
		basename = filepath.ToSlash(strings.TrimSpace(basename))
		baseURL, err1 := url.Parse(basename)
		if err1 == nil && path.IsAbs(baseURL.Path) {
			smURL = baseURL.ResolveReference(smURL)
		} else {
			// pathological case where both are not absolute paths and using Resolve
			// as above will produce an absolute one
			smURL, _ = url.Parse(path.Join(path.Dir(basename), smURL.Path))
		}
	}
	return smURL
}

func findNextLineStart(s string) int {
	for pos, ch := range s {
		switch ch {
		case '\r':
			if pos < len(s)-1 && s[pos+1] == '\n' {
				return pos + 2
			}
			return pos + 1
		case '\n':
			return pos + 1
		case '\u2028', '\u2029':
			return pos + 3
		}
	}
	return -1
}

func (fl *File) scanTo(offset int) int {
	o := fl.lastScannedOffset
	for o < offset {
		p := findNextLineStart(fl.src[o:])
		if p == -1 {
			fl.lastScannedOffset = len(fl.src)
			return len(fl.lineOffsets) - 1
		}
		o = o + p
		fl.lineOffsets = append(fl.lineOffsets, o)
	}
	fl.lastScannedOffset = o

	if o == offset {
		return len(fl.lineOffsets) - 1
	}

	return len(fl.lineOffsets) - 2
}