-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
unknown
authored and
unknown
committed
Oct 19, 2012
1 parent
b17890e
commit a508cfd
Showing
7 changed files
with
123,651 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>javascript grammar tree preprocessr</title> | ||
<style> | ||
.symbolName { | ||
display:block; | ||
} | ||
.childContainer { | ||
padding-left:30px; | ||
} | ||
</style> | ||
<script src="../source/LexicalParser.js"></script> | ||
<script src="../source/SyntacticalParser.js"></script> | ||
<script src="../source/Parser.js"></script> | ||
<script> | ||
|
||
var preprocessors = { | ||
"VariableDeclarationListNoIn":preprocessVariable, | ||
"VariableDeclarationList":preprocessVariable, | ||
"FunctionDeclaration":preprocessFunctionDeclaration, | ||
"FunctionExpression":preprocessFunctionExpression | ||
} | ||
var processors = { | ||
"FunctionDeclaration":processFunctionDeclaration, | ||
"FunctionExpression":processFunctionExpression, | ||
"FunctionBody":processCode, | ||
"Program":processCode, | ||
"FormalParameterList":processParameter, | ||
"Identifier":processIdentifier | ||
} | ||
|
||
function preprocessVariable(scope,node) { | ||
var i = node.childNodes.length; | ||
scope.addIdentifier(node.childNodes[0].childNodes[node.childNodes[0].childNodes.length-1].token.toString()); | ||
while(i--) { | ||
preprocess(scope,node.childNodes[i]); | ||
} | ||
} | ||
|
||
function preprocessFunctionDeclaration(scope,node) { | ||
var i = node.childNodes.length; | ||
while(i--){ | ||
if(node.childNodes[i].name == "Identifier") { | ||
scope.addIdentifier(node.childNodes[i].token.toString()); | ||
} | ||
} | ||
} | ||
function preprocessFunctionExpression(scope,node) { | ||
|
||
} | ||
function preprocessCode(scope,node) { | ||
var i = node.childNodes.length; | ||
while(i--)preprocess(scope,node.childNodes[i]); | ||
} | ||
|
||
function preprocess(scope,node) { | ||
if(preprocessors[node.name]) return preprocessors[node.name](scope,node); | ||
|
||
var i = node.childNodes.length; | ||
while(i--)preprocess(scope,node.childNodes[i]); | ||
} | ||
|
||
function processParameter(scope,node) { | ||
var i = node.childNodes.length; | ||
while(i--) { | ||
if(node.childNodes[i].token) | ||
scope.addIdentifier(node.childNodes[i].token.toString()); | ||
else | ||
preprocessParameter(node.childNodes[i]); | ||
} | ||
} | ||
function processFunctionExpression(scope,node) { | ||
scope = new Scope(scope); | ||
var i = node.childNodes.length; | ||
while(i--){ | ||
if(node.childNodes[i].name == "Identifier") { | ||
scope.addIdentifier(node.childNodes[i].token.toString()); | ||
} | ||
} | ||
scope = new Scope(scope); | ||
|
||
var i = node.childNodes.length; | ||
while(i--){ | ||
if(node.childNodes[i].name == "FormalParameterList") { | ||
process(scope,node.childNodes[i]); | ||
} | ||
} | ||
var i = node.childNodes.length; | ||
while(i--){ | ||
if(node.childNodes[i].name == "FunctionBody") { | ||
process(scope,node.childNodes[i]); | ||
} | ||
} | ||
} | ||
function processFunctionDeclaration(scope,node) { | ||
scope = new Scope(scope); | ||
var i = node.childNodes.length; | ||
while(i--){ | ||
if(node.childNodes[i].name == "FormalParameterList") { | ||
process(scope,node.childNodes[i]); | ||
} | ||
} | ||
var i = node.childNodes.length; | ||
while(i--){ | ||
if(node.childNodes[i].name == "FunctionBody") { | ||
process(scope,node.childNodes[i]); | ||
} | ||
} | ||
} | ||
function processIdentifier(scope,node) { | ||
scope.accessIdentifier(node.token.toString()); | ||
} | ||
function processCode(scope,node) { | ||
preprocess(scope,node); | ||
var i = node.childNodes.length; | ||
while(i--)process(scope,node.childNodes[i]); | ||
} | ||
function process(scope,node) { | ||
if(processors[node.name]) return processors[node.name](scope,node); | ||
var i = node.childNodes.length; | ||
while(i--)process(scope,node.childNodes[i]); | ||
} | ||
|
||
function Scope(parent){ | ||
|
||
this.parent = parent || null; | ||
|
||
var table = Object.create(null); | ||
|
||
this.accessIdentifier = function(id){ | ||
if(!this.parent) { | ||
table[id] = undefined; | ||
} | ||
else if(!Object.prototype.hasOwnProperty.call(table,id)) { | ||
this.parent.accessIdentifier(id); | ||
} | ||
} | ||
this.addIdentifier = function(id){ | ||
table[id] = undefined; | ||
} | ||
this.toString = function(){ | ||
return table; | ||
} | ||
} | ||
|
||
window.onload = function(){ | ||
|
||
document.querySelector("button").onclick = function(){ | ||
try { | ||
var parser = new Parser(); | ||
preprocessr.innerHTML = ""; | ||
var tree = parser.parse(document.querySelector("#code").value); | ||
var o = new Scope(); | ||
process(o,tree); | ||
console.log(o.toString()); | ||
} catch(e) { | ||
preprocessr.innerHTML = "<span style=\"color:red;\">"+e+"</span>" | ||
} | ||
} | ||
} | ||
</script> | ||
</head> | ||
<body> | ||
<textarea id="code" style="width:100%;height:500px;"></textarea> | ||
<button >parse</button> | ||
<div id="preprocessr"> | ||
|
||
</div> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.