-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathStream.js
44 lines (39 loc) · 1.16 KB
/
Stream.js
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
// Character streamer. Streams characters from the input (a string) one at a
// time, including peeking. Returns -1 on end of file.
define(function () {
var Stream = function (input) {
this.input = input;
this.position = 0;
this.lineNumber = 1;
};
// Returns the next character, or -1 on end of file.
Stream.prototype.next = function () {
var ch = this.peek();
if (ch == "\n") {
this.lineNumber++;
}
if (ch != -1) {
this.position++;
}
return ch;
};
// Peeks at the next character, or -1 on end of file.
Stream.prototype.peek = function () {
if (this.position >= this.input.length) {
return -1;
}
return this.input[this.position];
};
// Inverse of "next()" method.
Stream.prototype.pushBack = function (ch) {
if (this.position === 0) {
throw new "Can't push back at start of stream";
}
this.position--;
// Sanity check.
if (this.input[this.position] != ch) {
throw new "Pushed back character doesn't match";
}
};
return Stream;
});