diff --git a/lib/compiler/passes/generate-bytecode.js b/lib/compiler/passes/generate-bytecode.js index 2ba346a..18611cd 100644 --- a/lib/compiler/passes/generate-bytecode.js +++ b/lib/compiler/passes/generate-bytecode.js @@ -273,9 +273,9 @@ module.exports = function(ast) { rule: function(node) { node.bytecode = generate(node.expression, { - sp: -1, // stack pointer - env: { }, // mapping of label names to stack positions - action: null // action nodes pass themselves to children here + sp: -1, // stack pointer + env: { }, // mapping of label names to stack positions + action: null // action nodes pass themselves to children here }); }, diff --git a/lib/compiler/passes/generate-javascript.js b/lib/compiler/passes/generate-javascript.js index 3c10dbc..2f1a6ed 100644 --- a/lib/compiler/passes/generate-javascript.js +++ b/lib/compiler/passes/generate-javascript.js @@ -172,99 +172,99 @@ module.exports = function(ast, options) { ' ip += 2;', ' break;', '', - ' case ' + op.PUSH_CURR_POS + ':', // PUSH_CURR_POS + ' case ' + op.PUSH_CURR_POS + ':', // PUSH_CURR_POS ' stack.push(peg$currPos);', ' ip++;', ' break;', '', - ' case ' + op.POP + ':', // POP + ' case ' + op.POP + ':', // POP ' stack.pop();', ' ip++;', ' break;', '', - ' case ' + op.POP_CURR_POS + ':', // POP_CURR_POS + ' case ' + op.POP_CURR_POS + ':', // POP_CURR_POS ' peg$currPos = stack.pop();', ' ip++;', ' break;', '', - ' case ' + op.POP_N + ':', // POP_N n + ' case ' + op.POP_N + ':', // POP_N n ' stack.length -= bc[ip + 1];', ' ip += 2;', ' break;', '', - ' case ' + op.NIP + ':', // NIP + ' case ' + op.NIP + ':', // NIP ' stack.splice(-2, 1);', ' ip++;', ' break;', '', - ' case ' + op.APPEND + ':', // APPEND + ' case ' + op.APPEND + ':', // APPEND ' stack[stack.length - 2].push(stack.pop());', ' ip++;', ' break;', '', - ' case ' + op.WRAP + ':', // WRAP n + ' case ' + op.WRAP + ':', // WRAP n ' stack.push(stack.splice(stack.length - bc[ip + 1], bc[ip + 1]));', ' ip += 2;', ' break;', '', - ' case ' + op.TEXT + ':', // TEXT + ' case ' + op.TEXT + ':', // TEXT ' stack.pop();', ' stack.push(input.substring(stack[stack.length - 1], peg$currPos));', ' ip++;', ' break;', '', - ' case ' + op.IF + ':', // IF t, f + ' case ' + op.IF + ':', // IF t, f indent10(generateCondition('stack[stack.length - 1]', 0)), '', - ' case ' + op.IF_ERROR + ':', // IF_ERROR t, f + ' case ' + op.IF_ERROR + ':', // IF_ERROR t, f indent10(generateCondition( 'stack[stack.length - 1] === peg$FAILED', 0 )), '', - ' case ' + op.IF_NOT_ERROR + ':', // IF_NOT_ERROR t, f + ' case ' + op.IF_NOT_ERROR + ':', // IF_NOT_ERROR t, f indent10( generateCondition('stack[stack.length - 1] !== peg$FAILED', 0 )), '', - ' case ' + op.WHILE_NOT_ERROR + ':', // WHILE_NOT_ERROR b + ' case ' + op.WHILE_NOT_ERROR + ':', // WHILE_NOT_ERROR b indent10(generateLoop('stack[stack.length - 1] !== peg$FAILED')), '', - ' case ' + op.MATCH_ANY + ':', // MATCH_ANY a, f, ... + ' case ' + op.MATCH_ANY + ':', // MATCH_ANY a, f, ... indent10(generateCondition('input.length > peg$currPos', 0)), '', - ' case ' + op.MATCH_STRING + ':', // MATCH_STRING s, a, f, ... + ' case ' + op.MATCH_STRING + ':', // MATCH_STRING s, a, f, ... indent10(generateCondition( 'input.substr(peg$currPos, peg$consts[bc[ip + 1]].length) === peg$consts[bc[ip + 1]]', 1 )), '', - ' case ' + op.MATCH_STRING_IC + ':', // MATCH_STRING_IC s, a, f, ... + ' case ' + op.MATCH_STRING_IC + ':', // MATCH_STRING_IC s, a, f, ... indent10(generateCondition( 'input.substr(peg$currPos, peg$consts[bc[ip + 1]].length).toLowerCase() === peg$consts[bc[ip + 1]]', 1 )), '', - ' case ' + op.MATCH_REGEXP + ':', // MATCH_REGEXP r, a, f, ... + ' case ' + op.MATCH_REGEXP + ':', // MATCH_REGEXP r, a, f, ... indent10(generateCondition( 'peg$consts[bc[ip + 1]].test(input.charAt(peg$currPos))', 1 )), '', - ' case ' + op.ACCEPT_N + ':', // ACCEPT_N n + ' case ' + op.ACCEPT_N + ':', // ACCEPT_N n ' stack.push(input.substr(peg$currPos, bc[ip + 1]));', ' peg$currPos += bc[ip + 1];', ' ip += 2;', ' break;', '', - ' case ' + op.ACCEPT_STRING + ':', // ACCEPT_STRING s + ' case ' + op.ACCEPT_STRING + ':', // ACCEPT_STRING s ' stack.push(peg$consts[bc[ip + 1]]);', ' peg$currPos += peg$consts[bc[ip + 1]].length;', ' ip += 2;', ' break;', '', - ' case ' + op.FAIL + ':', // FAIL e + ' case ' + op.FAIL + ':', // FAIL e ' stack.push(peg$FAILED);', ' if (peg$silentFails === 0) {', ' peg$fail(peg$consts[bc[ip + 1]]);', @@ -272,30 +272,30 @@ module.exports = function(ast, options) { ' ip += 2;', ' break;', '', - ' case ' + op.REPORT_SAVED_POS + ':', // REPORT_SAVED_POS p + ' case ' + op.REPORT_SAVED_POS + ':', // REPORT_SAVED_POS p ' peg$reportedPos = stack[stack.length - 1 - bc[ip + 1]];', ' ip += 2;', ' break;', '', - ' case ' + op.REPORT_CURR_POS + ':', // REPORT_CURR_POS + ' case ' + op.REPORT_CURR_POS + ':', // REPORT_CURR_POS ' peg$reportedPos = peg$currPos;', ' ip++;', ' break;', '', - ' case ' + op.CALL + ':', // CALL f, n, pc, p1, p2, ..., pN + ' case ' + op.CALL + ':', // CALL f, n, pc, p1, p2, ..., pN indent10(generateCall()), '', - ' case ' + op.RULE + ':', // RULE r + ' case ' + op.RULE + ':', // RULE r ' stack.push(peg$parseRule(bc[ip + 1]));', ' ip += 2;', ' break;', '', - ' case ' + op.SILENT_FAILS_ON + ':', // SILENT_FAILS_ON + ' case ' + op.SILENT_FAILS_ON + ':', // SILENT_FAILS_ON ' peg$silentFails++;', ' ip++;', ' break;', '', - ' case ' + op.SILENT_FAILS_OFF + ':', // SILENT_FAILS_OFF + ' case ' + op.SILENT_FAILS_OFF + ':', // SILENT_FAILS_OFF ' peg$silentFails--;', ' ip++;', ' break;', @@ -445,7 +445,7 @@ module.exports = function(ast, options) { while (ip < end) { switch (bc[ip]) { - case op.PUSH: // PUSH c + case op.PUSH: // PUSH c /* * Hack: One of the constants can be an empty array. It needs to be * handled specially because it can be modified later on the stack @@ -457,47 +457,47 @@ module.exports = function(ast, options) { ip += 2; break; - case op.PUSH_CURR_POS: // PUSH_CURR_POS + case op.PUSH_CURR_POS: // PUSH_CURR_POS parts.push(stack.push('peg$currPos')); ip++; break; - case op.POP: // POP + case op.POP: // POP stack.pop(); ip++; break; - case op.POP_CURR_POS: // POP_CURR_POS + case op.POP_CURR_POS: // POP_CURR_POS parts.push('peg$currPos = ' + stack.pop() + ';'); ip++; break; - case op.POP_N: // POP_N n + case op.POP_N: // POP_N n stack.pop(bc[ip + 1]); ip += 2; break; - case op.NIP: // NIP + case op.NIP: // NIP value = stack.pop(); stack.pop(); parts.push(stack.push(value)); ip++; break; - case op.APPEND: // APPEND + case op.APPEND: // APPEND value = stack.pop(); parts.push(stack.top() + '.push(' + value + ');'); ip++; break; - case op.WRAP: // WRAP n + case op.WRAP: // WRAP n parts.push( stack.push('[' + stack.pop(bc[ip + 1]).join(', ') + ']') ); ip += 2; break; - case op.TEXT: // TEXT + case op.TEXT: // TEXT stack.pop(); parts.push( stack.push('input.substring(' + stack.top() + ', peg$currPos)') @@ -505,27 +505,27 @@ module.exports = function(ast, options) { ip++; break; - case op.IF: // IF t, f + case op.IF: // IF t, f compileCondition(stack.top(), 0); break; - case op.IF_ERROR: // IF_ERROR t, f + case op.IF_ERROR: // IF_ERROR t, f compileCondition(stack.top() + ' === peg$FAILED', 0); break; - case op.IF_NOT_ERROR: // IF_NOT_ERROR t, f + case op.IF_NOT_ERROR: // IF_NOT_ERROR t, f compileCondition(stack.top() + ' !== peg$FAILED', 0); break; - case op.WHILE_NOT_ERROR: // WHILE_NOT_ERROR b + case op.WHILE_NOT_ERROR: // WHILE_NOT_ERROR b compileLoop(stack.top() + ' !== peg$FAILED', 0); break; - case op.MATCH_ANY: // MATCH_ANY a, f, ... + case op.MATCH_ANY: // MATCH_ANY a, f, ... compileCondition('input.length > peg$currPos', 0); break; - case op.MATCH_STRING: // MATCH_STRING s, a, f, ... + case op.MATCH_STRING: // MATCH_STRING s, a, f, ... compileCondition( eval(ast.consts[bc[ip + 1]]).length > 1 ? 'input.substr(peg$currPos, ' @@ -538,7 +538,7 @@ module.exports = function(ast, options) { ); break; - case op.MATCH_STRING_IC: // MATCH_STRING_IC s, a, f, ... + case op.MATCH_STRING_IC: // MATCH_STRING_IC s, a, f, ... compileCondition( 'input.substr(peg$currPos, ' + eval(ast.consts[bc[ip + 1]]).length @@ -548,14 +548,14 @@ module.exports = function(ast, options) { ); break; - case op.MATCH_REGEXP: // MATCH_REGEXP r, a, f, ... + case op.MATCH_REGEXP: // MATCH_REGEXP r, a, f, ... compileCondition( c(bc[ip + 1]) + '.test(input.charAt(peg$currPos))', 1 ); break; - case op.ACCEPT_N: // ACCEPT_N n + case op.ACCEPT_N: // ACCEPT_N n parts.push(stack.push( bc[ip + 1] > 1 ? 'input.substr(peg$currPos, ' + bc[ip + 1] + ')' @@ -569,7 +569,7 @@ module.exports = function(ast, options) { ip += 2; break; - case op.ACCEPT_STRING: // ACCEPT_STRING s + case op.ACCEPT_STRING: // ACCEPT_STRING s parts.push(stack.push(c(bc[ip + 1]))); parts.push( eval(ast.consts[bc[ip + 1]]).length > 1 @@ -579,37 +579,37 @@ module.exports = function(ast, options) { ip += 2; break; - case op.FAIL: // FAIL e + case op.FAIL: // FAIL e parts.push(stack.push('peg$FAILED')); parts.push('if (peg$silentFails === 0) { peg$fail(' + c(bc[ip + 1]) + '); }'); ip += 2; break; - case op.REPORT_SAVED_POS: // REPORT_SAVED_POS p + case op.REPORT_SAVED_POS: // REPORT_SAVED_POS p parts.push('peg$reportedPos = ' + stack.index(bc[ip + 1]) + ';'); ip += 2; break; - case op.REPORT_CURR_POS: // REPORT_CURR_POS + case op.REPORT_CURR_POS: // REPORT_CURR_POS parts.push('peg$reportedPos = peg$currPos;'); ip++; break; - case op.CALL: // CALL f, n, pc, p1, p2, ..., pN + case op.CALL: // CALL f, n, pc, p1, p2, ..., pN compileCall(); break; - case op.RULE: // RULE r + case op.RULE: // RULE r parts.push(stack.push("peg$parse" + ast.rules[bc[ip + 1]].name + "()")); ip += 2; break; - case op.SILENT_FAILS_ON: // SILENT_FAILS_ON + case op.SILENT_FAILS_ON: // SILENT_FAILS_ON parts.push('peg$silentFails++;'); ip++; break; - case op.SILENT_FAILS_OFF: // SILENT_FAILS_OFF + case op.SILENT_FAILS_OFF: // SILENT_FAILS_OFF parts.push('peg$silentFails--;'); ip++; break; @@ -730,7 +730,7 @@ module.exports = function(ast, options) { ' peg$cachedPosDetails = { line: 1, column: 1, seenCR: false },', ' peg$maxFailPos = 0,', ' peg$maxFailExpected = [],', - ' peg$silentFails = 0,', // 0 = report failures, > 0 = silence failures + ' peg$silentFails = 0,', // 0 = report failures, > 0 = silence failures '' ].join('\n')); @@ -882,13 +882,13 @@ module.exports = function(ast, options) { * not like the first and IE the second. */ ' return s', - ' .replace(/\\\\/g, \'\\\\\\\\\')', // backslash - ' .replace(/"/g, \'\\\\"\')', // closing double quote - ' .replace(/\\x08/g, \'\\\\b\')', // backspace - ' .replace(/\\t/g, \'\\\\t\')', // horizontal tab - ' .replace(/\\n/g, \'\\\\n\')', // line feed - ' .replace(/\\f/g, \'\\\\f\')', // form feed - ' .replace(/\\r/g, \'\\\\r\')', // carriage return + ' .replace(/\\\\/g, \'\\\\\\\\\')', // backslash + ' .replace(/"/g, \'\\\\"\')', // closing double quote + ' .replace(/\\x08/g, \'\\\\b\')', // backspace + ' .replace(/\\t/g, \'\\\\t\')', // horizontal tab + ' .replace(/\\n/g, \'\\\\n\')', // line feed + ' .replace(/\\f/g, \'\\\\f\')', // form feed + ' .replace(/\\r/g, \'\\\\r\')', // carriage return ' .replace(/[\\x00-\\x07\\x0B\\x0E\\x0F]/g, function(ch) { return \'\\\\x0\' + hex(ch); })', ' .replace(/[\\x10-\\x1F\\x80-\\xFF]/g, function(ch) { return \'\\\\x\' + hex(ch); })', ' .replace(/[\\u0180-\\u0FFF]/g, function(ch) { return \'\\\\u0\' + hex(ch); })', diff --git a/lib/utils.js b/lib/utils.js index 3cdeca4..d491099 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -175,13 +175,13 @@ var utils = { * not like the first and IE the second. */ return '"' + s - .replace(/\\/g, '\\\\') // backslash - .replace(/"/g, '\\"') // closing quote character - .replace(/\x08/g, '\\b') // backspace - .replace(/\t/g, '\\t') // horizontal tab - .replace(/\n/g, '\\n') // line feed - .replace(/\f/g, '\\f') // form feed - .replace(/\r/g, '\\r') // carriage return + .replace(/\\/g, '\\\\') // backslash + .replace(/"/g, '\\"') // closing quote character + .replace(/\x08/g, '\\b') // backspace + .replace(/\t/g, '\\t') // horizontal tab + .replace(/\n/g, '\\n') // line feed + .replace(/\f/g, '\\f') // form feed + .replace(/\r/g, '\\r') // carriage return .replace(/[\x00-\x07\x0B\x0E-\x1F\x80-\uFFFF]/g, utils.escape) + '"'; }, @@ -197,17 +197,17 @@ var utils = { * For portability, we also escape all control and non-ASCII characters. */ return s - .replace(/\\/g, '\\\\') // backslash - .replace(/\//g, '\\/') // closing slash - .replace(/\]/g, '\\]') // closing bracket - .replace(/\^/g, '\\^') // caret - .replace(/-/g, '\\-') // dash - .replace(/\0/g, '\\0') // null - .replace(/\t/g, '\\t') // horizontal tab - .replace(/\n/g, '\\n') // line feed - .replace(/\v/g, '\\x0B') // vertical tab - .replace(/\f/g, '\\f') // form feed - .replace(/\r/g, '\\r') // carriage return + .replace(/\\/g, '\\\\') // backslash + .replace(/\//g, '\\/') // closing slash + .replace(/\]/g, '\\]') // closing bracket + .replace(/\^/g, '\\^') // caret + .replace(/-/g, '\\-') // dash + .replace(/\0/g, '\\0') // null + .replace(/\t/g, '\\t') // horizontal tab + .replace(/\n/g, '\\n') // line feed + .replace(/\v/g, '\\x0B') // vertical tab + .replace(/\f/g, '\\f') // form feed + .replace(/\r/g, '\\r') // carriage return .replace(/[\x01-\x08\x0E-\x1F\x80-\uFFFF]/g, utils.escape); }, diff --git a/spec/generated-parser.spec.js b/spec/generated-parser.spec.js index b527a93..13c8e5b 100644 --- a/spec/generated-parser.spec.js +++ b/spec/generated-parser.spec.js @@ -323,13 +323,13 @@ describe("generated parser", function() { expect(parser).toParse("1\n2\n\n3\n\n\n4 5 x", [7, 5]); /* Non-Unix newlines */ - expect(parser).toParse("1\rx", [2, 1]); // Old Mac - expect(parser).toParse("1\r\nx", [2, 1]); // Windows - expect(parser).toParse("1\n\rx", [3, 1]); // mismatched + expect(parser).toParse("1\rx", [2, 1]); // Old Mac + expect(parser).toParse("1\r\nx", [2, 1]); // Windows + expect(parser).toParse("1\n\rx", [3, 1]); // mismatched /* Strange newlines */ - expect(parser).toParse("1\u2028x", [2, 1]); // line separator - expect(parser).toParse("1\u2029x", [2, 1]); // paragraph separator + expect(parser).toParse("1\u2028x", [2, 1]); // line separator + expect(parser).toParse("1\u2029x", [2, 1]); // paragraph separator }); it("can use variables defined in the initializer", function() { @@ -528,13 +528,13 @@ describe("generated parser", function() { expect(parser).toParse("1\n2\n\n3\n\n\n4 5 x", [7, 5]); /* Non-Unix newlines */ - expect(parser).toParse("1\rx", [2, 1]); // Old Mac - expect(parser).toParse("1\r\nx", [2, 1]); // Windows - expect(parser).toParse("1\n\rx", [3, 1]); // mismatched + expect(parser).toParse("1\rx", [2, 1]); // Old Mac + expect(parser).toParse("1\r\nx", [2, 1]); // Windows + expect(parser).toParse("1\n\rx", [3, 1]); // mismatched /* Strange newlines */ - expect(parser).toParse("1\u2028x", [2, 1]); // line separator - expect(parser).toParse("1\u2029x", [2, 1]); // paragraph separator + expect(parser).toParse("1\u2028x", [2, 1]); // line separator + expect(parser).toParse("1\u2029x", [2, 1]); // paragraph separator }); it("can use variables defined in the initializer", function() { @@ -628,13 +628,13 @@ describe("generated parser", function() { expect(parser).toParse("1\n2\n\n3\n\n\n4 5 x", [7, 5]); /* Non-Unix newlines */ - expect(parser).toParse("1\rx", [2, 1]); // Old Mac - expect(parser).toParse("1\r\nx", [2, 1]); // Windows - expect(parser).toParse("1\n\rx", [3, 1]); // mismatched + expect(parser).toParse("1\rx", [2, 1]); // Old Mac + expect(parser).toParse("1\r\nx", [2, 1]); // Windows + expect(parser).toParse("1\n\rx", [3, 1]); // mismatched /* Strange newlines */ - expect(parser).toParse("1\u2028x", [2, 1]); // line separator - expect(parser).toParse("1\u2029x", [2, 1]); // paragraph separator + expect(parser).toParse("1\u2028x", [2, 1]); // line separator + expect(parser).toParse("1\u2029x", [2, 1]); // paragraph separator }); it("can use variables defined in the initializer", function() {