[xtext] Some more null guards related to XtextResource / IParseResult

This commit is contained in:
Sebastian Zarnekow 2012-03-09 16:21:38 +01:00
parent 5f3f3a640c
commit 665213a0e6
8 changed files with 31 additions and 12 deletions

View file

@ -13,7 +13,6 @@ import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.conversion.ValueConverterException;
import org.eclipse.xtext.parser.IAstFactory;
import org.eclipse.xtext.parser.IParseResult;
import org.eclipse.xtext.parser.ParseResult;
import org.eclipse.xtext.parser.packrat.debug.DebugUtil;
import org.eclipse.xtext.parser.packrat.debug.ParsedTokenPrinter;
import org.eclipse.xtext.parser.packrat.tokens.AbstractParsedToken;
@ -66,8 +65,9 @@ public class ParseResultFactory extends AbstractParsedTokenVisitor implements IP
} else {
token.accept(this);
}
throw new UnsupportedOperationException();
// this.input = null;
return new ParseResult(currentStack.isEmpty() ? null : currentStack.getLast(), null, false);
// return new ParseResult(currentStack.isEmpty() ? null : currentStack.getLast(), null, false);
}
// private LeafNode createLeafNode(AbstractParsedToken parsedToken) {

View file

@ -12,6 +12,8 @@ import java.util.List;
import java.util.Map;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.xtext.RuleCall;
import org.eclipse.xtext.nodemodel.BidiTreeIterator;
import org.eclipse.xtext.nodemodel.ICompositeNode;
@ -24,11 +26,12 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
/**
* A statefull (!) builder that provides call back methods for clients who
* A stateful (!) builder that provides call back methods for clients who
* want to create a node model and maintain its invariants.
* @author Sebastian Zarnekow - Initial contribution and API
* @noextend This class is not intended to be subclassed by clients.
*/
@NonNullByDefault
public class NodeModelBuilder {
private EObject forcedGrammarElement;
@ -84,7 +87,7 @@ public class NodeModelBuilder {
child.basicSetPreviousSibling(child);
}
protected void checkValidNewChild(AbstractNode child) {
protected void checkValidNewChild(@Nullable AbstractNode child) {
if (child == null)
throw new IllegalArgumentException("child may not be null");
if (child.basicGetNextSibling() != null || child.basicGetPreviousSibling() != null)
@ -132,7 +135,7 @@ public class NodeModelBuilder {
return result;
}
public ILeafNode newLeafNode(int offset, int length, EObject grammarElement, boolean isHidden, SyntaxErrorMessage errorMessage,
public ILeafNode newLeafNode(int offset, int length, EObject grammarElement, boolean isHidden, @Nullable SyntaxErrorMessage errorMessage,
ICompositeNode parent) {
LeafNode result = null;
if (errorMessage != null) {

View file

@ -9,6 +9,8 @@
package org.eclipse.xtext.parser;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.xtext.nodemodel.ICompositeNode;
import org.eclipse.xtext.nodemodel.INode;
@ -19,10 +21,13 @@ import org.eclipse.xtext.nodemodel.INode;
*/
public interface IParseResult {
@Nullable
EObject getRootASTElement();
@NonNull
ICompositeNode getRootNode();
@NonNull
Iterable<INode> getSyntaxErrors();
boolean hasSyntaxErrors();

View file

@ -12,11 +12,14 @@ import java.util.Collections;
import java.util.Iterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.xtext.nodemodel.ICompositeNode;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.nodemodel.impl.AbstractNode;
import org.eclipse.xtext.nodemodel.impl.CompositeNode;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterators;
@ -31,20 +34,23 @@ public class ParseResult implements IParseResult {
private ICompositeNode rootNode;
private final boolean hasErrors;
public ParseResult(EObject rootAstElement, ICompositeNode rootNode, boolean hasErrors) {
public ParseResult(@Nullable EObject rootAstElement, @NonNull ICompositeNode rootNode, boolean hasErrors) {
Preconditions.checkNotNull(rootNode);
this.rootAstElement = rootAstElement;
this.rootNode = rootNode;
this.hasErrors = hasErrors;
}
public void setRootASTElement(EObject rootAstElement) {
public void setRootASTElement(@Nullable EObject rootAstElement) {
this.rootAstElement = rootAstElement;
}
@Nullable
public EObject getRootASTElement() {
return rootAstElement;
}
@NonNull
public Iterable<INode> getSyntaxErrors() {
if (rootNode == null || !hasSyntaxErrors())
return Collections.emptyList();
@ -62,11 +68,13 @@ public class ParseResult implements IParseResult {
};
}
@NonNull
public ICompositeNode getRootNode() {
return rootNode;
}
public void setRootNode(ICompositeNode rootNode) {
public void setRootNode(@NonNull ICompositeNode rootNode) {
Preconditions.checkNotNull(rootNode);
this.rootNode = rootNode;
}

View file

@ -66,7 +66,7 @@ public class PartialParsingHelper implements IPartialParsingHelper {
public IParseResult reparse(IParser parser, IParseResult previousParseResult, ReplaceRegion replaceRegion) {
if (parser == null)
throw new NullPointerException("parser may not be null");
if (previousParseResult == null || previousParseResult.getRootNode() == null) {
if (previousParseResult == null) {
throw new NullPointerException("previousParseResult and previousParseResult.rootNode may not be null");
}
ICompositeNode oldRootNode = previousParseResult.getRootNode();

View file

@ -59,7 +59,7 @@ public class EObjectAtOffsetHelper {
*/
public INode getCrossReferenceNode(XtextResource resource, ITextRegion region) {
IParseResult parseResult = resource.getParseResult();
if (parseResult != null && parseResult.getRootNode() != null) {
if (parseResult != null) {
ILeafNode leaf = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), region.getOffset());
INode crossRefNode = findCrossReferenceNode(leaf);
// if not a cross reference position and the cursor is at the beginning of a node try the previous one.
@ -119,7 +119,7 @@ public class EObjectAtOffsetHelper {
return crossRef;
}
IParseResult parseResult = resource.getParseResult();
if (parseResult != null && parseResult.getRootNode() != null) {
if (parseResult != null) {
ILeafNode leaf = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset);
if (leaf != null && leaf.isHidden() && leaf.getOffset() == offset) {
leaf = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset - 1);

View file

@ -23,6 +23,7 @@ import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.impl.ResourceImpl;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.xtext.Constants;
import org.eclipse.xtext.diagnostics.Severity;
import org.eclipse.xtext.linking.ILinker;
@ -147,6 +148,7 @@ public class XtextResource extends ResourceImpl {
super();
}
@Nullable
public IParseResult getParseResult() {
return parseResult;
}

View file

@ -104,7 +104,8 @@ public class XtextLinkingService extends DefaultLinkingService {
EObject rootElement = null;
if (resource instanceof XtextResource) {
IParseResult parseResult = ((XtextResource) resource).getParseResult();
rootElement = parseResult.getRootASTElement();
if (parseResult != null)
rootElement = parseResult.getRootASTElement();
} else if (!resource.getContents().isEmpty()) {
rootElement = resource.getContents().get(0);
}