1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-09-07 13:06:47 +00:00
TiddlyWiki5/js/JavaScriptParseTree.js

52 lines
1.6 KiB
JavaScript
Raw Normal View History

/*\
title: js/JavaScriptParseTree.js
2012-01-05 10:00:21 +00:00
This object stores a JavaScript parse tree in the format produced by the pegjs JavaScript parser.
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 compileNode() for a list of the types).
2012-01-05 10:00:21 +00:00
Depending on the type, other fields provide further details about the node.
2012-01-07 11:43:24 +00:00
The pegjs parser uses "StringLiteral" nodes to represent individual string literals. TiddlyWiki adds
support for nodes of type "StringLiterals" that represent a contiguous sequence of string constants.
This simplifies coalescing adjacent constants into a single string.
\*/
(function(){
/*jslint node: true */
"use strict";
var esprima = require("esprima"),
utils = require("./Utils.js");
// Create a new JavaScript tree object
2012-01-05 10:00:21 +00:00
var JavaScriptParseTree = function(tree) {
this.tree = tree;
};
JavaScriptParseTree.prototype.compile = function(targetType) {
if(targetType === "application/javascript") {
return this.compileJS();
} else if(targetType === "text/html") {
return {render: this.toString};
} else {
return null;
}
};
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 */
return {render: eval(esprima.generate(this.tree))};
};
exports.JavaScriptParseTree = JavaScriptParseTree;
})();