Skip to content

Commit

Permalink
Add save() and accept state in reset()
Browse files Browse the repository at this point in the history
Closes #12.
  • Loading branch information
nathan committed Mar 13, 2017
1 parent 9dc816f commit b4d2416
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
13 changes: 10 additions & 3 deletions moo.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,21 @@
return this.re.lastIndex
}

Lexer.prototype.reset = function(data) {
Lexer.prototype.reset = function(data, state) {
this.buffer = data || ''
this.re.lastIndex = 0
this.line = 1
this.col = 1
this.line = state ? state.line : 1
this.col = state ? state.col : 1
return this
}

Lexer.prototype.save = function() {
return {
line: this.line,
col: this.col,
}
}

Lexer.prototype.feed = function(data) {
this.buffer += data
return this
Expand Down
21 changes: 21 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,27 @@ describe('line numbers', () => {
})


describe('state', () => {

const testLexer = compile({
word: /[a-z]+/,
NL: { match: '\n', lineBreaks: true },
})

test('can be saved', () => {
testLexer.reset('one\ntwo')
testLexer.lexAll()
expect(testLexer.save()).toEqual({line: 2, col: 4})
})

test('can be restored', () => {
testLexer.reset('\nthree', {line: 2, col: 4})
expect(testLexer).toMatchObject({line: 2, col: 4, buffer: '\nthree'})
})

})


describe('python tokenizer', () => {

test("1 + 2", () => {
Expand Down

0 comments on commit b4d2416

Please sign in to comment.