|
|
|
@ -14,11 +14,11 @@ function generateJS(ast, options) {
|
|
|
|
|
if (options.optimize === "size") {
|
|
|
|
|
return [
|
|
|
|
|
"var peg$consts = [",
|
|
|
|
|
indent2(ast.consts.join(",\n")),
|
|
|
|
|
indent2(ast.consts.join(",\n")),
|
|
|
|
|
"];",
|
|
|
|
|
"",
|
|
|
|
|
"var peg$bytecode = [",
|
|
|
|
|
indent2(ast.rules.map(rule =>
|
|
|
|
|
indent2(ast.rules.map(rule =>
|
|
|
|
|
"peg$decode(\""
|
|
|
|
|
+ js.stringEscape(rule.bytecode.map(
|
|
|
|
|
b => String.fromCharCode(b + 32)
|
|
|
|
@ -296,40 +296,40 @@ function generateJS(ast, options) {
|
|
|
|
|
" break;",
|
|
|
|
|
"",
|
|
|
|
|
" case " + op.IF + ":", // IF t, f
|
|
|
|
|
indent10(generateCondition("stack[stack.length - 1]", 0)),
|
|
|
|
|
indent10(generateCondition("stack[stack.length - 1]", 0)),
|
|
|
|
|
"",
|
|
|
|
|
" case " + op.IF_ERROR + ":", // IF_ERROR t, f
|
|
|
|
|
indent10(generateCondition(
|
|
|
|
|
indent10(generateCondition(
|
|
|
|
|
"stack[stack.length - 1] === peg$FAILED",
|
|
|
|
|
0
|
|
|
|
|
)),
|
|
|
|
|
"",
|
|
|
|
|
" case " + op.IF_NOT_ERROR + ":", // IF_NOT_ERROR t, f
|
|
|
|
|
indent10(
|
|
|
|
|
indent10(
|
|
|
|
|
generateCondition("stack[stack.length - 1] !== peg$FAILED",
|
|
|
|
|
0
|
|
|
|
|
)),
|
|
|
|
|
"",
|
|
|
|
|
" case " + op.WHILE_NOT_ERROR + ":", // WHILE_NOT_ERROR b
|
|
|
|
|
indent10(generateLoop("stack[stack.length - 1] !== peg$FAILED")),
|
|
|
|
|
indent10(generateLoop("stack[stack.length - 1] !== peg$FAILED")),
|
|
|
|
|
"",
|
|
|
|
|
" case " + op.MATCH_ANY + ":", // MATCH_ANY a, f, ...
|
|
|
|
|
indent10(generateCondition("input.length > peg$currPos", 0)),
|
|
|
|
|
indent10(generateCondition("input.length > peg$currPos", 0)),
|
|
|
|
|
"",
|
|
|
|
|
" case " + op.MATCH_STRING + ":", // MATCH_STRING s, a, f, ...
|
|
|
|
|
indent10(generateCondition(
|
|
|
|
|
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, ...
|
|
|
|
|
indent10(generateCondition(
|
|
|
|
|
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, ...
|
|
|
|
|
indent10(generateCondition(
|
|
|
|
|
indent10(generateCondition(
|
|
|
|
|
"peg$consts[bc[ip + 1]].test(input.charAt(peg$currPos))",
|
|
|
|
|
1
|
|
|
|
|
)),
|
|
|
|
@ -365,7 +365,7 @@ function generateJS(ast, options) {
|
|
|
|
|
" break;",
|
|
|
|
|
"",
|
|
|
|
|
" case " + op.CALL + ":", // CALL f, n, pc, p1, p2, ..., pN
|
|
|
|
|
indent10(generateCall()),
|
|
|
|
|
indent10(generateCall()),
|
|
|
|
|
"",
|
|
|
|
|
" case " + op.RULE + ":", // RULE r
|
|
|
|
|
" stack.push(peg$parseRule(bc[ip + 1]));",
|
|
|
|
@ -1208,18 +1208,18 @@ function generateJS(ast, options) {
|
|
|
|
|
function generateParserObject() {
|
|
|
|
|
return options.trace
|
|
|
|
|
? [
|
|
|
|
|
"{",
|
|
|
|
|
" SyntaxError: peg$SyntaxError,",
|
|
|
|
|
" DefaultTracer: peg$DefaultTracer,",
|
|
|
|
|
" parse: peg$parse",
|
|
|
|
|
"}"
|
|
|
|
|
].join("\n")
|
|
|
|
|
"{",
|
|
|
|
|
" SyntaxError: peg$SyntaxError,",
|
|
|
|
|
" DefaultTracer: peg$DefaultTracer,",
|
|
|
|
|
" parse: peg$parse",
|
|
|
|
|
"}"
|
|
|
|
|
].join("\n")
|
|
|
|
|
: [
|
|
|
|
|
"{",
|
|
|
|
|
" SyntaxError: peg$SyntaxError,",
|
|
|
|
|
" parse: peg$parse",
|
|
|
|
|
"}"
|
|
|
|
|
].join("\n");
|
|
|
|
|
"{",
|
|
|
|
|
" SyntaxError: peg$SyntaxError,",
|
|
|
|
|
" parse: peg$parse",
|
|
|
|
|
"}"
|
|
|
|
|
].join("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let generators = {
|
|
|
|
@ -1229,9 +1229,9 @@ function generateJS(ast, options) {
|
|
|
|
|
"(function() {",
|
|
|
|
|
" \"use strict\";",
|
|
|
|
|
"",
|
|
|
|
|
indent2(toplevelCode),
|
|
|
|
|
indent2(toplevelCode),
|
|
|
|
|
"",
|
|
|
|
|
indent2("return " + generateParserObject() + ";"),
|
|
|
|
|
indent2("return " + generateParserObject() + ";"),
|
|
|
|
|
"})()"
|
|
|
|
|
].join("\n");
|
|
|
|
|
},
|
|
|
|
@ -1283,9 +1283,9 @@ function generateJS(ast, options) {
|
|
|
|
|
"define(" + dependencies + ", function(" + params + ") {",
|
|
|
|
|
" \"use strict\";",
|
|
|
|
|
"",
|
|
|
|
|
indent2(toplevelCode),
|
|
|
|
|
indent2(toplevelCode),
|
|
|
|
|
"",
|
|
|
|
|
indent2("return " + generateParserObject() + ";"),
|
|
|
|
|
indent2("return " + generateParserObject() + ";"),
|
|
|
|
|
"});",
|
|
|
|
|
""
|
|
|
|
|
].join("\n");
|
|
|
|
@ -1297,9 +1297,9 @@ function generateJS(ast, options) {
|
|
|
|
|
"(function(root) {",
|
|
|
|
|
" \"use strict\";",
|
|
|
|
|
"",
|
|
|
|
|
indent2(toplevelCode),
|
|
|
|
|
indent2(toplevelCode),
|
|
|
|
|
"",
|
|
|
|
|
indent2("root." + options.exportVar + " = " + generateParserObject() + ";"),
|
|
|
|
|
indent2("root." + options.exportVar + " = " + generateParserObject() + ";"),
|
|
|
|
|
"})(this);",
|
|
|
|
|
""
|
|
|
|
|
].join("\n");
|
|
|
|
@ -1340,9 +1340,9 @@ function generateJS(ast, options) {
|
|
|
|
|
"})(this, function(" + params + ") {",
|
|
|
|
|
" \"use strict\";",
|
|
|
|
|
"",
|
|
|
|
|
indent2(toplevelCode),
|
|
|
|
|
indent2(toplevelCode),
|
|
|
|
|
"",
|
|
|
|
|
indent2("return " + generateParserObject() + ";"),
|
|
|
|
|
indent2("return " + generateParserObject() + ";"),
|
|
|
|
|
"});",
|
|
|
|
|
""
|
|
|
|
|
].join("\n"));
|
|
|
|
|