Rename and generalize |generateCache{Header,Footer}|
Rename |generateCache{Header,Footer}| to |generateRule{Header,Footer}| and change their responsibility to generate overall header/footer of a rule function (when optimizing for speed) or the |peg$parseRule| function (when optimizing for speed). This creates a natural place where to generate tracing code (coming soon).
This commit is contained in:
parent
fb5f6c6ee9
commit
675561f085
|
@ -37,8 +37,10 @@ function generateJavascript(ast, options) {
|
|||
}
|
||||
}
|
||||
|
||||
function generateCacheHeader(ruleIndexCode) {
|
||||
function generateRuleHeader(ruleIndexCode) {
|
||||
if (options.cache) {
|
||||
return [
|
||||
'',
|
||||
'var key = peg$currPos * ' + ast.rules.length + ' + ' + ruleIndexCode + ',',
|
||||
' cached = peg$cache[key];',
|
||||
'',
|
||||
|
@ -48,13 +50,27 @@ function generateJavascript(ast, options) {
|
|||
'}',
|
||||
''
|
||||
].join('\n');
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
function generateCacheFooter(resultCode) {
|
||||
return [
|
||||
function generateRuleFooter(resultCode) {
|
||||
var parts = [];
|
||||
|
||||
if (options.cache) {
|
||||
parts.push([
|
||||
'',
|
||||
'peg$cache[key] = { nextPos: peg$currPos, result: ' + resultCode + ' };'
|
||||
].join('\n');
|
||||
].join('\n'));
|
||||
}
|
||||
|
||||
parts.push([
|
||||
'',
|
||||
'return ' + resultCode + ';'
|
||||
].join('\n'));
|
||||
|
||||
return parts.join('\n');
|
||||
}
|
||||
|
||||
function generateInterpreter() {
|
||||
|
@ -140,12 +156,9 @@ function generateJavascript(ast, options) {
|
|||
' ends = [],',
|
||||
' stack = [],',
|
||||
' params, i;',
|
||||
''
|
||||
].join('\n'));
|
||||
|
||||
if (options.cache) {
|
||||
parts.push(indent2(generateCacheHeader('index')));
|
||||
}
|
||||
parts.push(indent2(generateRuleHeader('index')));
|
||||
|
||||
parts.push([
|
||||
/*
|
||||
|
@ -324,15 +337,8 @@ function generateJavascript(ast, options) {
|
|||
' }'
|
||||
].join('\n'));
|
||||
|
||||
if (options.cache) {
|
||||
parts.push(indent2(generateCacheFooter('stack[0]')));
|
||||
}
|
||||
|
||||
parts.push([
|
||||
'',
|
||||
' return stack[0];',
|
||||
'}'
|
||||
].join('\n'));
|
||||
parts.push(indent2(generateRuleFooter('stack[0]')));
|
||||
parts.push('}');
|
||||
|
||||
return parts.join('\n');
|
||||
}
|
||||
|
@ -649,26 +655,13 @@ function generateJavascript(ast, options) {
|
|||
parts.push([
|
||||
'function peg$parse' + rule.name + '() {',
|
||||
' var ' + arrays.map(arrays.range(0, stack.maxSp + 1), s).join(', ') + ';',
|
||||
''
|
||||
].join('\n'));
|
||||
|
||||
if (options.cache) {
|
||||
parts.push(indent2(
|
||||
generateCacheHeader(asts.indexOfRule(ast, rule.name))
|
||||
));
|
||||
}
|
||||
|
||||
parts.push(indent2(generateRuleHeader(asts.indexOfRule(ast, rule.name))));
|
||||
parts.push(indent2(code));
|
||||
parts.push(indent2(generateRuleFooter(s(0))));
|
||||
|
||||
if (options.cache) {
|
||||
parts.push(indent2(generateCacheFooter(s(0))));
|
||||
}
|
||||
|
||||
parts.push([
|
||||
'',
|
||||
' return ' + s(0) + ';',
|
||||
'}'
|
||||
].join('\n'));
|
||||
parts.push('}');
|
||||
|
||||
return parts.join('\n');
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue