@parser-generator/grammar-interpreter
Version:
A Parser Generator that supports LL,SLR,LR1,LALR
94 lines • 3.06 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const should_1 = __importDefault(require("should"));
const interp_1 = require("../interp");
const core_1 = require("@parser-generator/core");
describe(`Grammar Interpreter Test`, function () {
describe(`Patterns Test`, function () {
it(`script`, function () {
let re = /<%(.+)?%>/;
should_1.default(re.exec("aaa <%a<a%a>a>%%>")).has.property("1", "a<a%a>a>%");
});
});
let rawGrammar = `
<%
//脚本片段1
%>
#TOKEN_PROTOTYPES
digit
<%
//脚本片段2
%>
#GRAMMAR
S->E;
E->F T <% (e)=>new Expr(e) %> | F;
T-> "+"F T| '-'F T | NIL <% (e)=>new Term(e) %>;
F->digit | '('E')' <% (e)=>new Factor(e)%>;
<%
//脚本片段3
%>
#SYMBOL_ASSOC_PREC
'+' left 0
'-' left 0
'*' left 1
'/' left 1
<%
//脚本片段4
%>
`;
describe(`Generate LL Parser`, function () {
let program;
it(`parser`, function () {
should_1.default.doesNotThrow(function () {
program = interp_1.parse(new interp_1.GrammarLexer(rawGrammar));
});
});
let g;
it(`eval`, function () {
should_1.default.doesNotThrow(function () {
let visitor = new interp_1.EvalVisitor({ parser: "LL" });
program.accpet(visitor);
g = visitor.grammar;
console.log(g + "");
});
});
it(`gen`, function () {
should_1.default.doesNotThrow(function () {
let first = new core_1.FirstCalculator(g);
let firstTable = first.getFirstTable();
let follow = new core_1.FollowCalculator(g, first);
let followTable = follow.getFollowTable();
let vi = new interp_1.TSCodegenVisitor({ firstTable, followTable, parser: "LL" });
program.accpet(vi);
});
});
});
describe(`Generate LR Parser`, function () {
let program;
it(`parser`, function () {
should_1.default.doesNotThrow(function () {
program = interp_1.parse(new interp_1.GrammarLexer(rawGrammar));
});
});
let g;
it(`eval`, function () {
should_1.default.doesNotThrow(function () {
let visitor = new interp_1.EvalVisitor({ parser: "LR1" });
program.accpet(visitor);
g = visitor.grammar;
console.log(g + "");
});
});
it(`gen`, function () {
should_1.default.doesNotThrow(function () {
let t = core_1.getLR1ParsingTable(g);
let vi = new interp_1.TSCodegenVisitor({ lrTable: t, parser: "LR1" });
program.accpet(vi);
});
});
});
});
//# sourceMappingURL=interpreter.test.js.map