@ -558,8 +558,8 @@ PEG.compiler.passes.generateCode = function(ast, options) {
' }' ,
' ' ,
' #end' ,
' #if node.re sul tCount > 0',
' var #{map(range(node.re sul tCount), resultVa r).join(", ")};',
' #if node.re gi ster Count > 0',
' var #{map(range(node.re gi ster Count), r).join(", ")};',
' #end' ,
' ' ,
' #block emit(node.expression)' ,
@ -567,17 +567,17 @@ PEG.compiler.passes.generateCode = function(ast, options) {
' ' ,
' cache[cacheKey] = {' ,
' nextPos: #{posClone("pos")},' ,
' result: #{r esultVar (node.resultIndex)}',
' result: #{r (node.resultIndex)}',
' };' ,
' #end' ,
' return #{r esultVar (node.resultIndex)};',
' return #{r (node.resultIndex)};',
'}'
] ,
named : [
'reportFailures++;' ,
'#block emit(node.expression)' ,
'reportFailures--;' ,
'if (reportFailures === 0 && #{r esultVar (node.resultIndex)} === null) {',
'if (reportFailures === 0 && #{r (node.resultIndex)} === null) {',
' matchFailed(#{string(node.name)});' ,
'}'
] ,
@ -586,17 +586,17 @@ PEG.compiler.passes.generateCode = function(ast, options) {
'#block nextAlternativesCode'
] ,
"choice.next" : [
'if (#{r esultVar (node.resultIndex)} === null) {',
'if (#{r (node.resultIndex)} === null) {',
' #block code' ,
'}'
] ,
action : [
'#{posSave(node)};' ,
'#block emit(node.expression)' ,
'if (#{r esultVar (node.resultIndex)} !== null) {',
' #{r esultVar (node.resultIndex)} = (function(#{(options.trackLineAndColumn ? ["offset", "line", "column"] : ["offset"]).concat(keys(node.params)).join(", ")}) {#{node.code}})(#{(options.trackLineAndColumn ? [resultVar (node.posIndex) + ".offset", resultVa r(node.posIndex) + ".line", resultVar (node.posIndex) + ".column"] : [resultVa r(node.posIndex)]).concat(map(values(node.params), param)).join(", ")});',
'if (#{r (node.resultIndex)} !== null) {',
' #{r (node.resultIndex)} = (function(#{(options.trackLineAndColumn ? ["offset", "line", "column"] : ["offset"]).concat(keys(node.params)).join(", ")}) {#{node.code}})(#{(options.trackLineAndColumn ? [r(node.posIndex) + ".offset", r(node.posIndex) + ".line", r(node.posIndex) + ".column"] : [r(node.posIndex)]).concat(map(values(node.params), param)).join(", ")});',
'}' ,
'if (#{r esultVar (node.resultIndex)} === null) {',
'if (#{r (node.resultIndex)} === null) {',
' #{posRestore(node)};' ,
'}'
] ,
@ -606,26 +606,26 @@ PEG.compiler.passes.generateCode = function(ast, options) {
] ,
"sequence.iteration" : [
'#block emit(element)' ,
'if (#{r esultVar (element.resultIndex)} !== null) {',
'if (#{r (element.resultIndex)} !== null) {',
' #block code' ,
'} else {' ,
' #{r esultVar (node.resultIndex)} = null;',
' #{r (node.resultIndex)} = null;',
' #{posRestore(node)};' ,
'}'
] ,
"sequence.inner" : [
'#{r esultVar (node.resultIndex)} = [#{map(pluck(node.elements, "resultIndex"), resultVa r).join(", ")}];'
'#{r (node.resultIndex)} = [#{map(pluck(node.elements, "resultIndex"), r).join(", ")}];'
] ,
simple _and : [
'#{posSave(node)};' ,
'reportFailures++;' ,
'#block emit(node.expression)' ,
'reportFailures--;' ,
'if (#{r esultVar (node.resultIndex)} !== null) {',
' #{r esultVar (node.resultIndex)} = "";',
'if (#{r (node.resultIndex)} !== null) {',
' #{r (node.resultIndex)} = "";',
' #{posRestore(node)};' ,
'} else {' ,
' #{r esultVar (node.resultIndex)} = null;',
' #{r (node.resultIndex)} = null;',
'}'
] ,
simple _not : [
@ -633,49 +633,49 @@ PEG.compiler.passes.generateCode = function(ast, options) {
'reportFailures++;' ,
'#block emit(node.expression)' ,
'reportFailures--;' ,
'if (#{r esultVar (node.resultIndex)} === null) {',
' #{r esultVar (node.resultIndex)} = "";',
'if (#{r (node.resultIndex)} === null) {',
' #{r (node.resultIndex)} = "";',
'} else {' ,
' #{r esultVar (node.resultIndex)} = null;',
' #{r (node.resultIndex)} = null;',
' #{posRestore(node)};' ,
'}'
] ,
semantic _and : [
'#{r esultVar (node.resultIndex)} = (function(#{(options.trackLineAndColumn ? ["offset", "line", "column"] : ["offset"]).concat(keys(node.params)).join(", ")}) {#{node.code}})(#{(options.trackLineAndColumn ? ["pos.offset", "pos.line", "pos.column"] : ["pos"]).concat(map(values(node.params), param)).join(", ")}) ? "" : null;'
'#{r (node.resultIndex)} = (function(#{(options.trackLineAndColumn ? ["offset", "line", "column"] : ["offset"]).concat(keys(node.params)).join(", ")}) {#{node.code}})(#{(options.trackLineAndColumn ? ["pos.offset", "pos.line", "pos.column"] : ["pos"]).concat(map(values(node.params), param)).join(", ")}) ? "" : null;'
] ,
semantic _not : [
'#{r esultVar (node.resultIndex)} = (function(#{(options.trackLineAndColumn ? ["offset", "line", "column"] : ["offset"]).concat(keys(node.params)).join(", ")}) {#{node.code}})(#{(options.trackLineAndColumn ? ["pos.offset", "pos.line", "pos.column"] : ["pos"]).concat(map(values(node.params), param)).join(", ")}) ? null : "";'
'#{r (node.resultIndex)} = (function(#{(options.trackLineAndColumn ? ["offset", "line", "column"] : ["offset"]).concat(keys(node.params)).join(", ")}) {#{node.code}})(#{(options.trackLineAndColumn ? ["pos.offset", "pos.line", "pos.column"] : ["pos"]).concat(map(values(node.params), param)).join(", ")}) ? null : "";'
] ,
optional : [
'#block emit(node.expression)' ,
'#{r esultVar (node.resultIndex)} = #{resultVar (node.resultIndex)} !== null ? #{resultVa r(node.resultIndex)} : "";'
'#{r (node.resultIndex)} = #{r(node.resultIndex)} !== null ? #{r(node.resultIndex)} : "";'
] ,
zero _or _more : [
'#{r esultVar (node.resultIndex)} = [];',
'#{r (node.resultIndex)} = [];',
'#block emit(node.expression)' ,
'while (#{r esultVar (node.expression.resultIndex)} !== null) {',
' #{r esultVar (node.resultIndex)}.push(#{resultVa r(node.expression.resultIndex)});',
'while (#{r (node.expression.resultIndex)} !== null) {',
' #{r (node.resultIndex)}.push(#{r(node.expression.resultIndex)});',
' #block emit(node.expression)' ,
'}'
] ,
one _or _more : [
'#block emit(node.expression)' ,
'if (#{r esultVar (node.expression.resultIndex)} !== null) {',
' #{r esultVar (node.resultIndex)} = [];',
' while (#{r esultVar (node.expression.resultIndex)} !== null) {',
' #{r esultVar (node.resultIndex)}.push(#{resultVa r(node.expression.resultIndex)});',
'if (#{r (node.expression.resultIndex)} !== null) {',
' #{r (node.resultIndex)} = [];',
' while (#{r (node.expression.resultIndex)} !== null) {',
' #{r (node.resultIndex)}.push(#{r(node.expression.resultIndex)});',
' #block emit(node.expression)' ,
' }' ,
'} else {' ,
' #{r esultVar (node.resultIndex)} = null;',
' #{r (node.resultIndex)} = null;',
'}'
] ,
rule _ref : [
'#{r esultVar (node.resultIndex)} = parse_#{node.name}();'
'#{r (node.resultIndex)} = parse_#{node.name}();'
] ,
literal : [
'#if node.value.length === 0' ,
' #{r esultVar (node.resultIndex)} = "";',
' #{r (node.resultIndex)} = "";',
'#else' ,
' #if !node.ignoreCase' ,
' #if node.value.length === 1' ,
@ -695,13 +695,13 @@ PEG.compiler.passes.generateCode = function(ast, options) {
' if (input.substr(#{posOffset("pos")}, #{node.value.length}).toLowerCase() === #{string(node.value.toLowerCase())}) {' ,
' #end' ,
' #if !node.ignoreCase' ,
' #{r esultVar (node.resultIndex)} = #{string(node.value)};',
' #{r (node.resultIndex)} = #{string(node.value)};',
' #else' ,
' #{r esultVar (node.resultIndex)} = input.substr(#{posOffset("pos")}, #{node.value.length});',
' #{r (node.resultIndex)} = input.substr(#{posOffset("pos")}, #{node.value.length});',
' #end' ,
' #{posAdvance(node.value.length)};' ,
' } else {' ,
' #{r esultVar (node.resultIndex)} = null;',
' #{r (node.resultIndex)} = null;',
' if (reportFailures === 0) {' ,
' matchFailed(#{string(string(node.value))});' ,
' }' ,
@ -710,10 +710,10 @@ PEG.compiler.passes.generateCode = function(ast, options) {
] ,
"class" : [
'if (#{regexp}.test(input.charAt(#{posOffset("pos")}))) {' ,
' #{r esultVar (node.resultIndex)} = input.charAt(#{posOffset("pos")});',
' #{r (node.resultIndex)} = input.charAt(#{posOffset("pos")});',
' #{posAdvance(1)};' ,
'} else {' ,
' #{r esultVar (node.resultIndex)} = null;',
' #{r (node.resultIndex)} = null;',
' if (reportFailures === 0) {' ,
' matchFailed(#{string(node.rawText)});' ,
' }' ,
@ -721,10 +721,10 @@ PEG.compiler.passes.generateCode = function(ast, options) {
] ,
any : [
'if (input.length > #{posOffset("pos")}) {' ,
' #{r esultVar (node.resultIndex)} = input.charAt(#{posOffset("pos")});',
' #{r (node.resultIndex)} = input.charAt(#{posOffset("pos")});',
' #{posAdvance(1)};' ,
'} else {' ,
' #{r esultVar (node.resultIndex)} = null;',
' #{r (node.resultIndex)} = null;',
' if (reportFailures === 0) {' ,
' matchFailed("any character");' ,
' }' ,
@ -749,10 +749,10 @@ PEG.compiler.passes.generateCode = function(ast, options) {
vars . emit = emit ;
vars . options = options ;
vars . r esultVar = function ( index ) { return "r esult " + index ; } ;
vars . r = function ( index ) { return "r " + index ; } ;
vars . param = function ( param ) {
return vars . r esultVar ( param . resultIndex )
return vars . r ( param . resultIndex )
+ map ( param . subindices , function ( i ) { return "[" + i + "]" ; } ) ;
} ;
@ -778,10 +778,10 @@ PEG.compiler.passes.generateCode = function(ast, options) {
} ;
}
vars . posSave = function ( node ) {
return vars . r esultVar ( node . posIndex ) + " = " + vars . posClone ( "pos" ) ;
return vars . r ( node . posIndex ) + " = " + vars . posClone ( "pos" ) ;
} ;
vars . posRestore = function ( node ) {
return "pos" + " = " + vars . posClone ( vars . r esultVar ( node . posIndex ) ) ;
return "pos" + " = " + vars . posClone ( vars . r ( node . posIndex ) ) ;
} ;
return templates [ name ] ( vars ) ;
@ -807,17 +807,17 @@ PEG.compiler.passes.generateCode = function(ast, options) {
* input .
*
* * If the code fragment matches the input , it advances | pos | to point to
* the first chracter following the matched part of the input and sets
* variable with a name | resultVar ( node . resultIndex ) | to an appropriate
* the first chracter following the matched part of the input and sets a
* register with index specified by | node . resultIndex | to an appropriate
* value . This value is always non - | null | .
*
* * If the code fragment does not match the input , it returns with | pos |
* set to the original value and it sets a variable with a name
* | resultVar ( node . resultIndex ) | to | null | .
* set to the original value and it sets a register with index specified
* by | node . posIndex | to | null | .
*
* The code can use variables with names | resultVar ( node . resultIndex ) |
* where | node | is any of the current node 's subnodes. It can' t use any
* other variable s.
* The code uses only registers with indices specified by | node . resultIndex |
* and | node . posIndex | where | node | is the processed node or some of its
* subnodes. It does not use any other register s.
* /
named : emitSimple ( "named" ) ,