Skip to content

Commit 4c2f937

Browse files
committed
Merge branch 'fix-handle-function-token-matchers' of https://github.com/airportyh/nearley into airportyh-fix-handle-function-token-matchers
2 parents 7232017 + 95249bd commit 4c2f937

File tree

3 files changed

+53
-19
lines changed

3 files changed

+53
-19
lines changed

examples/token-2.ne

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@{%
2+
const tokenPrint = { literal: "print" };
3+
const tokenNumber = { test: x => Number.isInteger(x) };
4+
%}
5+
6+
main -> %tokenPrint %tokenNumber ";;"

lib/nearley.js

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,11 @@
1616
Rule.highestId = 0;
1717

1818
Rule.prototype.toString = function(withCursorAt) {
19-
function stringifySymbolSequence (e) {
20-
return e.literal ? JSON.stringify(e.literal) :
21-
e.type ? '%' + e.type : e.toString();
22-
}
2319
var symbolSequence = (typeof withCursorAt === "undefined")
24-
? this.symbols.map(stringifySymbolSequence).join(' ')
25-
: ( this.symbols.slice(0, withCursorAt).map(stringifySymbolSequence).join(' ')
20+
? this.symbols.map(getSymbolShortDisplay).join(' ')
21+
: ( this.symbols.slice(0, withCursorAt).map(getSymbolShortDisplay).join(' ')
2622
+ " ● "
27-
+ this.symbols.slice(withCursorAt).map(stringifySymbolSequence).join(' ') );
23+
+ this.symbols.slice(withCursorAt).map(getSymbolShortDisplay).join(' ') );
2824
return this.name + " → " + symbolSequence;
2925
}
3026

@@ -391,18 +387,7 @@
391387
};
392388

393389
Parser.prototype.getSymbolDisplay = function(symbol) {
394-
var type = typeof symbol;
395-
if (type === "string") {
396-
return symbol;
397-
} else if (type === "object" && symbol.literal) {
398-
return JSON.stringify(symbol.literal);
399-
} else if (type === "object" && symbol instanceof RegExp) {
400-
return 'character matching ' + symbol;
401-
} else if (type === "object" && symbol.type) {
402-
return symbol.type + ' token';
403-
} else {
404-
throw new Error('Unknown symbol type: ' + symbol);
405-
}
390+
return getSymbolLongDisplay(symbol);
406391
};
407392

408393
/*
@@ -478,6 +463,44 @@
478463
return considerations.map(function(c) {return c.data; });
479464
};
480465

466+
function getSymbolLongDisplay(symbol) {
467+
var type = typeof symbol;
468+
if (type === "string") {
469+
return symbol;
470+
} else if (type === "object") {
471+
if (symbol.literal) {
472+
return JSON.stringify(symbol.literal);
473+
} else if (symbol instanceof RegExp) {
474+
return 'character matching ' + symbol;
475+
} else if (symbol.type) {
476+
return symbol.type + ' token';
477+
} else if (symbol.test) {
478+
return 'token matching ' + String(symbol.test);
479+
} else {
480+
throw new Error('Unknown symbol type: ' + symbol);
481+
}
482+
}
483+
}
484+
485+
function getSymbolShortDisplay(symbol) {
486+
var type = typeof symbol;
487+
if (type === "string") {
488+
return symbol;
489+
} else if (type === "object") {
490+
if (symbol.literal) {
491+
return JSON.stringify(symbol.literal);
492+
} else if (symbol instanceof RegExp) {
493+
return symbol.toString();
494+
} else if (symbol.type) {
495+
return '%' + symbol.type;
496+
} else if (symbol.test) {
497+
return '<' + String(symbol.test) + '>';
498+
} else {
499+
throw new Error('Unknown symbol type: ' + symbol);
500+
}
501+
}
502+
}
503+
481504
return {
482505
Parser: Parser,
483506
Grammar: Grammar,

test/parser.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ describe('Parser: examples', () => {
183183
expect(parse(tokc, [123, 456, " ", 789])).toEqual([ [123, [ [ 456, " ", 789 ] ]] ]);
184184
});
185185

186+
it('tokens 2', () => {
187+
var tokc = compile(read("examples/token-2.ne"));
188+
expect(() => parse(tokc, ["print", "blah", 12, ";", ";"])).toThrow(/A token matching x \=\> Number\.isInteger\(x\)/);
189+
})
190+
186191
const json = compile(read("examples/json.ne"));
187192
it('json', () => {
188193
const test1 = '{ "a" : true, "b" : "䕧⡱a\\\\\\"b\\u4567\\u2871䕧⡱\\t\\r\\f\\b\\n", "c" : null, "d" : [null, true, false, null] }\n'

0 commit comments

Comments
 (0)