UNPKG

7.91 kBJavaScriptView Raw
1"use strict";
2/*!
3 * Copyright 2016 The ANTLR Project. All rights reserved.
4 * Licensed under the BSD-3-Clause license. See LICENSE file in the project root for license information.
5 */
6var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
7 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
8 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
9 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;
10 return c > 3 && r && Object.defineProperty(target, key, r), r;
11};
12var __param = (this && this.__param) || function (paramIndex, decorator) {
13 return function (target, key) { decorator(target, key, paramIndex); }
14};
15Object.defineProperty(exports, "__esModule", { value: true });
16exports.CommonToken = void 0;
17const Interval_1 = require("./misc/Interval");
18const Decorators_1 = require("./Decorators");
19const Token_1 = require("./Token");
20let CommonToken = class CommonToken {
21 constructor(type, text, source = CommonToken.EMPTY_SOURCE, channel = Token_1.Token.DEFAULT_CHANNEL, start = 0, stop = 0) {
22 /**
23 * This is the backing field for {@link #getLine} and {@link #setLine}.
24 */
25 this._line = 0;
26 /**
27 * This is the backing field for {@link #getCharPositionInLine} and
28 * {@link #setCharPositionInLine}.
29 */
30 this._charPositionInLine = -1; // set to invalid position
31 /**
32 * This is the backing field for {@link #getChannel} and
33 * {@link #setChannel}.
34 */
35 this._channel = Token_1.Token.DEFAULT_CHANNEL;
36 /**
37 * This is the backing field for `tokenIndex`.
38 */
39 this.index = -1;
40 this._text = text;
41 this._type = type;
42 this.source = source;
43 this._channel = channel;
44 this.start = start;
45 this.stop = stop;
46 if (source.source != null) {
47 this._line = source.source.line;
48 this._charPositionInLine = source.source.charPositionInLine;
49 }
50 }
51 /**
52 * Constructs a new {@link CommonToken} as a copy of another {@link Token}.
53 *
54 * If `oldToken` is also a {@link CommonToken} instance, the newly
55 * constructed token will share a reference to the {@link #text} field and
56 * the {@link Tuple2} stored in {@link #source}. Otherwise, {@link #text} will
57 * be assigned the result of calling {@link #getText}, and {@link #source}
58 * will be constructed from the result of {@link Token#getTokenSource} and
59 * {@link Token#getInputStream}.
60 *
61 * @param oldToken The token to copy.
62 */
63 static fromToken(oldToken) {
64 let result = new CommonToken(oldToken.type, undefined, CommonToken.EMPTY_SOURCE, oldToken.channel, oldToken.startIndex, oldToken.stopIndex);
65 result._line = oldToken.line;
66 result.index = oldToken.tokenIndex;
67 result._charPositionInLine = oldToken.charPositionInLine;
68 if (oldToken instanceof CommonToken) {
69 result._text = oldToken._text;
70 result.source = oldToken.source;
71 }
72 else {
73 result._text = oldToken.text;
74 result.source = { source: oldToken.tokenSource, stream: oldToken.inputStream };
75 }
76 return result;
77 }
78 get type() {
79 return this._type;
80 }
81 // @Override
82 set type(type) {
83 this._type = type;
84 }
85 get line() {
86 return this._line;
87 }
88 // @Override
89 set line(line) {
90 this._line = line;
91 }
92 get text() {
93 if (this._text != null) {
94 return this._text;
95 }
96 let input = this.inputStream;
97 if (input == null) {
98 return undefined;
99 }
100 let n = input.size;
101 if (this.start < n && this.stop < n) {
102 return input.getText(Interval_1.Interval.of(this.start, this.stop));
103 }
104 else {
105 return "<EOF>";
106 }
107 }
108 /**
109 * Explicitly set the text for this token. If {code text} is not
110 * `undefined`, then {@link #getText} will return this value rather than
111 * extracting the text from the input.
112 *
113 * @param text The explicit text of the token, or `undefined` if the text
114 * should be obtained from the input along with the start and stop indexes
115 * of the token.
116 */
117 // @Override
118 set text(text) {
119 this._text = text;
120 }
121 get charPositionInLine() {
122 return this._charPositionInLine;
123 }
124 // @Override
125 set charPositionInLine(charPositionInLine) {
126 this._charPositionInLine = charPositionInLine;
127 }
128 get channel() {
129 return this._channel;
130 }
131 // @Override
132 set channel(channel) {
133 this._channel = channel;
134 }
135 get startIndex() {
136 return this.start;
137 }
138 set startIndex(start) {
139 this.start = start;
140 }
141 get stopIndex() {
142 return this.stop;
143 }
144 set stopIndex(stop) {
145 this.stop = stop;
146 }
147 get tokenIndex() {
148 return this.index;
149 }
150 // @Override
151 set tokenIndex(index) {
152 this.index = index;
153 }
154 get tokenSource() {
155 return this.source.source;
156 }
157 get inputStream() {
158 return this.source.stream;
159 }
160 toString(recognizer) {
161 let channelStr = "";
162 if (this._channel > 0) {
163 channelStr = ",channel=" + this._channel;
164 }
165 let txt = this.text;
166 if (txt != null) {
167 txt = txt.replace(/\n/g, "\\n");
168 txt = txt.replace(/\r/g, "\\r");
169 txt = txt.replace(/\t/g, "\\t");
170 }
171 else {
172 txt = "<no text>";
173 }
174 let typeString = String(this._type);
175 if (recognizer) {
176 typeString = recognizer.vocabulary.getDisplayName(this._type);
177 }
178 return "[@" + this.tokenIndex + "," + this.start + ":" + this.stop + "='" + txt + "',<" + typeString + ">" + channelStr + "," + this._line + ":" + this.charPositionInLine + "]";
179 }
180};
181/**
182 * An empty {@link Tuple2} which is used as the default value of
183 * {@link #source} for tokens that do not have a source.
184 */
185CommonToken.EMPTY_SOURCE = { source: undefined, stream: undefined };
186__decorate([
187 Decorators_1.NotNull
188], CommonToken.prototype, "source", void 0);
189__decorate([
190 Decorators_1.Override
191], CommonToken.prototype, "type", null);
192__decorate([
193 Decorators_1.Override
194], CommonToken.prototype, "line", null);
195__decorate([
196 Decorators_1.Override
197], CommonToken.prototype, "text", null);
198__decorate([
199 Decorators_1.Override
200], CommonToken.prototype, "charPositionInLine", null);
201__decorate([
202 Decorators_1.Override
203], CommonToken.prototype, "channel", null);
204__decorate([
205 Decorators_1.Override
206], CommonToken.prototype, "startIndex", null);
207__decorate([
208 Decorators_1.Override
209], CommonToken.prototype, "stopIndex", null);
210__decorate([
211 Decorators_1.Override
212], CommonToken.prototype, "tokenIndex", null);
213__decorate([
214 Decorators_1.Override
215], CommonToken.prototype, "tokenSource", null);
216__decorate([
217 Decorators_1.Override
218], CommonToken.prototype, "inputStream", null);
219__decorate([
220 Decorators_1.Override
221], CommonToken.prototype, "toString", null);
222__decorate([
223 __param(0, Decorators_1.NotNull)
224], CommonToken, "fromToken", null);
225CommonToken = __decorate([
226 __param(2, Decorators_1.NotNull)
227], CommonToken);
228exports.CommonToken = CommonToken;
229//# sourceMappingURL=CommonToken.js.map
\No newline at end of file