-
Notifications
You must be signed in to change notification settings - Fork 106
/
ANTLRInputStream.ts
160 lines (139 loc) · 4 KB
/
ANTLRInputStream.ts
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
/*!
* 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.
*/
// ConvertTo-TS run at 2016-10-04T11:26:49.0828748-07:00
import * as assert from "assert";
import { CharStream } from "./CharStream";
import { Arrays } from "./misc/Arrays";
import { Override } from "./Decorators";
import { IntStream } from "./IntStream";
import { Interval } from "./misc/Interval";
const READ_BUFFER_SIZE: number = 1024;
const INITIAL_BUFFER_SIZE: number = 1024;
/**
* Vacuum all input from a {@link Reader}/{@link InputStream} and then treat it
* like a `char[]` buffer. Can also pass in a {@link String} or
* `char[]` to use.
*
* If you need encoding, pass in stream/reader with correct encoding.
*
* @deprecated as of 4.7, please use `CharStreams` interface.
*/
export class ANTLRInputStream implements CharStream {
/** The data being scanned */
protected data: string;
/** How many characters are actually in the buffer */
protected n: number;
/** 0..n-1 index into string of next char */
protected p: number = 0;
/** What is name or source of this char stream? */
public name?: string;
/** Copy data in string to a local char array */
constructor(input: string) {
this.data = input;
this.n = input.length;
}
/** Reset the stream so that it's in the same state it was
* when the object was created *except* the data array is not
* touched.
*/
public reset(): void {
this.p = 0;
}
@Override
public consume(): void {
if (this.p >= this.n) {
assert(this.LA(1) === IntStream.EOF);
throw new Error("cannot consume EOF");
}
//System.out.println("prev p="+p+", c="+(char)data[p]);
if (this.p < this.n) {
this.p++;
//System.out.println("p moves to "+p+" (c='"+(char)data[p]+"')");
}
}
@Override
public LA(i: number): number {
if (i === 0) {
return 0; // undefined
}
if (i < 0) {
i++; // e.g., translate LA(-1) to use offset i=0; then data[p+0-1]
if ((this.p + i - 1) < 0) {
return IntStream.EOF; // invalid; no char before first char
}
}
if ((this.p + i - 1) >= this.n) {
//System.out.println("char LA("+i+")=EOF; p="+p);
return IntStream.EOF;
}
//System.out.println("char LA("+i+")="+(char)data[p+i-1]+"; p="+p);
//System.out.println("LA("+i+"); p="+p+" n="+n+" data.length="+data.length);
return this.data.charCodeAt(this.p + i - 1);
}
public LT(i: number): number {
return this.LA(i);
}
/** Return the current input symbol index 0..n where n indicates the
* last symbol has been read. The index is the index of char to
* be returned from LA(1).
*/
@Override
get index(): number {
return this.p;
}
@Override
get size(): number {
return this.n;
}
/** mark/release do nothing; we have entire buffer */
@Override
public mark(): number {
return -1;
}
@Override
public release(marker: number): void {
// No default implementation since this stream buffers the entire input
}
/** consume() ahead until p==index; can't just set p=index as we must
* update line and charPositionInLine. If we seek backwards, just set p
*/
@Override
public seek(index: number): void {
if (index <= this.p) {
this.p = index; // just jump; don't update stream state (line, ...)
return;
}
// seek forward, consume until p hits index or n (whichever comes first)
index = Math.min(index, this.n);
while (this.p < index) {
this.consume();
}
}
@Override
public getText(interval: Interval): string {
let start: number = interval.a;
let stop: number = interval.b;
if (stop >= this.n) {
stop = this.n - 1;
}
let count: number = stop - start + 1;
if (start >= this.n) {
return "";
}
// System.err.println("data: "+Arrays.toString(data)+", n="+n+
// ", start="+start+
// ", stop="+stop);
return this.data.substr(start, count);
}
@Override
get sourceName(): string {
if (!this.name) {
return IntStream.UNKNOWN_SOURCE_NAME;
}
return this.name;
}
@Override
public toString() { return this.data; }
}