From 6166ca3d368fdf82bc04bd6027114c5c1ce3e678 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Thu, 9 Feb 2012 13:15:37 +0000 Subject: [PATCH] Refactored the naming of the JavaScript parser methods --- js/JavaScriptParseTree.js | 70 +++++++++++++++++++++++++-------------- js/WikiTextParseTree.js | 10 +++--- 2 files changed, 50 insertions(+), 30 deletions(-) diff --git a/js/JavaScriptParseTree.js b/js/JavaScriptParseTree.js index 04e3fbc86..2090d4bbc 100644 --- a/js/JavaScriptParseTree.js +++ b/js/JavaScriptParseTree.js @@ -5,7 +5,7 @@ This object stores a JavaScript parse tree in the format produced by the pegjs J The parse tree represents the syntactical structure of a JavaScript program, represented as a tree structure built from JavaScript arrays and objects. The nodes of the tree are objects with a "type" -field that indicates the type of the node (see the function renderNode() for a list of the types). +field that indicates the type of the node (see the function compileNode() for a list of the types). Depending on the type, other fields provide further details about the node. The pegjs parser uses "StringLiteral" nodes to represent individual string literals. TiddlyWiki adds @@ -25,30 +25,50 @@ var JavaScriptParseTree = function(tree) { this.tree = tree; }; -// Render the entire JavaScript tree object to JavaScript source code -JavaScriptParseTree.prototype.render = function() { - var output = []; - if(this.tree instanceof Array) { - this.renderSubTree(output,this.tree); +JavaScriptParseTree.prototype.compile = function(targetType) { + if(targetType === "application/javascript") { + return this.compileJS(); + } else if(targetType === "text/html") { + return {render: this.toString}; } else { - this.renderNode(output,this.tree); + return null; } - var r = output.join(""); - return r; }; -// Render a subtree of the parse tree to an array of fragments of JavaScript source code -JavaScriptParseTree.prototype.renderSubTree = function(output,tree) { +JavaScriptParseTree.prototype.toString = function() { + return JSON.stringify(this.tree); +}; + +// Compile the entire JavaScript tree object to a renderer object +JavaScriptParseTree.prototype.compileJS = function() { + /*jslint evil: true */ + var output = []; + if(this.tree instanceof Array) { + this.compileSubTree(output,this.tree); + } else { + this.compileNode(output,this.tree); + } + var r = output.join(""); + return {render: eval(r)}; +}; + +// Compile a subtree of the parse tree to an array of fragments of JavaScript source code +JavaScriptParseTree.prototype.compileSubTree = function(output,tree) { for(var t=0; t