forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtoken-iterator.js
80 lines (69 loc) · 1.63 KB
/
token-iterator.js
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
module.exports = class TokenIterator {
constructor(languageMode) {
this.languageMode = languageMode;
}
reset(line) {
this.line = line;
this.index = null;
this.startColumn = 0;
this.endColumn = 0;
this.scopes = this.line.openScopes.map(id =>
this.languageMode.grammar.scopeForId(id)
);
this.scopeStarts = this.scopes.slice();
this.scopeEnds = [];
return this;
}
next() {
const { tags } = this.line;
if (this.index != null) {
this.startColumn = this.endColumn;
this.scopeEnds.length = 0;
this.scopeStarts.length = 0;
this.index++;
} else {
this.index = 0;
}
while (this.index < tags.length) {
const tag = tags[this.index];
if (tag < 0) {
const scope = this.languageMode.grammar.scopeForId(tag);
if (tag % 2 === 0) {
if (this.scopeStarts[this.scopeStarts.length - 1] === scope) {
this.scopeStarts.pop();
} else {
this.scopeEnds.push(scope);
}
this.scopes.pop();
} else {
this.scopeStarts.push(scope);
this.scopes.push(scope);
}
this.index++;
} else {
this.endColumn += tag;
this.text = this.line.text.substring(this.startColumn, this.endColumn);
return true;
}
}
return false;
}
getScopes() {
return this.scopes;
}
getScopeStarts() {
return this.scopeStarts;
}
getScopeEnds() {
return this.scopeEnds;
}
getText() {
return this.text;
}
getBufferStart() {
return this.startColumn;
}
getBufferEnd() {
return this.endColumn;
}
};