use of org.eclipse.xtext.serializer.analysis.ISerState in project xtext-core by eclipse.
the class InsertionPointFinder method findInsertionPoint.
public IHiddenRegion findInsertionPoint(ISerializationContext ctx, IEObjectRegion obj, AbstractElement ins) {
ISerState insertionState = findState(ctx, ins);
Set<AbstractElement> followers = collectAdjacent(insertionState, s -> s.getFollowers());
Set<AbstractElement> precendents = collectAdjacent(insertionState, s -> s.getPrecedents());
List<IAstRegion> regions = Lists.newArrayList(obj.getAstRegions());
if (regions.isEmpty()) {
return obj.getPreviousHiddenRegion();
}
if (followers.contains(regions.get(0).getGrammarElement())) {
return obj.getPreviousHiddenRegion();
}
if (precendents.contains(regions.get(regions.size() - 1).getGrammarElement())) {
return obj.getNextHiddenRegion();
}
for (int i = 0; i < regions.size() - 1; i++) {
IAstRegion leading = regions.get(i);
IAstRegion trailing = regions.get(i + 1);
if (precendents.contains(leading.getGrammarElement()) && followers.contains(trailing.getGrammarElement())) {
return leading.getNextHiddenRegion();
}
}
return null;
}
use of org.eclipse.xtext.serializer.analysis.ISerState in project xtext-core by eclipse.
the class InsertionPointFinder method collectAdjacent.
protected Set<AbstractElement> collectAdjacent(ISerState start, Function<ISerState, List<? extends ISerState>> followers) {
LinkedList<ISerState> todo = new LinkedList<>();
Set<ISerState> seen = Sets.newHashSet();
todo.addAll(followers.apply(start));
Set<AbstractElement> result = Sets.newLinkedHashSet();
while (!todo.isEmpty()) {
ISerState next = todo.pop();
if (!seen.add(next)) {
continue;
}
if (next.getType() == SerStateType.ELEMENT && !GrammarUtil.isUnassignedAction(next.getGrammarElement())) {
result.add(next.getGrammarElement());
} else {
todo.addAll(followers.apply(next));
}
}
return result;
}
use of org.eclipse.xtext.serializer.analysis.ISerState in project xtext-core by eclipse.
the class ContextPDAProviderTest method getParserRule.
protected String getParserRule(String body) throws Exception {
Grammar grammar = (Grammar) getModel(HEADER + body);
List<String> result = Lists.newArrayList();
PdaListFormatter<ISerState, RuleCall> formatter = new PdaListFormatter<ISerState, RuleCall>();
formatter.setStateFormatter(new ToStr());
formatter.setStackitemFormatter(new GrammarElementTitleSwitch().showAssignments().hideCardinality());
formatter.sortFollowers();
IContextPDAProvider pdaProvider = get(IContextPDAProvider.class);
SerializationContextMap<Pda<ISerState, RuleCall>> pdas = pdaProvider.getContextPDAs(grammar);
for (Entry<Pda<ISerState, RuleCall>> ctx : pdas.sortedCopy().values()) {
result.add(Joiner.on(", ").join(ctx.getContexts()) + ":");
Pda<ISerState, RuleCall> pda = ctx.getValue();
result.add(" " + formatter.format(pda).replace("\n", "\n "));
// StackTraceElement ele = Thread.currentThread().getStackTrace()[2];
// String name = getClass().getSimpleName() + "_" + ele.getMethodName() + "_" + ctx.getSecond() + ".pdf";
// new PdaToDot<ISerState, RuleCall>().draw(pda, "dot/" + name, "-T pdf");
}
return Joiner.on("\n").join(result);
}
use of org.eclipse.xtext.serializer.analysis.ISerState in project xtext-core by eclipse.
the class ContextTypePDAProviderTest method getParserRule.
protected String getParserRule(String body) throws Exception {
Grammar grammar = (Grammar) getModel(HEADER + body);
// drawGrammar("pdf/" + getName(), grammar);
List<String> result = Lists.newArrayList();
PdaListFormatter<ISerState, RuleCall> formatter = new PdaListFormatter<ISerState, RuleCall>();
formatter.setStateFormatter(new ToStr());
formatter.setStackitemFormatter(new GrammarElementTitleSwitch().showAssignments().hideCardinality());
formatter.sortFollowers();
IContextTypePDAProvider typePDAProvider = get(IContextTypePDAProvider.class);
SerializationContextMap<Pda<ISerState, RuleCall>> pdas = typePDAProvider.getContextTypePDAs(grammar);
for (Entry<Pda<ISerState, RuleCall>> ctx : pdas.sortedCopy().values()) {
result.add(Joiner.on(", ").join(ctx.getContexts()) + ":");
result.add(" " + formatter.format(ctx.getValue()).replace("\n", "\n "));
}
return Joiner.on("\n").join(result);
}
use of org.eclipse.xtext.serializer.analysis.ISerState in project xtext-core by eclipse.
the class GrammarPDAProviderTest method toPda.
private String toPda(final CharSequence rulesText) {
try {
StringConcatenation _builder = new StringConcatenation();
_builder.append("grammar org.eclipse.xtext.serializer.GrammarPDAProviderTestLanguage with org.eclipse.xtext.common.Terminals");
_builder.newLine();
_builder.newLine();
_builder.append("generate GrammarPDAProviderTest \"http://www.eclipse.org/2010/tmf/xtext/GrammarPDAProviderTestLanguage\"");
_builder.newLine();
_builder.newLine();
_builder.append(rulesText);
_builder.newLineIfNotEmpty();
final Grammar grammar = this.parser.parse(_builder);
this.validator.assertNoErrors(grammar);
final SerializationContextMap<Pda<ISerState, RuleCall>> pdas = this.pdaProvider.getGrammarPDAs(grammar);
final Consumer<SerializationContextMap.Entry<Pda<ISerState, RuleCall>>> _function = (SerializationContextMap.Entry<Pda<ISerState, RuleCall>> it) -> {
this.assertNoLeakedGrammarElements(grammar, it.getValue());
};
pdas.values().forEach(_function);
final Function1<SerializationContextMap.Entry<Pda<ISerState, RuleCall>>, List<ISerializationContext>> _function_1 = (SerializationContextMap.Entry<Pda<ISerState, RuleCall>> it) -> {
return it.getContexts();
};
final Function1<ISerializationContext, String> _function_2 = (ISerializationContext it) -> {
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append(it);
_builder_1.append(":");
_builder_1.newLineIfNotEmpty();
_builder_1.append("\t");
String _listString = this.toListString(pdas.get(it));
_builder_1.append(_listString, "\t");
_builder_1.newLineIfNotEmpty();
return _builder_1.toString();
};
return IterableExtensions.join(ListExtensions.<ISerializationContext, String>map(IterableExtensions.<ISerializationContext>sort(Iterables.<ISerializationContext>concat(ListExtensions.<SerializationContextMap.Entry<Pda<ISerState, RuleCall>>, List<ISerializationContext>>map(pdas.values(), _function_1))), _function_2));
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
Aggregations