merge all into single repo
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
description = 'GumTree tree generator for JavaScript code (Rhino based).'
|
||||
|
||||
dependencies {
|
||||
compile 'org.mozilla:rhino:1.7.7'
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.github.gumtreediff</groupId>
|
||||
<artifactId>gumtree</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>gen.js</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>Gumtree JS Tree Generator</name>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.github.gumtreediff</groupId>
|
||||
<artifactId>core</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mozilla</groupId>
|
||||
<artifactId>rhino</artifactId>
|
||||
<version>1.7R4</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* This file is part of GumTree.
|
||||
*
|
||||
* GumTree is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GumTree is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with GumTree. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Copyright 2011 Jean-Rémy Falleri
|
||||
*/
|
||||
|
||||
package com.github.gumtreediff.gen.js;
|
||||
|
||||
import com.github.gumtreediff.gen.Register;
|
||||
import com.github.gumtreediff.gen.Registry;
|
||||
import com.github.gumtreediff.gen.TreeGenerator;
|
||||
import com.github.gumtreediff.tree.TreeContext;
|
||||
import org.mozilla.javascript.CompilerEnvirons;
|
||||
import org.mozilla.javascript.Parser;
|
||||
import org.mozilla.javascript.ast.AstRoot;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
|
||||
@Register(id = "js-rhino", accept = "\\.js$", priority = Registry.Priority.MAXIMUM)
|
||||
public class RhinoTreeGenerator extends TreeGenerator {
|
||||
|
||||
public TreeContext generate(Reader r) throws IOException {
|
||||
CompilerEnvirons env = new CompilerEnvirons();
|
||||
env.setRecordingLocalJsDocComments(true);
|
||||
env.setAllowSharpComments(true);
|
||||
env.setRecordingComments(true);
|
||||
Parser p = new Parser(env);
|
||||
AstRoot root = p.parse(r, null, 1);
|
||||
RhinoTreeVisitor visitor = new RhinoTreeVisitor(root);
|
||||
root.visitAll(visitor);
|
||||
return visitor.getTree(root);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* This file is part of GumTree.
|
||||
*
|
||||
* GumTree is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GumTree is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with GumTree. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Copyright 2011-2015 Jean-Rémy Falleri <jr.falleri@gmail.com>
|
||||
* Copyright 2011-2015 Floréal Morandat <florealm@gmail.com>
|
||||
*/
|
||||
|
||||
package com.github.gumtreediff.gen.js;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.mozilla.javascript.Token;
|
||||
import org.mozilla.javascript.ast.*;
|
||||
|
||||
import com.github.gumtreediff.tree.ITree;
|
||||
import com.github.gumtreediff.tree.TreeContext;
|
||||
|
||||
public class RhinoTreeVisitor implements NodeVisitor {
|
||||
|
||||
private Map<, ITree> trees;
|
||||
private TreeContext context;
|
||||
|
||||
public RhinoTreeVisitor(AstRoot root) {
|
||||
trees = new HashMap<>();
|
||||
context = new TreeContext();
|
||||
ITree tree = buildTree(root);
|
||||
context.setRoot(tree);
|
||||
}
|
||||
|
||||
public TreeContext getTree(AstNode root) {
|
||||
return context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(AstNode node) {
|
||||
if (node instanceof AstRoot)
|
||||
return true;
|
||||
else {
|
||||
ITree t = buildTree(node);
|
||||
ITree p = trees.get(node.getParent());
|
||||
p.addChild(t);
|
||||
|
||||
if (node instanceof Name) {
|
||||
Name name = (Name) node;
|
||||
t.setLabel(name.getIdentifier());
|
||||
} else if ( node instanceof StringLiteral) {
|
||||
StringLiteral literal = (StringLiteral) node;
|
||||
t.setLabel(literal.getValue());
|
||||
} else if ( node instanceof NumberLiteral) {
|
||||
NumberLiteral l = (NumberLiteral) node;
|
||||
t.setLabel(l.getValue());
|
||||
} else if ( node instanceof Comment) {
|
||||
Comment c = (Comment) node;
|
||||
t.setLabel(c.getValue());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private ITree buildTree(AstNode node) {
|
||||
ITree t = context.createTree(node.getType(), ITree.NO_LABEL, Token.typeToName(node.getType()));
|
||||
t.setPos(node.getAbsolutePosition());
|
||||
t.setLength(node.getLength());
|
||||
trees.put(node, t);
|
||||
return t;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* This file is part of GumTree.
|
||||
*
|
||||
* GumTree is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GumTree is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with GumTree. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Copyright 2011-2015 Jean-Rémy Falleri <jr.falleri@gmail.com>
|
||||
* Copyright 2011-2015 Floréal Morandat <florealm@gmail.com>
|
||||
*/
|
||||
|
||||
package com.github.gumtreediff.gen.js;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.github.gumtreediff.tree.ITree;
|
||||
|
||||
public class TestJsGenerator {
|
||||
|
||||
@Test
|
||||
public void testStatement() throws IOException {
|
||||
String input = "console.log(\"Hello world!\");";
|
||||
ITree tree = new RhinoTreeGenerator().generateFromString(input).getRoot();
|
||||
assertEquals(7, tree.getSize());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComment() throws IOException {
|
||||
String input = "console.log(\"Hello world!\"); /* with comment */";
|
||||
ITree tree = new RhinoTreeGenerator().generateFromString(input).getRoot();
|
||||
assertEquals(8, tree.getSize());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComplexFile() throws IOException {
|
||||
Reader r = new InputStreamReader(getClass().getResourceAsStream("/sample.js"));
|
||||
ITree tree = new RhinoTreeGenerator().generateFromReader(r).getRoot();
|
||||
assertEquals(402, tree.getSize());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* This file is part of GumTree.
|
||||
*
|
||||
* GumTree is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GumTree is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with GumTree. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Copyright 2011-2015 Jean-Rémy Falleri <jr.falleri@gmail.com>
|
||||
* Copyright 2011-2015 Floréal Morandat <florealm@gmail.com>
|
||||
*/
|
||||
|
||||
currentMapping = 0;
|
||||
|
||||
if (typeof String.prototype.startsWith != 'function') {
|
||||
String.prototype.startsWith = function (str){
|
||||
return this.slice(0, str.length) == str;
|
||||
};
|
||||
}
|
||||
|
||||
function getMappedElement(eltId) {
|
||||
if (eltId.startsWith("move-src")) {
|
||||
return eltId.replace("src","dst");
|
||||
}
|
||||
else {
|
||||
return eltId.replace("dst","src");
|
||||
}
|
||||
}
|
||||
|
||||
function nextMapping() {
|
||||
if (currentMapping == 0) {
|
||||
currentMapping = 1;
|
||||
return "#mapping-" + currentMapping.toString();
|
||||
} else {
|
||||
currentMapping++;
|
||||
|
||||
if ($("#mapping-" + currentMapping.toString()).length > 0) {
|
||||
return "#mapping-" + currentMapping.toString();
|
||||
} else {
|
||||
currentMapping = 1;
|
||||
return "#mapping-" + currentMapping.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function isSrc(eltId) {
|
||||
return eltId.startsWith("move-src");
|
||||
}
|
||||
|
||||
$("body").keypress(
|
||||
function (event) {
|
||||
console.log(event.which.toString());
|
||||
if (event.which == 110) {
|
||||
var mapping = nextMapping();
|
||||
$('html, body').animate({scrollTop: $(mapping).offset().top - 200}, 100);
|
||||
} else if (event.which == 116) {
|
||||
$('html, body').animate({scrollTop: 0}, 100);
|
||||
} else if (event.which == 98) {
|
||||
$("html, body").animate({ scrollTop: $(document).height() }, 100);
|
||||
} else if (event.which == 113) {
|
||||
window.location = "/quit";
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
$("span.mv.token, span.token.upd").click(
|
||||
function(event) {
|
||||
if ($(this).hasClass("selected")) {
|
||||
$("span.mv.token, span.token.upd").removeClass("selected");
|
||||
} else {
|
||||
$("span.mv.token, span.token.upd").removeClass("selected");
|
||||
var eltId = $(this).attr("id");
|
||||
var refEltId = getMappedElement(eltId);
|
||||
$("#" + refEltId).addClass("selected");
|
||||
$(this).addClass("selected");
|
||||
var sel = "#dst";
|
||||
if (isSrc(refEltId)) var sel = "#src";
|
||||
|
||||
$div = $(sel);
|
||||
$span = $("#" + refEltId);
|
||||
}
|
||||
event.stopPropagation();
|
||||
}
|
||||
)
|
||||
|
||||
$("span.add.token, span.token.del").click(
|
||||
function(event) {
|
||||
$("span.mv.token, span.token.upd").removeClass("selected");
|
||||
event.stopPropagation();
|
||||
}
|
||||
)
|
||||
|
||||
$("span.token").hover(
|
||||
function (event) {
|
||||
$(this).tooltip('show');
|
||||
event.stopPropagation();
|
||||
},
|
||||
function (event) {
|
||||
$(this).tooltip('hide');
|
||||
event.stopPropagation();
|
||||
}
|
||||
);
|
||||
Reference in New Issue
Block a user