use of org.eclipse.xtext.util.formallang.Pda 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.util.formallang.Pda 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.util.formallang.Pda 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);
}
}
use of org.eclipse.xtext.util.formallang.Pda in project xtext-core by eclipse.
the class ContextPDAProvider method getContextPDAs.
@Override
public SerializationContextMap<Pda<ISerState, RuleCall>> getContextPDAs(Grammar grammar) {
Builder<Pda<ISerState, RuleCall>> result = SerializationContextMap.<Pda<ISerState, RuleCall>>builder();
SerializationContextMap<Pda<ISerState, RuleCall>> grammarPDAs = grammarPdaProvider.getGrammarPDAs(grammar);
Multimap<Action, SerializerPDA> actionPdas = ArrayListMultimap.create();
Multimap<Action, ISerializationContext> actionContexts = LinkedHashMultimap.create();
Map<ParserRule, Integer> indexedRules = indexRules(grammar);
for (SerializationContextMap.Entry<Pda<ISerState, RuleCall>> e : grammarPDAs.values()) {
List<ISerializationContext> contexts = e.getContexts();
Pda<ISerState, RuleCall> pda = e.getValue();
List<ISerState> actions = Lists.newArrayList();
for (ISerState state : nfaUtil.collect(pda)) {
if (GrammarUtil.isAssignedAction(state.getGrammarElement())) {
actions.add(state);
}
}
if (actions.isEmpty()) {
Pda<ISerState, RuleCall> filtered = filterUnneededUnassignedRuleCalls(pda, indexedRules);
result.put(contexts, filtered);
} else {
try {
SerializerPDA rulePda = extract(pda.getStop());
Pda<ISerState, RuleCall> filtered = filterUnneededUnassignedRuleCalls(rulePda, indexedRules);
result.put(contexts, filtered);
for (ISerState state : actions) {
Action action = (Action) state.getGrammarElement();
SerializerPDA actionPda = extract(state);
actionPdas.put(action, actionPda);
actionContexts.putAll(action, contexts);
}
} catch (Exception x) {
LOG.error("Error extracting PDA for action in context '" + contexts + "': " + x.getMessage(), x);
}
}
}
for (Map.Entry<Action, Collection<SerializerPDA>> action : actionPdas.asMap().entrySet()) {
SerializerPDA merged = merge(new ActionContext(null, action.getKey()), action.getValue());
Set<Set<Parameter>> parameterPermutations = Sets.newLinkedHashSet();
for (ISerializationContext container : actionContexts.get(action.getKey())) {
parameterPermutations.add(container.getEnabledBooleanParameters());
}
// for (IContext container : actionContexts.get(action.getKey())) {
for (Set<Parameter> parameters : parameterPermutations) {
ISerializationContext context = new ActionContext(/* container */
null, action.getKey());
if (!parameters.isEmpty())
context = new SerializationContext.ParameterValueContext(context, parameters);
Pda<ISerState, RuleCall> filtered = filterUnneededUnassignedRuleCalls(merged, indexedRules);
result.put(context, filtered);
}
// }
}
return result.create();
}
use of org.eclipse.xtext.util.formallang.Pda in project xtext-core by eclipse.
the class ContextTypePDAProvider method getContextTypePDAs.
@Override
public SerializationContextMap<Pda<ISerState, RuleCall>> getContextTypePDAs(Grammar grammar) {
SerializationContextMap<Pda<ISerState, RuleCall>> cached = cache.get(grammar);
if (cached != null)
return cached;
SerializationContextMap.Builder<Pda<ISerState, RuleCall>> builder = SerializationContextMap.builder();
SerializationContextMap<Pda<ISerState, RuleCall>> contextPDAs = pdaProvider.getContextPDAs(grammar);
for (SerializationContextMap.Entry<Pda<ISerState, RuleCall>> e : contextPDAs.values()) {
List<ISerializationContext> parents = e.getContexts();
Pda<ISerState, RuleCall> contextPDA = e.getValue();
try {
Map<ISerState, Integer> distances = nfaUtil.distanceToFinalStateMap(contextPDA);
Set<EClass> types = collectTypes(contextPDA, distances);
if (types.size() == 1) {
for (ISerializationContext parent : parents) {
TypeContext ctx = new TypeContext(parent, types.iterator().next());
builder.put(ctx, contextPDA);
}
} else {
for (EClass type : types) {
Pda<ISerState, RuleCall> filtered = filterByType(contextPDA, type, distances);
for (ISerializationContext parent : parents) {
TypeContext typeContext = new TypeContext(parent, type);
builder.put(typeContext, filtered);
}
}
}
} catch (Exception x) {
LOG.error("Error extracting PDAs for types for context '" + parents + "': " + x.getMessage(), x);
}
}
SerializationContextMap<Pda<ISerState, RuleCall>> result = builder.create();
cache.put(grammar, result);
return result;
}
Aggregations