-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookaheadLexer.js
39 lines (36 loc) · 979 Bytes
/
lookaheadLexer.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
export class LookaheadLexer {
lookForNextToken() {
while (!(this.nextSymbol = this.baseLexer.next())) {
this.noTokenMatch += this.baseLexer.match;
}
this.nextMatch = this.baseLexer.match;
}
constructor(baseLexer) {
this.baseLexer = baseLexer;
this.EOF = baseLexer.EOF;
this.noTokenMatch = '';
this.lookForNextToken();
}
next() {
if (this.noTokenMatch.length > 0) {
this.match = this.noTokenMatch;
this.noTokenMatch = '';
return false;
} else {
const currSymbol = this.nextSymbol;
this.match = this.nextMatch;
if (currSymbol !== this.baseLexer.EOF) {
this.lookForNextToken();
}
return currSymbol;
}
}
lex() {
const r = this.next();
if (r) {
return r;
} else {
return this.lex();
}
}
}