Search in sources :

Example 36 with IParseResult

use of org.eclipse.xtext.parser.IParseResult in project xtext-core by eclipse.

the class XtextResourceTest method testUpdate.

@Test
public void testUpdate() throws Exception {
    resource.update(0, 0, simpleModel);
    IParseResult parseResult = resource.getParseResult();
    assertNotNull("parseResult", parseResult);
    assertNotNull(parseResult.getRootNode());
    assertEquals(0, Iterables.size(parseResult.getSyntaxErrors()));
    assertEquals(simpleModel.length(), parseResult.getRootNode().getTotalLength());
    assertNotNull(parseResult.getRootASTElement());
}
Also used : IParseResult(org.eclipse.xtext.parser.IParseResult) Test(org.junit.Test)

Example 37 with IParseResult

use of org.eclipse.xtext.parser.IParseResult in project xtext-core by eclipse.

the class SerializationConversionContext method fillGrammarElementToIdMap.

protected void fillGrammarElementToIdMap(XtextResource r) {
    IParseResult parseResult = r.getParseResult();
    if (parseResult != null) {
        RootNode it = (RootNode) parseResult.getRootNode();
        it.fillGrammarElementToIdMap(grammarElementToIdMap, grammarIdToURIMap);
    }
}
Also used : RootNode(org.eclipse.xtext.nodemodel.impl.RootNode) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 38 with IParseResult

use of org.eclipse.xtext.parser.IParseResult in project xtext-core by eclipse.

the class AbstractInternalAntlrParser method parse.

public final IParseResult parse(String entryRuleName) throws RecognitionException {
    long time = System.currentTimeMillis();
    IParseResult result = null;
    EObject current = null;
    String completeContent = input.toString();
    if (// who had the crazy idea to return null from toString() ...
    completeContent == null)
        completeContent = "";
    currentNode = nodeBuilder.newRootNode(completeContent);
    String antlrEntryRuleName = normalizeEntryRuleName(entryRuleName);
    try {
        Method method = this.getClass().getMethod(antlrEntryRuleName);
        method.setAccessible(true);
        Object parseResult = method.invoke(this);
        if (parseResult instanceof EObject)
            current = (EObject) parseResult;
        appendSkippedTokens();
        appendTrailingHiddenTokens();
        try {
            appendAllTokens();
        } finally {
            ICompositeNode root = nodeBuilder.compressAndReturnParent(currentNode);
            result = new ParseResult(current, root, hadErrors);
        }
    } catch (InvocationTargetException ite) {
        Throwable targetException = ite.getTargetException();
        if (targetException instanceof RecognitionException) {
            try {
                appendAllTokens();
            } finally {
                ICompositeNode root = nodeBuilder.compressAndReturnParent(currentNode);
                result = new ParseResult(current, root, hadErrors);
            }
            throw (RecognitionException) targetException;
        }
        if (targetException instanceof Exception) {
            throw new WrappedException((Exception) targetException);
        }
        throw new RuntimeException(targetException);
    } catch (Exception e) {
        throw new WrappedException(e);
    } finally {
        if (logger.isDebugEnabled()) {
            logger.debug("Parsing took: " + (System.currentTimeMillis() - time) + " ms");
        }
    }
    return result;
}
Also used : WrappedException(org.eclipse.emf.common.util.WrappedException) IParseResult(org.eclipse.xtext.parser.IParseResult) ParseResult(org.eclipse.xtext.parser.ParseResult) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) MissingTokenException(org.antlr.runtime.MissingTokenException) RecognitionException(org.antlr.runtime.RecognitionException) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) UnwantedTokenException(org.antlr.runtime.UnwantedTokenException) WrappedException(org.eclipse.emf.common.util.WrappedException) InvocationTargetException(java.lang.reflect.InvocationTargetException) FailedPredicateException(org.antlr.runtime.FailedPredicateException) MismatchedTokenException(org.antlr.runtime.MismatchedTokenException) EObject(org.eclipse.emf.ecore.EObject) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) EObject(org.eclipse.emf.ecore.EObject) IParseResult(org.eclipse.xtext.parser.IParseResult) RecognitionException(org.antlr.runtime.RecognitionException)

Example 39 with IParseResult

use of org.eclipse.xtext.parser.IParseResult in project xtext-core by eclipse.

the class PartialParsingHelper method reparse.

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public IParseResult reparse(IParser parser, IParseResult previousParseResult, ReplaceRegion changedRegion) {
    if (parser == null)
        throw new NullPointerException("parser may not be null");
    if (previousParseResult == null) {
        throw new NullPointerException("previousParseResult and previousParseResult.rootNode may not be null");
    }
    ICompositeNode oldRootNode = previousParseResult.getRootNode();
    if (changedRegion.getEndOffset() > oldRootNode.getTotalLength()) {
        log.error("Invalid " + changedRegion + " originalLength=" + oldRootNode.getTotalLength());
        return fullyReparse(parser, previousParseResult, changedRegion);
    }
    if (changedRegion.getOffset() >= oldRootNode.getTotalLength() && changedRegion.getText().trim().length() == 0) {
        return fullyReparse(parser, previousParseResult, changedRegion);
    }
    ReplaceRegion replaceRegion;
    if (tokenRegionProvider != null) {
        replaceRegion = tokenRegionProvider.getTokenReplaceRegion(insertChangeIntoReplaceRegion(oldRootNode, changedRegion), changedRegion);
    } else {
        replaceRegion = changedRegion;
    }
    if (isNullEdit(oldRootNode, replaceRegion)) {
        return previousParseResult;
    }
    PartialParsingPointers parsingPointers = calculatePartialParsingPointers(previousParseResult, replaceRegion.getOffset(), replaceRegion.getLength());
    List<ICompositeNode> validReplaceRootNodes = parsingPointers.getValidReplaceRootNodes();
    ICompositeNode oldCompositeNode = null;
    String reparseRegion = "";
    for (int i = validReplaceRootNodes.size() - 1; i >= 0; --i) {
        oldCompositeNode = validReplaceRootNodes.get(i);
        if (!(oldCompositeNode instanceof SyntheticCompositeNode) && !isRangePartOfExceedingLookAhead((CompositeNode) oldCompositeNode, replaceRegion)) {
            boolean replaceAtEnd = oldCompositeNode.getTotalEndOffset() == replaceRegion.getEndOffset();
            reparseRegion = insertChangeIntoReplaceRegion(oldCompositeNode, replaceRegion);
            if (!"".equals(reparseRegion)) {
                if (!replaceAtEnd || !Character.isWhitespace(reparseRegion.charAt(reparseRegion.length() - 1))) {
                    if (log.isDebugEnabled()) {
                        log.debug("replace region: [" + oldCompositeNode.getTotalOffset() + " / length: " + oldCompositeNode.getTotalLength() + " of [" + oldRootNode.getTotalOffset() + " / lenght: " + oldRootNode.getTotalLength() + "]");
                    }
                    break;
                }
            }
        }
    }
    if (oldCompositeNode == null || reparseRegion.equals("") || oldCompositeNode == oldRootNode) {
        return fullyReparse(parser, previousParseResult, replaceRegion);
    }
    EObject entryRuleOrRuleCall = parsingPointers.findEntryRuleOrRuleCall(oldCompositeNode);
    IParseResult newParseResult = null;
    try {
        if (entryRuleOrRuleCall instanceof RuleCall)
            newParseResult = parser.parse((RuleCall) entryRuleOrRuleCall, new StringReader(reparseRegion), oldCompositeNode.getLookAhead());
        else
            newParseResult = parser.parse((ParserRule) entryRuleOrRuleCall, new StringReader(reparseRegion));
    } catch (ParseException exc) {
    }
    if (newParseResult == null || newParseResult.hasSyntaxErrors()) {
        // on error fully reparse
        return fullyReparse(parser, previousParseResult, replaceRegion);
    }
    if (oldRootNode.equals(oldCompositeNode)) {
        unloadSemanticObject(previousParseResult.getRootASTElement());
        return newParseResult;
    }
    EObject oldSemanticParentElement = oldCompositeNode.getParent().getSemanticElement();
    EObject oldSemanticElement = null;
    if (oldCompositeNode.hasDirectSemanticElement()) {
        oldSemanticElement = oldCompositeNode.getSemanticElement();
    } else {
        List<ICompositeNode> nodesEnclosingRegion = parsingPointers.getNodesEnclosingRegion();
        for (int i = nodesEnclosingRegion.size() - 1; i >= 0; --i) {
            ICompositeNode enclosingNode = nodesEnclosingRegion.get(i);
            if (enclosingNode == oldCompositeNode) {
                break;
            }
            if (enclosingNode.hasDirectSemanticElement())
                oldSemanticElement = enclosingNode.getSemanticElement();
        }
        if (oldSemanticElement == null)
            return fullyReparse(parser, previousParseResult, replaceRegion);
    }
    if (oldSemanticElement == oldSemanticParentElement) {
        throw new IllegalStateException("oldParent == oldElement");
    }
    if (oldSemanticParentElement != null) {
        EStructuralFeature feature = oldSemanticElement.eContainingFeature();
        if (feature == null)
            return fullyReparse(parser, previousParseResult, replaceRegion);
        oldSemanticParentElement = oldSemanticElement.eContainer();
        if (feature.isMany()) {
            List featureValueList = (List) oldSemanticParentElement.eGet(feature);
            int index = featureValueList.indexOf(oldSemanticElement);
            unloadSemanticObject(oldSemanticElement);
            EObject newSemanticObject = newParseResult.getRootASTElement();
            if (newSemanticObject != null) {
                featureValueList.set(index, newParseResult.getRootASTElement());
            } else {
                featureValueList.remove(index);
            }
        } else {
            unloadSemanticObject(oldSemanticElement);
            oldSemanticParentElement.eSet(feature, newParseResult.getRootASTElement());
        }
        ((ParseResult) newParseResult).setRootASTElement(previousParseResult.getRootASTElement());
    } else {
        unloadSemanticObject(oldSemanticElement);
    }
    if (oldCompositeNode != oldRootNode) {
        nodeModelBuilder.replaceAndTransferLookAhead(oldCompositeNode, newParseResult.getRootNode());
        ((ParseResult) newParseResult).setRootNode(oldRootNode);
        StringBuilder builder = new StringBuilder(oldRootNode.getText());
        replaceRegion.applyTo(builder);
        nodeModelBuilder.setCompleteContent(oldRootNode, builder.toString());
    }
    return newParseResult;
}
Also used : SyntheticCompositeNode(org.eclipse.xtext.nodemodel.impl.SyntheticCompositeNode) ParseResult(org.eclipse.xtext.parser.ParseResult) IParseResult(org.eclipse.xtext.parser.IParseResult) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) RuleCall(org.eclipse.xtext.RuleCall) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) EObject(org.eclipse.emf.ecore.EObject) StringReader(java.io.StringReader) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) ArrayList(java.util.ArrayList) List(java.util.List) IParseResult(org.eclipse.xtext.parser.IParseResult) ParseException(org.eclipse.xtext.parser.ParseException)

Example 40 with IParseResult

use of org.eclipse.xtext.parser.IParseResult in project xtext-core by eclipse.

the class EObjectAtOffsetHelper method getCrossReferenceNode.

/**
 * @since 2.1
 */
public INode getCrossReferenceNode(XtextResource resource, ITextRegion region) {
    IParseResult parseResult = resource.getParseResult();
    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.
        if (crossRefNode == null && leaf != null && region.getLength() == 0 && leaf.getOffset() == region.getOffset()) {
            leaf = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), region.getOffset() - 1);
            return findCrossReferenceNode(leaf);
        } else if (crossRefNode != null && crossRefNode.getEndOffset() >= region.getOffset() + region.getLength()) {
            return crossRefNode;
        }
    }
    return null;
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) IParseResult(org.eclipse.xtext.parser.IParseResult)

Aggregations

IParseResult (org.eclipse.xtext.parser.IParseResult)60 EObject (org.eclipse.emf.ecore.EObject)22 StringReader (java.io.StringReader)16 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)14 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)10 INode (org.eclipse.xtext.nodemodel.INode)10 XtextResource (org.eclipse.xtext.resource.XtextResource)10 ReplaceRegion (org.eclipse.xtext.util.ReplaceRegion)9 ParserRule (org.eclipse.xtext.ParserRule)8 Test (org.junit.Test)7 ITextRegion (org.eclipse.xtext.util.ITextRegion)6 Resource (org.eclipse.emf.ecore.resource.Resource)4 TextRegion (org.eclipse.xtext.util.TextRegion)4 URI (org.eclipse.emf.common.util.URI)3 WrappedException (org.eclipse.emf.common.util.WrappedException)3 ParseResult (org.eclipse.xtext.parser.ParseResult)3 Scope (org.yakindu.sct.model.sgraph.Scope)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2