Skip to content

Commit ed1cb08

Browse files
committed
Test compiler interface in order to generate MatchResult. Fix type for Stages .d.ts to match reality. Simplify compiler.passes; it doesn't need to be an object that gets converted to an array; it can just be an array that gets copied
1 parent 8a4051f commit ed1cb08

File tree

5 files changed

+31
-21
lines changed

5 files changed

+31
-21
lines changed

lib/compiler/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,22 @@ const compiler = {
3939
// or modify it as needed. If the pass encounters a semantic error, it throws
4040
// |peg.GrammarError|.
4141
passes: {
42-
check: {
42+
check: [
4343
reportUndefinedRules,
4444
reportDuplicateRules,
4545
reportDuplicateLabels,
4646
reportInfiniteRecursion,
4747
reportInfiniteRepetition,
4848
reportIncorrectPlucking
49-
},
50-
transform: {
49+
],
50+
transform: [
5151
removeProxyRules,
5252
inferenceMatchResult,
53-
},
54-
generate: {
53+
],
54+
generate: [
5555
generateBytecode,
5656
generateJS
57-
}
57+
]
5858
},
5959

6060
// Generates a parser from a specified grammar AST. Throws |peg.GrammarError|

lib/compiler/passes/inference-match-result.js

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ function inferenceMatchResult(ast) {
5959
// 6 == 3! -- permutations count for all transitions from one match
6060
// state to another.
6161
// After 6 iterations the cycle with guarantee begins
62+
// For example, an input of `start = [] start` will generate the
63+
// sequence: 0 -> 1 -> 0 -> -1 -> 0 -> 1 (then cycle)
64+
6265
// istanbul ignore next This is canary test, shouldn't trigger in real life
6366
if (++count > 6) {
6467
throw new GrammarError(

lib/peg.d.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -641,12 +641,6 @@ export namespace compiler {
641641
function build<F extends NodeTypes>(functions: F): Visitor<F>;
642642
}
643643

644-
/** Mapping from the pass name to the function that represents pass. */
645-
interface Passes {
646-
/** List of passes in the stage. Any concrete set of passes are not guaranteed. */
647-
[key: string]: Pass;
648-
}
649-
650644
/**
651645
* Mapping from the stage name to the default pass suite.
652646
* Plugins can extend or replace the list of passes during configuration.
@@ -656,17 +650,17 @@ export namespace compiler {
656650
* Pack of passes that performing checks on the AST. This bunch of passes
657651
* executed in the very beginning of the compilation stage.
658652
*/
659-
check: Passes;
653+
check: Pass[];
660654
/**
661655
* Pack of passes that performing transformation of the AST.
662656
* Various types of optimizations are performed here.
663657
*/
664-
transform: Passes;
658+
transform: Pass[];
665659
/** Pack of passes that generates the code. */
666-
generate: Passes;
660+
generate: Pass[];
667661

668662
/** Any additional stages that can be added in the future. */
669-
[key: string]: Passes;
663+
[key: string]: Pass[];
670664
}
671665

672666
/** List of the compilation stages. */

lib/peg.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,10 @@ const peg = {
9191
generate(grammar, options) {
9292
options = options !== undefined ? options : {};
9393

94-
function convertPasses(passes) {
94+
function copyPasses(passes) {
9595
const converted = {};
96-
9796
Object.keys(passes).forEach(stage => {
98-
converted[stage] = Object.keys(passes[stage])
99-
.map(name => passes[stage][name]);
97+
converted[stage] = passes[stage].slice();
10098
});
10199

102100
return converted;
@@ -105,7 +103,7 @@ const peg = {
105103
const plugins = "plugins" in options ? options.plugins : [];
106104
const config = {
107105
parser: peg.parser,
108-
passes: convertPasses(peg.compiler.passes),
106+
passes: copyPasses(peg.compiler.passes),
109107
reservedWords: peg.RESERVED_WORDS.slice(),
110108
};
111109

test/types/peg.test-d.ts

+15
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,21 @@ describe("peg.d.ts", () => {
308308
"zero_or_more",
309309
]);
310310
});
311+
312+
it("compiles", () => {
313+
const ast = peggy.parser.parse("start = 'foo'", {
314+
grammarSource: "it compiles",
315+
reservedWords: peggy.RESERVED_WORDS.slice(),
316+
});
317+
expectType<peggy.ast.Grammar>(ast);
318+
const parser = peggy.compiler.compile(
319+
ast,
320+
peggy.compiler.passes
321+
);
322+
expectType<peggy.Parser>(parser);
323+
expectType<peggy.ast.MatchResult|undefined>(ast.rules[0].match);
324+
expect(ast.rules[0].match).toBe(0);
325+
});
311326
});
312327

313328
describe("run tsd", () => {

0 commit comments

Comments
 (0)