summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsefftinge2008-02-29 03:09:44 (EST)
committer sefftinge2008-02-29 03:09:44 (EST)
commit1a5bdbec732bbea19aee99949b077779dee00aed (patch)
tree539a055476bc890c2c8b926c72faf4c1c4fa9f18
parentb824cfe46e51515a7bb7b13bba1178a11ab457fd (diff)
downloadorg.eclipse.xpand-1a5bdbec732bbea19aee99949b077779dee00aed.zip
org.eclipse.xpand-1a5bdbec732bbea19aee99949b077779dee00aed.tar.gz
org.eclipse.xpand-1a5bdbec732bbea19aee99949b077779dee00aed.tar.bz2
- normailize root nodes if ppropriate
- fix text indices
-rw-r--r--plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/node/NodeUtil.java31
-rw-r--r--plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/parser/AbstractXpand3NodeParser.java40
2 files changed, 41 insertions, 30 deletions
diff --git a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/node/NodeUtil.java b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/node/NodeUtil.java
index b6f85e9..334b131 100644
--- a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/node/NodeUtil.java
+++ b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/node/NodeUtil.java
@@ -12,7 +12,7 @@ public class NodeUtil {
if (n.getChildren().isEmpty()) {
return -1;
}
- return n.getChildren().get(n.getChildren().size()-1).end();
+ return n.getChildren().get(n.getChildren().size() - 1).end();
}
public static int line(CompositeNode n) {
@@ -29,7 +29,7 @@ public class NodeUtil {
}
return buff.toString();
}
-
+
public static int start(LeafNode ln) {
return ln.getToken().getStart();
}
@@ -45,16 +45,16 @@ public class NodeUtil {
public static String text(LeafNode ln) {
return ln.getToken().getText();
}
-
+
public static String toString(Node n) {
if (n instanceof CompositeNode) {
- return toString((CompositeNode)n);
+ return toString((CompositeNode) n);
} else if (n instanceof LeafNode) {
- return toString((LeafNode)n);
+ return toString((LeafNode) n);
}
throw new IllegalArgumentException();
}
-
+
public static String toString(CompositeNode n) {
String s = indent(n)+(n.getAlias()!=null?n.getAlias()+"=":"")+"Rule: "+n.getRule()+"\n";
for (Node node : n.getChildren()) {
@@ -62,22 +62,25 @@ public class NodeUtil {
}
return s;
}
-
+
public static String toString(LeafNode n) {
return indent(n)+(n.getAlias()!=null?n.getAlias()+"=":"")+n.getToken().getText()+"\n";
}
-
+
private static String indent(Node n) {
- return n.eContainer()==null ? "" : "\t"+indent((Node) n.eContainer());
+ return n.eContainer() == null ? "" : "\t"
+ + indent((Node) n.eContainer());
}
+
public static String serialize(Node n) {
if (n instanceof CompositeNode) {
- return serialize((CompositeNode)n);
+ return serialize((CompositeNode) n);
} else if (n instanceof LeafNode) {
- return serialize((LeafNode)n);
+ return serialize((LeafNode) n);
}
throw new IllegalArgumentException();
}
+
public static String serialize(CompositeNode n) {
String s = "";
for (Node node : n.getChildren()) {
@@ -85,9 +88,9 @@ public class NodeUtil {
}
return s;
}
-
+
public static String serialize(LeafNode n) {
- return n.getToken().getText()+"\n";
+ return n.getToken().getText() + "\n";
}
-
+
}
diff --git a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/parser/AbstractXpand3NodeParser.java b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/parser/AbstractXpand3NodeParser.java
index 37c6e61..8dfb0f9 100644
--- a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/parser/AbstractXpand3NodeParser.java
+++ b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/parser/AbstractXpand3NodeParser.java
@@ -7,6 +7,7 @@ import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.Token;
import org.antlr.runtime.TokenStream;
import org.antlr.runtime.tree.TreeAdaptor;
+import org.eclipse.emf.common.util.EList;
import org.eclipse.xpand3.node.CompositeNode;
import org.eclipse.xpand3.node.LeafNode;
import org.eclipse.xpand3.node.LexedToken;
@@ -20,7 +21,7 @@ public abstract class AbstractXpand3NodeParser extends Xpand3Parser {
public AbstractXpand3NodeParser(TokenStream input) {
super(input);
- setTreeAdaptor(new TreeAdaptor(){
+ setTreeAdaptor(new TreeAdaptor() {
public void addChild(Object t, Object child) {
}
@@ -109,7 +110,8 @@ public abstract class AbstractXpand3NodeParser extends Xpand3Parser {
}
public void setType(Object t, int type) {
- }});
+ }
+ });
}
protected abstract Set<String> normalizableRules();
@@ -119,7 +121,7 @@ public abstract class AbstractXpand3NodeParser extends Xpand3Parser {
CompositeNode newOne = NodeFactory.eINSTANCE.createCompositeNode();
newOne.setRule(rulename);
newOne.setAlias(next);
- next=null;
+ next = null;
if (current != null) {
current.getChildren().add(newOne);
} else {
@@ -127,26 +129,32 @@ public abstract class AbstractXpand3NodeParser extends Xpand3Parser {
}
current = newOne;
}
- private String next=null;
+
+ private String next = null;
+
@Override
protected void assignNextNodeTo(String alias) {
this.next = alias;
}
-
@Override
public void ruleLeft(String ruleName) {
CompositeNode parent = (CompositeNode) current.eContainer();
- if (normalizableRules().contains(current.getRule())
- && current.getChildren().size() == 1
- && (current.getChildren().get(0) instanceof CompositeNode)) {
- Node child = current.getChildren().get(0);
- if (parent != null) {
- int i = parent.getChildren().indexOf(current);
- parent.getChildren().remove(i);
- parent.getChildren().add(i, child);
- } else {
- rootNode = child;
+ if (normalizableRules().contains(current.getRule())) {
+ EList<Node> children = current.getChildren();
+ if (children.isEmpty() && parent == null) {
+ rootNode = null;
+ } else if (children.size() == 1
+ && (children.get(0) instanceof CompositeNode)) {
+ Node child = children.get(0);
+ if (parent != null) {
+ int i = parent.getChildren().indexOf(current);
+ parent.getChildren().remove(i);
+ parent.getChildren().add(i, child);
+ } else {
+ children.remove(0);
+ rootNode = child;
+ }
}
}
current = parent;
@@ -168,7 +176,7 @@ public abstract class AbstractXpand3NodeParser extends Xpand3Parser {
n.setToken(myToken);
myToken.setText(ct.getText());
myToken.setStart(ct.getStartIndex());
- myToken.setEnd(ct.getStopIndex());
+ myToken.setEnd(ct.getStopIndex() + 1);
myToken.setLine(ct.getLine());
}
}