Search in sources :

Example 1 with ParseException

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

the class AbstractAntlrParser method doParse.

protected IParseResult doParse(String ruleName, CharStream in, NodeModelBuilder nodeModelBuilder, int initialLookAhead) {
    TokenSource tokenSource = createLexer(in);
    XtextTokenStream tokenStream = createTokenStream(tokenSource);
    tokenStream.initCurrentLookAhead(initialLookAhead);
    setInitialHiddenTokens(tokenStream);
    AbstractInternalAntlrParser parser = createParser(tokenStream);
    parser.setTokenTypeMap(getTokenDefProvider().getTokenDefMap());
    parser.setSyntaxErrorProvider(getSyntaxErrorProvider());
    parser.setNodeModelBuilder(nodeModelBuilder);
    parser.setSemanticModelBuilder(getElementFactory());
    IUnorderedGroupHelper helper = getUnorderedGroupHelper().get();
    parser.setUnorderedGroupHelper(helper);
    helper.initializeWith(parser);
    try {
        if (ruleName != null)
            return parser.parse(ruleName);
        return parser.parse();
    } catch (Exception re) {
        throw new ParseException(re.getMessage(), re);
    }
}
Also used : TokenSource(org.antlr.runtime.TokenSource) ParseException(org.eclipse.xtext.parser.ParseException) IOException(java.io.IOException) WrappedException(org.eclipse.emf.common.util.WrappedException) ParseException(org.eclipse.xtext.parser.ParseException)

Example 2 with ParseException

use of org.eclipse.xtext.parser.ParseException 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 3 with ParseException

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

the class ParseErrorHandlingTest method testExpectNoSuchMethodException.

@Test
public void testExpectNoSuchMethodException() throws Exception {
    IParser parser = get(IParser.class);
    ParserRule parserRule = XtextFactory.eINSTANCE.createParserRule();
    parserRule.setName("ruleDoesNotExist");
    try {
        parser.parse(parserRule, new StringReader("empty"));
        fail("Expected WrappedException");
    } catch (ParseException e) {
        assertTrue(e.getCause() instanceof WrappedException);
        WrappedException cause = (WrappedException) e.getCause();
        assertTrue(cause.getCause() instanceof NoSuchMethodException);
    }
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) WrappedException(org.eclipse.emf.common.util.WrappedException) StringReader(java.io.StringReader) ParseException(org.eclipse.xtext.parser.ParseException) IParser(org.eclipse.xtext.parser.IParser) Test(org.junit.Test)

Aggregations

ParseException (org.eclipse.xtext.parser.ParseException)3 StringReader (java.io.StringReader)2 WrappedException (org.eclipse.emf.common.util.WrappedException)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 TokenSource (org.antlr.runtime.TokenSource)1 EObject (org.eclipse.emf.ecore.EObject)1 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)1 ParserRule (org.eclipse.xtext.ParserRule)1 RuleCall (org.eclipse.xtext.RuleCall)1 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)1 SyntheticCompositeNode (org.eclipse.xtext.nodemodel.impl.SyntheticCompositeNode)1 IParseResult (org.eclipse.xtext.parser.IParseResult)1 IParser (org.eclipse.xtext.parser.IParser)1 ParseResult (org.eclipse.xtext.parser.ParseResult)1 ReplaceRegion (org.eclipse.xtext.util.ReplaceRegion)1 Test (org.junit.Test)1