Search in sources :

Example 1 with UnorderedGroup

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

the class AbstractAntlrGrammarWithActionsGenerator method _compileInitUnorderedGroups.

protected CharSequence _compileInitUnorderedGroups(final ParserRule it, final AntlrOptions options) {
    StringConcatenation _builder = new StringConcatenation();
    {
        boolean _definesUnorderedGroups = this._grammarAccessExtensions.definesUnorderedGroups(it, options);
        if (_definesUnorderedGroups) {
            _builder.newLineIfNotEmpty();
            _builder.append("UnorderedGroupState myUnorderedGroupState = getUnorderedGroupHelper().snapShot(");
            _builder.newLine();
            {
                Iterable<UnorderedGroup> _filter = Iterables.<UnorderedGroup>filter(EcoreUtil2.eAllContentsAsList(it), UnorderedGroup.class);
                boolean _hasElements = false;
                for (final UnorderedGroup group : _filter) {
                    if (!_hasElements) {
                        _hasElements = true;
                    } else {
                        _builder.appendImmediate(", ", "");
                    }
                    _builder.append("grammarAccess.");
                    String _gaRuleElementAccessor = this._grammarAccess.gaRuleElementAccessor(AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(group));
                    _builder.append(_gaRuleElementAccessor);
                    _builder.newLineIfNotEmpty();
                }
            }
            _builder.append(");");
        }
    }
    return _builder;
}
Also used : UnorderedGroup(org.eclipse.xtext.UnorderedGroup) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation)

Example 2 with UnorderedGroup

use of org.eclipse.xtext.UnorderedGroup in project xtext-eclipse 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 org.eclipse.xtext.parser.antlr.AbstractInternalAntlrParser;");
    _builder.newLine();
    _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 _gaFQName = this._grammarAccess.gaFQName(it);
    _builder.append(_gaFQName);
    _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 3 with UnorderedGroup

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

the class PsiAntlrGrammarGenerator method compileParserImports.

@Override
protected String compileParserImports(final Grammar it, final AntlrOptions options) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.newLine();
    _builder.append("import org.eclipse.xtext.idea.parser.AbstractPsiAntlrParser;");
    _builder.newLine();
    _builder.append("import ");
    String _elementTypeProviderName = this._ideaPluginClassNames.getElementTypeProviderName(GrammarUtil.getGrammar(it));
    _builder.append(_elementTypeProviderName);
    _builder.append(";");
    _builder.newLineIfNotEmpty();
    _builder.append("import org.eclipse.xtext.idea.parser.TokenTypeProvider;");
    _builder.newLine();
    _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 ");
    String _gaFQName = this._grammarAccess.gaFQName(it);
    _builder.append(_gaFQName);
    _builder.append(";");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("import com.intellij.lang.PsiBuilder;");
    _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 4 with UnorderedGroup

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

the class BaseContentAssistParser method getFollowElements.

public Collection<FE> getFollowElements(FE element) {
    if (element.getLookAhead() <= 1)
        throw new IllegalArgumentException("lookahead may not be less than or equal to 1");
    Collection<FE> result = new ArrayList<FE>();
    for (AbstractElement elementToParse : getElementsToParse(element)) {
        elementToParse = unwrapSingleElementGroups(elementToParse);
        String ruleName = getRuleName(elementToParse);
        String[][] allRuleNames = getRequiredRuleNames(ruleName, element.getParamStack(), elementToParse);
        for (String[] ruleNames : allRuleNames) {
            for (int i = 0; i < ruleNames.length; i++) {
                InternalParser parser = createParser();
                parser.setUnorderedGroupHelper(createUnorderedGroupHelper());
                parser.getUnorderedGroupHelper().initializeWith(parser);
                ObservableXtextTokenStream tokens = setTokensFromFollowElement(parser, element);
                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;
                    IUnorderedGroupHelper helper = getInitializedUnorderedGroupHelper(element, parser, group);
                    parser.setUnorderedGroupHelper(ignoreFirstEntrance(helper));
                }
                Collection<FE> elements = getFollowElements(parser, elementToParse, ruleNames, i);
                result.addAll(elements);
            }
        }
    }
    return result;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) ArrayList(java.util.ArrayList) IUnorderedGroupHelper(org.eclipse.xtext.parser.antlr.IUnorderedGroupHelper) UnorderedGroup(org.eclipse.xtext.UnorderedGroup)

Example 5 with UnorderedGroup

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

the class ConcreteSyntaxConstraintProvider method createElement.

protected ISyntaxConstraint createElement(EObject obj) {
    if (!(obj instanceof AbstractElement))
        return null;
    AbstractElement ele = (AbstractElement) obj;
    boolean multiple = false;
    boolean optional = false;
    EClass semanticType = null;
    while (true) {
        multiple = multiple || isMultipleCardinality(ele);
        optional = optional || isOptionalCardinality(ele);
        if (ele.eContainer() instanceof ParserRule && ((ParserRule) ele.eContainer()).getType().getClassifier() instanceof EClass)
            semanticType = (EClass) ((ParserRule) ele.eContainer()).getType().getClassifier();
        if (ele instanceof Assignment) {
            return createElement(ConstraintType.ASSIGNMENT, ele, semanticType, multiple, optional);
        } else if (ele instanceof Group || ele instanceof UnorderedGroup) {
            CompoundElement comp = (CompoundElement) ele;
            AbstractElement lastChild = null;
            for (AbstractElement o : comp.getElements()) if (containsRelevantElement(o)) {
                if (lastChild == null)
                    lastChild = o;
                else {
                    List<AbstractElement> c = new ArrayList<AbstractElement>(comp.getElements());
                    List<ISyntaxConstraint> e = createSummarizedAssignments(comp, c, semanticType, optional);
                    if (e.size() == 1 && c.size() == 0)
                        return e.get(0);
                    return createElement(ConstraintType.GROUP, ele, c, e, semanticType, multiple, optional);
                }
            }
            if (lastChild == null)
                return null;
            ele = lastChild;
            continue;
        } else if (ele instanceof Alternatives) {
            int relevantChildren = 0;
            AbstractElement lastChild = null;
            for (AbstractElement o : ((CompoundElement) ele).getElements()) if (containsRelevantElement(o)) {
                relevantChildren++;
                lastChild = o;
            }
            if (relevantChildren < ((CompoundElement) ele).getElements().size())
                optional = true;
            if (relevantChildren > 1)
                return createElement(ConstraintType.ALTERNATIVE, ele, semanticType, multiple, optional);
            if (lastChild == null)
                return null;
            ele = lastChild;
            continue;
        } else if (ele instanceof Action) {
            semanticType = (EClass) ((Action) ele).getType().getClassifier();
            return createElement(ConstraintType.ACTION, ele, semanticType, multiple, optional);
        } else if (ele instanceof RuleCall) {
            AbstractRule rule = ((RuleCall) ele).getRule();
            if (rule.getType().getClassifier() instanceof EClass) {
                ele = rule.getAlternatives();
                continue;
            }
        }
        return null;
    }
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) Group(org.eclipse.xtext.Group) Action(org.eclipse.xtext.Action) AbstractElement(org.eclipse.xtext.AbstractElement) ArrayList(java.util.ArrayList) Alternatives(org.eclipse.xtext.Alternatives) RuleCall(org.eclipse.xtext.RuleCall) Assignment(org.eclipse.xtext.Assignment) EClass(org.eclipse.emf.ecore.EClass) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) CompoundElement(org.eclipse.xtext.CompoundElement) AbstractRule(org.eclipse.xtext.AbstractRule)

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