Closed alastairzotos closed 4 years ago
const peekable = lexer => ({
here: lexer.next(),
next() {
const old = this.here
this.here = lexer.next()
return old
}
})
const lexer = moo.compile({ws: /[ \t]+/, id: /\w+/})
const tokens = peekable(lexer.reset(`abc def ghi`))
tokens.here
tokens.next()
That's nice but it's not very good with Typescript. Notable, this.here
is just implicitly any
.
I have this for now but I'd rather not have to write it:
let lastPeekedToken: moo.Token | null = null;
export const peek = (lexer: moo.Lexer, type?: ITokenType): moo.Token | null => {
if (!lastPeekedToken) {
lastPeekedToken = lexer.next();
}
if (type && (!lastPeekedToken || lastPeekedToken.type !== type)) {
return null;
}
return lastPeekedToken;
}
export const consume = (lexer: moo.Lexer, type?: ITokenType): moo.Token => {
const token = lastPeekedToken || lexer.next();
if (lastPeekedToken) {
lastPeekedToken = null;
}
if (type && (!token || token.type !== type)) {
throw new Error(`Expected '${type}', got ${token ? token.type : 'eos'}`);
}
return token;
}
I agree with Nathan; you can write this yourself :-) Weâd rather keep Mooâs core simple.
For what itâs worth, I donât see a reason you couldnât add types to Nathanâs version.
I want to make a recursive descent parser, which requires the ability to check the next token and act accordingly. Ideally, there would also be a function to consume a token of a certain type, and to throw an error if the type of the token doesn't match.
Example:
Is something like this possible?