-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathincluded_ranges.go
More file actions
250 lines (225 loc) · 6.2 KB
/
Copy pathincluded_ranges.go
File metadata and controls
250 lines (225 loc) · 6.2 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package gotreesitter
import "sort"
type includedRangeTokenSource struct {
base TokenSource
ranges []Range
idx int
}
type includedRangeAwareTokenSource interface {
setIncludedRanges([]Range) bool
}
func newIncludedRangeTokenSource(base TokenSource, ranges []Range) TokenSource {
if base == nil || len(ranges) == 0 {
return base
}
ranges = normalizeIncludedRanges(ranges)
if len(ranges) == 0 {
return base
}
return &includedRangeTokenSource{
base: base,
ranges: ranges,
}
}
func normalizeIncludedRanges(ranges []Range) []Range {
if len(ranges) == 0 {
return nil
}
tmp := make([]Range, 0, len(ranges))
for _, r := range ranges {
if r.EndByte <= r.StartByte {
continue
}
tmp = append(tmp, r)
}
if len(tmp) == 0 {
return nil
}
sort.Slice(tmp, func(i, j int) bool {
if tmp[i].StartByte != tmp[j].StartByte {
return tmp[i].StartByte < tmp[j].StartByte
}
return tmp[i].EndByte < tmp[j].EndByte
})
out := make([]Range, 0, len(tmp))
cur := tmp[0]
for i := 1; i < len(tmp); i++ {
r := tmp[i]
if r.StartByte <= cur.EndByte {
if r.EndByte > cur.EndByte {
cur.EndByte = r.EndByte
cur.EndPoint = r.EndPoint
}
continue
}
out = append(out, cur)
cur = r
}
out = append(out, cur)
return out
}
func (s *includedRangeTokenSource) SetParserState(state StateID) {
if p, ok := s.base.(parserStateTokenSource); ok {
p.SetParserState(state)
}
}
func (s *includedRangeTokenSource) SetGLRStates(states []StateID) {
if p, ok := s.base.(parserStateTokenSource); ok {
p.SetGLRStates(states)
}
}
// lexesErrorModeAtErrorState forwards to the base source's answer. The C
// recovery port (parser_recover_c.go) uses this to decide whether it may
// safely trust a SetParserState(0) token's identity as C-equivalent
// error-mode lookahead, or must substitute its own raw-source Lexer. An
// included-range wrapper has no lexing of its own — it only filters the
// base's tokens against the active ranges — so it must defer entirely to the
// base: answering true unconditionally (or synthesizing lexing) would let the
// C-recovery engine-side error-mode substitution run its Lexer over the
// WHOLE underlying document, ignoring the active ranges, which C never does.
func (s *includedRangeTokenSource) lexesErrorModeAtErrorState() bool {
if s == nil || s.base == nil {
return false
}
em, ok := s.base.(errorModeLexingTokenSource)
return ok && em.lexesErrorModeAtErrorState()
}
func (s *includedRangeTokenSource) SupportsIncrementalReuse() bool {
if s == nil || s.base == nil {
return false
}
return tokenSourceSupportsIncrementalReuse(s.base)
}
func (s *includedRangeTokenSource) IncrementalReuseUnsupportedReason() string {
if s == nil || s.base == nil {
return "token_source_nil"
}
return incrementalReuseUnavailableReason(s.base)
}
func (s *includedRangeTokenSource) Reset(source []byte) {
if s == nil {
return
}
s.idx = 0
if resettable, ok := s.base.(interface{ Reset([]byte) }); ok {
resettable.Reset(source)
}
}
func (s *includedRangeTokenSource) Close() {
if s == nil || s.base == nil {
return
}
if closer, ok := s.base.(interface{ Close() }); ok {
closer.Close()
}
}
func (s *includedRangeTokenSource) Next() Token {
return s.filterToken(Token{}, false)
}
func (s *includedRangeTokenSource) SkipToByte(offset uint32) Token {
if skipper, ok := s.base.(ByteSkippableTokenSource); ok {
return s.filterToken(skipper.SkipToByte(offset), true)
}
for {
tok := s.Next()
if tok.Symbol == 0 || tok.StartByte >= offset {
return tok
}
}
}
func (s *includedRangeTokenSource) SkipToByteWithPoint(offset uint32, pt Point) Token {
if skipper, ok := s.base.(PointSkippableTokenSource); ok {
return s.filterToken(skipper.SkipToByteWithPoint(offset, pt), true)
}
return s.SkipToByte(offset)
}
func (s *includedRangeTokenSource) RelexFromTokenStart(tok Token) (Token, bool) {
relexer, ok := s.base.(tokenSourceRelexer)
if !ok {
return Token{}, false
}
idx := s.idx
if !s.tokenInCurrentRange(tok) {
s.idx = idx
return Token{}, false
}
next, ok := relexer.RelexFromTokenStart(tok)
if !ok {
return Token{}, false
}
if next.StartByte != tok.StartByte || next.StartPoint != tok.StartPoint || !s.tokenInCurrentRange(next) {
s.idx = idx
return Token{}, false
}
return next, true
}
func (s *includedRangeTokenSource) CanRelexFromTokenStart(tok Token) bool {
relexer, ok := s.base.(tokenSourceRelexer)
return ok && relexer.CanRelexFromTokenStart(tok)
}
func (s *includedRangeTokenSource) filterToken(tok Token, hasToken bool) Token {
for {
if !hasToken {
tok = s.base.Next()
}
hasToken = false
if tok.Symbol == 0 {
return tok
}
if !s.advanceToMatchingRange(tok) {
return Token{
StartByte: tok.EndByte,
EndByte: tok.EndByte,
StartPoint: tok.EndPoint,
EndPoint: tok.EndPoint,
}
}
r := s.ranges[s.idx]
if tok.StartByte < r.StartByte && tok.EndByte <= r.StartByte {
if skipper, ok := s.base.(ByteSkippableTokenSource); ok {
tok = skipper.SkipToByte(r.StartByte)
hasToken = true
}
continue
}
if tok.StartByte < r.StartByte {
// Re-seek a token that overlaps the selected boundary. A source
// with byte seeking can reproduce the token from that boundary.
if skipper, ok := s.base.(ByteSkippableTokenSource); ok {
tok = skipper.SkipToByte(r.StartByte)
hasToken = true
continue
}
// A source without byte seeking cannot reproduce a trimmed token.
// Preserve the complete overlap, which is the conservative existing
// behavior. Its start can remain before the selected boundary.
return tok
}
if tok.EndByte <= r.StartByte {
if skipper, ok := s.base.(ByteSkippableTokenSource); ok {
tok = skipper.SkipToByte(r.StartByte)
hasToken = true
}
continue
}
if tok.StartByte >= r.EndByte {
s.idx++
hasToken = true
continue
}
return tok
}
}
func (s *includedRangeTokenSource) tokenInCurrentRange(tok Token) bool {
if tok.Symbol == 0 || s.idx >= len(s.ranges) {
return false
}
r := s.ranges[s.idx]
return tok.StartByte < r.EndByte && tok.EndByte > r.StartByte
}
func (s *includedRangeTokenSource) advanceToMatchingRange(tok Token) bool {
for s.idx < len(s.ranges) && tok.StartByte >= s.ranges[s.idx].EndByte {
s.idx++
}
return s.idx < len(s.ranges)
}