Skip to content

Commit

Permalink
Make Lexer a transform stream
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan committed Mar 18, 2017
1 parent 47ac663 commit ca92f26
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
18 changes: 7 additions & 11 deletions moo.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,22 +271,18 @@
this.stack = []
this.setState(state)
this.reset()
if (Transform) Transform.call(this, {readableObjectMode: true})
}

if (typeof module !== 'undefined' && module.exports) {
var Transform = require('stream').Transform
require('util').inherits(Lexer, Transform)

Lexer.prototype.stream = function(state) {
var self = this.reset('', state)
return new Transform({
readableObjectMode: true,
transform(chunk, encoding, cb) {
self.feed(chunk.toString())
var token
while (token = self.next()) this.push(token)
cb()
}
})
Lexer.prototype._transform = function(chunk, encoding, cb) {
this.feed(chunk.toString())
var token
while (token = this.next()) this.push(token)
cb()
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ describe('streams', () => {
let index = 0
expect.assertions(tokens.length)

const s = lexer.stream()
const s = lexer.clone()
s.write(inputs[0])
s.end(inputs[1])

Expand Down Expand Up @@ -528,7 +528,7 @@ describe('streams', () => {
})

rs
.on('error', reject).pipe(lexer.stream())
.on('error', reject).pipe(lexer.clone())
.on('error', reject).pipe(ws)
.on('error', reject)
.on('finish', resolve)
Expand Down

0 comments on commit ca92f26

Please sign in to comment.