Search in sources :

Example 11 with ISerializationContext

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

the class SerializationContext method forChild.

public static ISerializationContext forChild(ISerializationContext container, RuleCall ruleCall, EObject sem) {
    EClass type = sem == null ? null : sem.eClass();
    ISerializationContext result = new TypeContext(new RuleContext(null, (ParserRule) ruleCall.getRule()), type);
    EList<NamedArgument> arguments = ruleCall.getArguments();
    if (!arguments.isEmpty() && container != null) {
        Set<Parameter> params = Sets.newLinkedHashSet();
        ConditionEvaluator evaluator = new ConditionEvaluator(container.getEnabledBooleanParameters());
        for (NamedArgument argument : arguments) {
            if (evaluator.evaluate(argument.getValue())) {
                params.add(argument.getParameter());
            }
        }
        result = new SerializationContext.ParameterValueContext(result, params);
    }
    return result;
}
Also used : ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) ParserRule(org.eclipse.xtext.ParserRule) EClass(org.eclipse.emf.ecore.EClass) ConditionEvaluator(org.eclipse.xtext.xtext.ConditionEvaluator) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) Parameter(org.eclipse.xtext.Parameter) NamedArgument(org.eclipse.xtext.NamedArgument)

Example 12 with ISerializationContext

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

the class SyntacticSequencerPDAProvider method getSyntacticSequencerPDAs.

@Override
public SerializationContextMap<ISynAbsorberState> getSyntacticSequencerPDAs(Grammar grammar) {
    SerializationContextMap<ISynAbsorberState> cached = cache.get(grammar);
    if (cached != null)
        return cached;
    SerializationContextMap.Builder<ISynAbsorberState> builder = SerializationContextMap.builder();
    SerializationContextMap<Pda<ISerState, RuleCall>> typePDAs = pdaProvider.getContextTypePDAs(grammar);
    for (Entry<Pda<ISerState, RuleCall>> e : typePDAs.values()) {
        Pda<ISerState, RuleCall> pda = e.getValue();
        List<ISerializationContext> contexts = e.getContexts();
        try {
            EClass type = contexts.get(0).getType();
            Map<ISerState, SynAbsorberState> absorbers = Maps.newLinkedHashMap();
            Map<SynAbsorberState, Map<ISerState, SynState>> emitters = Maps.newLinkedHashMap();
            SynAbsorberState state = createAbsorberState(pda.getStart(), absorbers, emitters, type);
            builder.put(contexts, state);
        } catch (Exception x) {
            String ctxs = Joiner.on(", ").join(contexts);
            LOG.error("Error creating PDA for syntactic sequencer for contexts: " + ctxs + ": " + x.getMessage(), x);
        }
    }
    SerializationContextMap<ISynAbsorberState> result = builder.create();
    cache.put(grammar, result);
    return result;
}
Also used : Pda(org.eclipse.xtext.util.formallang.Pda) RuleCall(org.eclipse.xtext.RuleCall) EClass(org.eclipse.emf.ecore.EClass) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) Map(java.util.Map)

Example 13 with ISerializationContext

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

the class SequencerDiagnosticProvider method createInvalidContextOrTypeDiagnostic.

@Override
public ISerializationDiagnostic createInvalidContextOrTypeDiagnostic(EObject sem, ISerializationContext context) {
    Set<ISerializationContext> contexts = Sets.newHashSet(contextFinder.findByContentsAndContainer(sem, null));
    Set<EClass> validTypes = getValidTypes(context);
    List<ISerializationContext> recommendedCtxs = Lists.newArrayList();
    List<ISerializationContext> otherValidCtxs = Lists.newArrayList();
    for (ISerializationContext ctx : getValidContexts(sem.eClass())) {
        if (contexts.contains(ctx))
            recommendedCtxs.add(ctx);
        else
            otherValidCtxs.add(ctx);
    }
    String semanticType = sem.eClass().getName();
    String validTypeNames = Joiner.on(", ").join(Iterables.transform(validTypes, new NamedElement2Name()));
    StringBuilder msg = new StringBuilder();
    msg.append("The context '" + context + "' is not valid for type '" + semanticType + "'\n");
    msg.append("Recommended contexts for type '" + semanticType + "': " + recommendedCtxs + "\n");
    if (!otherValidCtxs.isEmpty())
        msg.append("Other valid contexts for type '" + semanticType + "': " + otherValidCtxs);
    msg.append("The context '" + context + "' is valid for types: " + validTypeNames + "\n");
    return new SerializationDiagnostic(INVALID_CONTEXT_OR_TYPE, sem, grammarAccess.getGrammar(), msg.toString());
}
Also used : EClass(org.eclipse.emf.ecore.EClass) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext)

Example 14 with ISerializationContext

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

the class Serializer method serializeToRegions.

public ITextRegionAccess serializeToRegions(EObject obj) {
    checkNotNull(obj, "obj must not be null.");
    ISerializationContext context = getIContext(obj);
    TextRegionAccessBuilder builder = textRegionBuilderProvider.get();
    ISerializationDiagnostic.Acceptor errors = ISerializationDiagnostic.EXCEPTION_THROWING_ACCEPTOR;
    serialize(context, obj, builder.forSequence(context, obj), errors);
    ITextRegionAccess regionAccess = builder.create();
    return regionAccess;
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ISerializationDiagnostic(org.eclipse.xtext.serializer.diagnostic.ISerializationDiagnostic) TextRegionAccessBuilder(org.eclipse.xtext.formatting2.regionaccess.TextRegionAccessBuilder) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext)

Example 15 with ISerializationContext

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

the class AbstractSyntacticSequencer method init.

@Override
@Deprecated
public void init(EObject context, EObject semanticObject, ISyntacticSequenceAcceptor sequenceAcceptor, Acceptor errorAcceptor) {
    ISerializationContext ctx = SerializationContext.fromEObject(context, semanticObject);
    init(ctx, semanticObject, sequenceAcceptor, errorAcceptor);
}
Also used : ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext)

Aggregations

ISerializationContext (org.eclipse.xtext.serializer.ISerializationContext)39 EClass (org.eclipse.emf.ecore.EClass)12 IConstraint (org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint)8 EObject (org.eclipse.emf.ecore.EObject)7 RuleCall (org.eclipse.xtext.RuleCall)7 Parameter (org.eclipse.xtext.Parameter)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 Pda (org.eclipse.xtext.util.formallang.Pda)5 Collection (java.util.Collection)4 ISerializationDiagnostic (org.eclipse.xtext.serializer.diagnostic.ISerializationDiagnostic)4 Map (java.util.Map)3 Action (org.eclipse.xtext.Action)3 ISynAbsorberState (org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider.ISynAbsorberState)3 SerializationContextMap (org.eclipse.xtext.serializer.analysis.SerializationContextMap)3 LinkedHashMultimap (com.google.common.collect.LinkedHashMultimap)2 EReference (org.eclipse.emf.ecore.EReference)2 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)2