From ab0e85b006638114bb9a643e7431726d78b09f05 Mon Sep 17 00:00:00 2001 From: David Majda Date: Mon, 2 May 2016 12:29:29 +0200 Subject: [PATCH] UMD parsers: Refactor "generateWrapper" Extract "generateWrapper" code which generates code of the intro and the returned parser object into helper functions. This is pure refactoring, generated parser code is exactly the same as before. This change will make it easier to modifiy "generateWrapper" to produce UMD modules. Part of work on #362. --- lib/compiler/passes/generate-js.js | 70 +++++++++++++++--------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/lib/compiler/passes/generate-js.js b/lib/compiler/passes/generate-js.js index 0a9a8ee..d03a8ae 100644 --- a/lib/compiler/passes/generate-js.js +++ b/lib/compiler/passes/generate-js.js @@ -1139,44 +1139,44 @@ function generateJS(ast, options) { } 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')); + function generateIntro() { + return [ + '"use strict";', + '', + '/*', + ' * Generated by PEG.js 0.9.0.', + ' *', + ' * http://pegjs.org/', + ' */' + ].join('\n'); + } - if (options.trace) { - parts.push([ - ' SyntaxError: peg$SyntaxError,', - ' DefaultTracer: peg$DefaultTracer,', - ' parse: peg$parse' - ].join('\n')); - } else { - parts.push([ - ' SyntaxError: peg$SyntaxError,', - ' parse: peg$parse' - ].join('\n')); + function generateParserObject() { + return options.trace + ? [ + '{', + ' SyntaxError: peg$SyntaxError,', + ' DefaultTracer: peg$DefaultTracer,', + ' parse: peg$parse', + '}' + ].join('\n') + : [ + '{', + ' SyntaxError: peg$SyntaxError,', + ' parse: peg$parse', + '}' + ].join('\n'); } - parts.push([ - ' };', - '})()' - ].join('\n')); + var parts = []; + + parts.push('(function() {'); + parts.push(indent2(generateIntro())); + parts.push(''); + parts.push(indent2(toplevelCode)); + parts.push(''); + parts.push(indent2('return ' + generateParserObject() + ';')); + parts.push('})()'); return parts.join('\n'); }