UNPKG

3.67 kBJavaScriptView Raw
1import { TextRange } from './TextRange';
2/**
3 * Represents a sequence of tokens extracted from `ParserContext.tokens`.
4 * This sequence is defined by a starting index and ending index into that array.
5 */
6var TokenSequence = /** @class */ (function () {
7 function TokenSequence(parameters) {
8 this.parserContext = parameters.parserContext;
9 this._startIndex = parameters.startIndex;
10 this._endIndex = parameters.endIndex;
11 this._validateBounds();
12 }
13 /**
14 * Constructs a TokenSequence object with no tokens.
15 */
16 TokenSequence.createEmpty = function (parserContext) {
17 return new TokenSequence({ parserContext: parserContext, startIndex: 0, endIndex: 0 });
18 };
19 Object.defineProperty(TokenSequence.prototype, "startIndex", {
20 /**
21 * The starting index into the associated `ParserContext.tokens` list.
22 */
23 get: function () {
24 return this._startIndex;
25 },
26 enumerable: false,
27 configurable: true
28 });
29 Object.defineProperty(TokenSequence.prototype, "endIndex", {
30 /**
31 * The (non-inclusive) ending index into the associated `ParserContext.tokens` list.
32 */
33 get: function () {
34 return this._endIndex;
35 },
36 enumerable: false,
37 configurable: true
38 });
39 Object.defineProperty(TokenSequence.prototype, "tokens", {
40 get: function () {
41 return this.parserContext.tokens.slice(this._startIndex, this._endIndex);
42 },
43 enumerable: false,
44 configurable: true
45 });
46 /**
47 * Constructs a TokenSequence that corresponds to a different range of tokens,
48 * e.g. a subrange.
49 */
50 TokenSequence.prototype.getNewSequence = function (startIndex, endIndex) {
51 return new TokenSequence({
52 parserContext: this.parserContext,
53 startIndex: startIndex,
54 endIndex: endIndex
55 });
56 };
57 /**
58 * Returns a TextRange that includes all tokens in the sequence (including any additional
59 * characters between doc comment lines).
60 */
61 TokenSequence.prototype.getContainingTextRange = function () {
62 if (this.isEmpty()) {
63 return TextRange.empty;
64 }
65 return this.parserContext.sourceRange.getNewRange(this.parserContext.tokens[this._startIndex].range.pos, this.parserContext.tokens[this._endIndex - 1].range.end);
66 };
67 TokenSequence.prototype.isEmpty = function () {
68 return this._startIndex === this._endIndex;
69 };
70 /**
71 * Returns the concatenated text of all the tokens.
72 */
73 TokenSequence.prototype.toString = function () {
74 return this.tokens.map(function (x) { return x.toString(); }).join('');
75 };
76 TokenSequence.prototype._validateBounds = function () {
77 if (this.startIndex < 0) {
78 throw new Error('TokenSequence.startIndex cannot be negative');
79 }
80 if (this.endIndex < 0) {
81 throw new Error('TokenSequence.endIndex cannot be negative');
82 }
83 if (this.endIndex < this.startIndex) {
84 throw new Error('TokenSequence.endIndex cannot be smaller than TokenSequence.startIndex');
85 }
86 if (this.startIndex > this.parserContext.tokens.length) {
87 throw new Error('TokenSequence.startIndex cannot exceed the associated token array');
88 }
89 if (this.endIndex > this.parserContext.tokens.length) {
90 throw new Error('TokenSequence.endIndex cannot exceed the associated token array');
91 }
92 };
93 return TokenSequence;
94}());
95export { TokenSequence };
96//# sourceMappingURL=TokenSequence.js.map
\No newline at end of file