1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.TokenReader = void 0;
|
4 | var Token_1 = require("./Token");
|
5 | var TokenSequence_1 = require("./TokenSequence");
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | var TokenReader = (function () {
|
17 | function TokenReader(parserContext, embeddedTokenSequence) {
|
18 | this._parserContext = parserContext;
|
19 | this.tokens = parserContext.tokens;
|
20 | if (embeddedTokenSequence) {
|
21 | if (embeddedTokenSequence.parserContext !== this._parserContext) {
|
22 | throw new Error('The embeddedTokenSequence must use the same parser context');
|
23 | }
|
24 | this._readerStartIndex = embeddedTokenSequence.startIndex;
|
25 | this._readerEndIndex = embeddedTokenSequence.endIndex;
|
26 | }
|
27 | else {
|
28 | this._readerStartIndex = 0;
|
29 | this._readerEndIndex = this.tokens.length;
|
30 | }
|
31 | this._currentIndex = this._readerStartIndex;
|
32 | this._accumulatedStartIndex = this._readerStartIndex;
|
33 | }
|
34 | |
35 |
|
36 |
|
37 |
|
38 | TokenReader.prototype.extractAccumulatedSequence = function () {
|
39 | if (this._accumulatedStartIndex === this._currentIndex) {
|
40 |
|
41 | throw new Error('Parser assertion failed: The queue should not be empty when' +
|
42 | ' extractAccumulatedSequence() is called');
|
43 | }
|
44 | var sequence = new TokenSequence_1.TokenSequence({
|
45 | parserContext: this._parserContext,
|
46 | startIndex: this._accumulatedStartIndex,
|
47 | endIndex: this._currentIndex
|
48 | });
|
49 | this._accumulatedStartIndex = this._currentIndex;
|
50 | return sequence;
|
51 | };
|
52 | |
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | TokenReader.prototype.isAccumulatedSequenceEmpty = function () {
|
59 | return this._accumulatedStartIndex === this._currentIndex;
|
60 | };
|
61 | |
62 |
|
63 |
|
64 |
|
65 | TokenReader.prototype.tryExtractAccumulatedSequence = function () {
|
66 | if (this.isAccumulatedSequenceEmpty()) {
|
67 | return undefined;
|
68 | }
|
69 | return this.extractAccumulatedSequence();
|
70 | };
|
71 | |
72 |
|
73 |
|
74 |
|
75 | TokenReader.prototype.assertAccumulatedSequenceIsEmpty = function () {
|
76 | if (!this.isAccumulatedSequenceEmpty()) {
|
77 |
|
78 | var sequence = new TokenSequence_1.TokenSequence({
|
79 | parserContext: this._parserContext,
|
80 | startIndex: this._accumulatedStartIndex,
|
81 | endIndex: this._currentIndex
|
82 | });
|
83 | var tokenStrings = sequence.tokens.map(function (x) { return x.toString(); });
|
84 | throw new Error('Parser assertion failed: The queue should be empty, but it contains:\n' +
|
85 | JSON.stringify(tokenStrings));
|
86 | }
|
87 | };
|
88 | |
89 |
|
90 |
|
91 |
|
92 | TokenReader.prototype.peekToken = function () {
|
93 | return this.tokens[this._currentIndex];
|
94 | };
|
95 | |
96 |
|
97 |
|
98 |
|
99 | TokenReader.prototype.peekTokenKind = function () {
|
100 | if (this._currentIndex >= this._readerEndIndex) {
|
101 | return Token_1.TokenKind.EndOfInput;
|
102 | }
|
103 | return this.tokens[this._currentIndex].kind;
|
104 | };
|
105 | |
106 |
|
107 |
|
108 | TokenReader.prototype.peekTokenAfterKind = function () {
|
109 | if (this._currentIndex + 1 >= this._readerEndIndex) {
|
110 | return Token_1.TokenKind.EndOfInput;
|
111 | }
|
112 | return this.tokens[this._currentIndex + 1].kind;
|
113 | };
|
114 | |
115 |
|
116 |
|
117 | TokenReader.prototype.peekTokenAfterAfterKind = function () {
|
118 | if (this._currentIndex + 2 >= this._readerEndIndex) {
|
119 | return Token_1.TokenKind.EndOfInput;
|
120 | }
|
121 | return this.tokens[this._currentIndex + 2].kind;
|
122 | };
|
123 | |
124 |
|
125 |
|
126 |
|
127 |
|
128 | TokenReader.prototype.readToken = function () {
|
129 | if (this._currentIndex >= this._readerEndIndex) {
|
130 |
|
131 | throw new Error('Cannot read past end of stream');
|
132 | }
|
133 | var token = this.tokens[this._currentIndex];
|
134 | if (token.kind === Token_1.TokenKind.EndOfInput) {
|
135 |
|
136 |
|
137 |
|
138 | throw new Error('The EndOfInput token cannot be read');
|
139 | }
|
140 | this._currentIndex++;
|
141 | return token;
|
142 | };
|
143 | |
144 |
|
145 |
|
146 | TokenReader.prototype.peekPreviousTokenKind = function () {
|
147 | if (this._currentIndex === 0) {
|
148 | return Token_1.TokenKind.EndOfInput;
|
149 | }
|
150 | return this.tokens[this._currentIndex - 1].kind;
|
151 | };
|
152 | |
153 |
|
154 |
|
155 | TokenReader.prototype.createMarker = function () {
|
156 | return this._currentIndex;
|
157 | };
|
158 | |
159 |
|
160 |
|
161 | TokenReader.prototype.backtrackToMarker = function (marker) {
|
162 | if (marker > this._currentIndex) {
|
163 |
|
164 | throw new Error('The marker has expired');
|
165 | }
|
166 | this._currentIndex = marker;
|
167 | if (marker < this._accumulatedStartIndex) {
|
168 | this._accumulatedStartIndex = marker;
|
169 | }
|
170 | };
|
171 | return TokenReader;
|
172 | }());
|
173 | exports.TokenReader = TokenReader;
|
174 |
|
\ | No newline at end of file |