use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.
the class SyntacticSequencerPDAProvider method getType.
protected SynStateType getType(ISerState state) {
switch(state.getType()) {
case ELEMENT:
AbstractElement ele = state.getGrammarElement();
Assignment ass;
if (ele instanceof Action) {
if (((Action) ele).getFeature() == null)
return SynStateType.UNASSIGEND_ACTION_CALL;
else
return SynStateType.ASSIGNED_ACTION_CALL;
} else if (GrammarUtil.containingCrossReference(ele) != null) {
if (ele instanceof RuleCall) {
RuleCall rc = (RuleCall) ele;
if (rc.getRule() instanceof ParserRule)
return SynStateType.ASSIGNED_CROSSREF_DATATYPE_RULE_CALL;
if (rc.getRule() instanceof TerminalRule)
return SynStateType.ASSIGNED_CROSSREF_TERMINAL_RULE_CALL;
if (rc.getRule() instanceof EnumRule)
return SynStateType.ASSIGNED_CROSSREF_ENUM_RULE_CALL;
} else if (ele instanceof Keyword)
return SynStateType.ASSIGNED_CROSSREF_KEYWORD;
} else if ((ass = GrammarUtil.containingAssignment(ele)) != null) {
if (ele instanceof RuleCall) {
RuleCall rc = (RuleCall) ele;
if (rc.getRule() instanceof ParserRule) {
if (rc.getRule().getType().getClassifier() instanceof EClass)
return SynStateType.ASSIGNED_PARSER_RULE_CALL;
return SynStateType.ASSIGNED_DATATYPE_RULE_CALL;
}
if (rc.getRule() instanceof TerminalRule)
return SynStateType.ASSIGNED_TERMINAL_RULE_CALL;
if (rc.getRule() instanceof EnumRule)
return SynStateType.ASSIGNED_ENUM_RULE_CALL;
} else if (ele instanceof Keyword) {
if (GrammarUtil.isBooleanAssignment(ass))
return SynStateType.ASSIGNED_BOOLEAN_KEYWORD;
else
return SynStateType.ASSIGNED_KEYWORD;
}
} else {
if (ele instanceof RuleCall) {
RuleCall rc = (RuleCall) ele;
if (rc.getRule() instanceof ParserRule)
return SynStateType.UNASSIGNED_DATATYPE_RULE_CALL;
if (rc.getRule() instanceof TerminalRule)
return SynStateType.UNASSIGNED_TERMINAL_RULE_CALL;
} else if (ele instanceof Keyword)
return SynStateType.UNASSIGEND_KEYWORD;
}
break;
case PUSH:
return SynStateType.UNASSIGNED_PARSER_RULE_ENTER;
case POP:
return SynStateType.UNASSIGNED_PARSER_RULE_EXIT;
case START:
return SynStateType.START;
case STOP:
return SynStateType.STOP;
}
throw new RuntimeException("no type found for " + state);
}
use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.
the class AbstractSyntacticSequencer method getNodesFor.
protected List<INode> getNodesFor(List<INode> nodes, AbstractElementAlias ele) {
if (nodes == null)
return null;
Set<AbstractElement> elments = Sets.newHashSet();
collectAbstractElements(ele, elments);
List<INode> result = Lists.newArrayList();
for (INode n : nodes) if (elments.contains(n.getGrammarElement()))
result.add(n);
return result;
}
use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.
the class AssignmentFinder method findValidAssignmentsForContainmentRef.
protected Set<AbstractElement> findValidAssignmentsForContainmentRef(EObject semanticObj, Multimap<AbstractElement, ISerializationContext> assignments, EObject value) {
Multimap<ISerializationContext, AbstractElement> children = ArrayListMultimap.create();
for (Entry<AbstractElement, Collection<ISerializationContext>> e : assignments.asMap().entrySet()) {
AbstractElement ele = e.getKey();
if (ele instanceof RuleCall) {
EClassifier classifier = ((RuleCall) ele).getRule().getType().getClassifier();
if (!classifier.isInstance(value))
continue;
}
for (ISerializationContext container : e.getValue()) {
ISerializationContext child = SerializationContext.forChild(container, ele, value);
children.put(child, ele);
}
}
if (children.size() < 2)
return Sets.newHashSet(children.values());
Set<ISerializationContext> found = contextFinder.findByContents(value, children.keySet());
Set<AbstractElement> result = Sets.newLinkedHashSet();
for (ISerializationContext ctx : children.keySet()) if (found.contains(ctx))
result.addAll(children.get(ctx));
return result;
}
use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.
the class AssignmentFinder method findValidAssignmentsForCrossRef.
protected Set<AbstractElement> findValidAssignmentsForCrossRef(EObject semanticObj, Iterable<AbstractElement> assignedElements, EObject value, INode node) {
Set<AbstractElement> result = Sets.newLinkedHashSet();
for (AbstractElement ass : assignedElements) {
CrossReference crossref = GrammarUtil.containingCrossReference(ass);
EReference eref = GrammarUtil.getReference(crossref, semanticObj.eClass());
if (EcoreUtil2.isAssignableFrom(eref.getEReferenceType(), value.eClass()) && crossRefSerializer.isValid(semanticObj, crossref, value, node, null))
result.add(ass);
}
return result;
}
use of org.eclipse.xtext.AbstractElement 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();
}
Aggregations