Search in sources :

Example 31 with UnorderedGroup

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

the class GrammarParserTest method testPrecedencies_03.

@Test
public void testPrecedencies_03() throws Exception {
    UnorderedGroup group = (UnorderedGroup) getModel("'keyword' 'keyword' & 'keyword' 'keyword'");
    assertNotNull(group);
    assertEquals(2, group.getElements().size());
    assertTrue(group.getElements().get(0) instanceof Group);
    assertTrue(group.getElements().get(1) instanceof Group);
}
Also used : Group(org.eclipse.xtext.Group) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) Test(org.junit.Test)

Example 32 with UnorderedGroup

use of org.eclipse.xtext.UnorderedGroup in project xtext-eclipse 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.");
            String _gaRuleElementAccessor = this._grammarAccess.gaRuleElementAccessor(AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it));
            _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.");
                    String _gaRuleElementAccessor_1 = this._grammarAccess.gaRuleElementAccessor(AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it));
                    _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.");
                    String _gaRuleElementAccessor_2 = this._grammarAccess.gaRuleElementAccessor(AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it));
                    _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.");
                    String _gaRuleElementAccessor_3 = this._grammarAccess.gaRuleElementAccessor(AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it));
                    _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.");
                    String _gaRuleElementAccessor_4 = this._grammarAccess.gaRuleElementAccessor(AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it));
                    _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.");
            String _gaRuleElementAccessor_5 = this._grammarAccess.gaRuleElementAccessor(AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it));
            _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)

Example 33 with UnorderedGroup

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

the class AbstractAntlrGrammarWithActionsGenerator method _ebnf2.

@Override
protected String _ebnf2(final UnorderedGroup it, final AntlrOptions options, 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.");
            String _gaRuleElementAccessor = this._grammarAccess.gaRuleElementAccessor(AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it));
            _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.");
                    String _gaRuleElementAccessor_1 = this._grammarAccess.gaRuleElementAccessor(AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it));
                    _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.");
                    String _gaRuleElementAccessor_2 = this._grammarAccess.gaRuleElementAccessor(AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it));
                    _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 _ebnf2 = this.ebnf2(element.getValue(), options, supportActions);
                    _builder.append(_ebnf2, "\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.");
                    String _gaRuleElementAccessor_3 = this._grammarAccess.gaRuleElementAccessor(AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it));
                    _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.");
                    String _gaRuleElementAccessor_4 = this._grammarAccess.gaRuleElementAccessor(AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it));
                    _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.");
            String _gaRuleElementAccessor_5 = this._grammarAccess.gaRuleElementAccessor(AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it));
            _builder.append(_gaRuleElementAccessor_5, "\t  ");
            _builder.append(");");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _xblockexpression = _builder.toString();
        }
        _xifexpression = _xblockexpression;
    } else {
        _xifexpression = super._ebnf2(it, options, 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)

Example 34 with UnorderedGroup

use of org.eclipse.xtext.UnorderedGroup in project n4js by eclipse.

the class CustomN4JSParser method getFollowElements.

@SuppressWarnings("hiding")
@Override
public Collection<FollowElement> getFollowElements(FollowElement element) {
    if (element.getLookAhead() <= 1)
        throw new IllegalArgumentException("lookahead may not be less than or equal to 1");
    Collection<FollowElement> result = new ArrayList<>();
    Collection<AbstractElement> elementsToParse = this.reflective("getElementsToParse", element);
    for (AbstractElement elementToParse : elementsToParse) {
        // fix is here
        elementToParse = unwrapSingleElementGroups(elementToParse);
        // done
        String ruleName = getRuleName(elementToParse);
        String[][] allRuleNames = reflective("getRequiredRuleNames", ruleName, element.getParamStack(), elementToParse);
        for (String[] ruleNames : allRuleNames) {
            for (int i = 0; i < ruleNames.length; i++) {
                AbstractInternalContentAssistParser parser = createParser();
                parser.setUnorderedGroupHelper(getUnorderedGroupHelper().get());
                parser.getUnorderedGroupHelper().initializeWith(parser);
                final Iterator<LookAheadTerminal> iter = element.getLookAheadTerminals().iterator();
                ObservableXtextTokenStream tokens = new ObservableXtextTokenStream(new TokenSource() {

                    @Override
                    public Token nextToken() {
                        if (iter.hasNext()) {
                            LookAheadTerminal lookAhead = iter.next();
                            return lookAhead.getToken();
                        }
                        return Token.EOF_TOKEN;
                    }

                    @Override
                    public String getSourceName() {
                        return "LookAheadTerminalTokenSource";
                    }
                }, parser);
                parser.setTokenStream(tokens);
                tokens.setListener(parser);
                parser.getGrammarElements().addAll(element.getTrace());
                parser.getGrammarElements().add(elementToParse);
                parser.getLocalTrace().addAll(element.getLocalTrace());
                parser.getLocalTrace().add(elementToParse);
                parser.getParamStack().addAll(element.getParamStack());
                if (elementToParse instanceof UnorderedGroup && element.getGrammarElement() == elementToParse) {
                    UnorderedGroup group = (UnorderedGroup) elementToParse;
                    final IUnorderedGroupHelper helper = parser.getUnorderedGroupHelper();
                    helper.enter(group);
                    for (AbstractElement consumed : element.getHandledUnorderedGroupElements()) {
                        parser.before(consumed);
                        helper.select(group, group.getElements().indexOf(consumed));
                        helper.returnFromSelection(group);
                        parser.after(consumed);
                    }
                    parser.setUnorderedGroupHelper(new IUnorderedGroupHelper() {

                        boolean first = true;

                        @Override
                        public void initializeWith(BaseRecognizer recognizer) {
                            helper.initializeWith(recognizer);
                        }

                        @Override
                        public void enter(UnorderedGroup group) {
                            if (!first)
                                helper.enter(group);
                            first = false;
                        }

                        @Override
                        public void leave(UnorderedGroup group) {
                            helper.leave(group);
                        }

                        @Override
                        public boolean canSelect(UnorderedGroup group, int index) {
                            return helper.canSelect(group, index);
                        }

                        @Override
                        public void select(UnorderedGroup group, int index) {
                            helper.select(group, index);
                        }

                        @Override
                        public void returnFromSelection(UnorderedGroup group) {
                            helper.returnFromSelection(group);
                        }

                        @Override
                        public boolean canLeave(UnorderedGroup group) {
                            return helper.canLeave(group);
                        }

                        @Override
                        public UnorderedGroupState snapShot(UnorderedGroup... groups) {
                            return helper.snapShot(groups);
                        }
                    });
                }
                Collection<FollowElement> elements = reflective("getFollowElements", parser, elementToParse, ruleNames, i);
                result.addAll(elements);
            }
        }
    }
    return result;
}
Also used : TokenSource(org.antlr.runtime.TokenSource) AbstractElement(org.eclipse.xtext.AbstractElement) ObservableXtextTokenStream(org.eclipse.xtext.ide.editor.contentassist.antlr.ObservableXtextTokenStream) ArrayList(java.util.ArrayList) Token(org.antlr.runtime.Token) CommonToken(org.antlr.runtime.CommonToken) AbstractInternalContentAssistParser(org.eclipse.xtext.ide.editor.contentassist.antlr.internal.AbstractInternalContentAssistParser) FollowElement(org.eclipse.xtext.ide.editor.contentassist.antlr.FollowElement) IUnorderedGroupHelper(org.eclipse.xtext.parser.antlr.IUnorderedGroupHelper) LookAheadTerminal(org.eclipse.xtext.ide.editor.contentassist.antlr.LookAheadTerminal) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) BaseRecognizer(org.antlr.runtime.BaseRecognizer)

Example 35 with UnorderedGroup

use of org.eclipse.xtext.UnorderedGroup in project n4js by eclipse.

the class PatchedFollowElementComputer method computeFollowElements.

@Override
protected void computeFollowElements(FollowElementCalculator calculator, FollowElement element, Multimap<Integer, List<AbstractElement>> visited) {
    List<AbstractElement> currentState = Lists.newArrayList(element.getLocalTrace());
    currentState.add(element.getGrammarElement());
    if (!visited.put(element.getLookAhead(), currentState))
        return;
    if (element.getLookAhead() <= 1) {
        for (AbstractElement abstractElement : currentState) {
            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 : Assignment(org.eclipse.xtext.Assignment) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) Group(org.eclipse.xtext.Group) ParserRule(org.eclipse.xtext.ParserRule) AbstractElement(org.eclipse.xtext.AbstractElement) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) EObject(org.eclipse.emf.ecore.EObject) RuleCall(org.eclipse.xtext.RuleCall) FollowElement(org.eclipse.xtext.ide.editor.contentassist.antlr.FollowElement)

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