UMD parsers: Generate parser wrapper separately from its toplevel code

Code which was at the toplevel of the "generateJS" function in the code
generator is now split into "generateToplevel" (which genreates parser
toplevel code) and "generateWrapper" (which generates a wrapper around
it). This is pure refactoring, generated parser code is exactly the same
as before.

This change will make it easier to modifiy the code genreator to produce
UMD modules.

Part of work on #362.
redux
David Majda 8 years ago
parent 5e702b5901
commit 7f8b3f7012

@ -9,8 +9,7 @@ var arrays = require("../../utils/arrays"),
function generateJS(ast, options) {
/* These only indent non-empty lines to avoid trailing whitespace. */
function indent2(code) { return code.replace(/^(.+)$/gm, ' $1'); }
function indent4(code) { return code.replace(/^(.+)$/gm, ' $1'); }
function indent8(code) { return code.replace(/^(.+)$/gm, ' $1'); }
function indent6(code) { return code.replace(/^(.+)$/gm, ' $1'); }
function indent10(code) { return code.replace(/^(.+)$/gm, ' $1'); }
function generateTables() {
@ -755,21 +754,13 @@ function generateJS(ast, options) {
return parts.join('\n');
}
function generateToplevel() {
var parts = [],
startRuleIndices, startRuleIndex,
startRuleFunctions, startRuleFunction,
ruleNames;
parts.push([
'(function() {',
' "use strict";',
'',
' /*',
' * Generated by PEG.js 0.9.0.',
' *',
' * http://pegjs.org/',
' */',
'',
'function peg$subclass(child, parent) {',
' function ctor() { this.constructor = child; }',
' ctor.prototype = parent.prototype;',
@ -889,7 +880,7 @@ function generateJS(ast, options) {
parts.push('');
parts.push(indent8(generateTables()));
parts.push(indent6(generateTables()));
parts.push([
'',
@ -1106,17 +1097,17 @@ function generateJS(ast, options) {
].join('\n'));
if (options.optimize === "size") {
parts.push(indent4(generateInterpreter()));
parts.push(indent2(generateInterpreter()));
parts.push('');
} else {
arrays.each(ast.rules, function(rule) {
parts.push(indent4(generateRuleFunction(rule)));
parts.push(indent2(generateRuleFunction(rule)));
parts.push('');
});
}
if (ast.initializer) {
parts.push(indent4(ast.initializer.code));
parts.push(indent2(ast.initializer.code));
parts.push('');
}
@ -1141,7 +1132,30 @@ function generateJS(ast, options) {
' peg$computeLocation(peg$maxFailPos, peg$maxFailPos)',
' );',
' }',
' }',
'}'
].join('\n'));
return parts.join('\n');
}
function generateWrapper(toplevelCode) {
var parts = [];
parts.push([
'(function() {',
' "use strict";',
'',
' /*',
' * Generated by PEG.js 0.9.0.',
' *',
' * http://pegjs.org/',
' */',
''
].join('\n'));
parts.push(indent2(toplevelCode));
parts.push([
'',
' return {'
].join('\n'));
@ -1164,7 +1178,10 @@ function generateJS(ast, options) {
'})()'
].join('\n'));
ast.code = parts.join('\n');
return parts.join('\n');
}
ast.code = generateWrapper(generateToplevel());
}
module.exports = generateJS;

Loading…
Cancel
Save