@@ -168,11 +168,20 @@ describe('Parser', function() {
168
168
)
169
169
} )
170
170
171
- // TODO: save/restore
172
-
173
- /*
174
171
var tosh = compile ( read ( "examples/tosh.ne" ) ) ;
175
172
173
+ it ( 'can save state' , function ( ) {
174
+ let first = "say 'hello'" ;
175
+ let second = " for 2 secs" ;
176
+ let p = new nearley . Parser ( tosh , { keepHistory : true } ) ;
177
+ p . feed ( first ) ;
178
+ p . current . should . equal ( 11 )
179
+ p . table . length . should . equal ( 12 )
180
+ var col = p . save ( ) ;
181
+ col . index . should . equal ( 11 )
182
+ col . lexerState . col . should . equal ( first . length )
183
+ } ) ;
184
+
176
185
it ( 'can rewind' , function ( ) {
177
186
let first = "say 'hello'" ;
178
187
let second = " for 2 secs" ;
@@ -184,6 +193,7 @@ describe('Parser', function() {
184
193
p . feed ( second ) ;
185
194
186
195
p . rewind ( first . length ) ;
196
+
187
197
p . current . should . equal ( 11 )
188
198
p . table . length . should . equal ( 12 )
189
199
@@ -194,6 +204,36 @@ describe('Parser', function() {
194
204
let p = new nearley . Parser ( tosh , { } ) ;
195
205
p . rewind . should . throw ( ) ;
196
206
} )
197
- */
207
+
208
+ it ( 'restores line numbers' , function ( ) {
209
+ let p = new nearley . Parser ( testGrammar ) ;
210
+ p . feed ( 'abc\n' )
211
+ p . save ( ) . lexerState . line . should . equal ( 2 )
212
+ p . feed ( '123\n' )
213
+ var col = p . save ( ) ;
214
+ col . lexerState . line . should . equal ( 3 )
215
+ p . feed ( 'q' )
216
+ p . restore ( col ) ;
217
+ p . lexer . line . should . equal ( 3 )
218
+ p . feed ( 'z' )
219
+ } ) ;
220
+
221
+ it ( 'restores column number' , function ( ) {
222
+ let p = new nearley . Parser ( testGrammar ) ;
223
+ p . feed ( 'foo\nbar' )
224
+ var col = p . save ( ) ;
225
+ col . lexerState . line . should . equal ( 2 )
226
+ col . lexerState . col . should . equal ( 3 )
227
+ p . feed ( '123' ) ;
228
+ p . lexerState . col . should . equal ( 6 )
229
+
230
+ p . restore ( col ) ;
231
+ p . lexerState . line . should . equal ( 2 )
232
+ p . lexerState . col . should . equal ( 3 )
233
+ p . feed ( '456' )
234
+ p . lexerState . col . should . equal ( 6 )
235
+ } ) ;
236
+
237
+ // TODO: moo save/restore
198
238
199
239
} ) ;
0 commit comments