Search in sources :

Example 66 with AbstractElement

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

the class ContentAssistContextFactory method doCreateContexts.

protected void doCreateContexts(INode lastCompleteNode, INode currentNode, String prefix, EObject previousModel, Collection<FollowElement> followElements) {
    Set<AbstractElement> followElementAsAbstractElements = Sets.newLinkedHashSet();
    followElementComputer.computeFollowElements(followElements, followElementAsAbstractElements);
    Multimap<EObject, AbstractElement> contextMap = computeCurrentModel(previousModel, lastCompleteNode, followElementAsAbstractElements);
    currentNode = getContainingDatatypeRuleNode(currentNode);
    for (Entry<EObject, Collection<AbstractElement>> entry : contextMap.asMap().entrySet()) {
        ContentAssistContext.Builder contextBuilder = doCreateContext(lastCompleteNode, entry.getKey(), previousModel, currentNode, prefix);
        for (AbstractElement element : entry.getValue()) {
            contextBuilder.accept(element);
        }
        contextBuilders.add(contextBuilder);
    }
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) EObject(org.eclipse.emf.ecore.EObject) ContentAssistContext(org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext) Collection(java.util.Collection) Builder(org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext.Builder)

Example 67 with AbstractElement

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

the class ContentAssistContextTestHelper method firstSetGrammarElementsToString.

public String firstSetGrammarElementsToString(final ContentAssistContextFactory factory) {
    final int offset = this.document.indexOf(this.cursor);
    Preconditions.checkArgument((offset >= 0), "you forgot to provide a cursor");
    final String doc = this.document.replace(this.cursor, "");
    final XtextResource res = this.parse(doc);
    factory.setPool(Executors.newCachedThreadPool());
    TextRegion _textRegion = new TextRegion(0, 0);
    final ContentAssistContext[] ctxs = factory.create(doc, _textRegion, offset, res);
    final GrammarElementTitleSwitch f = new GrammarElementTitleSwitch().showAssignments().showQualified().showRule();
    StringConcatenation _builder = new StringConcatenation();
    {
        Iterable<Pair<Integer, ContentAssistContext>> _indexed = IterableExtensions.<ContentAssistContext>indexed(((Iterable<? extends ContentAssistContext>) Conversions.doWrapArray(ctxs)));
        for (final Pair<Integer, ContentAssistContext> ctx : _indexed) {
            _builder.append("context");
            Integer _key = ctx.getKey();
            _builder.append(_key);
            _builder.append(" {");
            _builder.newLineIfNotEmpty();
            {
                ImmutableList<AbstractElement> _firstSetGrammarElements = ctx.getValue().getFirstSetGrammarElements();
                for (final AbstractElement ele : _firstSetGrammarElements) {
                    _builder.append("\t");
                    String _name = ele.eClass().getName();
                    _builder.append(_name, "\t");
                    _builder.append(": ");
                    String _apply = f.apply(ele);
                    _builder.append(_apply, "\t");
                    _builder.newLineIfNotEmpty();
                }
            }
            _builder.append("}");
            _builder.newLine();
        }
    }
    return _builder.toString();
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) AbstractElement(org.eclipse.xtext.AbstractElement) XtextResource(org.eclipse.xtext.resource.XtextResource) GrammarElementTitleSwitch(org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch) ContentAssistContext(org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Pair(org.eclipse.xtext.xbase.lib.Pair)

Example 68 with AbstractElement

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

the class FirstSetComputationTest method assertFirstSet.

protected void assertFirstSet(String expectation, AbstractRule rule) {
    RuleCall ruleCall = XtextFactory.eINSTANCE.createRuleCall();
    ruleCall.setRule(rule);
    List<AbstractElement> firstSet = AntlrGrammarGenUtil.getFirstSet(ruleCall);
    StringBuilder actual = new StringBuilder();
    GrammarElementTitleSwitch stringifier = new GrammarElementTitleSwitch();
    for (int i = 0; i < firstSet.size(); i++) {
        if (i != 0)
            actual.append(", ");
        actual.append(stringifier.apply(firstSet.get(i)));
    }
    assertEquals(expectation, actual.toString());
}
Also used : GrammarElementTitleSwitch(org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch) AbstractElement(org.eclipse.xtext.AbstractElement) RuleCall(org.eclipse.xtext.RuleCall)

Example 69 with AbstractElement

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

the class ContextPDAProvider method collectExtracted.

protected void collectExtracted(ISerState orig, Collection<? extends ISerState> precedents, SerializerPDAState copy, Map<Pair<AbstractElement, SerStateType>, SerializerPDAState> oldToNew, final CallStack inTop, SerializerPDAState start) {
    for (ISerState pre : precedents) {
        CallStack top = inTop;
        AbstractElement element = pre.getGrammarElement();
        switch(pre.getType()) {
            case START:
                if (top.call == null)
                    connect(start, copy);
                continue;
            case POP:
                top = new CallStack(top, (RuleCall) element);
                if (top.isLoop())
                    continue;
                break;
            case PUSH:
                if (top.call == null) {
                    connect(start, copy);
                    continue;
                } else if (top.call == element) {
                    top = top.parent;
                } else {
                    continue;
                }
            default:
                break;
        }
        Pair<AbstractElement, SerStateType> key = Tuples.create(element, pre.getType());
        SerializerPDAState pre2 = oldToNew.get(key);
        if (pre2 == null) {
            pre2 = new SerializerPDAState(element, pre.getType());
            oldToNew.put(key, pre2);
        }
        if (GrammarUtil.isAssignedAction(pre.getGrammarElement())) /* && pre.getType() != STOP */
        {
            Set<ISerState> entries = collectPushForAction(pre);
            collectExtracted(pre, entries, pre2, oldToNew, top, start);
        } else {
            if (top.visited.add(pre))
                collectExtracted(pre, pre.getPrecedents(), pre2, oldToNew, top, start);
        }
        connect(pre2, copy);
    }
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) SerializerPDAState(org.eclipse.xtext.serializer.analysis.SerializerPDA.SerializerPDAState) RuleCall(org.eclipse.xtext.RuleCall) SerStateType(org.eclipse.xtext.serializer.analysis.ISerState.SerStateType)

Example 70 with AbstractElement

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

the class GrammarConstraintProvider method findBestConstraintName.

protected String findBestConstraintName(Grammar grammar, SerializationContextMap<Pda<ISerState, RuleCall>> typePDAs, IConstraint constraint) {
    Set<ParserRule> relevantRules = Sets.newLinkedHashSet();
    Set<Action> relevantActions = Sets.newLinkedHashSet();
    Set<ParserRule> contextRules = Sets.newLinkedHashSet();
    for (ISerializationContext ctx : constraint.getContexts()) {
        ParserRule rule = ctx.getParserRule();
        if (rule != null) {
            contextRules.add(rule);
        }
    }
    // 1. find relevant rules based on assignments
    for (ISemState s : nfaUtil.collect(constraint.getNfa())) {
        AbstractElement element = s.getAssignedGrammarElement();
        if (element != null)
            relevantRules.add(GrammarUtil.containingParserRule(element));
    }
    // 2. find relevant rules based on unassigned actions
    for (ISerializationContext ctx : constraint.getContexts()) {
        for (ISerState s : nfaUtil.collect(typePDAs.get(ctx))) {
            AbstractElement element = s.getGrammarElement();
            if (element instanceof Action && ((Action) element).getFeature() == null)
                relevantRules.add(GrammarUtil.containingParserRule(element));
        }
    }
    if (relevantRules.isEmpty()) {
        Set<ParserRule> allRules = Sets.newLinkedHashSet(contextRules);
        for (ISerializationContext ctx : constraint.getContexts()) {
            Action action = ctx.getAssignedAction();
            if (action != null)
                allRules.add(GrammarUtil.containingParserRule(action));
        }
        // 3. use all rules, because the constraint returns null.
        relevantRules.addAll(allRules);
    }
    for (ISerializationContext ctx : constraint.getContexts()) {
        Action action = ctx.getAssignedAction();
        if (action != null) {
            ParserRule rule = GrammarUtil.containingParserRule(action);
            if (!contextRules.contains(rule) && relevantRules.contains(rule)) {
                relevantActions.add(action);
            }
        }
    }
    List<String> actions = Lists.newArrayList();
    List<String> rules = Lists.newArrayList();
    Multimap<Parameter, Boolean> parameterValues = collectAllParameterValues(constraint);
    for (Action a : relevantActions) actions.add(context2Name.getUniqueActionName(a));
    for (ParserRule rule : relevantRules) {
        StringBuilder segments = new StringBuilder();
        for (Parameter param : rule.getParameters()) {
            Collection<Boolean> values = parameterValues.get(param);
            if (values.size() == 1) {
                segments.append(param.getName() + "$" + values.iterator().next() + "$");
            }
        }
        if (segments.length() == 0) {
            rules.add(rule.getName());
        } else {
            rules.add(rule.getName() + "$" + segments);
        }
    }
    Collections.sort(rules);
    String result = Joiner.on("_").join(rules);
    if (!actions.isEmpty()) {
        Collections.sort(actions);
        result += "_" + Joiner.on('_').join(actions);
    }
    return result;
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Action(org.eclipse.xtext.Action) AbstractElement(org.eclipse.xtext.AbstractElement) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) Parameter(org.eclipse.xtext.Parameter) ISemState(org.eclipse.xtext.serializer.analysis.ISemanticSequencerNfaProvider.ISemState)

Aggregations

AbstractElement (org.eclipse.xtext.AbstractElement)126 EObject (org.eclipse.emf.ecore.EObject)39 AbstractRule (org.eclipse.xtext.AbstractRule)34 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)31 RuleCall (org.eclipse.xtext.RuleCall)26 ParserRule (org.eclipse.xtext.ParserRule)18 Grammar (org.eclipse.xtext.Grammar)17 EList (org.eclipse.emf.common.util.EList)15 Assignment (org.eclipse.xtext.Assignment)15 Test (org.junit.Test)14 Group (org.eclipse.xtext.Group)13 UnorderedGroup (org.eclipse.xtext.UnorderedGroup)13 List (java.util.List)12 ArrayList (java.util.ArrayList)11 XtextResource (org.eclipse.xtext.resource.XtextResource)11 Action (org.eclipse.xtext.Action)10 TerminalRule (org.eclipse.xtext.TerminalRule)10 Alternatives (org.eclipse.xtext.Alternatives)9 EClassifier (org.eclipse.emf.ecore.EClassifier)8 InternalEObject (org.eclipse.emf.ecore.InternalEObject)8