Skip to content

Commit

Permalink
[compiler] prepare for super-calls
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed Dec 2, 2011
1 parent f71c2ae commit 3096f87
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions lib/xjst/compiler.js
Expand Up @@ -11,12 +11,13 @@ var xjst = require('../xjst'),
XJSTLocalAndApplyCompiler = xjst.ometa.XJSTLocalAndApplyCompiler;

//
// ### function parse (code)
// ### function parse (code, filename, id)
// #### @code {String} XJST source
// #### @filename {String} (optional) Template's filename
// #### @id {Number} (internal) id
// Returns AST for input string
//
exports.parse = function parse(code, filename) {
exports.parse = function parse(code, filename, id) {
var tree = XJSTParser.matchAll(code, 'topLevel', undefined,
utils.errorHandler(code));

Expand All @@ -25,19 +26,46 @@ exports.parse = function parse(code, filename) {

// Load parent templates
if (filename) {
var dir = path.dirname(filename);
var dir = path.dirname(filename),
templates = tree[1];

// Store initial id
if (id === undefined) id = 0;
tree[3] = [id];

// Load each dependency
tree[2].map(function(filename) {
return path.join(
dir,
path.extname(filename) ? filename : filename + '.xjst'
);
}).forEach(function(filename) {
var content = fs.readFileSync(filename).toString(),
dependency = exports.parse(content, filename);
dependency = exports.parse(content, filename, ++id);

// And add it's templates to current ones
tree[0] = tree[0].concat(dependency[0]);
tree[1] = tree[1].concat(dependency[1]);

// Also propagate it's dependency ids
tree[3] = tree[3].concat(dependency[3]);
});

// If we're in nested dependency or
// this transformation has at least one dependency
if (tree[3].length > 1 || id > 0) {
templates.forEach(function(template) {
tree[3].forEach(function(id) {
// Add !this.__d%id === true to each template statement
// This will allow us to do super-calls
template[0].unshift([
'dep' + id,
['unop', '!', ['get', 'this.__d' + id]],
['get', 'true']
]);
});
});
}
}

return tree;
Expand Down

0 comments on commit 3096f87

Please sign in to comment.