Skip to content

Commit c80ef2d

Browse files
committed
Add @lexer directive
1 parent e67f494 commit c80ef2d

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

lib/generate.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ generate.js = generate._default = generate.javascript = function (parser, export
112112
output += "function id(x) {return x[0]; }\n";
113113
output += parser.body.join('\n');
114114
output += "var grammar = {\n";
115+
output += " Lexer: " + parser.config.lexer + ",\n";
115116
output += " ParserRules: " +
116117
serializeRules(parser.rules, generate.javascript.builtinPostprocessors)
117118
+ "\n";
@@ -141,6 +142,7 @@ generate.cs = generate.coffee = generate.coffeescript = function (parser, export
141142
output += " id = (d)->d[0]\n";
142143
output += tabulateString(dedentFunc(parser.body.join('\n')), ' ') + '\n';
143144
output += " grammar = {\n";
145+
output += " Lexer: " + parser.config.lexer + ",\n";
144146
output += " ParserRules: " +
145147
tabulateString(
146148
serializeRules(parser.rules, generate.coffeescript.builtinPostprocessors),
@@ -173,6 +175,7 @@ generate.ts = generate.typescript = function (parser, exportName) {
173175
output += "interface NearleyRule {name:string; symbols:NearleySymbol[]; postprocess?:(d:any[],loc?:number,reject?:{})=>any};\n";
174176
output += "type NearleySymbol = string | {literal:any} | {test:(token:any) => boolean};\n";
175177
output += "export var grammar : NearleyGrammar = {\n";
178+
output += " Lexer: " + parser.config.lexer + ",\n";
176179
output += " ParserRules: " + serializeRules(parser.rules, generate.typescript.builtinPostprocessors) + "\n";
177180
output += " , ParserStart: " + JSON.stringify(parser.start) + "\n";
178181
output += "}\n";

lib/nearley-language-bootstrapped.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function $(o) {
2020
};
2121
}
2222
var grammar = {
23+
Lexer: undefined,
2324
ParserRules: [
2425
{"name": "dqstring$ebnf$1", "symbols": []},
2526
{"name": "dqstring$ebnf$1", "symbols": ["dqstring$ebnf$1", "dstrchar"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},

lib/nearley.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,15 @@ function Grammar(rules, start) {
168168

169169
// So we can allow passing (rules, start) directly to Parser for backwards compatibility
170170
Grammar.fromCompiled = function(rules, start) {
171+
var lexer = rules.Lexer;
171172
if (rules.ParserStart) {
172173
start = rules.ParserStart;
173174
rules = rules.ParserRules;
174175
}
175176
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;
177180
}
178181

179182

@@ -218,7 +221,7 @@ function Parser(rules, start, options) {
218221
// Read options
219222
this.options = {
220223
keepHistory: false,
221-
lexer: ChunkLexer,
224+
lexer: grammar.lexer || ChunkLexer,
222225
};
223226
for (var key in (options || {})) {
224227
this.options[key] = options[key];
@@ -261,14 +264,17 @@ Parser.prototype.feed = function(chunk) {
261264
// Advance all tokens that expect the symbol
262265
// So for each state in the previous row,
263266

264-
var value = token.value;
267+
var literal = token.value;
268+
var value = lexer.constructor === ChunkLexer ? token.value : token;
265269
var scannable = column.scannable;
266270
for (var w = scannable.length; w--; ) {
267271
var state = scannable[w];
268272
var expect = state.rule.symbols[state.dot];
269273
// Try to consume the token
270274
// 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) {
272278
// Add it
273279
var next = state.nextState(value);
274280
nextColumn.states.push(next);
@@ -290,7 +296,7 @@ Parser.prototype.feed = function(chunk) {
290296
// No states at all! This is not good.
291297
var err = new Error(
292298
"nearley: No possible parsings (@" + (this.current)
293-
+ ": '" + token + "')."
299+
+ ": '" + JSON.stringify(token) + "')."
294300
);
295301
err.offset = this.current;
296302
throw err;

0 commit comments

Comments
 (0)