1234567891011121314151617181920212223242526272829303132 |
- const Error = require('./error')
-
- module.exports = class TokenStream {
- constructor() {
- this.tokens = []
- this.position = 0
- }
-
- advance() {
- this.position++
- }
-
- eat(tokenType) {
- let token = this.peek()
-
- if (token && token.type === tokenType) {
- this.advance()
- return token
- }
-
- this.error = new Error({
- line: token.line,
- message: `Encountered an unexpected ${
- token.type
- } while looking for a ${tokenType}.`,
- })
- }
-
- peek(step = 0) {
- return this.tokens[this.position + step]
- }
- }
|