use of org.eclipse.xtext.serializer.ISerializationContext in project xtext-core by eclipse.
the class PartialSerializer method updateSingleValue.
protected SerializationStrategy updateSingleValue(EObject object, EStructuralFeature feature, IAstRegion region) {
Preconditions.checkArgument(!feature.isMany());
Object value = object.eGet(feature);
EObject grammarElement = region.getGrammarElement();
if (feature instanceof EAttribute) {
if (grammarElement instanceof RuleCall) {
RuleCall rc = (RuleCall) grammarElement;
String newValue = valueSerializer.serializeAssignedValue(object, rc, value, null, errorAcceptor);
if (newValue != null) {
return new ReplaceRegionStrategy((ISemanticRegion) region, newValue);
}
}
return null;
} else if (feature instanceof EReference) {
if (((EReference) feature).isContainment()) {
IEObjectRegion reg = (IEObjectRegion) region;
EObject newEObject = (EObject) object.eGet(feature);
ISerializationContext newContext = getSerializationContext(newEObject);
ISerializationContext oldContext = getSerializationContext(reg);
if (!oldContext.equals(newContext)) {
return null;
}
return new SerializeRecursiveStrategy(reg, newEObject, newContext);
} else {
CrossReference cr = GrammarUtil.containingCrossReference(grammarElement);
if (cr != null) {
EObject target = (EObject) value;
String newValue = crossRefSerializer.serializeCrossRef(object, cr, target, null, errorAcceptor);
if (newValue != null) {
return new ReplaceRegionStrategy((ISemanticRegion) region, newValue);
}
}
}
return null;
}
return null;
}
use of org.eclipse.xtext.serializer.ISerializationContext in project xtext-core by eclipse.
the class PartialSerializer method trySerializeMultiValue.
protected List<SerializationStrategy> trySerializeMultiValue(EObject owner, FeatureChange change, IEObjectRegion ownerRegion, IConstraint constraint) {
EStructuralFeature feature = change.getFeature();
List<SerializationStrategy> result = Lists.newArrayList();
IFeatureInfo featureInfo = constraint.getFeatures()[owner.eClass().getFeatureID(feature)];
List<IConstraintElement> assignments = featureInfo.getAssignments();
if (assignments.size() != 1) {
return null;
}
IConstraintElement assignment = assignments.get(0);
if (!assignment.isMany()) {
return null;
}
List<IAstRegion> originals = findRegions(ownerRegion, change);
EList<ListChange> listChanges = change.getListChanges();
if (listChanges.isEmpty() && originals.isEmpty()) {
ISerializationContext ctx = getSerializationContext(owner);
AbstractElement ins = assignment.getGrammarElement();
IHiddenRegion insertAt = insertionPointFinder.findInsertionPoint(ctx, ownerRegion, ins);
if (insertAt == null) {
return null;
}
for (Object value : (List<?>) owner.eGet(feature)) {
EObject obj = (EObject) value;
ISerializationContext context = getSerializationContext(obj);
result.add(new SerializeRecursiveStrategy(insertAt, obj, context));
}
return result;
}
// ListTransient listTransient = transientValues.isListTransient(owner, feature);
List<Object> modifying = Lists.newArrayList(((List<?>) owner.eGet(feature)));
for (ListChange lc : listChanges) {
ChangeKind kind = lc.getKind();
if (kind == ADD_LITERAL) {
IAstRegion region = originals.get(lc.getIndex());
result.add(new DeleteRegionStrategy(region));
} else if (kind == MOVE_LITERAL || kind == REMOVE_LITERAL) {
if (originals.isEmpty()) {
return null;
}
int index = lc.getIndex();
IHiddenRegion insertAt;
if (index >= originals.size()) {
insertAt = ((ISequentialRegion) originals.get(originals.size() - 1)).getNextHiddenRegion();
} else {
insertAt = ((ISequentialRegion) originals.get(index)).getPreviousHiddenRegion();
}
EObject value = (EObject) modifying.get(index);
modifying.remove(index);
if (kind == REMOVE_LITERAL) {
ISerializationContext context = getSerializationContext(value);
result.add(new SerializeRecursiveStrategy(insertAt, value, context));
} else if (kind == ChangeKind.MOVE_LITERAL) {
int moveToIndex = lc.getMoveToIndex();
IAstRegion source = originals.get(moveToIndex);
result.add(new DeleteRegionStrategy(source));
result.add(new InsertRegionStrategy(insertAt, source));
}
}
}
return result;
}
use of org.eclipse.xtext.serializer.ISerializationContext in project xtext-core by eclipse.
the class GrammarConstraintProvider method getConstraints.
@Override
public SerializationContextMap<IConstraint> getConstraints(Grammar grammar) {
SerializationContextMap<IConstraint> cached = cache.get(grammar);
if (cached != null)
return cached;
SerializationContextMap.Builder<IConstraint> builder = SerializationContextMap.builder();
GrammarElementDeclarationOrder.get(grammar);
SerializationContextMap<Nfa<ISemState>> nfas = nfaProvider.getSemanticSequencerNFAs(grammar);
for (Entry<Nfa<ISemState>> e : nfas.values()) {
Nfa<ISemState> nfa = e.getValue();
for (EClass type : e.getTypes()) {
Constraint constraint = new Constraint(grammar, type, nfa);
List<ISerializationContext> contexts = e.getContexts(type);
constraint.contexts.addAll(contexts);
builder.put(contexts, constraint);
}
}
SerializationContextMap<IConstraint> result = builder.create();
SerializationContextMap<Pda<ISerState, RuleCall>> typePDAs = typeProvider.getContextTypePDAs(grammar);
for (Entry<IConstraint> e : result.values()) {
Constraint constraint = (Constraint) e.getValue();
constraint.setName(findBestConstraintName(grammar, typePDAs, constraint));
}
cache.put(grammar, result);
return result;
}
use of org.eclipse.xtext.serializer.ISerializationContext in project xtext-core by eclipse.
the class GrammarConstraintProvider method findBestConstraintName.
protected String findBestConstraintName(Grammar grammar, SerializationContextMap<Pda<ISerState, RuleCall>> typePDAs, IConstraint constraint) {
Set<ParserRule> relevantRules = Sets.newLinkedHashSet();
Set<Action> relevantActions = Sets.newLinkedHashSet();
Set<ParserRule> contextRules = Sets.newLinkedHashSet();
for (ISerializationContext ctx : constraint.getContexts()) {
ParserRule rule = ctx.getParserRule();
if (rule != null) {
contextRules.add(rule);
}
}
// 1. find relevant rules based on assignments
for (ISemState s : nfaUtil.collect(constraint.getNfa())) {
AbstractElement element = s.getAssignedGrammarElement();
if (element != null)
relevantRules.add(GrammarUtil.containingParserRule(element));
}
// 2. find relevant rules based on unassigned actions
for (ISerializationContext ctx : constraint.getContexts()) {
for (ISerState s : nfaUtil.collect(typePDAs.get(ctx))) {
AbstractElement element = s.getGrammarElement();
if (element instanceof Action && ((Action) element).getFeature() == null)
relevantRules.add(GrammarUtil.containingParserRule(element));
}
}
if (relevantRules.isEmpty()) {
Set<ParserRule> allRules = Sets.newLinkedHashSet(contextRules);
for (ISerializationContext ctx : constraint.getContexts()) {
Action action = ctx.getAssignedAction();
if (action != null)
allRules.add(GrammarUtil.containingParserRule(action));
}
// 3. use all rules, because the constraint returns null.
relevantRules.addAll(allRules);
}
for (ISerializationContext ctx : constraint.getContexts()) {
Action action = ctx.getAssignedAction();
if (action != null) {
ParserRule rule = GrammarUtil.containingParserRule(action);
if (!contextRules.contains(rule) && relevantRules.contains(rule)) {
relevantActions.add(action);
}
}
}
List<String> actions = Lists.newArrayList();
List<String> rules = Lists.newArrayList();
Multimap<Parameter, Boolean> parameterValues = collectAllParameterValues(constraint);
for (Action a : relevantActions) actions.add(context2Name.getUniqueActionName(a));
for (ParserRule rule : relevantRules) {
StringBuilder segments = new StringBuilder();
for (Parameter param : rule.getParameters()) {
Collection<Boolean> values = parameterValues.get(param);
if (values.size() == 1) {
segments.append(param.getName() + "$" + values.iterator().next() + "$");
}
}
if (segments.length() == 0) {
rules.add(rule.getName());
} else {
rules.add(rule.getName() + "$" + segments);
}
}
Collections.sort(rules);
String result = Joiner.on("_").join(rules);
if (!actions.isEmpty()) {
Collections.sort(actions);
result += "_" + Joiner.on('_').join(actions);
}
return result;
}
use of org.eclipse.xtext.serializer.ISerializationContext in project xtext-core by eclipse.
the class SerializationContext method compareTo.
@Override
public int compareTo(ISerializationContext o) {
EObject o1 = getActionOrRule();
EObject o2 = ((SerializationContext) o).getActionOrRule();
if (o1 != o2) {
GrammarElementDeclarationOrder order = GrammarElementDeclarationOrder.get(GrammarUtil.getGrammar(o1));
int compare = order.compare(o1, o2);
if (compare != 0)
return compare;
}
EClass t1 = getType();
EClass t2 = o.getType();
if (t1 != t2) {
if (t1 != null && t2 != null) {
int compare = t1.getName().compareTo(t2.getName());
if (compare != 0)
return compare;
}
if (t1 != null)
return -1;
if (t2 != null)
return 1;
}
List<Parameter> params = getDeclaredParameters();
if (params.equals(((SerializationContext) o).getDeclaredParameters())) {
Set<Parameter> v1 = getEnabledBooleanParameters();
Set<Parameter> v2 = o.getEnabledBooleanParameters();
if (v1 == null || v2 == null) {
return v1 != null ? -1 : 1;
} else {
for (Parameter param : params) {
boolean b1 = v1.contains(param);
boolean b2 = v2.contains(param);
if (b1 != b2) {
return b1 ? -1 : 1;
}
}
}
}
ISerializationContext p1 = getParent();
ISerializationContext p2 = ((SerializationContext) o).getParent();
if (p1 != p2) {
if (p1 != null && p2 != null)
return p1.compareTo(p2);
if (p1 != null)
return -1;
if (p2 != null)
return 1;
}
return 0;
}
Aggregations