Search in sources :

Example 16 with UnorderedGroup

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

the class XtextValidationTest method testCheckActionInUnorderedGroup_01.

@Test
public void testCheckActionInUnorderedGroup_01() throws Exception {
    XtextValidator validator = get(XtextValidator.class);
    UnorderedGroup unorderedGroup = XtextFactory.eINSTANCE.createUnorderedGroup();
    Action action = XtextFactory.eINSTANCE.createAction();
    unorderedGroup.getElements().add(action);
    ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(action, true, false);
    configureValidator(validator, messageAcceptor, action);
    validator.checkActionInUnorderedGroup(action);
    messageAcceptor.validate();
}
Also used : Action(org.eclipse.xtext.Action) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) Test(org.junit.Test)

Example 17 with UnorderedGroup

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

the class AntlrGrammarGenerator method compileParserImports.

@Override
protected String compileParserImports(final Grammar it, final AntlrOptions options) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.newLine();
    _builder.append("import org.eclipse.xtext.*;");
    _builder.newLine();
    _builder.append("import org.eclipse.xtext.parser.*;");
    _builder.newLine();
    _builder.append("import org.eclipse.xtext.parser.impl.*;");
    _builder.newLine();
    _builder.append("import org.eclipse.emf.ecore.util.EcoreUtil;");
    _builder.newLine();
    _builder.append("import org.eclipse.emf.ecore.EObject;");
    _builder.newLine();
    {
        boolean _isEmpty = GrammarUtil.allEnumRules(it).isEmpty();
        boolean _not = (!_isEmpty);
        if (_not) {
            _builder.append("import org.eclipse.emf.common.util.Enumerator;");
            _builder.newLine();
        }
    }
    _builder.append("import ");
    String _name = this.getGrammarNaming().getInternalParserSuperClass(it).getName();
    _builder.append(_name);
    _builder.append(";");
    _builder.newLineIfNotEmpty();
    _builder.append("import org.eclipse.xtext.parser.antlr.XtextTokenStream;");
    _builder.newLine();
    _builder.append("import org.eclipse.xtext.parser.antlr.XtextTokenStream.HiddenTokens;");
    _builder.newLine();
    {
        if (((!IterableExtensions.isEmpty(Iterables.<UnorderedGroup>filter(Iterables.<EObject>concat(ListExtensions.<ParserRule, List<EObject>>map(GrammarUtil.allParserRules(it), ((Function1<ParserRule, List<EObject>>) (ParserRule it_1) -> {
            return EcoreUtil2.eAllContentsAsList(it_1);
        }))), UnorderedGroup.class))) && options.isBacktrack())) {
            _builder.append("import org.eclipse.xtext.parser.antlr.IUnorderedGroupHelper.UnorderedGroupState;");
            _builder.newLine();
        }
    }
    _builder.append("import org.eclipse.xtext.parser.antlr.AntlrDatatypeRuleToken;");
    _builder.newLine();
    _builder.append("import ");
    String _name_1 = this._grammarAccessExtensions.getGrammarAccess(it).getName();
    _builder.append(_name_1);
    _builder.append(";");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    return _builder.toString();
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) List(java.util.List)

Example 18 with UnorderedGroup

use of org.eclipse.xtext.UnorderedGroup 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 19 with UnorderedGroup

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

the class AbstractFollowElementFactory method createFollowElement.

public FollowElement createFollowElement(AbstractElement current, int lookAhead) {
    if (logger.isDebugEnabled())
        logger.debug("Creating FollowElement for: " + current);
    FollowElement result = doCreateElement();
    result.setLookAhead(lookAhead);
    if (lookAhead != 1) {
        int from = parser.input.index();
        int to = parser.input.size();
        if (parser.marked > 0) {
            from = parser.firstMarker;
        }
        List<LookAheadTerminal> lookAheadTerminals = Lists.newArrayListWithExpectedSize(to - from);
        for (int tokenIndex = from; tokenIndex < to; tokenIndex++) {
            Token token = parser.input.get(tokenIndex);
            if (token != null) {
                LookAheadTerminal lookAheadTerminal = doCreateLookAheadTerminal(token);
                lookAheadTerminals.add(lookAheadTerminal);
            }
        }
        result.setLookAheadTerminals(lookAheadTerminals);
        result.setLookAhead(lookAheadTerminals.size() + 1);
    }
    result.setGrammarElement(current);
    result.setTrace(Lists.newArrayList(Iterators.filter(parser.grammarElements.iterator(), AbstractElement.class)));
    result.setLocalTrace(Lists.newArrayList(Iterators.filter(parser.localTrace.iterator(), AbstractElement.class)));
    result.setParamStack(Lists.newArrayList(parser.paramStack));
    if (current instanceof UnorderedGroup) {
        if (parser.indexToHandledElements != null) {
            int index = parser.grammarElements.lastIndexOf(current);
            List<AbstractElement> alreadyHandled = Lists.newArrayList(Iterators.filter(parser.indexToHandledElements.get(index).iterator(), AbstractElement.class));
            result.setHandledUnorderedGroupElements(alreadyHandled);
        } else {
            result.setHandledUnorderedGroupElements(Collections.<AbstractElement>emptyList());
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("FollowElement is: " + current);
        logger.debug("==================================");
    }
    return result;
}
Also used : ILookAheadTerminal(org.eclipse.xtext.ide.editor.contentassist.antlr.ILookAheadTerminal) AbstractElement(org.eclipse.xtext.AbstractElement) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) Token(org.antlr.runtime.Token) BaseFollowElement(org.eclipse.xtext.ide.editor.contentassist.antlr.BaseFollowElement)

Example 20 with UnorderedGroup

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

the class AbstractAntlrGrammarWithActionsGenerator method _dataTypeEbnf2.

@Override
protected String _dataTypeEbnf2(final UnorderedGroup it, final boolean supportActions) {
    String _xifexpression = null;
    if (supportActions) {
        String _xblockexpression = null;
        {
            final Function1<AbstractElement, Boolean> _function = (AbstractElement it_1) -> {
                boolean _isOptionalCardinality = GrammarUtil.isOptionalCardinality(it_1);
                return Boolean.valueOf((!_isOptionalCardinality));
            };
            final int mandatoryContent = IterableExtensions.size(IterableExtensions.<AbstractElement>filter(it.getElements(), _function));
            StringConcatenation _builder = new StringConcatenation();
            _builder.append("(");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("{ ");
            _builder.newLine();
            _builder.append("\t  ");
            _builder.append("getUnorderedGroupHelper().enter(grammarAccess.");
            UnorderedGroup _originalElement = AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it);
            String _gaRuleElementAccessor = this._grammarAccessExtensions.gaRuleElementAccessor(((AbstractElement) _originalElement));
            _builder.append(_gaRuleElementAccessor, "\t  ");
            _builder.append(");");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("(");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("(");
            _builder.newLine();
            {
                Iterable<Pair<Integer, AbstractElement>> _indexed = IterableExtensions.<AbstractElement>indexed(it.getElements());
                boolean _hasElements = false;
                for (final Pair<Integer, AbstractElement> element : _indexed) {
                    if (!_hasElements) {
                        _hasElements = true;
                    } else {
                        _builder.appendImmediate("|", "");
                    }
                    _builder.append("(");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("{getUnorderedGroupHelper().canSelect(grammarAccess.");
                    UnorderedGroup _originalElement_1 = AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it);
                    String _gaRuleElementAccessor_1 = this._grammarAccessExtensions.gaRuleElementAccessor(((AbstractElement) _originalElement_1));
                    _builder.append(_gaRuleElementAccessor_1, "\t");
                    _builder.append(", ");
                    Integer _key = element.getKey();
                    _builder.append(_key, "\t");
                    _builder.append(")}?=>(");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t\t");
                    _builder.append("{");
                    _builder.newLine();
                    _builder.append("\t\t\t");
                    _builder.append("getUnorderedGroupHelper().select(grammarAccess.");
                    UnorderedGroup _originalElement_2 = AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it);
                    String _gaRuleElementAccessor_2 = this._grammarAccessExtensions.gaRuleElementAccessor(((AbstractElement) _originalElement_2));
                    _builder.append(_gaRuleElementAccessor_2, "\t\t\t");
                    _builder.append(", ");
                    Integer _key_1 = element.getKey();
                    _builder.append(_key_1, "\t\t\t");
                    _builder.append(");");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t\t");
                    _builder.append("}");
                    _builder.newLine();
                    _builder.append("\t\t\t\t\t");
                    _builder.append("({true}?=>(");
                    String _dataTypeEbnf2 = this.dataTypeEbnf2(element.getValue(), supportActions);
                    _builder.append(_dataTypeEbnf2, "\t\t\t\t\t");
                    _builder.append("))");
                    {
                        boolean _isMultipleCardinality = GrammarUtil.isMultipleCardinality(element.getValue());
                        if (_isMultipleCardinality) {
                            _builder.append("+");
                        }
                    }
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t\t");
                    _builder.append("{ ");
                    _builder.newLine();
                    _builder.append("\t\t\t");
                    _builder.append("getUnorderedGroupHelper().returnFromSelection(grammarAccess.");
                    UnorderedGroup _originalElement_3 = AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it);
                    String _gaRuleElementAccessor_3 = this._grammarAccessExtensions.gaRuleElementAccessor(((AbstractElement) _originalElement_3));
                    _builder.append(_gaRuleElementAccessor_3, "\t\t\t");
                    _builder.append(");");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t\t");
                    _builder.append("}");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append(")");
                    _builder.newLine();
                    _builder.append(")");
                    _builder.newLine();
                }
            }
            _builder.append("\t\t");
            _builder.append(")");
            {
                if ((mandatoryContent != 0)) {
                    _builder.append("+");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t\t");
                    _builder.append("{getUnorderedGroupHelper().canLeave(grammarAccess.");
                    UnorderedGroup _originalElement_4 = AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it);
                    String _gaRuleElementAccessor_4 = this._grammarAccessExtensions.gaRuleElementAccessor(((AbstractElement) _originalElement_4));
                    _builder.append(_gaRuleElementAccessor_4, "\t\t");
                    _builder.append(")}?");
                } else {
                    _builder.append("*");
                }
            }
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append(")");
            _builder.newLine();
            _builder.append(")");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("{ ");
            _builder.newLine();
            _builder.append("\t  ");
            _builder.append("getUnorderedGroupHelper().leave(grammarAccess.");
            UnorderedGroup _originalElement_5 = AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it);
            String _gaRuleElementAccessor_5 = this._grammarAccessExtensions.gaRuleElementAccessor(((AbstractElement) _originalElement_5));
            _builder.append(_gaRuleElementAccessor_5, "\t  ");
            _builder.append(");");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _xblockexpression = _builder.toString();
        }
        _xifexpression = _xblockexpression;
    } else {
        _xifexpression = super._dataTypeEbnf2(it, supportActions);
    }
    return _xifexpression;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Pair(org.eclipse.xtext.xbase.lib.Pair)

Aggregations

UnorderedGroup (org.eclipse.xtext.UnorderedGroup)35 AbstractElement (org.eclipse.xtext.AbstractElement)16 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)13 ParserRule (org.eclipse.xtext.ParserRule)13 Test (org.junit.Test)12 RuleCall (org.eclipse.xtext.RuleCall)11 Group (org.eclipse.xtext.Group)9 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)9 Assignment (org.eclipse.xtext.Assignment)8 EObject (org.eclipse.emf.ecore.EObject)7 Action (org.eclipse.xtext.Action)5 Alternatives (org.eclipse.xtext.Alternatives)5 Pair (org.eclipse.xtext.xbase.lib.Pair)5 List (java.util.List)4 TerminalRule (org.eclipse.xtext.TerminalRule)4 ArrayList (java.util.ArrayList)3 AbstractRule (org.eclipse.xtext.AbstractRule)3 Keyword (org.eclipse.xtext.Keyword)3 TypeRef (org.eclipse.xtext.TypeRef)3 Token (org.antlr.runtime.Token)2