use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.
the class ContentAssistContextFactory method doCreateContexts.
protected void doCreateContexts(INode lastCompleteNode, INode currentNode, String prefix, EObject previousModel, Collection<FollowElement> followElements) {
Set<AbstractElement> followElementAsAbstractElements = Sets.newLinkedHashSet();
followElementComputer.computeFollowElements(followElements, followElementAsAbstractElements);
Multimap<EObject, AbstractElement> contextMap = computeCurrentModel(previousModel, lastCompleteNode, followElementAsAbstractElements);
currentNode = getContainingDatatypeRuleNode(currentNode);
for (Entry<EObject, Collection<AbstractElement>> entry : contextMap.asMap().entrySet()) {
ContentAssistContext.Builder contextBuilder = doCreateContext(lastCompleteNode, entry.getKey(), previousModel, currentNode, prefix);
for (AbstractElement element : entry.getValue()) {
contextBuilder.accept(element);
}
contextBuilders.add(contextBuilder);
}
}
use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.
the class ContentAssistContextTestHelper method firstSetGrammarElementsToString.
public String firstSetGrammarElementsToString(final ContentAssistContextFactory factory) {
final int offset = this.document.indexOf(this.cursor);
Preconditions.checkArgument((offset >= 0), "you forgot to provide a cursor");
final String doc = this.document.replace(this.cursor, "");
final XtextResource res = this.parse(doc);
factory.setPool(Executors.newCachedThreadPool());
TextRegion _textRegion = new TextRegion(0, 0);
final ContentAssistContext[] ctxs = factory.create(doc, _textRegion, offset, res);
final GrammarElementTitleSwitch f = new GrammarElementTitleSwitch().showAssignments().showQualified().showRule();
StringConcatenation _builder = new StringConcatenation();
{
Iterable<Pair<Integer, ContentAssistContext>> _indexed = IterableExtensions.<ContentAssistContext>indexed(((Iterable<? extends ContentAssistContext>) Conversions.doWrapArray(ctxs)));
for (final Pair<Integer, ContentAssistContext> ctx : _indexed) {
_builder.append("context");
Integer _key = ctx.getKey();
_builder.append(_key);
_builder.append(" {");
_builder.newLineIfNotEmpty();
{
ImmutableList<AbstractElement> _firstSetGrammarElements = ctx.getValue().getFirstSetGrammarElements();
for (final AbstractElement ele : _firstSetGrammarElements) {
_builder.append("\t");
String _name = ele.eClass().getName();
_builder.append(_name, "\t");
_builder.append(": ");
String _apply = f.apply(ele);
_builder.append(_apply, "\t");
_builder.newLineIfNotEmpty();
}
}
_builder.append("}");
_builder.newLine();
}
}
return _builder.toString();
}
use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.
the class FirstSetComputationTest method assertFirstSet.
protected void assertFirstSet(String expectation, AbstractRule rule) {
RuleCall ruleCall = XtextFactory.eINSTANCE.createRuleCall();
ruleCall.setRule(rule);
List<AbstractElement> firstSet = AntlrGrammarGenUtil.getFirstSet(ruleCall);
StringBuilder actual = new StringBuilder();
GrammarElementTitleSwitch stringifier = new GrammarElementTitleSwitch();
for (int i = 0; i < firstSet.size(); i++) {
if (i != 0)
actual.append(", ");
actual.append(stringifier.apply(firstSet.get(i)));
}
assertEquals(expectation, actual.toString());
}
use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.
the class ContextPDAProvider method collectExtracted.
protected void collectExtracted(ISerState orig, Collection<? extends ISerState> precedents, SerializerPDAState copy, Map<Pair<AbstractElement, SerStateType>, SerializerPDAState> oldToNew, final CallStack inTop, SerializerPDAState start) {
for (ISerState pre : precedents) {
CallStack top = inTop;
AbstractElement element = pre.getGrammarElement();
switch(pre.getType()) {
case START:
if (top.call == null)
connect(start, copy);
continue;
case POP:
top = new CallStack(top, (RuleCall) element);
if (top.isLoop())
continue;
break;
case PUSH:
if (top.call == null) {
connect(start, copy);
continue;
} else if (top.call == element) {
top = top.parent;
} else {
continue;
}
default:
break;
}
Pair<AbstractElement, SerStateType> key = Tuples.create(element, pre.getType());
SerializerPDAState pre2 = oldToNew.get(key);
if (pre2 == null) {
pre2 = new SerializerPDAState(element, pre.getType());
oldToNew.put(key, pre2);
}
if (GrammarUtil.isAssignedAction(pre.getGrammarElement())) /* && pre.getType() != STOP */
{
Set<ISerState> entries = collectPushForAction(pre);
collectExtracted(pre, entries, pre2, oldToNew, top, start);
} else {
if (top.visited.add(pre))
collectExtracted(pre, pre.getPrecedents(), pre2, oldToNew, top, start);
}
connect(pre2, copy);
}
}
use of org.eclipse.xtext.AbstractElement 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;
}
Aggregations