Search in sources :

Example 1 with ISerState

use of org.eclipse.xtext.serializer.analysis.ISerState in project xtext-core by eclipse.

the class InsertionPointFinder method findInsertionPoint.

public IHiddenRegion findInsertionPoint(ISerializationContext ctx, IEObjectRegion obj, AbstractElement ins) {
    ISerState insertionState = findState(ctx, ins);
    Set<AbstractElement> followers = collectAdjacent(insertionState, s -> s.getFollowers());
    Set<AbstractElement> precendents = collectAdjacent(insertionState, s -> s.getPrecedents());
    List<IAstRegion> regions = Lists.newArrayList(obj.getAstRegions());
    if (regions.isEmpty()) {
        return obj.getPreviousHiddenRegion();
    }
    if (followers.contains(regions.get(0).getGrammarElement())) {
        return obj.getPreviousHiddenRegion();
    }
    if (precendents.contains(regions.get(regions.size() - 1).getGrammarElement())) {
        return obj.getNextHiddenRegion();
    }
    for (int i = 0; i < regions.size() - 1; i++) {
        IAstRegion leading = regions.get(i);
        IAstRegion trailing = regions.get(i + 1);
        if (precendents.contains(leading.getGrammarElement()) && followers.contains(trailing.getGrammarElement())) {
            return leading.getNextHiddenRegion();
        }
    }
    return null;
}
Also used : ISerState(org.eclipse.xtext.serializer.analysis.ISerState) AbstractElement(org.eclipse.xtext.AbstractElement) IAstRegion(org.eclipse.xtext.formatting2.regionaccess.IAstRegion)

Example 2 with ISerState

use of org.eclipse.xtext.serializer.analysis.ISerState in project xtext-core by eclipse.

the class InsertionPointFinder method collectAdjacent.

protected Set<AbstractElement> collectAdjacent(ISerState start, Function<ISerState, List<? extends ISerState>> followers) {
    LinkedList<ISerState> todo = new LinkedList<>();
    Set<ISerState> seen = Sets.newHashSet();
    todo.addAll(followers.apply(start));
    Set<AbstractElement> result = Sets.newLinkedHashSet();
    while (!todo.isEmpty()) {
        ISerState next = todo.pop();
        if (!seen.add(next)) {
            continue;
        }
        if (next.getType() == SerStateType.ELEMENT && !GrammarUtil.isUnassignedAction(next.getGrammarElement())) {
            result.add(next.getGrammarElement());
        } else {
            todo.addAll(followers.apply(next));
        }
    }
    return result;
}
Also used : ISerState(org.eclipse.xtext.serializer.analysis.ISerState) AbstractElement(org.eclipse.xtext.AbstractElement) LinkedList(java.util.LinkedList)

Example 3 with ISerState

use of org.eclipse.xtext.serializer.analysis.ISerState in project xtext-core by eclipse.

the class ContextPDAProviderTest method getParserRule.

protected String getParserRule(String body) throws Exception {
    Grammar grammar = (Grammar) getModel(HEADER + body);
    List<String> result = Lists.newArrayList();
    PdaListFormatter<ISerState, RuleCall> formatter = new PdaListFormatter<ISerState, RuleCall>();
    formatter.setStateFormatter(new ToStr());
    formatter.setStackitemFormatter(new GrammarElementTitleSwitch().showAssignments().hideCardinality());
    formatter.sortFollowers();
    IContextPDAProvider pdaProvider = get(IContextPDAProvider.class);
    SerializationContextMap<Pda<ISerState, RuleCall>> pdas = pdaProvider.getContextPDAs(grammar);
    for (Entry<Pda<ISerState, RuleCall>> ctx : pdas.sortedCopy().values()) {
        result.add(Joiner.on(", ").join(ctx.getContexts()) + ":");
        Pda<ISerState, RuleCall> pda = ctx.getValue();
        result.add("  " + formatter.format(pda).replace("\n", "\n  "));
    // StackTraceElement ele = Thread.currentThread().getStackTrace()[2];
    // String name = getClass().getSimpleName() + "_" + ele.getMethodName() + "_" + ctx.getSecond() + ".pdf";
    // new PdaToDot<ISerState, RuleCall>().draw(pda, "dot/" + name, "-T pdf");
    }
    return Joiner.on("\n").join(result);
}
Also used : Grammar(org.eclipse.xtext.Grammar) Pda(org.eclipse.xtext.util.formallang.Pda) RuleCall(org.eclipse.xtext.RuleCall) ISerState(org.eclipse.xtext.serializer.analysis.ISerState) GrammarElementTitleSwitch(org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch) PdaListFormatter(org.eclipse.xtext.util.formallang.PdaListFormatter) IContextPDAProvider(org.eclipse.xtext.serializer.analysis.IContextPDAProvider)

Example 4 with ISerState

use of org.eclipse.xtext.serializer.analysis.ISerState in project xtext-core by eclipse.

the class ContextTypePDAProviderTest method getParserRule.

protected String getParserRule(String body) throws Exception {
    Grammar grammar = (Grammar) getModel(HEADER + body);
    // drawGrammar("pdf/" + getName(), grammar);
    List<String> result = Lists.newArrayList();
    PdaListFormatter<ISerState, RuleCall> formatter = new PdaListFormatter<ISerState, RuleCall>();
    formatter.setStateFormatter(new ToStr());
    formatter.setStackitemFormatter(new GrammarElementTitleSwitch().showAssignments().hideCardinality());
    formatter.sortFollowers();
    IContextTypePDAProvider typePDAProvider = get(IContextTypePDAProvider.class);
    SerializationContextMap<Pda<ISerState, RuleCall>> pdas = typePDAProvider.getContextTypePDAs(grammar);
    for (Entry<Pda<ISerState, RuleCall>> ctx : pdas.sortedCopy().values()) {
        result.add(Joiner.on(", ").join(ctx.getContexts()) + ":");
        result.add("  " + formatter.format(ctx.getValue()).replace("\n", "\n  "));
    }
    return Joiner.on("\n").join(result);
}
Also used : Grammar(org.eclipse.xtext.Grammar) Pda(org.eclipse.xtext.util.formallang.Pda) RuleCall(org.eclipse.xtext.RuleCall) IContextTypePDAProvider(org.eclipse.xtext.serializer.analysis.IContextTypePDAProvider) ISerState(org.eclipse.xtext.serializer.analysis.ISerState) GrammarElementTitleSwitch(org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch) PdaListFormatter(org.eclipse.xtext.util.formallang.PdaListFormatter)

Example 5 with ISerState

use of org.eclipse.xtext.serializer.analysis.ISerState in project xtext-core by eclipse.

the class GrammarPDAProviderTest method toPda.

private String toPda(final CharSequence rulesText) {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("grammar org.eclipse.xtext.serializer.GrammarPDAProviderTestLanguage with org.eclipse.xtext.common.Terminals");
        _builder.newLine();
        _builder.newLine();
        _builder.append("generate GrammarPDAProviderTest \"http://www.eclipse.org/2010/tmf/xtext/GrammarPDAProviderTestLanguage\"");
        _builder.newLine();
        _builder.newLine();
        _builder.append(rulesText);
        _builder.newLineIfNotEmpty();
        final Grammar grammar = this.parser.parse(_builder);
        this.validator.assertNoErrors(grammar);
        final SerializationContextMap<Pda<ISerState, RuleCall>> pdas = this.pdaProvider.getGrammarPDAs(grammar);
        final Consumer<SerializationContextMap.Entry<Pda<ISerState, RuleCall>>> _function = (SerializationContextMap.Entry<Pda<ISerState, RuleCall>> it) -> {
            this.assertNoLeakedGrammarElements(grammar, it.getValue());
        };
        pdas.values().forEach(_function);
        final Function1<SerializationContextMap.Entry<Pda<ISerState, RuleCall>>, List<ISerializationContext>> _function_1 = (SerializationContextMap.Entry<Pda<ISerState, RuleCall>> it) -> {
            return it.getContexts();
        };
        final Function1<ISerializationContext, String> _function_2 = (ISerializationContext it) -> {
            StringConcatenation _builder_1 = new StringConcatenation();
            _builder_1.append(it);
            _builder_1.append(":");
            _builder_1.newLineIfNotEmpty();
            _builder_1.append("\t");
            String _listString = this.toListString(pdas.get(it));
            _builder_1.append(_listString, "\t");
            _builder_1.newLineIfNotEmpty();
            return _builder_1.toString();
        };
        return IterableExtensions.join(ListExtensions.<ISerializationContext, String>map(IterableExtensions.<ISerializationContext>sort(Iterables.<ISerializationContext>concat(ListExtensions.<SerializationContextMap.Entry<Pda<ISerState, RuleCall>>, List<ISerializationContext>>map(pdas.values(), _function_1))), _function_2));
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : Grammar(org.eclipse.xtext.Grammar) Pda(org.eclipse.xtext.util.formallang.Pda) RuleCall(org.eclipse.xtext.RuleCall) ISerState(org.eclipse.xtext.serializer.analysis.ISerState) SerializationContextMap(org.eclipse.xtext.serializer.analysis.SerializationContextMap) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) List(java.util.List)

Aggregations

ISerState (org.eclipse.xtext.serializer.analysis.ISerState)8 RuleCall (org.eclipse.xtext.RuleCall)5 Grammar (org.eclipse.xtext.Grammar)4 Pda (org.eclipse.xtext.util.formallang.Pda)4 AbstractElement (org.eclipse.xtext.AbstractElement)3 GrammarElementTitleSwitch (org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch)3 PdaListFormatter (org.eclipse.xtext.util.formallang.PdaListFormatter)3 List (java.util.List)2 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 IAstRegion (org.eclipse.xtext.formatting2.regionaccess.IAstRegion)1 ISerializationContext (org.eclipse.xtext.serializer.ISerializationContext)1 IContextPDAProvider (org.eclipse.xtext.serializer.analysis.IContextPDAProvider)1 IContextTypePDAProvider (org.eclipse.xtext.serializer.analysis.IContextTypePDAProvider)1 ISemanticSequencerNfaProvider (org.eclipse.xtext.serializer.analysis.ISemanticSequencerNfaProvider)1 ISyntacticSequencerPDAProvider (org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider)1 SerializationContextMap (org.eclipse.xtext.serializer.analysis.SerializationContextMap)1 Nfa (org.eclipse.xtext.util.formallang.Nfa)1 NfaUtil (org.eclipse.xtext.util.formallang.NfaUtil)1