Search in sources :

Example 1 with NfaUtil

use of org.eclipse.xtext.util.formallang.NfaUtil in project xtext-core by eclipse.

the class SemanticSequencerNfaProvider method initContentValidationNeeded.

protected void initContentValidationNeeded(EClass clazz, Nfa<ISemState> nfa) {
    Multimap<EStructuralFeature, AbstractElement> assignments = LinkedHashMultimap.create();
    Set<ISemState> states = new NfaUtil().collect(nfa);
    for (ISemState state : states) if (state.getFeature() != null)
        assignments.put(state.getFeature(), state.getAssignedGrammarElement());
    boolean[] validationNeeded = new boolean[clazz.getFeatureCount()];
    for (EStructuralFeature feature : clazz.getEAllStructuralFeatures()) validationNeeded[clazz.getFeatureID(feature)] = isContentValidationNeeded(assignments.get(feature));
    for (ISemState state : states) if (state.getFeature() != null && validationNeeded[state.getFeatureID()])
        ((SemState) state).contentValidationNeeded = Lists.newArrayList(assignments.get(state.getFeature()));
    else
        ((SemState) state).contentValidationNeeded = Collections.emptyList();
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) NfaUtil(org.eclipse.xtext.util.formallang.NfaUtil)

Example 2 with NfaUtil

use of org.eclipse.xtext.util.formallang.NfaUtil in project xtext-core by eclipse.

the class BacktrackingSemanticSequencer method createSequence.

@Override
public void createSequence(ISerializationContext context, EObject obj) {
    INodesForEObjectProvider nodes = nodeProvider.getNodesForSemanticObject(obj, null);
    SerializationContextMap<IConstraint> constraints = constraintProvider.getConstraints(grammar.getGrammar());
    IConstraint constraint = constraints.get(context);
    if (constraint == null)
        throw new IllegalStateException("Invalid context: " + context);
    Nfa<ISemState> nfa = constraint.getNfa();
    final SerializableObject object = new SerializableObject(context, obj, nodes);
    TraceItem co = new TraceItem(object);
    List<TraceItem> trace = new NfaUtil().backtrack(nfa, co, new NfaUtil.BacktrackHandler<ISemState, TraceItem>() {

        @Override
        public TraceItem handle(ISemState state, TraceItem previous) {
            if (!previous.canEnter(state))
                return null;
            if (state.getFeature() != null) {
                return previous.cloneAndConsume(state);
            } else
                return previous.clone(state);
        }

        @Override
        public boolean isSolution(TraceItem result) {
            return result.isConsumed();
        }

        @Override
        public Iterable<ISemState> sortFollowers(TraceItem result, Iterable<ISemState> followers) {
            AbstractElement next = result.getNextGrammarElement();
            List<ISemState> r = Lists.newArrayList(followers);
            Collections.sort(r, createFollowerSorter(object, next));
            return r;
        }
    });
    SequenceFeeder feeder = feederProvider.create(context, obj, nodes, masterSequencer, sequenceAcceptor, errorAcceptor);
    if (trace != null) {
        for (TraceItem ti : trace) if (ti.getState() != null && ti.getState().getFeature() != null)
            accept(ti, feeder);
    } else if (errorAcceptor != null)
        errorAcceptor.accept(diagnosticProvider.createBacktrackingFailedDiagnostic(object, context, constraint));
    feeder.finish();
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) IConstraint(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint) SequenceFeeder(org.eclipse.xtext.serializer.acceptor.SequenceFeeder) INodesForEObjectProvider(org.eclipse.xtext.serializer.sequencer.ISemanticNodeProvider.INodesForEObjectProvider) NfaUtil(org.eclipse.xtext.util.formallang.NfaUtil) InternalEList(org.eclipse.emf.ecore.util.InternalEList) List(java.util.List) ISemState(org.eclipse.xtext.serializer.analysis.ISemanticSequencerNfaProvider.ISemState)

Example 3 with NfaUtil

use of org.eclipse.xtext.util.formallang.NfaUtil in project xtext-core by eclipse.

the class GrammarPDAProviderTest method assertNoLeakedGrammarElements.

private void assertNoLeakedGrammarElements(final Grammar grammar, final Pda<ISerState, RuleCall> pda) {
    final Function1<ISerState, AbstractElement> _function = (ISerState it) -> {
        return it.getGrammarElement();
    };
    Iterable<AbstractElement> _filterNull = IterableExtensions.<AbstractElement>filterNull(IterableExtensions.<ISerState, AbstractElement>map(new NfaUtil().<ISerState>collect(pda), _function));
    for (final AbstractElement ele : _filterNull) {
        {
            final Grammar actual = GrammarUtil.getGrammar(ele);
            if ((actual != grammar)) {
                String _objPath = EmfFormatter.objPath(ele);
                String _plus = ("Element " + _objPath);
                String _plus_1 = (_plus + " leaked!");
                Assert.fail(_plus_1);
            }
        }
    }
}
Also used : ISerState(org.eclipse.xtext.serializer.analysis.ISerState) AbstractElement(org.eclipse.xtext.AbstractElement) NfaUtil(org.eclipse.xtext.util.formallang.NfaUtil) Grammar(org.eclipse.xtext.Grammar)

Aggregations

AbstractElement (org.eclipse.xtext.AbstractElement)3 NfaUtil (org.eclipse.xtext.util.formallang.NfaUtil)3 List (java.util.List)1 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)1 InternalEList (org.eclipse.emf.ecore.util.InternalEList)1 Grammar (org.eclipse.xtext.Grammar)1 SequenceFeeder (org.eclipse.xtext.serializer.acceptor.SequenceFeeder)1 IConstraint (org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint)1 ISemState (org.eclipse.xtext.serializer.analysis.ISemanticSequencerNfaProvider.ISemState)1 ISerState (org.eclipse.xtext.serializer.analysis.ISerState)1 INodesForEObjectProvider (org.eclipse.xtext.serializer.sequencer.ISemanticNodeProvider.INodesForEObjectProvider)1