@ -130,7 +130,7 @@ describe("generated parser behavior", function() {
varyOptimizationOptions ( function ( options ) {
describe ( "initializer" , function ( ) {
it ( "executes the code before parsing starts" , function ( ) {
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'{ var result = 42; }' ,
'start = "a" { return result; }'
] . join ( "\n" ) , options ) ;
@ -140,7 +140,7 @@ describe("generated parser behavior", function() {
describe ( "available variables and functions" , function ( ) {
it ( "|parser| contains the parser object" , function ( ) {
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'{ var result = parser; }' ,
'start = "a" { return result; }'
] . join ( "\n" ) , options ) ;
@ -149,7 +149,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "|options| contains options" , function ( ) {
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'{ var result = options; }' ,
'start = "a" { return result; }'
] . join ( "\n" ) , options ) ;
@ -162,7 +162,7 @@ describe("generated parser behavior", function() {
describe ( "rule" , function ( ) {
if ( options . cache ) {
it ( "caches rule match results" , function ( ) {
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'{ var n = 0; }' ,
'start = (a "b") / (a "c") { return n; }' ,
'a = "a" { n++; }'
@ -172,7 +172,7 @@ describe("generated parser behavior", function() {
} ) ;
} else {
it ( "doesn't cache rule match results" , function ( ) {
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'{ var n = 0; }' ,
'start = (a "b") / (a "c") { return n; }' ,
'a = "a" { n++; }'
@ -184,7 +184,7 @@ describe("generated parser behavior", function() {
describe ( "when the expression matches" , function ( ) {
it ( "returns its match result" , function ( ) {
var parser = peg . buildParser ( 'start = "a"' ) ;
var parser = peg . generate ( 'start = "a"' ) ;
expect ( parser ) . toParse ( "a" , "a" ) ;
} ) ;
@ -193,7 +193,7 @@ describe("generated parser behavior", function() {
describe ( "when the expression doesn't match" , function ( ) {
describe ( "without display name" , function ( ) {
it ( "reports match failure and doesn't record any expectation" , function ( ) {
var parser = peg . buildParser ( 'start = "a"' ) ;
var parser = peg . generate ( 'start = "a"' ) ;
expect ( parser ) . toFailToParse ( "b" , {
expected : [ { type : "literal" , value : "a" , description : '"a"' } ]
@ -203,7 +203,7 @@ describe("generated parser behavior", function() {
describe ( "with display name" , function ( ) {
it ( "reports match failure and records an expectation of type \"other\"" , function ( ) {
var parser = peg . buildParser ( 'start "start" = "a"' ) ;
var parser = peg . generate ( 'start "start" = "a"' ) ;
expect ( parser ) . toFailToParse ( "b" , {
expected : [ { type : "other" , description : "start" } ]
@ -211,7 +211,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "discards any expectations recorded when matching the expression" , function ( ) {
var parser = peg . buildParser ( 'start "start" = "a"' ) ;
var parser = peg . generate ( 'start "start" = "a"' ) ;
expect ( parser ) . toFailToParse ( "b" , {
expected : [ { type : "other" , description : "start" } ]
@ -224,34 +224,34 @@ describe("generated parser behavior", function() {
describe ( "literal" , function ( ) {
describe ( "matching" , function ( ) {
it ( "matches empty literals" , function ( ) {
var parser = peg . buildParser ( 'start = ""' , options ) ;
var parser = peg . generate ( 'start = ""' , options ) ;
expect ( parser ) . toParse ( "" ) ;
} ) ;
it ( "matches one-character literals" , function ( ) {
var parser = peg . buildParser ( 'start = "a"' , options ) ;
var parser = peg . generate ( 'start = "a"' , options ) ;
expect ( parser ) . toParse ( "a" ) ;
expect ( parser ) . toFailToParse ( "b" ) ;
} ) ;
it ( "matches multi-character literals" , function ( ) {
var parser = peg . buildParser ( 'start = "abcd"' , options ) ;
var parser = peg . generate ( 'start = "abcd"' , options ) ;
expect ( parser ) . toParse ( "abcd" ) ;
expect ( parser ) . toFailToParse ( "efgh" ) ;
} ) ;
it ( "is case sensitive without the \"i\" flag" , function ( ) {
var parser = peg . buildParser ( 'start = "a"' , options ) ;
var parser = peg . generate ( 'start = "a"' , options ) ;
expect ( parser ) . toParse ( "a" ) ;
expect ( parser ) . toFailToParse ( "A" ) ;
} ) ;
it ( "is case insensitive with the \"i\" flag" , function ( ) {
var parser = peg . buildParser ( 'start = "a"i' , options ) ;
var parser = peg . generate ( 'start = "a"i' , options ) ;
expect ( parser ) . toParse ( "a" ) ;
expect ( parser ) . toParse ( "A" ) ;
@ -260,13 +260,13 @@ describe("generated parser behavior", function() {
describe ( "when it matches" , function ( ) {
it ( "returns the matched text" , function ( ) {
var parser = peg . buildParser ( 'start = "a"' , options ) ;
var parser = peg . generate ( 'start = "a"' , options ) ;
expect ( parser ) . toParse ( "a" , "a" ) ;
} ) ;
it ( "consumes the matched text" , function ( ) {
var parser = peg . buildParser ( 'start = "a" .' , options ) ;
var parser = peg . generate ( 'start = "a" .' , options ) ;
expect ( parser ) . toParse ( "ab" ) ;
} ) ;
@ -274,7 +274,7 @@ describe("generated parser behavior", function() {
describe ( "when it doesn't match" , function ( ) {
it ( "reports match failure and records an expectation of type \"literal\"" , function ( ) {
var parser = peg . buildParser ( 'start = "a"' , options ) ;
var parser = peg . generate ( 'start = "a"' , options ) ;
expect ( parser ) . toFailToParse ( "b" , {
expected : [ { type : "literal" , value : "a" , description : '"a"' } ]
@ -286,13 +286,13 @@ describe("generated parser behavior", function() {
describe ( "character class" , function ( ) {
describe ( "matching" , function ( ) {
it ( "matches empty classes" , function ( ) {
var parser = peg . buildParser ( 'start = []' , options ) ;
var parser = peg . generate ( 'start = []' , options ) ;
expect ( parser ) . toFailToParse ( "a" ) ;
} ) ;
it ( "matches classes with a character list" , function ( ) {
var parser = peg . buildParser ( 'start = [abc]' , options ) ;
var parser = peg . generate ( 'start = [abc]' , options ) ;
expect ( parser ) . toParse ( "a" ) ;
expect ( parser ) . toParse ( "b" ) ;
@ -301,7 +301,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "matches classes with a character range" , function ( ) {
var parser = peg . buildParser ( 'start = [a-c]' , options ) ;
var parser = peg . generate ( 'start = [a-c]' , options ) ;
expect ( parser ) . toParse ( "a" ) ;
expect ( parser ) . toParse ( "b" ) ;
@ -310,21 +310,21 @@ describe("generated parser behavior", function() {
} ) ;
it ( "matches inverted classes" , function ( ) {
var parser = peg . buildParser ( 'start = [^a]' , options ) ;
var parser = peg . generate ( 'start = [^a]' , options ) ;
expect ( parser ) . toFailToParse ( "a" ) ;
expect ( parser ) . toParse ( "b" ) ;
} ) ;
it ( "is case sensitive without the \"i\" flag" , function ( ) {
var parser = peg . buildParser ( 'start = [a]' , options ) ;
var parser = peg . generate ( 'start = [a]' , options ) ;
expect ( parser ) . toParse ( "a" ) ;
expect ( parser ) . toFailToParse ( "A" ) ;
} ) ;
it ( "is case insensitive with the \"i\" flag" , function ( ) {
var parser = peg . buildParser ( 'start = [a]i' , options ) ;
var parser = peg . generate ( 'start = [a]i' , options ) ;
expect ( parser ) . toParse ( "a" ) ;
expect ( parser ) . toParse ( "A" ) ;
@ -333,13 +333,13 @@ describe("generated parser behavior", function() {
describe ( "when it matches" , function ( ) {
it ( "returns the matched character" , function ( ) {
var parser = peg . buildParser ( 'start = [a]' , options ) ;
var parser = peg . generate ( 'start = [a]' , options ) ;
expect ( parser ) . toParse ( "a" , "a" ) ;
} ) ;
it ( "consumes the matched character" , function ( ) {
var parser = peg . buildParser ( 'start = [a] .' , options ) ;
var parser = peg . generate ( 'start = [a] .' , options ) ;
expect ( parser ) . toParse ( "ab" ) ;
} ) ;
@ -347,7 +347,7 @@ describe("generated parser behavior", function() {
describe ( "when it doesn't match" , function ( ) {
it ( "reports match failure and records an expectation of type \"class\"" , function ( ) {
var parser = peg . buildParser ( 'start = [a]' , options ) ;
var parser = peg . generate ( 'start = [a]' , options ) ;
expect ( parser ) . toFailToParse ( "b" , {
expected : [ { type : "class" , value : "[a]" , description : "[a]" } ]
@ -359,7 +359,7 @@ describe("generated parser behavior", function() {
describe ( "dot" , function ( ) {
describe ( "matching" , function ( ) {
it ( "matches any character" , function ( ) {
var parser = peg . buildParser ( 'start = .' , options ) ;
var parser = peg . generate ( 'start = .' , options ) ;
expect ( parser ) . toParse ( "a" ) ;
expect ( parser ) . toParse ( "b" ) ;
@ -369,13 +369,13 @@ describe("generated parser behavior", function() {
describe ( "when it matches" , function ( ) {
it ( "returns the matched character" , function ( ) {
var parser = peg . buildParser ( 'start = .' , options ) ;
var parser = peg . generate ( 'start = .' , options ) ;
expect ( parser ) . toParse ( "a" , "a" ) ;
} ) ;
it ( "consumes the matched character" , function ( ) {
var parser = peg . buildParser ( 'start = . .' , options ) ;
var parser = peg . generate ( 'start = . .' , options ) ;
expect ( parser ) . toParse ( "ab" ) ;
} ) ;
@ -383,7 +383,7 @@ describe("generated parser behavior", function() {
describe ( "when it doesn't match" , function ( ) {
it ( "reports match failure and records an expectation of type \"any\"" , function ( ) {
var parser = peg . buildParser ( 'start = .' , options ) ;
var parser = peg . generate ( 'start = .' , options ) ;
expect ( parser ) . toFailToParse ( "" , {
expected : [ { type : "any" , description : "any character" } ]
@ -395,7 +395,7 @@ describe("generated parser behavior", function() {
describe ( "rule reference" , function ( ) {
describe ( "when referenced rule's expression matches" , function ( ) {
it ( "returns its result" , function ( ) {
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'start = a' ,
'a = "a"'
] . join ( "\n" ) , options ) ;
@ -406,7 +406,7 @@ describe("generated parser behavior", function() {
describe ( "when referenced rule's expression doesn't match" , function ( ) {
it ( "reports match failure" , function ( ) {
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'start = a' ,
'a = "a"'
] . join ( "\n" ) , options ) ;
@ -424,7 +424,7 @@ describe("generated parser behavior", function() {
* | undefined | which we can ' t compare against in | toParse | due to the
* way optional parameters work .
* /
var parser = peg . buildParser ( 'start = &{ return true; } ""' , options ) ;
var parser = peg . generate ( 'start = &{ return true; } ""' , options ) ;
expect ( parser ) . toParse ( "" , [ undefined , "" ] ) ;
} ) ;
@ -432,7 +432,7 @@ describe("generated parser behavior", function() {
describe ( "when the code returns a falsey value" , function ( ) {
it ( "reports match failure" , function ( ) {
var parser = peg . buildParser ( 'start = &{ return false; }' , options ) ;
var parser = peg . generate ( 'start = &{ return false; }' , options ) ;
expect ( parser ) . toFailToParse ( "" ) ;
} ) ;
@ -441,7 +441,7 @@ describe("generated parser behavior", function() {
describe ( "label variables" , function ( ) {
describe ( "in containing sequence" , function ( ) {
it ( "can access variables defined by preceding labeled elements" , function ( ) {
var parser = peg . buildParser (
var parser = peg . generate (
'start = a:"a" &{ return a === "a"; }' ,
options
) ;
@ -450,7 +450,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "cannot access variable defined by labeled predicate element" , function ( ) {
var parser = peg . buildParser (
var parser = peg . generate (
'start = "a" b:&{ return b === undefined; } "c"' ,
options
) ;
@ -459,7 +459,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "cannot access variables defined by following labeled elements" , function ( ) {
var parser = peg . buildParser (
var parser = peg . generate (
'start = &{ return a === "a"; } a:"a"' ,
options
) ;
@ -517,7 +517,7 @@ describe("generated parser behavior", function() {
parser , i ;
for ( i = 0 ; i < testcases . length ; i ++ ) {
parser = peg . buildParser ( testcases [ i ] . grammar , options ) ;
parser = peg . generate ( testcases [ i ] . grammar , options ) ;
expect ( parser ) . toFailToParse ( testcases [ i ] . input ) ;
}
} ) ;
@ -525,7 +525,7 @@ describe("generated parser behavior", function() {
describe ( "in outer sequence" , function ( ) {
it ( "can access variables defined by preceding labeled elements" , function ( ) {
var parser = peg . buildParser (
var parser = peg . generate (
'start = a:"a" ("b" &{ return a === "a"; })' ,
options
) ;
@ -534,7 +534,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "cannot access variable defined by labeled predicate element" , function ( ) {
var parser = peg . buildParser (
var parser = peg . generate (
'start = "a" b:("b" &{ return b === undefined; }) "c"' ,
options
) ;
@ -543,7 +543,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "cannot access variables defined by following labeled elements" , function ( ) {
var parser = peg . buildParser (
var parser = peg . generate (
'start = ("a" &{ return b === "b"; }) b:"b"' ,
options
) ;
@ -555,7 +555,7 @@ describe("generated parser behavior", function() {
describe ( "initializer variables & functions" , function ( ) {
it ( "can access variables defined in the initializer" , function ( ) {
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'{ var v = 42 }' ,
'start = &{ return v === 42; }'
] . join ( "\n" ) , options ) ;
@ -564,7 +564,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "can access functions defined in the initializer" , function ( ) {
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'{ function f() { return 42; } }' ,
'start = &{ return f() === 42; }'
] . join ( "\n" ) , options ) ;
@ -575,7 +575,7 @@ describe("generated parser behavior", function() {
describe ( "available variables & functions" , function ( ) {
it ( "|parser| contains the parser object" , function ( ) {
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'{ var result; }' ,
'start = &{ result = parser; return true; } { return result; }'
] . join ( "\n" ) , options ) ;
@ -584,7 +584,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "|options| contains options" , function ( ) {
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'{ var result; }' ,
'start = &{ result = options; return true; } { return result; }'
] . join ( "\n" ) , options ) ;
@ -593,7 +593,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "|location| returns current location info" , function ( ) {
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'{ var result; }' ,
'start = line (nl+ line)* { return result; }' ,
'line = thing (" "+ thing)*' ,
@ -629,7 +629,7 @@ describe("generated parser behavior", function() {
* | undefined | which we can ' t compare against in | toParse | due to the
* way optional parameters work .
* /
var parser = peg . buildParser ( 'start = !{ return false; } ""' , options ) ;
var parser = peg . generate ( 'start = !{ return false; } ""' , options ) ;
expect ( parser ) . toParse ( "" , [ undefined , "" ] ) ;
} ) ;
@ -637,7 +637,7 @@ describe("generated parser behavior", function() {
describe ( "when the code returns a truthy value" , function ( ) {
it ( "reports match failure" , function ( ) {
var parser = peg . buildParser ( 'start = !{ return true; }' , options ) ;
var parser = peg . generate ( 'start = !{ return true; }' , options ) ;
expect ( parser ) . toFailToParse ( "" ) ;
} ) ;
@ -646,7 +646,7 @@ describe("generated parser behavior", function() {
describe ( "label variables" , function ( ) {
describe ( "in containing sequence" , function ( ) {
it ( "can access variables defined by preceding labeled elements" , function ( ) {
var parser = peg . buildParser (
var parser = peg . generate (
'start = a:"a" !{ return a !== "a"; }' ,
options
) ;
@ -655,7 +655,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "cannot access variable defined by labeled predicate element" , function ( ) {
var parser = peg . buildParser (
var parser = peg . generate (
'start = "a" b:!{ return b !== undefined; } "c"' ,
options
) ;
@ -664,7 +664,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "cannot access variables defined by following labeled elements" , function ( ) {
var parser = peg . buildParser (
var parser = peg . generate (
'start = !{ return a !== "a"; } a:"a"' ,
options
) ;
@ -722,7 +722,7 @@ describe("generated parser behavior", function() {
parser , i ;
for ( i = 0 ; i < testcases . length ; i ++ ) {
parser = peg . buildParser ( testcases [ i ] . grammar , options ) ;
parser = peg . generate ( testcases [ i ] . grammar , options ) ;
expect ( parser ) . toFailToParse ( testcases [ i ] . input ) ;
}
} ) ;
@ -730,7 +730,7 @@ describe("generated parser behavior", function() {
describe ( "in outer sequence" , function ( ) {
it ( "can access variables defined by preceding labeled elements" , function ( ) {
var parser = peg . buildParser (
var parser = peg . generate (
'start = a:"a" ("b" !{ return a !== "a"; })' ,
options
) ;
@ -739,7 +739,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "cannot access variable defined by labeled predicate element" , function ( ) {
var parser = peg . buildParser (
var parser = peg . generate (
'start = "a" b:("b" !{ return b !== undefined; }) "c"' ,
options
) ;
@ -748,7 +748,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "cannot access variables defined by following labeled elements" , function ( ) {
var parser = peg . buildParser (
var parser = peg . generate (
'start = ("a" !{ return b !== "b"; }) b:"b"' ,
options
) ;
@ -760,7 +760,7 @@ describe("generated parser behavior", function() {
describe ( "initializer variables & functions" , function ( ) {
it ( "can access variables defined in the initializer" , function ( ) {
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'{ var v = 42 }' ,
'start = !{ return v !== 42; }'
] . join ( "\n" ) , options ) ;
@ -769,7 +769,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "can access functions defined in the initializer" , function ( ) {
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'{ function f() { return 42; } }' ,
'start = !{ return f() !== 42; }'
] . join ( "\n" ) , options ) ;
@ -780,7 +780,7 @@ describe("generated parser behavior", function() {
describe ( "available variables & functions" , function ( ) {
it ( "|parser| contains the parser object" , function ( ) {
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'{ var result; }' ,
'start = !{ result = parser; return false; } { return result; }'
] . join ( "\n" ) , options ) ;
@ -789,7 +789,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "|options| contains options" , function ( ) {
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'{ var result; }' ,
'start = !{ result = options; return false; } { return result; }'
] . join ( "\n" ) , options ) ;
@ -798,7 +798,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "|location| returns current location info" , function ( ) {
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'{ var result; }' ,
'start = line (nl+ line)* { return result; }' ,
'line = thing (" "+ thing)*' ,
@ -829,7 +829,7 @@ describe("generated parser behavior", function() {
describe ( "group" , function ( ) {
describe ( "when the expression matches" , function ( ) {
it ( "returns its match result" , function ( ) {
var parser = peg . buildParser ( 'start = ("a")' , options ) ;
var parser = peg . generate ( 'start = ("a")' , options ) ;
expect ( parser ) . toParse ( "a" , "a" ) ;
} ) ;
@ -837,7 +837,7 @@ describe("generated parser behavior", function() {
describe ( "when the expression doesn't match" , function ( ) {
it ( "reports match failure" , function ( ) {
var parser = peg . buildParser ( 'start = ("a")' , options ) ;
var parser = peg . generate ( 'start = ("a")' , options ) ;
expect ( parser ) . toFailToParse ( "b" ) ;
} ) ;
@ -847,7 +847,7 @@ describe("generated parser behavior", function() {
describe ( "optional" , function ( ) {
describe ( "when the expression matches" , function ( ) {
it ( "returns its match result" , function ( ) {
var parser = peg . buildParser ( 'start = "a"?' , options ) ;
var parser = peg . generate ( 'start = "a"?' , options ) ;
expect ( parser ) . toParse ( "a" , "a" ) ;
} ) ;
@ -855,7 +855,7 @@ describe("generated parser behavior", function() {
describe ( "when the expression doesn't match" , function ( ) {
it ( "returns |null|" , function ( ) {
var parser = peg . buildParser ( 'start = "a"?' , options ) ;
var parser = peg . generate ( 'start = "a"?' , options ) ;
expect ( parser ) . toParse ( "" , null ) ;
} ) ;
@ -865,7 +865,7 @@ describe("generated parser behavior", function() {
describe ( "zero or more" , function ( ) {
describe ( "when the expression matches zero or more times" , function ( ) {
it ( "returns an array of its match results" , function ( ) {
var parser = peg . buildParser ( 'start = "a"*' , options ) ;
var parser = peg . generate ( 'start = "a"*' , options ) ;
expect ( parser ) . toParse ( "" , [ ] ) ;
expect ( parser ) . toParse ( "a" , [ "a" ] ) ;
@ -877,7 +877,7 @@ describe("generated parser behavior", function() {
describe ( "one or more" , function ( ) {
describe ( "when the expression matches one or more times" , function ( ) {
it ( "returns an array of its match results" , function ( ) {
var parser = peg . buildParser ( 'start = "a"+' , options ) ;
var parser = peg . generate ( 'start = "a"+' , options ) ;
expect ( parser ) . toParse ( "a" , [ "a" ] ) ;
expect ( parser ) . toParse ( "aaa" , [ "a" , "a" , "a" ] ) ;
@ -886,7 +886,7 @@ describe("generated parser behavior", function() {
describe ( "when the expression doesn't match" , function ( ) {
it ( "reports match failure" , function ( ) {
var parser = peg . buildParser ( 'start = "a"+' , options ) ;
var parser = peg . generate ( 'start = "a"+' , options ) ;
expect ( parser ) . toFailToParse ( "" ) ;
} ) ;
@ -896,7 +896,7 @@ describe("generated parser behavior", function() {
describe ( "text" , function ( ) {
describe ( "when the expression matches" , function ( ) {
it ( "returns the matched text" , function ( ) {
var parser = peg . buildParser ( 'start = $("a" "b" "c")' , options ) ;
var parser = peg . generate ( 'start = $("a" "b" "c")' , options ) ;
expect ( parser ) . toParse ( "abc" , "abc" ) ;
} ) ;
@ -904,7 +904,7 @@ describe("generated parser behavior", function() {
describe ( "when the expression doesn't match" , function ( ) {
it ( "reports match failure" , function ( ) {
var parser = peg . buildParser ( 'start = $("a")' , options ) ;
var parser = peg . generate ( 'start = $("a")' , options ) ;
expect ( parser ) . toFailToParse ( "b" ) ;
} ) ;
@ -914,13 +914,13 @@ describe("generated parser behavior", function() {
describe ( "positive simple predicate" , function ( ) {
describe ( "when the expression matches" , function ( ) {
it ( "returns |undefined|" , function ( ) {
var parser = peg . buildParser ( 'start = &"a" "a"' , options ) ;
var parser = peg . generate ( 'start = &"a" "a"' , options ) ;
expect ( parser ) . toParse ( "a" , [ undefined , "a" ] ) ;
} ) ;
it ( "resets parse position" , function ( ) {
var parser = peg . buildParser ( 'start = &"a" "a"' , options ) ;
var parser = peg . generate ( 'start = &"a" "a"' , options ) ;
expect ( parser ) . toParse ( "a" ) ;
} ) ;
@ -928,13 +928,13 @@ describe("generated parser behavior", function() {
describe ( "when the expression doesn't match" , function ( ) {
it ( "reports match failure" , function ( ) {
var parser = peg . buildParser ( 'start = &"a"' , options ) ;
var parser = peg . generate ( 'start = &"a"' , options ) ;
expect ( parser ) . toFailToParse ( "b" ) ;
} ) ;
it ( "discards any expectations recorded when matching the expression" , function ( ) {
var parser = peg . buildParser ( 'start = "a" / &"b" / "c"' , options ) ;
var parser = peg . generate ( 'start = "a" / &"b" / "c"' , options ) ;
expect ( parser ) . toFailToParse ( "d" , {
expected : [
@ -949,7 +949,7 @@ describe("generated parser behavior", function() {
describe ( "negative simple predicate" , function ( ) {
describe ( "when the expression matches" , function ( ) {
it ( "reports match failure" , function ( ) {
var parser = peg . buildParser ( 'start = !"a"' , options ) ;
var parser = peg . generate ( 'start = !"a"' , options ) ;
expect ( parser ) . toFailToParse ( "a" ) ;
} ) ;
@ -957,19 +957,19 @@ describe("generated parser behavior", function() {
describe ( "when the expression doesn't match" , function ( ) {
it ( "returns |undefined|" , function ( ) {
var parser = peg . buildParser ( 'start = !"a" "b"' , options ) ;
var parser = peg . generate ( 'start = !"a" "b"' , options ) ;
expect ( parser ) . toParse ( "b" , [ undefined , "b" ] ) ;
} ) ;
it ( "resets parse position" , function ( ) {
var parser = peg . buildParser ( 'start = !"a" "b"' , options ) ;
var parser = peg . generate ( 'start = !"a" "b"' , options ) ;
expect ( parser ) . toParse ( "b" ) ;
} ) ;
it ( "discards any expectations recorded when matching the expression" , function ( ) {
var parser = peg . buildParser ( 'start = "a" / !"b" / "c"' , options ) ;
var parser = peg . generate ( 'start = "a" / !"b" / "c"' , options ) ;
expect ( parser ) . toFailToParse ( "b" , {
expected : [
@ -984,7 +984,7 @@ describe("generated parser behavior", function() {
describe ( "label" , function ( ) {
describe ( "when the expression matches" , function ( ) {
it ( "returns its match result" , function ( ) {
var parser = peg . buildParser ( 'start = a:"a"' , options ) ;
var parser = peg . generate ( 'start = a:"a"' , options ) ;
expect ( parser ) . toParse ( "a" , "a" ) ;
} ) ;
@ -992,7 +992,7 @@ describe("generated parser behavior", function() {
describe ( "when the expression doesn't match" , function ( ) {
it ( "reports match failure" , function ( ) {
var parser = peg . buildParser ( 'start = a:"a"' , options ) ;
var parser = peg . generate ( 'start = a:"a"' , options ) ;
expect ( parser ) . toFailToParse ( "b" ) ;
} ) ;
@ -1002,7 +1002,7 @@ describe("generated parser behavior", function() {
describe ( "sequence" , function ( ) {
describe ( "when all expressions match" , function ( ) {
it ( "returns an array of their match results" , function ( ) {
var parser = peg . buildParser ( 'start = "a" "b" "c"' , options ) ;
var parser = peg . generate ( 'start = "a" "b" "c"' , options ) ;
expect ( parser ) . toParse ( "abc" , [ "a" , "b" , "c" ] ) ;
} ) ;
@ -1010,7 +1010,7 @@ describe("generated parser behavior", function() {
describe ( "when any expression doesn't match" , function ( ) {
it ( "reports match failure" , function ( ) {
var parser = peg . buildParser ( 'start = "a" "b" "c"' , options ) ;
var parser = peg . generate ( 'start = "a" "b" "c"' , options ) ;
expect ( parser ) . toFailToParse ( "dbc" ) ;
expect ( parser ) . toFailToParse ( "adc" ) ;
@ -1018,7 +1018,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "resets parse position" , function ( ) {
var parser = peg . buildParser ( 'start = "a" "b" / "a"' , options ) ;
var parser = peg . generate ( 'start = "a" "b" / "a"' , options ) ;
expect ( parser ) . toParse ( "a" , "a" ) ;
} ) ;
@ -1028,7 +1028,7 @@ describe("generated parser behavior", function() {
describe ( "action" , function ( ) {
describe ( "when the expression matches" , function ( ) {
it ( "returns the value returned by the code" , function ( ) {
var parser = peg . buildParser ( 'start = "a" { return 42; }' , options ) ;
var parser = peg . generate ( 'start = "a" { return 42; }' , options ) ;
expect ( parser ) . toParse ( "a" , 42 ) ;
} ) ;
@ -1036,13 +1036,13 @@ describe("generated parser behavior", function() {
describe ( "label variables" , function ( ) {
describe ( "in the expression" , function ( ) {
it ( "can access variable defined by labeled expression" , function ( ) {
var parser = peg . buildParser ( 'start = a:"a" { return a; }' , options ) ;
var parser = peg . generate ( 'start = a:"a" { return a; }' , options ) ;
expect ( parser ) . toParse ( "a" , "a" ) ;
} ) ;
it ( "can access variables defined by labeled sequence elements" , function ( ) {
var parser = peg . buildParser (
var parser = peg . generate (
'start = a:"a" b:"b" c:"c" { return [a, b, c]; }' ,
options
) ;
@ -1100,7 +1100,7 @@ describe("generated parser behavior", function() {
parser , i ;
for ( i = 0 ; i < testcases . length ; i ++ ) {
parser = peg . buildParser ( testcases [ i ] . grammar , options ) ;
parser = peg . generate ( testcases [ i ] . grammar , options ) ;
expect ( parser ) . toFailToParse ( testcases [ i ] . input ) ;
}
} ) ;
@ -1108,7 +1108,7 @@ describe("generated parser behavior", function() {
describe ( "in outer sequence" , function ( ) {
it ( "can access variables defined by preceding labeled elements" , function ( ) {
var parser = peg . buildParser (
var parser = peg . generate (
'start = a:"a" ("b" { return a; })' ,
options
) ;
@ -1117,7 +1117,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "cannot access variable defined by labeled action element" , function ( ) {
var parser = peg . buildParser (
var parser = peg . generate (
'start = "a" b:("b" { return b; }) c:"c"' ,
options
) ;
@ -1126,7 +1126,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "cannot access variables defined by following labeled elements" , function ( ) {
var parser = peg . buildParser (
var parser = peg . generate (
'start = ("a" { return b; }) b:"b"' ,
options
) ;
@ -1138,7 +1138,7 @@ describe("generated parser behavior", function() {
describe ( "initializer variables & functions" , function ( ) {
it ( "can access variables defined in the initializer" , function ( ) {
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'{ var v = 42 }' ,
'start = "a" { return v; }'
] . join ( "\n" ) , options ) ;
@ -1147,7 +1147,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "can access functions defined in the initializer" , function ( ) {
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'{ function f() { return 42; } }' ,
'start = "a" { return f(); }'
] . join ( "\n" ) , options ) ;
@ -1158,7 +1158,7 @@ describe("generated parser behavior", function() {
describe ( "available variables & functions" , function ( ) {
it ( "|parser| contains the parser object" , function ( ) {
var parser = peg . buildParser (
var parser = peg . generate (
'start = "a" { return parser; }' ,
options
) ;
@ -1167,7 +1167,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "|options| contains options" , function ( ) {
var parser = peg . buildParser (
var parser = peg . generate (
'start = "a" { return options; }' ,
options
) ;
@ -1176,7 +1176,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "|text| returns text matched by the expression" , function ( ) {
var parser = peg . buildParser (
var parser = peg . generate (
'start = "a" { return text(); }' ,
options
) ;
@ -1185,7 +1185,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "|location| returns location info of the expression" , function ( ) {
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'{ var result; }' ,
'start = line (nl+ line)* { return result; }' ,
'line = thing (" "+ thing)*' ,
@ -1213,7 +1213,7 @@ describe("generated parser behavior", function() {
describe ( "|expected|" , function ( ) {
it ( "terminates parsing and throws an exception" , function ( ) {
var parser = peg . buildParser (
var parser = peg . generate (
'start = "a" { expected("a"); }' ,
options
) ;
@ -1229,7 +1229,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "allows to set custom location info" , function ( ) {
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'start = "a" {' ,
' expected("a", {' ,
' start: { offset: 1, line: 1, column: 2 },' ,
@ -1251,7 +1251,7 @@ describe("generated parser behavior", function() {
describe ( "|error|" , function ( ) {
it ( "terminates parsing and throws an exception" , function ( ) {
var parser = peg . buildParser (
var parser = peg . generate (
'start = "a" { error("a"); }' ,
options
) ;
@ -1267,7 +1267,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "allows to set custom location info" , function ( ) {
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'start = "a" {' ,
' error("a", {' ,
' start: { offset: 1, line: 1, column: 2 },' ,
@ -1291,13 +1291,13 @@ describe("generated parser behavior", function() {
describe ( "when the expression doesn't match" , function ( ) {
it ( "reports match failure" , function ( ) {
var parser = peg . buildParser ( 'start = "a" { return 42; }' , options ) ;
var parser = peg . generate ( 'start = "a" { return 42; }' , options ) ;
expect ( parser ) . toFailToParse ( "b" ) ;
} ) ;
it ( "doesn't execute the code" , function ( ) {
var parser = peg . buildParser (
var parser = peg . generate (
'start = "a" { throw "Boom!"; } / "b"' ,
options
) ;
@ -1310,7 +1310,7 @@ describe("generated parser behavior", function() {
describe ( "choice" , function ( ) {
describe ( "when any expression matches" , function ( ) {
it ( "returns its match result" , function ( ) {
var parser = peg . buildParser ( 'start = "a" / "b" / "c"' , options ) ;
var parser = peg . generate ( 'start = "a" / "b" / "c"' , options ) ;
expect ( parser ) . toParse ( "a" , "a" ) ;
expect ( parser ) . toParse ( "b" , "b" ) ;
@ -1320,7 +1320,7 @@ describe("generated parser behavior", function() {
describe ( "when all expressions don't match" , function ( ) {
it ( "reports match failure" , function ( ) {
var parser = peg . buildParser ( 'start = "a" / "b" / "c"' , options ) ;
var parser = peg . generate ( 'start = "a" / "b" / "c"' , options ) ;
expect ( parser ) . toFailToParse ( "d" ) ;
} ) ;
@ -1330,7 +1330,7 @@ describe("generated parser behavior", function() {
describe ( "error reporting" , function ( ) {
describe ( "behavior" , function ( ) {
it ( "reports only the rightmost error" , function ( ) {
var parser = peg . buildParser ( 'start = "a" "b" / "a" "c" "d"' , options ) ;
var parser = peg . generate ( 'start = "a" "b" / "a" "c" "d"' , options ) ;
expect ( parser ) . toFailToParse ( "ace" , {
expected : [ { type : "literal" , value : "d" , description : '"d"' } ]
@ -1340,7 +1340,7 @@ describe("generated parser behavior", function() {
describe ( "expectations reporting" , function ( ) {
it ( "reports expectations correctly with no alternative" , function ( ) {
var parser = peg . buildParser ( 'start = "a"' , options ) ;
var parser = peg . generate ( 'start = "a"' , options ) ;
expect ( parser ) . toFailToParse ( "ab" , {
expected : [ { type : "end" , description : "end of input" } ]
@ -1348,7 +1348,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "reports expectations correctly with one alternative" , function ( ) {
var parser = peg . buildParser ( 'start = "a"' , options ) ;
var parser = peg . generate ( 'start = "a"' , options ) ;
expect ( parser ) . toFailToParse ( "b" , {
expected : [ { type : "literal" , value : "a" , description : '"a"' } ]
@ -1356,7 +1356,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "reports expectations correctly with multiple alternatives" , function ( ) {
var parser = peg . buildParser ( 'start = "a" / "b" / "c"' , options ) ;
var parser = peg . generate ( 'start = "a" / "b" / "c"' , options ) ;
expect ( parser ) . toFailToParse ( "d" , {
expected : [
@ -1375,7 +1375,7 @@ describe("generated parser behavior", function() {
*
* See https : //github.com/pegjs/pegjs/pull/146.
* /
var parser = peg . buildParser ( 'start = "a" / "a" / "a"' , options ) ;
var parser = peg . generate ( 'start = "a" / "a" / "a"' , options ) ;
expect ( parser ) . toFailToParse ( "b" , {
expected : [ { type : "literal" , value : "a" , description : '"a"' } ]
@ -1383,7 +1383,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "sorts expectations" , function ( ) {
var parser = peg . buildParser ( 'start = "c" / "b" / "a"' , options ) ;
var parser = peg . generate ( 'start = "c" / "b" / "a"' , options ) ;
expect ( parser ) . toFailToParse ( "d" , {
expected : [
@ -1397,7 +1397,7 @@ describe("generated parser behavior", function() {
describe ( "message building" , function ( ) {
it ( "builds message correctly with no alternative" , function ( ) {
var parser = peg . buildParser ( 'start = "a"' , options ) ;
var parser = peg . generate ( 'start = "a"' , options ) ;
expect ( parser ) . toFailToParse ( "ab" , {
message : 'Expected end of input.'
@ -1405,7 +1405,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "builds message correctly with one alternative" , function ( ) {
var parser = peg . buildParser ( 'start = "a"' , options ) ;
var parser = peg . generate ( 'start = "a"' , options ) ;
expect ( parser ) . toFailToParse ( "b" , {
message : 'Expected "a".'
@ -1413,7 +1413,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "builds message correctly with multiple alternatives" , function ( ) {
var parser = peg . buildParser ( 'start = "a" / "b" / "c"' , options ) ;
var parser = peg . generate ( 'start = "a" / "b" / "c"' , options ) ;
expect ( parser ) . toFailToParse ( "d" , {
message : 'Expected "a", "b" or "c".'
@ -1423,7 +1423,7 @@ describe("generated parser behavior", function() {
describe ( "position reporting" , function ( ) {
it ( "reports position correctly with no trailing input" , function ( ) {
var parser = peg . buildParser ( 'start = "a"' , options ) ;
var parser = peg . generate ( 'start = "a"' , options ) ;
expect ( parser ) . toFailToParse ( "b" , {
location : {
@ -1434,7 +1434,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "reports position correctly with trailing input" , function ( ) {
var parser = peg . buildParser ( 'start = "a"' , options ) ;
var parser = peg . generate ( 'start = "a"' , options ) ;
expect ( parser ) . toFailToParse ( "aa" , {
location : {
@ -1445,7 +1445,7 @@ describe("generated parser behavior", function() {
} ) ;
it ( "reports position correctly in complex cases" , function ( ) {
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'start = line (nl+ line)*' ,
'line = digit (" "+ digit)*' ,
'digit = [0-9]' ,
@ -1488,7 +1488,7 @@ describe("generated parser behavior", function() {
* Sum ← Product ( ( '+' / '-' ) Product ) *
* Expr ← Sum
* /
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'Expr = Sum' ,
'Sum = first:Product rest:(("+" / "-") Product)* {' ,
' var result = first, i;' ,
@ -1544,7 +1544,7 @@ describe("generated parser behavior", function() {
* A ← a A ? b
* B ← b B ? c
* /
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'S = &(A "c") a:"a"+ B:B !("a" / "b" / "c") { return a.join("") + B; }' ,
'A = a:"a" A:A? b:"b" { return [a, A, b].join(""); }' ,
'B = b:"b" B:B? c:"c" { return [b, B, c].join(""); }'
@ -1568,7 +1568,7 @@ describe("generated parser behavior", function() {
* N ← C / ( ! Begin ! End Z )
* Z ← any single character
* /
var parser = peg . buildParser ( [
var parser = peg . generate ( [
'C = begin:Begin ns:N* end:End { return begin + ns.join("") + end; }' ,
'N = C' ,
' / !Begin !End z:Z { return z; }' ,