-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCommonToken.js
295 lines (248 loc) · 8.49 KB
/
CommonToken.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
"use strict";
/*!
* Copyright 2016 The ANTLR Project. All rights reserved.
* Licensed under the BSD-3-Clause license. See LICENSE file in the project root for license information.
*/
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var __decorate = void 0 && (void 0).__decorate || function (decorators, target, key, desc) {
var c = arguments.length,
r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc,
d;
if ((typeof Reflect === "undefined" ? "undefined" : (0, _typeof2["default"])(Reflect)) === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);else for (var i = decorators.length - 1; i >= 0; i--) {
if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
}
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __param = void 0 && (void 0).__param || function (paramIndex, decorator) {
return function (target, key) {
decorator(target, key, paramIndex);
};
};
Object.defineProperty(exports, "__esModule", {
value: true
});
var Interval_1 = require("./misc/Interval");
var Decorators_1 = require("./Decorators");
var Token_1 = require("./Token");
var CommonToken =
/*#__PURE__*/
function () {
function CommonToken(type, text) {
var source = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : CommonToken.EMPTY_SOURCE;
var channel = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : Token_1.Token.DEFAULT_CHANNEL;
var start = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
var stop = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 0;
(0, _classCallCheck2["default"])(this, CommonToken);
/**
* This is the backing field for {@link #getLine} and {@link #setLine}.
*/
this._line = 0;
/**
* This is the backing field for {@link #getCharPositionInLine} and
* {@link #setCharPositionInLine}.
*/
this._charPositionInLine = -1; // set to invalid position
/**
* This is the backing field for {@link #getChannel} and
* {@link #setChannel}.
*/
this._channel = Token_1.Token.DEFAULT_CHANNEL;
/**
* This is the backing field for `tokenIndex`.
*/
this.index = -1;
this._text = text;
this._type = type;
this.source = source;
this._channel = channel;
this.start = start;
this.stop = stop;
if (source.source != null) {
this._line = source.source.line;
this._charPositionInLine = source.source.charPositionInLine;
}
}
/**
* Constructs a new {@link CommonToken} as a copy of another {@link Token}.
*
* If `oldToken` is also a {@link CommonToken} instance, the newly
* constructed token will share a reference to the {@link #text} field and
* the {@link Tuple2} stored in {@link #source}. Otherwise, {@link #text} will
* be assigned the result of calling {@link #getText}, and {@link #source}
* will be constructed from the result of {@link Token#getTokenSource} and
* {@link Token#getInputStream}.
*
* @param oldToken The token to copy.
*/
(0, _createClass2["default"])(CommonToken, [{
key: "toString",
value: function toString(recognizer) {
var channelStr = "";
if (this._channel > 0) {
channelStr = ",channel=" + this._channel;
}
var txt = this.text;
if (txt != null) {
txt = txt.replace(/\n/g, "\\n");
txt = txt.replace(/\r/g, "\\r");
txt = txt.replace(/\t/g, "\\t");
} else {
txt = "<no text>";
}
var typeString = String(this._type);
if (recognizer) {
typeString = recognizer.vocabulary.getDisplayName(this._type);
}
return "[@" + this.tokenIndex + "," + this.start + ":" + this.stop + "='" + txt + "',<" + typeString + ">" + channelStr + "," + this._line + ":" + this.charPositionInLine + "]";
}
}, {
key: "type",
get: function get() {
return this._type;
} // @Override
,
set: function set(type) {
this._type = type;
}
}, {
key: "line",
get: function get() {
return this._line;
} // @Override
,
set: function set(line) {
this._line = line;
}
}, {
key: "text",
get: function get() {
if (this._text != null) {
return this._text;
}
var input = this.inputStream;
if (input == null) {
return undefined;
}
var n = input.size;
if (this.start < n && this.stop < n) {
return input.getText(Interval_1.Interval.of(this.start, this.stop));
} else {
return "<EOF>";
}
}
/**
* Explicitly set the text for this token. If {code text} is not
* `undefined`, then {@link #getText} will return this value rather than
* extracting the text from the input.
*
* @param text The explicit text of the token, or `undefined` if the text
* should be obtained from the input along with the start and stop indexes
* of the token.
*/
// @Override
,
set: function set(text) {
this._text = text;
}
}, {
key: "charPositionInLine",
get: function get() {
return this._charPositionInLine;
} // @Override
,
set: function set(charPositionInLine) {
this._charPositionInLine = charPositionInLine;
}
}, {
key: "channel",
get: function get() {
return this._channel;
} // @Override
,
set: function set(channel) {
this._channel = channel;
}
}, {
key: "startIndex",
get: function get() {
return this.start;
},
set: function set(start) {
this.start = start;
}
}, {
key: "stopIndex",
get: function get() {
return this.stop;
},
set: function set(stop) {
this.stop = stop;
}
}, {
key: "tokenIndex",
get: function get() {
return this.index;
} // @Override
,
set: function set(index) {
this.index = index;
}
}, {
key: "tokenSource",
get: function get() {
return this.source.source;
}
}, {
key: "inputStream",
get: function get() {
return this.source.stream;
}
}], [{
key: "fromToken",
value: function fromToken(oldToken) {
var result = new CommonToken(oldToken.type, undefined, CommonToken.EMPTY_SOURCE, oldToken.channel, oldToken.startIndex, oldToken.stopIndex);
result._line = oldToken.line;
result.index = oldToken.tokenIndex;
result._charPositionInLine = oldToken.charPositionInLine;
if (oldToken instanceof CommonToken) {
result._text = oldToken._text;
result.source = oldToken.source;
} else {
result._text = oldToken.text;
result.source = {
source: oldToken.tokenSource,
stream: oldToken.inputStream
};
}
return result;
}
}]);
return CommonToken;
}();
/**
* An empty {@link Tuple2} which is used as the default value of
* {@link #source} for tokens that do not have a source.
*/
CommonToken.EMPTY_SOURCE = {
source: undefined,
stream: undefined
};
__decorate([Decorators_1.NotNull], CommonToken.prototype, "source", void 0);
__decorate([Decorators_1.Override], CommonToken.prototype, "type", null);
__decorate([Decorators_1.Override], CommonToken.prototype, "line", null);
__decorate([Decorators_1.Override], CommonToken.prototype, "text", null);
__decorate([Decorators_1.Override], CommonToken.prototype, "charPositionInLine", null);
__decorate([Decorators_1.Override], CommonToken.prototype, "channel", null);
__decorate([Decorators_1.Override], CommonToken.prototype, "startIndex", null);
__decorate([Decorators_1.Override], CommonToken.prototype, "stopIndex", null);
__decorate([Decorators_1.Override], CommonToken.prototype, "tokenIndex", null);
__decorate([Decorators_1.Override], CommonToken.prototype, "tokenSource", null);
__decorate([Decorators_1.Override], CommonToken.prototype, "inputStream", null);
__decorate([Decorators_1.Override], CommonToken.prototype, "toString", null);
__decorate([__param(0, Decorators_1.NotNull)], CommonToken, "fromToken", null);
CommonToken = __decorate([__param(2, Decorators_1.NotNull)], CommonToken);
exports.CommonToken = CommonToken;
//# sourceMappingURL=CommonToken.js.map