Search in sources :

Example 61 with RuleCall

use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.

the class SemanticNodeIterator method isEObjectNode.

protected boolean isEObjectNode(INode node) {
    if (node.getGrammarElement() instanceof AbstractRule)
        return true;
    if (node.getGrammarElement() instanceof Action)
        return true;
    if (GrammarUtil.isAssignedEObjectRuleCall(node.getGrammarElement())) {
        if (node.hasDirectSemanticElement())
            return true;
        AbstractRule rule = ((RuleCall) node.getGrammarElement()).getRule();
        node = node.getParent();
        while (node != null) {
            if (GrammarUtil.isAssigned(node.getGrammarElement()))
                return true;
            if (node.getGrammarElement() instanceof Action && GrammarUtil.containingRule(node.getGrammarElement()) == rule)
                return false;
            node = node.getParent();
        }
        return true;
    }
    return false;
}
Also used : Action(org.eclipse.xtext.Action) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall)

Example 62 with RuleCall

use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.

the class AbstractElementFinder method findRuleCalls.

public List<RuleCall> findRuleCalls(AbstractRule... rules) {
    Set<AbstractRule> rls = new HashSet<AbstractRule>(Arrays.asList(rules));
    ArrayList<RuleCall> r = new ArrayList<RuleCall>();
    for (AbstractRule ar : getRules()) {
        TreeIterator<EObject> i = ar.eAllContents();
        while (i.hasNext()) {
            EObject o = i.next();
            if (o instanceof RuleCall) {
                RuleCall c = (RuleCall) o;
                if (rls.contains(c.getRule()))
                    r.add(c);
            }
        }
    }
    return r;
}
Also used : EObject(org.eclipse.emf.ecore.EObject) ArrayList(java.util.ArrayList) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall) HashSet(java.util.HashSet)

Example 63 with RuleCall

use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.

the class AbstractElementFinder method findByNestedRuleCall.

@SuppressWarnings("unchecked")
protected <T> List<T> findByNestedRuleCall(Class<T> clazz, AbstractRule... rule) {
    Set<AbstractRule> rls = new HashSet<AbstractRule>(Arrays.asList(rule));
    ArrayList<T> r = new ArrayList<T>();
    for (AbstractRule ar : getRules()) {
        TreeIterator<EObject> i = ar.eAllContents();
        while (i.hasNext()) {
            EObject o = i.next();
            if (clazz.isInstance(o)) {
                TreeIterator<EObject> ct = o.eAllContents();
                while (ct.hasNext()) {
                    EObject cto = ct.next();
                    if (cto instanceof RuleCall && rls.contains(((RuleCall) cto).getRule())) {
                        r.add((T) o);
                        break;
                    }
                }
                i.prune();
            }
        }
    }
    return r;
}
Also used : EObject(org.eclipse.emf.ecore.EObject) ArrayList(java.util.ArrayList) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall) HashSet(java.util.HashSet)

Example 64 with RuleCall

use of org.eclipse.xtext.RuleCall 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 65 with RuleCall

use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.

the class PartialParsingHelper method isInvalidRootNode.

protected boolean isInvalidRootNode(ICompositeNode rootNode, ICompositeNode candidate) {
    int endOffset = candidate.getTotalEndOffset();
    if (candidate instanceof SyntheticCompositeNode)
        return true;
    if (candidate.getGrammarElement() instanceof RuleCall) {
        AbstractRule rule = ((RuleCall) candidate.getGrammarElement()).getRule();
        if (!(rule instanceof ParserRule))
            return true;
        ParserRule casted = (ParserRule) rule;
        if (GrammarUtil.isDatatypeRule(casted) || casted.isFragment() || !casted.getParameters().isEmpty()) {
            return true;
        }
        if (isInvalidDueToPredicates((AbstractElement) candidate.getGrammarElement()))
            return true;
    }
    if (candidate.getGrammarElement() instanceof Action) {
        return true;
    }
    if (endOffset == rootNode.getTotalEndOffset()) {
        INode lastChild = getLastChild(candidate);
        if (lastChild instanceof ICompositeNode) {
            INode lastLeaf = getLastLeaf(candidate);
            if (isInvalidLastChildNode(candidate, lastLeaf)) {
                return true;
            }
        }
        if (isInvalidLastChildNode(candidate, lastChild)) {
            return true;
        }
    }
    return false;
}
Also used : SyntheticCompositeNode(org.eclipse.xtext.nodemodel.impl.SyntheticCompositeNode) ParserRule(org.eclipse.xtext.ParserRule) Action(org.eclipse.xtext.Action) INode(org.eclipse.xtext.nodemodel.INode) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall)

Aggregations

RuleCall (org.eclipse.xtext.RuleCall)95 ParserRule (org.eclipse.xtext.ParserRule)41 AbstractRule (org.eclipse.xtext.AbstractRule)36 EObject (org.eclipse.emf.ecore.EObject)33 Test (org.junit.Test)33 Grammar (org.eclipse.xtext.Grammar)28 AbstractElement (org.eclipse.xtext.AbstractElement)26 Assignment (org.eclipse.xtext.Assignment)22 TerminalRule (org.eclipse.xtext.TerminalRule)19 Action (org.eclipse.xtext.Action)17 CrossReference (org.eclipse.xtext.CrossReference)13 Group (org.eclipse.xtext.Group)13 TypeRef (org.eclipse.xtext.TypeRef)13 UnorderedGroup (org.eclipse.xtext.UnorderedGroup)13 ArrayList (java.util.ArrayList)11 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)11 InternalEObject (org.eclipse.emf.ecore.InternalEObject)10 Keyword (org.eclipse.xtext.Keyword)10 EClass (org.eclipse.emf.ecore.EClass)9 EnumRule (org.eclipse.xtext.EnumRule)9