File tree 5 files changed +31
-21
lines changed
5 files changed +31
-21
lines changed Original file line number Diff line number Diff line change @@ -39,22 +39,22 @@ const compiler = {
39
39
// or modify it as needed. If the pass encounters a semantic error, it throws
40
40
// |peg.GrammarError|.
41
41
passes : {
42
- check : {
42
+ check : [
43
43
reportUndefinedRules ,
44
44
reportDuplicateRules ,
45
45
reportDuplicateLabels ,
46
46
reportInfiniteRecursion ,
47
47
reportInfiniteRepetition ,
48
48
reportIncorrectPlucking
49
- } ,
50
- transform : {
49
+ ] ,
50
+ transform : [
51
51
removeProxyRules ,
52
52
inferenceMatchResult ,
53
- } ,
54
- generate : {
53
+ ] ,
54
+ generate : [
55
55
generateBytecode ,
56
56
generateJS
57
- }
57
+ ]
58
58
} ,
59
59
60
60
// Generates a parser from a specified grammar AST. Throws |peg.GrammarError|
Original file line number Diff line number Diff line change @@ -59,6 +59,9 @@ function inferenceMatchResult(ast) {
59
59
// 6 == 3! -- permutations count for all transitions from one match
60
60
// state to another.
61
61
// 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
+
62
65
// istanbul ignore next This is canary test, shouldn't trigger in real life
63
66
if ( ++ count > 6 ) {
64
67
throw new GrammarError (
Original file line number Diff line number Diff line change @@ -641,12 +641,6 @@ export namespace compiler {
641
641
function build < F extends NodeTypes > ( functions : F ) : Visitor < F > ;
642
642
}
643
643
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
-
650
644
/**
651
645
* Mapping from the stage name to the default pass suite.
652
646
* Plugins can extend or replace the list of passes during configuration.
@@ -656,17 +650,17 @@ export namespace compiler {
656
650
* Pack of passes that performing checks on the AST. This bunch of passes
657
651
* executed in the very beginning of the compilation stage.
658
652
*/
659
- check : Passes ;
653
+ check : Pass [ ] ;
660
654
/**
661
655
* Pack of passes that performing transformation of the AST.
662
656
* Various types of optimizations are performed here.
663
657
*/
664
- transform : Passes ;
658
+ transform : Pass [ ] ;
665
659
/** Pack of passes that generates the code. */
666
- generate : Passes ;
660
+ generate : Pass [ ] ;
667
661
668
662
/** Any additional stages that can be added in the future. */
669
- [ key : string ] : Passes ;
663
+ [ key : string ] : Pass [ ] ;
670
664
}
671
665
672
666
/** List of the compilation stages. */
Original file line number Diff line number Diff line change @@ -91,12 +91,10 @@ const peg = {
91
91
generate ( grammar , options ) {
92
92
options = options !== undefined ? options : { } ;
93
93
94
- function convertPasses ( passes ) {
94
+ function copyPasses ( passes ) {
95
95
const converted = { } ;
96
-
97
96
Object . keys ( passes ) . forEach ( stage => {
98
- converted [ stage ] = Object . keys ( passes [ stage ] )
99
- . map ( name => passes [ stage ] [ name ] ) ;
97
+ converted [ stage ] = passes [ stage ] . slice ( ) ;
100
98
} ) ;
101
99
102
100
return converted ;
@@ -105,7 +103,7 @@ const peg = {
105
103
const plugins = "plugins" in options ? options . plugins : [ ] ;
106
104
const config = {
107
105
parser : peg . parser ,
108
- passes : convertPasses ( peg . compiler . passes ) ,
106
+ passes : copyPasses ( peg . compiler . passes ) ,
109
107
reservedWords : peg . RESERVED_WORDS . slice ( ) ,
110
108
} ;
111
109
Original file line number Diff line number Diff line change @@ -308,6 +308,21 @@ describe("peg.d.ts", () => {
308
308
"zero_or_more" ,
309
309
] ) ;
310
310
} ) ;
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
+ } ) ;
311
326
} ) ;
312
327
313
328
describe ( "run tsd" , ( ) => {
You can’t perform that action at this time.
0 commit comments