1 | import { TextRange } from './TextRange';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | var TokenSequence = (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 |
|
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 |
|
22 |
|
23 | get: function () {
|
24 | return this._startIndex;
|
25 | },
|
26 | enumerable: false,
|
27 | configurable: true
|
28 | });
|
29 | Object.defineProperty(TokenSequence.prototype, "endIndex", {
|
30 | |
31 |
|
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 |
|
48 |
|
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 |
|
59 |
|
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 |
|
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 | }());
|
95 | export { TokenSequence };
|
96 |
|
\ | No newline at end of file |