1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-27 03:57:21 +00:00

Cleaning up the JavaScript parser

This commit is contained in:
Jeremy Ruston 2012-01-05 10:00:21 +00:00
parent 1e02389f46
commit b1b1b84b3e
2 changed files with 14 additions and 6 deletions

View File

@ -1,7 +1,12 @@
/*\
title: js/JavaScriptParseTree.js
JavaScript parse tree
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 renderNode() for a list of the types).
Depending on the type, other fields provide further details about the node.
\*/
(function(){
@ -12,9 +17,8 @@ JavaScript parse tree
var utils = require("./Utils.js");
// Create a new JavaScript tree object
var JavaScriptParseTree = function(tree,processor) {
var JavaScriptParseTree = function(tree) {
this.tree = tree;
this.processor = processor;
};
// Render the entire JavaScript tree object to JavaScript source code
@ -24,6 +28,7 @@ JavaScriptParseTree.prototype.render = function() {
return this.output.join("");
};
// Render a subtree of the parse tree to an array of fragments of JavaScript source code
JavaScriptParseTree.prototype.renderSubTree = function(tree) {
for(var t=0; t<tree.length; t++) {
if(t) {
@ -33,7 +38,7 @@ JavaScriptParseTree.prototype.renderSubTree = function(tree) {
}
};
// Compile a javascript tree into an array of string fragments
// Compile a JavaScript node to an array of fragments of JavaScript source code
JavaScriptParseTree.prototype.renderNode = function(node) {
var p;
switch(node.type) {

View File

@ -1,7 +1,7 @@
/*\
title: js/JavaScriptParser.js
JavaScript processing
Parses JavaScript source code into a parse tree using PEGJS
\*/
(function(){
@ -12,16 +12,19 @@ JavaScript processing
var JavaScriptParseTree = require("./JavaScriptParseTree.js").JavaScriptParseTree,
pegjs = require("pegjs");
// Initialise the parser
var JavaScriptParser = function(parserText) {
this.parser = pegjs.buildParser(parserText);
};
// Parse a string of JavaScript code and return the parse tree
JavaScriptParser.prototype.parse = function(code) {
return new JavaScriptParseTree(this.parser.parse(code),this);
};
// Create a parse tree object from a raw tree
JavaScriptParser.prototype.createTree = function(tree) {
return new JavaScriptParseTree(tree,this);
return new JavaScriptParseTree(tree);
};
exports.JavaScriptParser = JavaScriptParser;