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();
}
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();
}
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);
}
}
}
}
Aggregations