Search in sources :

Example 51 with RuleCall

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

the class Linker method afterCreateAndSetProxy.

@Override
protected void afterCreateAndSetProxy(EObject obj, INode node, EReference eRef, CrossReference xref, IDiagnosticProducer diagnosticProducer) {
    AbstractElement terminal = xref.getTerminal();
    if (!(terminal instanceof RuleCall)) {
        throw new IllegalArgumentException(String.valueOf(xref));
    }
    AbstractRule rule = ((RuleCall) terminal).getRule();
    try {
        String tokenText = NodeModelUtils.getTokenText(node);
        valueConverterService.toValue(tokenText, rule.getName(), node);
    } catch (ValueConverterException e) {
        diagnosticProducer.addDiagnostic(new DiagnosticMessage(e.getMessage(), Severity.ERROR, Diagnostic.SYNTAX_DIAGNOSTIC, Strings.EMPTY_ARRAY));
    }
}
Also used : DiagnosticMessage(org.eclipse.xtext.diagnostics.DiagnosticMessage) AbstractElement(org.eclipse.xtext.AbstractElement) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall)

Example 52 with RuleCall

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

the class EntryPointFinder method shouldUseParent.

protected boolean shouldUseParent(ICompositeNode result, int offset, ILeafNode leaf) {
    if (leaf.getTotalEndOffset() == offset) {
        return true;
    }
    if (result.getGrammarElement() instanceof RuleCall) {
        RuleCall rc = (RuleCall) result.getGrammarElement();
        if (!rc.getArguments().isEmpty()) {
            return true;
        }
        Assignment assignment = GrammarUtil.containingAssignment(rc);
        if (assignment != null && (GrammarUtil.isMultipleCardinality(assignment) || (assignment.eContainer() instanceof AbstractElement && GrammarUtil.isMultipleCardinality((AbstractElement) assignment.eContainer())))) {
            return true;
        }
    }
    return false;
}
Also used : Assignment(org.eclipse.xtext.Assignment) AbstractElement(org.eclipse.xtext.AbstractElement) RuleCall(org.eclipse.xtext.RuleCall)

Example 53 with RuleCall

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

the class FollowElementComputer method computeFollowElements.

protected void computeFollowElements(FollowElementCalculator calculator, FollowElement element, Multimap<Integer, List<AbstractElement>> visited) {
    List<AbstractElement> currentState = Lists.newArrayList(element.getLocalTrace());
    if (currentState.isEmpty() || currentState.get(currentState.size() - 1) != element.getGrammarElement()) {
        currentState.add(element.getGrammarElement());
    }
    if (!visited.put(element.getLookAhead(), currentState))
        return;
    if (element.getLookAhead() <= 1) {
        List<Integer> paramStack = element.getParamStack();
        int paramIndex = computeParamStackOffset(currentState, paramStack);
        for (AbstractElement abstractElement : currentState) {
            paramIndex = setParamConfigAndUpdateOffset(calculator, paramStack, paramIndex, abstractElement);
            Assignment ass = EcoreUtil2.getContainerOfType(abstractElement, Assignment.class);
            if (ass != null)
                calculator.doSwitch(ass);
            else {
                if (abstractElement instanceof UnorderedGroup && abstractElement == element.getGrammarElement()) {
                    calculator.doSwitch((UnorderedGroup) abstractElement, element.getHandledUnorderedGroupElements());
                } else {
                    calculator.doSwitch(abstractElement);
                    if (GrammarUtil.isOptionalCardinality(abstractElement)) {
                        EObject container = abstractElement.eContainer();
                        if (container instanceof Group) {
                            Group group = (Group) container;
                            int idx = group.getElements().indexOf(abstractElement);
                            if (idx == group.getElements().size() - 1) {
                                if (!currentState.contains(group) && GrammarUtil.isMultipleCardinality(group)) {
                                    calculator.doSwitch(group);
                                }
                            } else if (idx < group.getElements().size() - 1 && "?".equals(abstractElement.getCardinality())) {
                                // loops are fine
                                AbstractElement nextElement = group.getElements().get(idx + 1);
                                if (!currentState.contains(nextElement)) {
                                    calculator.doSwitch(nextElement);
                                }
                            }
                        }
                    } else if (isAlternativeWithEmptyPath(abstractElement)) {
                        EObject container = abstractElement.eContainer();
                        if (container instanceof Group) {
                            Group group = (Group) container;
                            int idx = group.getElements().indexOf(abstractElement);
                            if (!currentState.contains(group) && idx != group.getElements().size() - 1) {
                                AbstractElement next = group.getElements().get(idx + 1);
                                if (!currentState.contains(next)) {
                                    calculator.doSwitch(next);
                                }
                            }
                        }
                    }
                }
            }
        }
        // we need a synthetic rule call
        if (element.getTrace().equals(element.getLocalTrace())) {
            ParserRule parserRule = GrammarUtil.containingParserRule(element.getGrammarElement());
            if (parserRule != null) {
                RuleCall ruleCall = XtextFactory.eINSTANCE.createRuleCall();
                ruleCall.setRule(parserRule);
                calculator.doSwitch(ruleCall);
            }
        }
        return;
    }
    Collection<FollowElement> followElements = parser.getFollowElements(element);
    for (FollowElement newElement : followElements) {
        if (newElement.getLookAhead() != element.getLookAhead() || newElement.getGrammarElement() != element.getGrammarElement()) {
            if (newElement.getLookAhead() == element.getLookAhead()) {
                int originalTraceSize = element.getLocalTrace().size();
                List<AbstractElement> newTrace = newElement.getLocalTrace();
                if (newTrace.size() > originalTraceSize) {
                    if (Collections.indexOfSubList(element.getLocalTrace(), newTrace.subList(originalTraceSize, newTrace.size())) != -1) {
                        continue;
                    }
                }
            }
            computeFollowElements(calculator, newElement, visited);
        }
    }
}
Also used : Group(org.eclipse.xtext.Group) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) ParserRule(org.eclipse.xtext.ParserRule) AbstractElement(org.eclipse.xtext.AbstractElement) RuleCall(org.eclipse.xtext.RuleCall) Assignment(org.eclipse.xtext.Assignment) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) EObject(org.eclipse.emf.ecore.EObject)

Example 54 with RuleCall

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

the class PartialSerializer method updateSingleValue.

protected SerializationStrategy updateSingleValue(EObject object, EStructuralFeature feature, IAstRegion region) {
    Preconditions.checkArgument(!feature.isMany());
    Object value = object.eGet(feature);
    EObject grammarElement = region.getGrammarElement();
    if (feature instanceof EAttribute) {
        if (grammarElement instanceof RuleCall) {
            RuleCall rc = (RuleCall) grammarElement;
            String newValue = valueSerializer.serializeAssignedValue(object, rc, value, null, errorAcceptor);
            if (newValue != null) {
                return new ReplaceRegionStrategy((ISemanticRegion) region, newValue);
            }
        }
        return null;
    } else if (feature instanceof EReference) {
        if (((EReference) feature).isContainment()) {
            IEObjectRegion reg = (IEObjectRegion) region;
            EObject newEObject = (EObject) object.eGet(feature);
            ISerializationContext newContext = getSerializationContext(newEObject);
            ISerializationContext oldContext = getSerializationContext(reg);
            if (!oldContext.equals(newContext)) {
                return null;
            }
            return new SerializeRecursiveStrategy(reg, newEObject, newContext);
        } else {
            CrossReference cr = GrammarUtil.containingCrossReference(grammarElement);
            if (cr != null) {
                EObject target = (EObject) value;
                String newValue = crossRefSerializer.serializeCrossRef(object, cr, target, null, errorAcceptor);
                if (newValue != null) {
                    return new ReplaceRegionStrategy((ISemanticRegion) region, newValue);
                }
            }
        }
        return null;
    }
    return null;
}
Also used : EAttribute(org.eclipse.emf.ecore.EAttribute) IEObjectRegion(org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) EObject(org.eclipse.emf.ecore.EObject) CrossReference(org.eclipse.xtext.CrossReference) EObject(org.eclipse.emf.ecore.EObject) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) RuleCall(org.eclipse.xtext.RuleCall) EReference(org.eclipse.emf.ecore.EReference)

Example 55 with RuleCall

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

the class IdeContentProposalProvider method _createProposals.

protected void _createProposals(final Assignment assignment, final ContentAssistContext context, final IIdeContentProposalAcceptor acceptor) {
    final AbstractElement terminal = assignment.getTerminal();
    if ((terminal instanceof CrossReference)) {
        this.createProposals(terminal, context, acceptor);
    } else {
        if ((terminal instanceof RuleCall)) {
            final AbstractRule rule = ((RuleCall) terminal).getRule();
            if (((rule instanceof TerminalRule) && context.getPrefix().isEmpty())) {
                String _xifexpression = null;
                String _name = rule.getName();
                boolean _equals = Objects.equal(_name, "STRING");
                if (_equals) {
                    String _feature = assignment.getFeature();
                    String _plus = ("\"" + _feature);
                    _xifexpression = (_plus + "\"");
                } else {
                    _xifexpression = assignment.getFeature();
                }
                final String proposal = _xifexpression;
                final Procedure1<ContentAssistEntry> _function = (ContentAssistEntry it) -> {
                    String _name_1 = rule.getName();
                    boolean _equals_1 = Objects.equal(_name_1, "STRING");
                    if (_equals_1) {
                        ArrayList<TextRegion> _editPositions = it.getEditPositions();
                        int _offset = context.getOffset();
                        int _plus_1 = (_offset + 1);
                        int _length = proposal.length();
                        int _minus = (_length - 2);
                        TextRegion _textRegion = new TextRegion(_plus_1, _minus);
                        _editPositions.add(_textRegion);
                        it.setKind(ContentAssistEntry.KIND_TEXT);
                    } else {
                        ArrayList<TextRegion> _editPositions_1 = it.getEditPositions();
                        int _offset_1 = context.getOffset();
                        int _length_1 = proposal.length();
                        TextRegion _textRegion_1 = new TextRegion(_offset_1, _length_1);
                        _editPositions_1.add(_textRegion_1);
                        it.setKind(ContentAssistEntry.KIND_VALUE);
                    }
                    it.setDescription(rule.getName());
                };
                final ContentAssistEntry entry = this.proposalCreator.createProposal(proposal, context, _function);
                acceptor.accept(entry, this.proposalPriorities.getDefaultPriority(entry));
            }
        }
    }
}
Also used : ContentAssistEntry(org.eclipse.xtext.ide.editor.contentassist.ContentAssistEntry) AbstractElement(org.eclipse.xtext.AbstractElement) TextRegion(org.eclipse.xtext.util.TextRegion) ArrayList(java.util.ArrayList) CrossReference(org.eclipse.xtext.CrossReference) TerminalRule(org.eclipse.xtext.TerminalRule) 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