UNPKG

@parser-generator/grammar-interpreter

Version:

A Parser Generator that supports LL,SLR,LR1,LALR

89 lines 2.75 kB
import should from "should"; import { GrammarLexer, parse, TSCodegenVisitor, EvalVisitor } from "../interp"; import { FirstCalculator, FollowCalculator, getLR1ParsingTable } from "@parser-generator/core"; describe(`Grammar Interpreter Test`, function () { describe(`Patterns Test`, function () { it(`script`, function () { let re = /<%(.+)?%>/; should(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.doesNotThrow(function () { program = parse(new GrammarLexer(rawGrammar)); }); }); let g; it(`eval`, function () { should.doesNotThrow(function () { let visitor = new EvalVisitor({ parser: "LL" }); program.accpet(visitor); g = visitor.grammar; console.log(g + ""); }); }); it(`gen`, function () { should.doesNotThrow(function () { let first = new FirstCalculator(g); let firstTable = first.getFirstTable(); let follow = new FollowCalculator(g, first); let followTable = follow.getFollowTable(); let vi = new TSCodegenVisitor({ firstTable, followTable, parser: "LL" }); program.accpet(vi); }); }); }); describe(`Generate LR Parser`, function () { let program; it(`parser`, function () { should.doesNotThrow(function () { program = parse(new GrammarLexer(rawGrammar)); }); }); let g; it(`eval`, function () { should.doesNotThrow(function () { let visitor = new EvalVisitor({ parser: "LR1" }); program.accpet(visitor); g = visitor.grammar; console.log(g + ""); }); }); it(`gen`, function () { should.doesNotThrow(function () { let t = getLR1ParsingTable(g); let vi = new TSCodegenVisitor({ lrTable: t, parser: "LR1" }); program.accpet(vi); }); }); }); }); //# sourceMappingURL=interpreter.test.js.map