@ -442,4 +442,144 @@ test("computes variable names", function() {
}
} ) ;
test ( "computes params" , function ( ) {
function extractNode ( node ) { return node ; }
function extractExpression ( node ) { return node . expression ; }
var cases = [
/* Recursive walk */
{
grammar : 'start = a:"a" { }' ,
extractor : extractNode ,
params : { a : "result0" }
} ,
{
grammar : 'start = a:"a" { } / "b" / "c"' ,
extractor : function ( node ) { return node . alternatives [ 0 ] ; } ,
params : { a : "result0" }
} ,
{
grammar : 'start = "a" / "b" / c:"c" { }' ,
extractor : function ( node ) { return node . alternatives [ 2 ] ; } ,
params : { c : "result0" }
} ,
{
grammar : 'start = (a:"a" { }) "b" "c"' ,
extractor : function ( node ) { return node . elements [ 0 ] ; } ,
params : { a : "result0" }
} ,
{
grammar : 'start = "a" "b" (c:"c" { })' ,
extractor : function ( node ) { return node . elements [ 2 ] ; } ,
params : { c : "result2" }
} ,
{
grammar : 'start = a:(b:"b" { })' ,
extractor : extractExpression ,
params : { b : "result0" }
} ,
{
grammar : 'start = &(a:"a" { })' ,
extractor : extractExpression ,
params : { a : "result0" }
} ,
{
grammar : 'start = !(a:"a" { })' ,
extractor : extractExpression ,
params : { a : "result0" }
} ,
{
grammar : 'start = (a:"a" { })?' ,
extractor : extractExpression ,
params : { a : "result0" }
} ,
{
grammar : 'start = (a:"a" { })*' ,
extractor : extractExpression ,
params : { a : "result1" }
} ,
{
grammar : 'start = (a:"a" { })+' ,
extractor : extractExpression ,
params : { a : "result1" }
} ,
{
grammar : 'start = (a:"a" { }) { }' ,
extractor : extractExpression ,
params : { a : "result0" }
} ,
/* Scoping */
{
grammar : 'start = (a:"a" / b:"b" / c:"c") { }' ,
extractor : extractNode ,
params : { }
} ,
{
grammar : 'start = a:(b:"b") { }' ,
extractor : extractNode ,
params : { a : "result0" }
} ,
{
grammar : 'start = &(a:"a") { }' ,
extractor : extractNode ,
params : { }
} ,
{
grammar : 'start = !(a:"a") { }' ,
extractor : extractNode ,
params : { }
} ,
{
grammar : 'start = (a:"a")? { }' ,
extractor : extractNode ,
params : { }
} ,
{
grammar : 'start = (a:"a")* { }' ,
extractor : extractNode ,
params : { }
} ,
{
grammar : 'start = (a:"a")+ { }' ,
extractor : extractNode ,
params : { }
} ,
{
grammar : 'start = (a:"a" { }) { }' ,
extractor : extractNode ,
params : { }
} ,
/* Sequences */
{
grammar : 'start = a:"a" b:"b" c:"c" { }' ,
extractor : extractNode ,
params : { a : "result0[0]" , b : "result0[1]" , c : "result0[2]" }
} ,
{
grammar : 'start = a:"a" (b:"b" c:"c" d:"d") e:"e"{ }' ,
extractor : extractNode ,
params : {
a : "result0[0]" ,
b : "result0[1][0]" ,
c : "result0[1][1]" ,
d : "result0[1][2]" ,
e : "result0[2]"
}
}
] ;
for ( var i = 0 ; i < cases . length ; i ++ ) {
var ast = PEG . parser . parse ( cases [ i ] . grammar ) ;
PEG . compiler . passes . computeVarNames ( ast ) ;
PEG . compiler . passes . computeParams ( ast ) ;
deepEqual (
cases [ i ] . extractor ( ast . rules [ "start" ] . expression ) . params ,
cases [ i ] . params
) ;
}
} ) ;
} ) ( ) ;