Permalink
Browse files

[exporter] added docs

  • Loading branch information...
1 parent dfe15d2 commit a9bbfce0699f1e4bfcdd542e2348b551e4d16a5b @indutny indutny committed Nov 30, 2011
Showing with 24 additions and 0 deletions.
  1. +24 −0 lib/xjst/exporter.js
View
@@ -4,20 +4,35 @@ var xjst = require('../xjst'),
utils = xjst.utils,
fs = require('fs');
+//
+// ### function stringify (value)
+// #### @value {Array} ast
+// Returns compiled javascript code.
+//
function stringify(value) {
return xjst.ometa.XJSTCompiler.match(value, 'trans');
}
+//
+// ### function traverse (tree, def)
+// #### @tree {Array} ast
+// #### @def {Boolean} Internal, true if we came here from 'default' clause
+// Traverse tree and return it's nodes and edges in graphviz format
+//
function traverse(tree, def) {
var result = [tree.id];
+ // Do not loop
if (tree.graph_seen) return;
tree.graph_seen = true;
+ // If we're entering switch - return labeled edges
if (tree['switch']) {
var labels = [];
tree.cases.forEach(function(branch, i) {
+ // Skip nodes with tag unexpected, because they're having too many
+ // predecessors
if (branch[1].tag === 'unexpected') return;
labels.push('<f' + i + '> ' + stringify(branch[0]));
@@ -28,6 +43,7 @@ function traverse(tree, def) {
);
});
+ // Enter default clause
if (tree['default'].tag !== 'unexpected') {
result = result.concat(traverse(tree['default'], true));
result.push(
@@ -46,10 +62,12 @@ function traverse(tree, def) {
'}';
result[0] += ' [label=' + JSON.stringify(label) + ']';
} else {
+ // Colorize end-points
if (def) {
result[0] += ' [shape=circle, color=red]';
}
+ // Add blue redirection edges
if (tree.routes) {
tree.routes.forEach(function(id) {
result.push(tree.id + ' -> ' + id + ' [color=blue]');
@@ -60,6 +78,12 @@ function traverse(tree, def) {
return result;
}
+//
+// ### function write (tree, filename)
+// #### @tree {Array} AST
+// #### @filename {String} Path to output filename
+// Export tree in graphviz format
+//
exporter.write = function write(tree, filename) {
var content = [
'digraph {',

0 comments on commit a9bbfce

Please sign in to comment.