Search in sources :

Example 1 with ISerializationContext

use of org.eclipse.xtext.serializer.ISerializationContext in project xtext-eclipse by eclipse.

the class SerializerTester method serialize.

/**
 * @since 2.3
 */
protected String serialize(EObject semanticObject, DelegatingSequenceAcceptor... acceptors) {
    ISequenceAcceptor debug = null;
    try {
        ISerializationDiagnostic.Acceptor errors = ISerializationDiagnostic.EXCEPTION_THROWING_ACCEPTOR;
        ISemanticSequencer semantic = semanticSequencerProvider.get();
        ISyntacticSequencer syntactic = syntacticSequencerProvider.get();
        IHiddenTokenSequencer hidden = hiddenTokenSequencerProvider.get();
        ISequenceAcceptor result = new StringBufferSequenceAcceptor();
        ISequenceAcceptor out = result;
        for (DelegatingSequenceAcceptor delegate : acceptors) {
            delegate.setDelegate(out);
            out = delegate;
        }
        out = debug = new DebugSequenceAcceptor(out);
        semantic.init((ISemanticSequenceAcceptor) syntactic, errors);
        ISerializationContext context = getContext(semanticObject);
        syntactic.init(context, semanticObject, (ISyntacticSequenceAcceptor) hidden, errors);
        hidden.init(context, semanticObject, out, errors);
        semantic.createSequence(context, semanticObject);
        return result.toString();
    } catch (Exception t) {
        if (debug != null) {
            System.out.println("Serializer debug output:");
            System.out.println(debug.toString());
        }
        Exceptions.sneakyThrow(t);
        return "";
    }
}
Also used : ISerializationDiagnostic(org.eclipse.xtext.serializer.diagnostic.ISerializationDiagnostic) ISyntacticSequencer(org.eclipse.xtext.serializer.sequencer.ISyntacticSequencer) IHiddenTokenSequencer(org.eclipse.xtext.serializer.sequencer.IHiddenTokenSequencer) StringBufferSequenceAcceptor(org.eclipse.xtext.serializer.acceptor.StringBufferSequenceAcceptor) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) ISequenceAcceptor(org.eclipse.xtext.serializer.acceptor.ISequenceAcceptor) DelegatingSequenceAcceptor(org.eclipse.xtext.serializer.acceptor.DelegatingSequenceAcceptor) ISemanticSequencer(org.eclipse.xtext.serializer.sequencer.ISemanticSequencer)

Example 2 with ISerializationContext

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

the class SerializerFragment2 method genMethodSequenceComment.

private StringConcatenationClient genMethodSequenceComment(final IGrammarConstraintProvider.IConstraint c) {
    StringConcatenationClient _client = new StringConcatenationClient() {

        @Override
        protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
            _builder.append("// This method is commented out because it has the same signature as another method in this class.");
            _builder.newLine();
            _builder.append("// This is probably a bug in Xtext\'s serializer, please report it here: ");
            _builder.newLine();
            _builder.append("// https://bugs.eclipse.org/bugs/enter_bug.cgi?product=TMF");
            _builder.newLine();
            _builder.append("//");
            _builder.newLine();
            _builder.append("// Contexts:");
            _builder.newLine();
            _builder.append("//     ");
            String _replaceAll = IterableExtensions.join(IterableExtensions.<ISerializationContext>sort(c.getContexts()), "\n").replaceAll("\\n", "\n//     ");
            _builder.append(_replaceAll);
            _builder.newLineIfNotEmpty();
            _builder.append("//");
            _builder.newLine();
            _builder.append("// Constraint:");
            _builder.newLine();
            _builder.append("//     ");
            {
                IGrammarConstraintProvider.IConstraintElement _body = c.getBody();
                boolean _tripleEquals = (_body == null);
                if (_tripleEquals) {
                    _builder.append("{");
                    String _name = c.getType().getName();
                    _builder.append(_name);
                    _builder.append("}");
                } else {
                    String _replaceAll_1 = c.getBody().toString().replaceAll("\\n", "\n//     ");
                    _builder.append(_replaceAll_1);
                }
            }
            _builder.newLineIfNotEmpty();
            _builder.append("//");
            _builder.newLine();
            _builder.append("// protected void sequence_");
            String _simpleName = c.getSimpleName();
            _builder.append(_simpleName);
            _builder.append("(");
            _builder.append(ISerializationContext.class);
            _builder.append(" context, ");
            EClass _type = c.getType();
            _builder.append(_type);
            _builder.append(" semanticObject) { }");
            _builder.newLineIfNotEmpty();
        }
    };
    return _client;
}
Also used : EClass(org.eclipse.emf.ecore.EClass) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient)

Example 3 with ISerializationContext

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

the class NamedSerializationContextProvider method getSignificantGrammarElement.

public String getSignificantGrammarElement(Iterable<ISerializationContext> contexts) {
    ParserRule rule = null;
    int index = Integer.MAX_VALUE;
    for (ISerializationContext ctx : contexts) {
        ParserRule pr = ctx.getParserRule();
        if (pr == null) {
            Action action = ctx.getAssignedAction();
            if (action != null) {
                pr = GrammarUtil.containingParserRule(action);
            }
        }
        if (pr != null) {
            Integer i = rules.get(pr);
            if (i.intValue() < index) {
                index = i.intValue();
                rule = pr;
            }
        }
    }
    if (rule != null) {
        return rule.getName();
    }
    return "unknown";
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Action(org.eclipse.xtext.Action) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext)

Example 4 with ISerializationContext

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

the class SequenceFeeder method acceptEObjectRuleCall.

protected void acceptEObjectRuleCall(RuleCall ruleCall, EObject semanticChild, ICompositeNode node) {
    if (sequenceAcceptor.enterAssignedParserRuleCall(ruleCall, semanticChild, node)) {
        ISerializationContext child = SerializationContext.forChild(context, ruleCall, semanticChild);
        masterSequencer.createSequence(child, semanticChild);
        sequenceAcceptor.leaveAssignedParserRuleCall(ruleCall, semanticChild);
    }
}
Also used : ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext)

Example 5 with ISerializationContext

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

the class ContextTypePDAProvider method getContextTypePDAs.

@Override
public SerializationContextMap<Pda<ISerState, RuleCall>> getContextTypePDAs(Grammar grammar) {
    SerializationContextMap<Pda<ISerState, RuleCall>> cached = cache.get(grammar);
    if (cached != null)
        return cached;
    SerializationContextMap.Builder<Pda<ISerState, RuleCall>> builder = SerializationContextMap.builder();
    SerializationContextMap<Pda<ISerState, RuleCall>> contextPDAs = pdaProvider.getContextPDAs(grammar);
    for (SerializationContextMap.Entry<Pda<ISerState, RuleCall>> e : contextPDAs.values()) {
        List<ISerializationContext> parents = e.getContexts();
        Pda<ISerState, RuleCall> contextPDA = e.getValue();
        try {
            Map<ISerState, Integer> distances = nfaUtil.distanceToFinalStateMap(contextPDA);
            Set<EClass> types = collectTypes(contextPDA, distances);
            if (types.size() == 1) {
                for (ISerializationContext parent : parents) {
                    TypeContext ctx = new TypeContext(parent, types.iterator().next());
                    builder.put(ctx, contextPDA);
                }
            } else {
                for (EClass type : types) {
                    Pda<ISerState, RuleCall> filtered = filterByType(contextPDA, type, distances);
                    for (ISerializationContext parent : parents) {
                        TypeContext typeContext = new TypeContext(parent, type);
                        builder.put(typeContext, filtered);
                    }
                }
            }
        } catch (Exception x) {
            LOG.error("Error extracting PDAs for types for context '" + parents + "': " + x.getMessage(), x);
        }
    }
    SerializationContextMap<Pda<ISerState, RuleCall>> result = builder.create();
    cache.put(grammar, result);
    return result;
}
Also used : Pda(org.eclipse.xtext.util.formallang.Pda) TypeContext(org.eclipse.xtext.serializer.analysis.SerializationContext.TypeContext) RuleCall(org.eclipse.xtext.RuleCall) EClass(org.eclipse.emf.ecore.EClass) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext)

Aggregations

ISerializationContext (org.eclipse.xtext.serializer.ISerializationContext)41 EClass (org.eclipse.emf.ecore.EClass)13 IConstraint (org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint)9 EObject (org.eclipse.emf.ecore.EObject)7 Parameter (org.eclipse.xtext.Parameter)6 RuleCall (org.eclipse.xtext.RuleCall)6 List (java.util.List)5 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)5 AbstractElement (org.eclipse.xtext.AbstractElement)5 ParserRule (org.eclipse.xtext.ParserRule)5 ISerializationDiagnostic (org.eclipse.xtext.serializer.diagnostic.ISerializationDiagnostic)5 Collection (java.util.Collection)4 SerializationContextMap (org.eclipse.xtext.serializer.analysis.SerializationContextMap)4 Pda (org.eclipse.xtext.util.formallang.Pda)4 Map (java.util.Map)3 Action (org.eclipse.xtext.Action)3 ISynAbsorberState (org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider.ISynAbsorberState)3 LinkedHashMultimap (com.google.common.collect.LinkedHashMultimap)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2