Skip to content

Commit

Permalink
a static analysis tool
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown authored and unknown committed Oct 19, 2012
1 parent b17890e commit a508cfd
Show file tree
Hide file tree
Showing 7 changed files with 123,651 additions and 5 deletions.
173 changes: 173 additions & 0 deletions samples/Variable.html
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>
2 changes: 1 addition & 1 deletion samples/Visualizer.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
</style>
<script src="../source/LexicalParser.js"></script>
<script src="../source/SyntaticalParser.js"></script>
<script src="../source/SyntacticalParser.js"></script>
<script src="../source/Parser.js"></script>
<script>

Expand Down
8 changes: 4 additions & 4 deletions source/SyntacticalParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@
"Catch":[["catch", "(", "Identifier", ")", "Block"]],
"Finally":[["finally", "Block"]],
"DebuggerStatement":[["debugger", ";"]],
"FunctionDeclaration":[["function", "Identifier", "(", "FormalParameterList", ")", "{", "FunctionBody", "}"], ["function", "Identifier", "(", ")", "{", "FunctionBody", "}"]],
"FunctionExpression":[["function", "Identifier", "(", "FormalParameterList", ")", "{", "FunctionBody", "}"], ["function", "(", "FormalParameterList", ")", "{", "FunctionBody", "}"], ["function", "Identifier", "(", ")", "{", "FunctionBody", "}"], ["function", "(", ")", "{", "FunctionBody", "}"]],
"FunctionDeclaration":[["function", "Identifier", "(", "FormalParameterList", ")", "{", "FunctionBody", "}"],["function", "Identifier", "(", "FormalParameterList", ")", "{", "}"], ["function", "Identifier", "(", ")", "{", "FunctionBody", "}"], ["function", "Identifier", "(", ")", "{", "}"]],
"FunctionExpression":[["function", "Identifier", "(", "FormalParameterList", ")", "{", "FunctionBody", "}"], ["function", "(", "FormalParameterList", ")", "{", "FunctionBody", "}"], ["function", "Identifier", "(", ")", "{", "FunctionBody", "}"], ["function", "(", ")", "{", "FunctionBody", "}"], ["function", "Identifier", "(", "FormalParameterList", ")", "{", "}"], ["function", "(", "FormalParameterList", ")", "{", "}"], ["function", "Identifier", "(", ")", "{", "}"], ["function", "(", ")", "{", "}"]],
"FormalParameterList":[["Identifier"], ["FormalParameterList", ",", "Identifier"]],
"FunctionBody":[["SourceElements"], [""]],
"Program":[["SourceElements"], [""]],
"FunctionBody":[["SourceElements"]],
"Program":[["SourceElements"]],
"SourceElements":[["SourceElement"], ["SourceElements", "SourceElement"]], "SourceElement":[["Statement"], ["FunctionDeclaration"]]

};
Expand Down
Loading

0 comments on commit a508cfd

Please sign in to comment.