Skip to content

Commit

Permalink
Rename Symbol to type
Browse files Browse the repository at this point in the history
  • Loading branch information
morandat committed Apr 18, 2019
1 parent 6e53318 commit 5cda2cd
Show file tree
Hide file tree
Showing 28 changed files with 132 additions and 134 deletions.
4 changes: 2 additions & 2 deletions core/src/main/java/com/github/gumtreediff/io/TreeIoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import java.util.Map.Entry;
import java.util.regex.Pattern;

import static com.github.gumtreediff.tree.SymbolSet.symbol;
import static com.github.gumtreediff.tree.TypeSet.type;

public final class TreeIoUtils {

Expand Down Expand Up @@ -756,7 +756,7 @@ protected TreeContext generate(Reader source) throws IOException {
StartElement s = (StartElement) e;
if (!s.getName().getLocalPart().equals("tree")) // FIXME need to deal with options
continue;
Symbol type = symbol(s.getAttributeByName(TYPE).getValue());
Type type = type(s.getAttributeByName(TYPE).getValue());

ITree t = context.createTree(type, labelForAttribute(s, LABEL));
// FIXME this iterator has no type, due to the API. We have to cast it later
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.github.gumtreediff.matchers.MappingStore;
import com.github.gumtreediff.matchers.Matcher;
import com.github.gumtreediff.tree.ITree;
import com.github.gumtreediff.tree.Symbol;
import com.github.gumtreediff.tree.Type;

import java.util.*;

Expand Down Expand Up @@ -93,8 +93,8 @@ private Set<ITree> getDstCandidates(ITree src) {
}

private void lastChanceMatch(ITree src, ITree dst) {
Map<Symbol, List<ITree>> srcKinds = new HashMap<>();
Map<Symbol, List<ITree>> dstKinds = new HashMap<>();
Map<Type, List<ITree>> srcKinds = new HashMap<>();
Map<Type, List<ITree>> dstKinds = new HashMap<>();
for (ITree c: src.getChildren()) {
if (!srcKinds.containsKey(c.getType())) srcKinds.put(c.getType(), new ArrayList<>());
srcKinds.get(c.getType()).add(c);
Expand All @@ -104,7 +104,7 @@ private void lastChanceMatch(ITree src, ITree dst) {
dstKinds.get(c.getType()).add(c);
}

for (Symbol t: srcKinds.keySet())
for (Type t: srcKinds.keySet())
if (dstKinds.get(t) != null && srcKinds.get(t).size() == dstKinds.get(t).size()
&& srcKinds.get(t).size() == 1)
mappings.addMapping(srcKinds.get(t).get(0), dstKinds.get(t).get(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ public int getEndPos() {
}

@Override
public Symbol getType() {
return Symbol.NO_SYMBOL;
public Type getType() {
return Type.NO_TYPE;
}

@Override
Expand Down Expand Up @@ -268,7 +268,7 @@ public void setPos(int pos) {
}

@Override
public void setType(Symbol type) {
public void setType(Type type) {
throw unsupportedOperation();
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/com/github/gumtreediff/tree/ITree.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,13 @@ default int getEndPos() {
* Returns the type (i.e. IfStatement).
* @return
*/
Symbol getType();
Type getType();

/**
* Sets the type of the node (i.e. IfStatement).
*
*/
void setType(Symbol type);
void setType(Type type);

/**
* @return a boolean indicating if the trees have the same type.
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/com/github/gumtreediff/tree/Tree.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

public class Tree extends AbstractTree implements ITree {

private Symbol type;
private Type type;

private String label;

Expand All @@ -40,9 +40,9 @@ public class Tree extends AbstractTree implements ITree {

/**
* Constructs a new node. If you need type labels corresponding to the integer
* @see TreeContext#createTree(Symbol, String)
* @see TreeContext#createTree(Type, String)
*/
public Tree(Symbol type, String label) {
public Tree(Type type, String label) {
this.type = type;
this.label = (label == null) ? NO_LABEL : label.intern();
this.children = new ArrayList<>();
Expand Down Expand Up @@ -104,7 +104,7 @@ public int getPos() {
}

@Override
public Symbol getType() {
public Type getType() {
return type;
}

Expand Down Expand Up @@ -145,7 +145,7 @@ public void setPos(int pos) {
}

@Override
public void setType(Symbol type) {
public void setType(Type type) {
this.type = type;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public ITree getRoot() {
return root;
}

public ITree createTree(Symbol type, String label) {
public ITree createTree(Type type, String label) {
return new Tree(type, label);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@

package com.github.gumtreediff.tree;

import static com.github.gumtreediff.tree.SymbolSet.symbol;
import static com.github.gumtreediff.tree.TypeSet.type;

public final class Symbol {
public final class Type {

public final String name;

public static final Symbol NO_SYMBOL = symbol("");
public static final Type NO_TYPE = type("");

private Symbol(String value) {
private Type(String value) {
name = value;
}

public boolean isEmpty() {
return this == NO_SYMBOL;
return this == NO_TYPE;
}

@Override
Expand All @@ -45,11 +45,11 @@ public int hashCode() {
return name.hashCode();
}

static class SymbolFactory {
protected SymbolFactory() {}
static class TypeFactory {
protected TypeFactory() {}

protected Symbol makeSymbol(String name) {
return new Symbol(name);
protected Type makeType(String name) {
return new Type(name);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@
import java.util.HashMap;
import java.util.Map;

public class SymbolSet {
private static final SymbolFactoryImplementation implementation = new SymbolFactoryImplementation();
public class TypeSet {
private static final TypeFactoryImplementation implementation = new TypeFactoryImplementation();

public static Symbol symbol(String value) {
return implementation.getSymbol(value);
public static Type type(String value) {
return implementation.makeOrGetType(value);
}

private static class SymbolFactoryImplementation extends Symbol.SymbolFactory {
private final Map<String, Symbol> symbols = new HashMap<>();
private static class TypeFactoryImplementation extends Type.TypeFactory {
private final Map<String, Type> types = new HashMap<>();

public Symbol getSymbol(String name) {
// return symbols.computeIfAbsent(name == null ? "" : name, (key) -> makeSymbol(key));
public Type makeOrGetType(String name) {
// return types.computeIfAbsent(name == null ? "" : name, (key) -> makeType(key));
if (name == null)
name = "";

Symbol sym = symbols.get(name);
Type sym = types.get(name);
if (sym == null) {
sym = makeSymbol(name);
symbols.put(name, sym);
sym = makeType(name);
types.put(name, sym);
}

return sym;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.github.gumtreediff.actions.model.*;
import com.github.gumtreediff.matchers.MappingStore;
import com.github.gumtreediff.tree.ITree;
import com.github.gumtreediff.tree.SymbolSet;
import com.github.gumtreediff.tree.TypeSet;
import com.github.gumtreediff.tree.Tree;
import com.github.gumtreediff.utils.Pair;
import com.github.gumtreediff.tree.TreeContext;
Expand Down Expand Up @@ -117,8 +117,8 @@ public void testWithActionExample() {

@Test
public void testWithUnmappedRoot() {
ITree src = new Tree(SymbolSet.symbol("foo"), "");
ITree dst = new Tree(SymbolSet.symbol("bar"), "");
ITree src = new Tree(TypeSet.type("foo"), "");
ITree dst = new Tree(TypeSet.type("bar"), "");
MappingStore ms = new MappingStore();
ActionGenerator ag = new ActionGenerator(src, dst, ms);
ag.generate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import com.github.gumtreediff.io.TreeIoUtils;
import com.github.gumtreediff.tree.ITree;
import com.github.gumtreediff.tree.Symbol;
import com.github.gumtreediff.tree.TreeContext;
import com.google.common.collect.Sets;
import org.junit.Before;
Expand All @@ -34,7 +33,7 @@
import java.util.List;
import java.util.Map.Entry;

import static com.github.gumtreediff.tree.SymbolSet.symbol;
import static com.github.gumtreediff.tree.TypeSet.type;
import static org.junit.Assert.*;

public class TestMetadata {
Expand All @@ -49,7 +48,7 @@ public class TestMetadata {
@Before
public void setUp() throws Exception {
tc = new TreeContext();
someNode = tc.createTree(symbol("type0"), "");
someNode = tc.createTree(type("type0"), "");
tc.setRoot(someNode);
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/test/java/com/github/gumtreediff/test/TestTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.ArrayList;
import java.util.List;

import com.github.gumtreediff.tree.SymbolSet;
import com.github.gumtreediff.tree.TypeSet;
import com.github.gumtreediff.tree.Tree;
import com.github.gumtreediff.tree.TreeUtils;
import org.junit.Test;
Expand Down Expand Up @@ -92,7 +92,7 @@ public void testIsomophism() {
assertFalse(root.isIsomorphicTo(rootCpy));
root.getChild(0).getChild(0).setLabel("foo");
assertTrue(root.isIsomorphicTo(rootCpy));
rootCpy.addChild(new Tree(SymbolSet.symbol("foo"), "toto"));
rootCpy.addChild(new Tree(TypeSet.type("foo"), "toto"));
assertFalse(root.isIsomorphicTo(rootCpy));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

import com.github.gumtreediff.io.TreeIoUtils;
import com.github.gumtreediff.tree.ITree;
import static com.github.gumtreediff.tree.SymbolSet.symbol;
import com.github.gumtreediff.tree.Symbol;
import static com.github.gumtreediff.tree.TypeSet.type;
import com.github.gumtreediff.tree.Type;
import com.github.gumtreediff.tree.TreeContext;
import org.junit.Test;

Expand All @@ -36,10 +36,10 @@

public class TestTreeIoUtils {

private static final Symbol TYPE_0 = symbol("TYPE_0");
private static final Symbol TYPE_1 = symbol("TYPE_1");
private static final Symbol TYPE_2 = symbol("TYPE_2");
private static final Symbol TYPE_3 = symbol("TYPE_3");
private static final Type TYPE_0 = type("TYPE_0");
private static final Type TYPE_1 = type("TYPE_1");
private static final Type TYPE_2 = type("TYPE_2");
private static final Type TYPE_3 = type("TYPE_3");

@Test
public void testSerializeTree() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@

package com.github.gumtreediff.gen.antlr3.json;

import static com.github.gumtreediff.tree.SymbolSet.symbol;
import static com.github.gumtreediff.tree.TypeSet.type;
import static org.junit.Assert.*;

import com.github.gumtreediff.tree.*;
import org.junit.Test;

public class TestJsonParsing {

public static final Symbol ARRAY = symbol(JSONParser.tokenNames[JSONParser.ARRAY]);
public static final Type ARRAY = type(JSONParser.tokenNames[JSONParser.ARRAY]);

@Test
public void testJsonParsing() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,27 @@

import com.github.gumtreediff.tree.ITree;
import com.github.gumtreediff.tree.MetricProviderFactory;
import com.github.gumtreediff.tree.Symbol;
import com.github.gumtreediff.tree.Type;
import com.github.gumtreediff.tree.TreeMetricsProvider;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import static com.github.gumtreediff.tree.SymbolSet.symbol;
import static com.github.gumtreediff.tree.TypeSet.type;
import static org.junit.Assert.*;

@RunWith(Parameterized.class)
public class TestRGenerator {

public static final Symbol SEQUENCE = symbol(RParser.tokenNames[RParser.SEQUENCE]);
public static final Type SEQUENCE = type(RParser.tokenNames[RParser.SEQUENCE]);

private final String input;
private final Symbol expectedRootSymbol;
private final Type expectedRootType;
private final int expectedSize;

public TestRGenerator(String input, Symbol expectedRootSymbol, int expectedSize) {
public TestRGenerator(String input, Type expectedRootType, int expectedSize) {
this.input = input;
this.expectedRootSymbol = expectedRootSymbol;
this.expectedRootType = expectedRootType;
this.expectedSize = expectedSize;
}

Expand All @@ -61,7 +61,7 @@ public static Collection provideStringAndExpectedLength() {
public void testSimpleParse() throws IOException {
ITree t = new RTreeGenerator().generateFrom().string(input).getRoot();
TreeMetricsProvider m = MetricProviderFactory.computeTreeMetrics(t);
assertEquals(expectedRootSymbol, t.getType());
assertEquals(expectedRootType, t.getType());
assertEquals(expectedSize, m.get(t).size);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,19 @@
import com.github.gumtreediff.gen.Register;
import com.github.gumtreediff.gen.antlr3.AbstractAntlr3TreeGenerator;
import com.github.gumtreediff.tree.ITree;
import com.github.gumtreediff.tree.Symbol;
import com.github.gumtreediff.tree.Type;
import com.github.gumtreediff.tree.TreeContext;
import com.github.gumtreediff.tree.TreeUtils;
import org.antlr.runtime.*;

import java.io.IOException;
import java.io.Reader;

import static com.github.gumtreediff.tree.SymbolSet.symbol;
import static com.github.gumtreediff.tree.TypeSet.type;

@Register(id = "xml-antlr", accept = {"\\.xml$", "\\.xsd$", "\\.wadl$"})
public class XmlTreeGenerator extends AbstractAntlr3TreeGenerator<XMLLexer, XMLParser> {

private static final Symbol PCDATA = symbol(XMLParser.tokenNames[XMLParser.PCDATA]);
private static final Type PCDATA = type(XMLParser.tokenNames[XMLParser.PCDATA]);

@Override
public TreeContext generate(Reader file) throws IOException {
Expand Down
Loading

0 comments on commit 5cda2cd

Please sign in to comment.