@@ -168,12 +168,15 @@ function Grammar(rules, start) {
168
168
169
169
// So we can allow passing (rules, start) directly to Parser for backwards compatibility
170
170
Grammar . fromCompiled = function ( rules , start ) {
171
+ var lexer = rules . Lexer ;
171
172
if ( rules . ParserStart ) {
172
173
start = rules . ParserStart ;
173
174
rules = rules . ParserRules ;
174
175
}
175
176
var rules = rules . map ( function ( r ) { return ( new Rule ( r . name , r . symbols , r . postprocess ) ) ; } ) ;
176
- return new Grammar ( rules , start ) ;
177
+ var g = new Grammar ( rules , start ) ;
178
+ g . lexer = lexer ; // TODO storing lexer onto Grammar seems iffy!
179
+ return g ;
177
180
}
178
181
179
182
@@ -218,7 +221,7 @@ function Parser(rules, start, options) {
218
221
// Read options
219
222
this . options = {
220
223
keepHistory : false ,
221
- lexer : ChunkLexer ,
224
+ lexer : grammar . lexer || ChunkLexer ,
222
225
} ;
223
226
for ( var key in ( options || { } ) ) {
224
227
this . options [ key ] = options [ key ] ;
@@ -261,14 +264,17 @@ Parser.prototype.feed = function(chunk) {
261
264
// Advance all tokens that expect the symbol
262
265
// So for each state in the previous row,
263
266
264
- var value = token . value ;
267
+ var literal = token . value ;
268
+ var value = lexer . constructor === ChunkLexer ? token . value : token ;
265
269
var scannable = column . scannable ;
266
270
for ( var w = scannable . length ; w -- ; ) {
267
271
var state = scannable [ w ] ;
268
272
var expect = state . rule . symbols [ state . dot ] ;
269
273
// Try to consume the token
270
274
// either regex or literal
271
- if ( expect . test ? expect . test ( value ) : expect . literal === value ) {
275
+ if ( expect . test ? expect . test ( value ) :
276
+ expect . type ? expect . type === token . type
277
+ : expect . literal === literal ) {
272
278
// Add it
273
279
var next = state . nextState ( value ) ;
274
280
nextColumn . states . push ( next ) ;
@@ -290,7 +296,7 @@ Parser.prototype.feed = function(chunk) {
290
296
// No states at all! This is not good.
291
297
var err = new Error (
292
298
"nearley: No possible parsings (@" + ( this . current )
293
- + ": '" + token + "')."
299
+ + ": '" + JSON . stringify ( token ) + "')."
294
300
) ;
295
301
err . offset = this . current ;
296
302
throw err ;
0 commit comments