calculating AbstrctNode.length() in NodeContentAdapter now

This commit is contained in:
jkohnlein 2008-07-03 15:25:56 +00:00
parent f1dcab3614
commit 8fa77ac6c2
11 changed files with 56 additions and 65 deletions

View file

@ -10,11 +10,6 @@
eType="#//LeafNode"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="AbstractNode" abstract="true">
<eOperations name="length" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt">
<eAnnotations source="http://www.eclipse.org/emf/2002/GenModel">
<details key="body" value="if (this instanceof CompositeNodeImpl) { return ParsetreeUtil.length((CompositeNodeImpl) this);} else if (this instanceof LeafNodeImpl) { return ParsetreeUtil.length((LeafNodeImpl) this);} else {return ParsetreeUtil.length((AbstractNodeImpl) this);}"/>
</eAnnotations>
</eOperations>
<eOperations name="serialize" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString">
<eAnnotations source="http://www.eclipse.org/emf/2002/GenModel">
<details key="body" value="if (this instanceof CompositeNodeImpl) { return ParsetreeUtil.serialize((CompositeNodeImpl) this);} else if (this instanceof LeafNodeImpl) { return ParsetreeUtil.serialize((LeafNodeImpl) this);} else {return ParsetreeUtil.serialize((AbstractNodeImpl) this);}"/>
@ -49,6 +44,8 @@
containment="true" eOpposite="#//SyntaxError/node"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="offset" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="line" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="length" ordered="false"
eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt" defaultValueLiteral="-1"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="LeafNode" eSuperTypes="#//AbstractNode">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="text" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>

View file

@ -49,7 +49,7 @@ public class EnclosingCompositeNodeFinder {
EList<AbstractNode> children = parentNode.getChildren();
for (AbstractNode child : children) {
if (child instanceof LeafNode) {
currentOffset += child.length();
currentOffset += child.getLength();
if (isLookingForStartToken && currentOffset > offset) {
isLookingForStartToken = false;
startTokenParentIndex = currentParentIndex;

View file

@ -133,7 +133,10 @@ public class PartialParsingUtil {
private static void collectNodesEnclosingChangeRegion(CompositeNode parent, int offset, int length,
List<CompositeNode> nodesEnclosingRegion) {
nodesEnclosingRegion.add(parent);
for (AbstractNode child : parent.getChildren()) {
EList<AbstractNode> children = parent.getChildren();
// Hack: iterate children backward, so we evaluate the node.length() function less often
for (int i=children.size()-1; i>=0; --i) {
AbstractNode child = children.get(i);
if (child instanceof CompositeNode) {
CompositeNode childCompositeNode = (CompositeNode) child;
if (nodeEnclosesRegion(childCompositeNode, offset, length)) {
@ -145,7 +148,10 @@ public class PartialParsingUtil {
}
private static boolean nodeEnclosesRegion(CompositeNode node, int offset, int length) {
return node.getOffset() <= offset && node.getOffset() + node.length() >= offset + length;
if(node.getOffset() <= offset) {
return node.getOffset() + node.getLength() >= offset + length;
}
return false;
}
/**
@ -225,7 +231,7 @@ public class PartialParsingUtil {
}
private static boolean nodeIsBeforeRegion(LeafNode node, int offset) {
return node.getOffset() + node.length() <= offset;
return node.getOffset() + node.getLength() <= offset;
}
}

View file

@ -37,7 +37,7 @@ public class NodeContentAdapter extends EContentAdapter {
}
else {
AbstractNode predecessor = parent.getChildren().get(position - 1);
updateOffsetAndLine(child, new NodeInfo((predecessor.getOffset() + predecessor.length()), predecessor.endLine()));
updateOffsetAndLine(child, new NodeInfo((predecessor.getOffset() + predecessor.getLength()), predecessor.endLine()));
}
break;
case Notification.REMOVE:
@ -74,7 +74,7 @@ public class NodeContentAdapter extends EContentAdapter {
}
else {
AbstractNode predecessor = siblings.get(index - 1);
updateOffsetAndLine(targetNode, new NodeInfo((predecessor.getOffset() + predecessor.length()), predecessor.endLine()));
updateOffsetAndLine(targetNode, new NodeInfo((predecessor.getOffset() + predecessor.getLength()), predecessor.endLine()));
}
}
else {
@ -97,13 +97,16 @@ public class NodeContentAdapter extends EContentAdapter {
node.setOffset(info.offset);
node.setLine(info.line);
if (node instanceof LeafNode) {
info.offset += node.length();
node.setLength(((LeafNode)node).getText().length());
info.offset += node.getLength();
info.line = node.endLine();
}
if (node instanceof CompositeNode) {
} else if (node instanceof CompositeNode) {
int length = 0;
for (AbstractNode child : ((CompositeNode) node).getChildren()) {
info = updateOffsetAndLineInContents(child, info);
length += child.getLength();
}
node.setLength(length);
}
return info;
}

View file

@ -106,7 +106,7 @@ public class NodeUtil {
for (LeafNode lookaheadNode : node.getLookaheadLeafNodes()) {
System.out.print("\""+ lookaheadNode.getText() +"\" ");
}
System.out.print(" (" + node.getOffset() + ", " + node.length() + ")");
System.out.print(" (" + node.getOffset() + ", " + node.getLength() + ")");
System.out.println("}");
}

View file

@ -24,23 +24,6 @@ import org.eclipse.xtext.parsetree.SyntaxError;
*/
public class ParsetreeUtil {
public static int length(CompositeNodeImpl compositeNodeImpl) {
int length = 0;
for (AbstractNode node : compositeNodeImpl.getChildren()) {
length += node.length();
}
return length;
}
public static int length(LeafNodeImpl leafNodeImpl) {
return leafNodeImpl.getText().length();
}
public static int length(AbstractNodeImpl abstractParserNode) {
throw new IllegalArgumentException("Illegal subtype of AbstarctParserNode "
+ abstractParserNode.eClass().getName());
}
private static void checkArgument(AbstractNodeImpl abstractParserNode) {
int classifierID = abstractParserNode.eClass().getClassifierID();
if (classifierID != ParsetreePackage.COMPOSITE_NODE && classifierID != ParsetreePackage.LEAF_NODE) {

View file

@ -56,7 +56,7 @@ public class XtextResource extends ResourceImpl {
public void update(int offset, String change) {
CompositeNode rootNode = parseResult.getRootNode();
int length = change.length();
int documentGrowth = length - rootNode.length();
int documentGrowth = length - rootNode.getLength();
int originalLength = length - documentGrowth;
// unloading is required to ensure that any EObjects hanging around (e.g. in the outline) get a proxied URI
@ -70,7 +70,7 @@ public class XtextResource extends ResourceImpl {
addNodeContentAdapter();
}
}
private void addNodeContentAdapter() {
parseResult.getRootNode().eAdapters().add(new NodeContentAdapter());
}

View file

@ -33,7 +33,7 @@ public class ParseErrorHandlingTest extends AbstractGeneratorTest {
assertEquals("%", ((LeafNode)errors.get(0).getNode()).getText());
assertEquals(1, errors.get(0).getNode().getLine());
assertEquals(15, errors.get(0).getNode().getOffset());
assertEquals(1, errors.get(0).getNode().length());
assertEquals(1, errors.get(0).getNode().getLength());
assertEquals(1, errors.size());
}
@ -43,7 +43,7 @@ public class ParseErrorHandlingTest extends AbstractGeneratorTest {
assertEquals("::", ((LeafNode)errors.get(0).getNode()).getText());
assertEquals(1, errors.get(0).getNode().getLine());
assertEquals(31, errors.get(0).getNode().getOffset());
assertEquals(2, errors.get(0).getNode().length());
assertEquals(2, errors.get(0).getNode().getLength());
assertEquals(1, errors.size());
}

View file

@ -10,6 +10,7 @@ package org.eclipse.xtext.parser;
import org.eclipse.xtext.parser.impl.PartialParsingUtil;
import org.eclipse.xtext.parsetree.CompositeNode;
import org.eclipse.xtext.testlanguages.ReferenceGrammarStandaloneSetup;
import org.eclipse.xtext.testlanguages.SimpleExpressionsStandaloneSetup;
/**
@ -18,7 +19,8 @@ import org.eclipse.xtext.testlanguages.SimpleExpressionsStandaloneSetup;
*/
public class PartialParsingPerformanceTest extends AbstractPartialParserTest {
private static final int NUM_ELEMENTS = 200;
private static final int NUM_ELEMENTS = 1;
public void testExpression() throws Exception {
with(SimpleExpressionsStandaloneSetup.class);
StringBuffer modelBuffer = new StringBuffer();
@ -35,29 +37,29 @@ public class PartialParsingPerformanceTest extends AbstractPartialParserTest {
assertTrue(reparse.getParseErrors() == null || reparse.getParseErrors().isEmpty());
}
// public void testReference() throws Exception {
// with(ReferenceGrammarStandaloneSetup.class);
// StringBuffer modelBuffer = new StringBuffer();
// modelBuffer.append("spielplatz 17 {\n");
// for(int i=0; i<NUM_ELEMENTS; ++i) {
// modelBuffer.append(" kind ( Herbert");
// modelBuffer.append(i);
// modelBuffer.append(" 11 )\n");
// }
// for(int i=0; i<NUM_ELEMENTS; ++i) {
// modelBuffer.append(" erwachsener ( Hugo");
// modelBuffer.append(i);
// modelBuffer.append(" 111 )\n");
// }
// modelBuffer.append(" erwachsener ( Sven 112 )\n");
// for(int i=0; i<NUM_ELEMENTS; ++i) {
// modelBuffer.append(" spielzeug ( Schaufel GRÜN )\n");
// }
// modelBuffer.append("}\n");
// String model = modelBuffer.toString();
// CompositeNode rootNode = getRootNode(model);
// IParseResult reparse = PartialParsingUtil.reparse(getParser(), rootNode, model.indexOf("Sven"), 4, "Peter");
// assertTrue(reparse.getParseErrors() == null || reparse.getParseErrors().isEmpty());
// }
public void testReference() throws Exception {
with(ReferenceGrammarStandaloneSetup.class);
StringBuffer modelBuffer = new StringBuffer();
modelBuffer.append("spielplatz 17 {\n");
for(int i=0; i<NUM_ELEMENTS; ++i) {
modelBuffer.append(" kind ( Herbert");
modelBuffer.append(i);
modelBuffer.append(" 11 )\n");
}
for(int i=0; i<NUM_ELEMENTS; ++i) {
modelBuffer.append(" erwachsener ( Hugo");
modelBuffer.append(i);
modelBuffer.append(" 111 )\n");
}
modelBuffer.append(" erwachsener ( Sven 112 )\n");
for(int i=0; i<NUM_ELEMENTS; ++i) {
modelBuffer.append(" spielzeug ( Schaufel GRÜN )\n");
}
modelBuffer.append("}\n");
String model = modelBuffer.toString();
CompositeNode rootNode = getRootNode(model);
IParseResult reparse = PartialParsingUtil.reparse(getParser(), rootNode, model.indexOf("Sven"), 4, "Peter");
assertTrue(reparse.getParseErrors() == null || reparse.getParseErrors().isEmpty());
}
}

View file

@ -81,7 +81,7 @@ public class LengthOffsetLineTest extends AbstractGeneratorTest {
int offset = 0;
for (LeafNode leafNode : nodes) {
assertEquals(offset,leafNode.getOffset());
offset += leafNode.length();
offset += leafNode.getLength();
}
}

View file

@ -30,9 +30,9 @@ public class LeafNodeBug_234132 extends AbstractGeneratorTest {
System.out.println("Model length=" + model.length());
for (LeafNode leafNode : leafNodes) {
String text = leafNode.getText();
System.out.println("Leaf node" + leafNode.toString() + " offset=" + leafNode.getOffset() + " length=" + leafNode.length() + " text=" + ((text != null)? text : ""));
assertTrue(leafNode.length() + leafNode.getOffset() <= model.length());
assertEquals(model.substring(leafNode.getOffset(), leafNode.getOffset() + leafNode.length()), leafNode.getText());
System.out.println("Leaf node" + leafNode.toString() + " offset=" + leafNode.getOffset() + " length=" + leafNode.getLength() + " text=" + ((text != null)? text : ""));
assertTrue(leafNode.getLength() + leafNode.getOffset() <= model.length());
assertEquals(model.substring(leafNode.getOffset(), leafNode.getOffset() + leafNode.getLength()), leafNode.getText());
}
}
}