Cleaning up the JavaScript parser

print-window-tiddler
Jeremy Ruston 2012-01-05 10:00:21 +00:00
rodzic 1e02389f46
commit b1b1b84b3e
2 zmienionych plików z 14 dodań i 6 usunięć

Wyświetl plik

@ -1,7 +1,12 @@
/*\ /*\
title: js/JavaScriptParseTree.js 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(){ (function(){
@ -12,9 +17,8 @@ JavaScript parse tree
var utils = require("./Utils.js"); var utils = require("./Utils.js");
// Create a new JavaScript tree object // Create a new JavaScript tree object
var JavaScriptParseTree = function(tree,processor) { var JavaScriptParseTree = function(tree) {
this.tree = tree; this.tree = tree;
this.processor = processor;
}; };
// Render the entire JavaScript tree object to JavaScript source code // Render the entire JavaScript tree object to JavaScript source code
@ -24,6 +28,7 @@ JavaScriptParseTree.prototype.render = function() {
return this.output.join(""); 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) { JavaScriptParseTree.prototype.renderSubTree = function(tree) {
for(var t=0; t<tree.length; t++) { for(var t=0; t<tree.length; t++) {
if(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) { JavaScriptParseTree.prototype.renderNode = function(node) {
var p; var p;
switch(node.type) { switch(node.type) {

Wyświetl plik

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