-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtoken.js
119 lines (100 loc) · 2.33 KB
/
token.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
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
'use strict';
/**
* A structure describing text fragment in content stream. It may contain
* other sub-fragments (also tokens) that represent current fragments’ logical
* parts
*/
export default class Token {
/**
* @param {StreamReader} stream
* @param {String} type Token type
* @param {Object} [start] Tokens’ start position in `stream`
* @param {Object} [end] Tokens’ end position in `stream`
*/
constructor(stream, type, start, end) {
this.stream = stream;
this.start = start != null ? start : stream.start;
this.end = end != null ? end : stream.pos;
this.type = type;
this._props = null;
this._value = null;
this._items = null;
}
get size() {
return this._items ? this._items.length : 0;
}
get items() {
return this._items;
}
clone(start, end) {
return new this.constructor(this.stream, this.type,
start != null ? start : this.start,
end != null ? end : this.end);
}
add(item) {
if (Array.isArray(item)) {
for (let i = 0, il = item.length; i < il; i++) {
this.add(item[i]);
}
} else if (item) {
if (!this._items) {
this._items = [item];
} else {
this._items.push(item);
}
}
return this;
}
remove(item) {
if (this._items) {
const ix = this._items.indexOf(item);
if (ix !== -1 ) {
this._items.splice(ix, 1);
}
}
return this;
}
item(i) {
const size = this.size;
return this._items && this._items[(size + i) % size];
}
limit() {
return this.stream.limit(this.start, this.end);
}
slice(from, to) {
const token = this.clone();
const items = this._items && this._items.slice(from, to);
if (items && items.length) {
token.start = items[0].start;
token.end = items[items.length - 1].end;
token.add(items);
} else if (items) {
// Empty token
token.start = token.end;
}
return token;
}
property(name, value) {
if (typeof value !== 'undefined') {
// set property value
if (!this._props) {
this._props = {};
}
this._props[name] = value;
}
return this._props && this._props[name];
}
/**
* Returns token textual representation
* @return {String}
*/
toString() {
return `${this.valueOf()} [${this.start}, ${this.end}] (${this.type})`;
}
valueOf() {
if (this._value === null) {
this._value = this.stream.substring(this.start, this.end);
}
return this._value;
}
}