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.
redux
David Majda 9 years ago
parent 7f8b3f7012
commit ab0e85b006

@ -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');
}

Loading…
Cancel
Save