Browse Source

Refactor "rule" emitter function a bit

redux
David Majda 10 years ago
parent
commit
2b09a7116d
  1. 69
      src/emitter.js
  2. 144
      src/parser.js

69
src/emitter.js

@ -495,54 +495,32 @@ PEG.compiler.emitter = function(ast) {
posIndex: 0
};
var resultVars = map(range(node.resultStackDepth), resultVar);
var posVars = map(range(node.posStackDepth), posVar);
var resultVarsCode = resultVars.length > 0 ? 'var ' + resultVars.join(', ') + ';' : '';
var posVarsCode = posVars.length > 0 ? 'var ' + posVars.join(', ') + ';' : '';
var setReportFailuresCode;
var restoreReportFailuresCode;
var reportFailureCode;
if (node.displayName !== null) {
setReportFailuresCode = formatCode(
'reportFailures++;'
);
restoreReportFailuresCode = formatCode(
'reportFailures--;'
);
reportFailureCode = formatCode(
'if (reportFailures === 0 && #{resultVar} === null) {',
' matchFailed(#{string(displayName)});',
'}',
{
displayName: node.displayName,
resultVar: resultVar(context.resultIndex)
}
);
} else {
setReportFailuresCode = "";
restoreReportFailuresCode = "";
reportFailureCode = "";
}
return formatCode(
'function parse_#{name}() {',
' var cacheKey = "#{name}@" + pos;',
'function parse_#{node.name}() {',
' var cacheKey = "#{node.name}@" + pos;',
' var cachedResult = cache[cacheKey];',
' if (cachedResult) {',
' pos = cachedResult.nextPos;',
' return cachedResult.result;',
' }',
' ',
' #block resultVarsCode',
' #block posVarsCode',
' #if resultVars.length > 0',
' var #{resultVars.join(", ")};',
' #end',
' #if posVars.length > 0',
' var #{posVars.join(", ")};',
' #end',
' ',
' #block setReportFailuresCode',
' #if node.displayName !== null',
' reportFailures++;',
' #end',
' #block code',
' #block restoreReportFailuresCode',
' #block reportFailureCode',
' #if node.displayName !== null',
' reportFailures--;',
' if (reportFailures === 0 && #{resultVar} === null) {',
' matchFailed(#{string(node.displayName)});',
' }',
' #end',
' ',
' cache[cacheKey] = {',
' nextPos: pos,',
@ -551,14 +529,11 @@ PEG.compiler.emitter = function(ast) {
' return #{resultVar};',
'}',
{
name: node.name,
resultVarsCode: resultVarsCode,
posVarsCode: posVarsCode,
setReportFailuresCode: setReportFailuresCode,
restoreReportFailuresCode: restoreReportFailuresCode,
reportFailureCode: reportFailureCode,
code: emit(node.expression, context),
resultVar: resultVar(context.resultIndex)
node: node,
resultVars: map(range(node.resultStackDepth), resultVar),
posVars: map(range(node.posStackDepth), posVar),
code: emit(node.expression, context),
resultVar: resultVar(context.resultIndex)
}
);
},

144
src/parser.js

@ -155,7 +155,6 @@ PEG.parser = (function(){
var result0, result1, result2, result3, result4;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
result0 = parse___();
@ -204,8 +203,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -224,7 +221,6 @@ PEG.parser = (function(){
var result0, result1, result2;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
result0 = parse_action();
@ -253,8 +249,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -273,7 +267,6 @@ PEG.parser = (function(){
var result0, result1, result2, result3, result4, result5;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
result0 = parse_identifier();
@ -325,8 +318,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -345,7 +336,6 @@ PEG.parser = (function(){
var result0, result1, result2, result3, result4, result5;
var pos0, pos1, pos2, pos3;
pos0 = pos;
pos1 = pos;
result0 = parse_sequence();
@ -412,8 +402,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -432,7 +420,6 @@ PEG.parser = (function(){
var result0, result1, result2;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
result0 = [];
@ -494,8 +481,6 @@ PEG.parser = (function(){
}
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -514,7 +499,6 @@ PEG.parser = (function(){
var result0, result1, result2, result3;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
result0 = parse_identifier();
@ -552,8 +536,6 @@ PEG.parser = (function(){
result0 = parse_prefixed();
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -572,7 +554,6 @@ PEG.parser = (function(){
var result0, result1, result2;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
result0 = parse_and();
@ -687,8 +668,6 @@ PEG.parser = (function(){
}
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -707,7 +686,6 @@ PEG.parser = (function(){
var result0, result1, result2;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
result0 = parse_primary();
@ -794,8 +772,6 @@ PEG.parser = (function(){
}
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -814,7 +790,6 @@ PEG.parser = (function(){
var result0, result1, result2, result3, result4;
var pos0, pos1, pos2, pos3, pos4;
pos0 = pos;
pos1 = pos;
result0 = parse_identifier();
@ -924,8 +899,6 @@ PEG.parser = (function(){
}
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -989,7 +962,6 @@ PEG.parser = (function(){
var result0, result1, result2, result3;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
if (input.charCodeAt(pos) === 123) {
@ -1047,8 +1019,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -1067,7 +1037,6 @@ PEG.parser = (function(){
var result0, result1;
var pos0, pos1;
pos0 = pos;
result1 = parse_nonBraceCharacter();
if (result1 !== null) {
@ -1086,8 +1055,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -1106,7 +1073,6 @@ PEG.parser = (function(){
var result0;
var pos0;
if (/^[^{}]/.test(input.charAt(pos))) {
result0 = input.charAt(pos);
pos++;
@ -1117,8 +1083,6 @@ PEG.parser = (function(){
}
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -1137,7 +1101,6 @@ PEG.parser = (function(){
var result0, result1, result2;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
if (input.charCodeAt(pos) === 61) {
@ -1168,8 +1131,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -1188,7 +1149,6 @@ PEG.parser = (function(){
var result0, result1, result2;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
if (input.charCodeAt(pos) === 58) {
@ -1219,8 +1179,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -1239,7 +1197,6 @@ PEG.parser = (function(){
var result0, result1, result2;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
if (input.charCodeAt(pos) === 59) {
@ -1270,8 +1227,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -1290,7 +1245,6 @@ PEG.parser = (function(){
var result0, result1, result2;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
if (input.charCodeAt(pos) === 47) {
@ -1321,8 +1275,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -1341,7 +1293,6 @@ PEG.parser = (function(){
var result0, result1, result2;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
if (input.charCodeAt(pos) === 38) {
@ -1372,8 +1323,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -1392,7 +1341,6 @@ PEG.parser = (function(){
var result0, result1, result2;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
if (input.charCodeAt(pos) === 33) {
@ -1423,8 +1371,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -1443,7 +1389,6 @@ PEG.parser = (function(){
var result0, result1, result2;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
if (input.charCodeAt(pos) === 63) {
@ -1474,8 +1419,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -1494,7 +1437,6 @@ PEG.parser = (function(){
var result0, result1, result2;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
if (input.charCodeAt(pos) === 42) {
@ -1525,8 +1467,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -1545,7 +1485,6 @@ PEG.parser = (function(){
var result0, result1, result2;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
if (input.charCodeAt(pos) === 43) {
@ -1576,8 +1515,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -1596,7 +1533,6 @@ PEG.parser = (function(){
var result0, result1, result2;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
if (input.charCodeAt(pos) === 40) {
@ -1627,8 +1563,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -1647,7 +1581,6 @@ PEG.parser = (function(){
var result0, result1, result2;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
if (input.charCodeAt(pos) === 41) {
@ -1678,8 +1611,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -1698,7 +1629,6 @@ PEG.parser = (function(){
var result0, result1, result2;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
if (input.charCodeAt(pos) === 46) {
@ -1729,8 +1659,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -1927,7 +1855,6 @@ PEG.parser = (function(){
var result0, result1, result2, result3;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
if (input.charCodeAt(pos) === 34) {
@ -1977,8 +1904,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -1997,7 +1922,6 @@ PEG.parser = (function(){
var result0;
var pos0;
result0 = parse_simpleDoubleQuotedCharacter();
if (result0 === null) {
result0 = parse_simpleEscapeSequence();
@ -2015,8 +1939,6 @@ PEG.parser = (function(){
}
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -2035,7 +1957,6 @@ PEG.parser = (function(){
var result0, result1, result2;
var pos0, pos1, pos2, pos3;
pos0 = pos;
pos1 = pos;
pos2 = pos;
@ -2097,8 +2018,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -2117,7 +2036,6 @@ PEG.parser = (function(){
var result0, result1, result2, result3;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
if (input.charCodeAt(pos) === 39) {
@ -2167,8 +2085,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -2187,7 +2103,6 @@ PEG.parser = (function(){
var result0;
var pos0;
result0 = parse_simpleSingleQuotedCharacter();
if (result0 === null) {
result0 = parse_simpleEscapeSequence();
@ -2205,8 +2120,6 @@ PEG.parser = (function(){
}
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -2225,7 +2138,6 @@ PEG.parser = (function(){
var result0, result1, result2;
var pos0, pos1, pos2, pos3;
pos0 = pos;
pos1 = pos;
pos2 = pos;
@ -2287,8 +2199,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -2420,7 +2330,6 @@ PEG.parser = (function(){
var result0, result1, result2, result3;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
result0 = parse_classCharacter();
@ -2469,8 +2378,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -2489,7 +2396,6 @@ PEG.parser = (function(){
var result0;
var pos0, pos1;
pos0 = pos;
result0 = parse_bracketDelimitedCharacter();
if (result0 !== null) {
@ -2505,8 +2411,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -2525,7 +2429,6 @@ PEG.parser = (function(){
var result0;
var pos0;
result0 = parse_simpleBracketDelimitedCharacter();
if (result0 === null) {
result0 = parse_simpleEscapeSequence();
@ -2543,8 +2446,6 @@ PEG.parser = (function(){
}
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -2563,7 +2464,6 @@ PEG.parser = (function(){
var result0, result1, result2;
var pos0, pos1, pos2, pos3;
pos0 = pos;
pos1 = pos;
pos2 = pos;
@ -2625,8 +2525,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -2645,7 +2543,6 @@ PEG.parser = (function(){
var result0, result1, result2, result3;
var pos0, pos1, pos2, pos3;
pos0 = pos;
pos1 = pos;
if (input.charCodeAt(pos) === 92) {
@ -2732,8 +2629,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -2752,7 +2647,6 @@ PEG.parser = (function(){
var result0, result1, result2;
var pos0, pos1, pos2, pos3;
pos0 = pos;
pos1 = pos;
if (input.substr(pos, 2) === "\\0") {
@ -2792,8 +2686,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -2812,7 +2704,6 @@ PEG.parser = (function(){
var result0, result1, result2, result3;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
if (input.substr(pos, 2) === "\\x") {
@ -2851,8 +2742,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -2871,7 +2760,6 @@ PEG.parser = (function(){
var result0, result1, result2, result3, result4, result5;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
if (input.substr(pos, 2) === "\\u") {
@ -2922,8 +2810,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -2942,7 +2828,6 @@ PEG.parser = (function(){
var result0, result1, result2;
var pos0, pos1, pos2;
pos0 = pos;
pos1 = pos;
if (input.charCodeAt(pos) === 92) {
@ -2973,8 +2858,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -2993,7 +2876,6 @@ PEG.parser = (function(){
var result0;
var pos0;
if (/^[0-9]/.test(input.charAt(pos))) {
result0 = input.charAt(pos);
pos++;
@ -3004,8 +2886,6 @@ PEG.parser = (function(){
}
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -3024,7 +2904,6 @@ PEG.parser = (function(){
var result0;
var pos0;
if (/^[0-9a-fA-F]/.test(input.charAt(pos))) {
result0 = input.charAt(pos);
pos++;
@ -3035,8 +2914,6 @@ PEG.parser = (function(){
}
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -3055,14 +2932,11 @@ PEG.parser = (function(){
var result0;
var pos0;
result0 = parse_lowerCaseLetter();
if (result0 === null) {
result0 = parse_upperCaseLetter();
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -3081,7 +2955,6 @@ PEG.parser = (function(){
var result0;
var pos0;
if (/^[a-z]/.test(input.charAt(pos))) {
result0 = input.charAt(pos);
pos++;
@ -3092,8 +2965,6 @@ PEG.parser = (function(){
}
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -3112,7 +2983,6 @@ PEG.parser = (function(){
var result0;
var pos0;
if (/^[A-Z]/.test(input.charAt(pos))) {
result0 = input.charAt(pos);
pos++;
@ -3123,8 +2993,6 @@ PEG.parser = (function(){
}
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -3143,7 +3011,6 @@ PEG.parser = (function(){
var result0, result1;
var pos0;
result0 = [];
result1 = parse_whitespace();
if (result1 === null) {
@ -3163,8 +3030,6 @@ PEG.parser = (function(){
}
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -3211,7 +3076,6 @@ PEG.parser = (function(){
var result0, result1, result2, result3, result4, result5;
var pos0, pos1, pos2, pos3;
pos0 = pos;
if (input.substr(pos, 2) === "//") {
result0 = "//";
@ -3300,8 +3164,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -3320,7 +3182,6 @@ PEG.parser = (function(){
var result0, result1, result2, result3, result4, result5;
var pos0, pos1, pos2, pos3;
pos0 = pos;
if (input.substr(pos, 2) === "/*") {
result0 = "/*";
@ -3439,8 +3300,6 @@ PEG.parser = (function(){
pos = pos0;
}
cache[cacheKey] = {
nextPos: pos,
result: result0
@ -3536,7 +3395,6 @@ PEG.parser = (function(){
var result0;
var pos0;
if (/^[\n\r\u2028\u2029]/.test(input.charAt(pos))) {
result0 = input.charAt(pos);
pos++;
@ -3547,8 +3405,6 @@ PEG.parser = (function(){
}
}
cache[cacheKey] = {
nextPos: pos,
result: result0

Loading…
Cancel
Save