use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.
the class GrammarAccessExtensions method getSingleElementDescription.
private List<String> getSingleElementDescription(final AbstractElement ele) {
final ArrayList<String> r = new ArrayList<String>(2);
boolean _matched = false;
if (ele instanceof Keyword) {
_matched = true;
String _value = ((Keyword) ele).getValue();
r.add(_value);
}
if (!_matched) {
if (ele instanceof Assignment) {
_matched = true;
String _feature = ((Assignment) ele).getFeature();
r.add(_feature);
}
}
if (!_matched) {
if (ele instanceof RuleCall) {
_matched = true;
String _name = ((RuleCall) ele).getRule().getName();
r.add(_name);
}
}
if (!_matched) {
if (ele instanceof Action) {
_matched = true;
TypeRef _type = ((Action) ele).getType();
EClassifier _classifier = null;
if (_type != null) {
_classifier = _type.getClassifier();
}
boolean _tripleNotEquals = (_classifier != null);
if (_tripleNotEquals) {
String _name = ((Action) ele).getType().getClassifier().getName();
r.add(_name);
}
boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(((Action) ele).getFeature());
boolean _not = (!_isNullOrEmpty);
if (_not) {
String _feature = ((Action) ele).getFeature();
r.add(_feature);
}
}
}
if (!_matched) {
if (ele instanceof CrossReference) {
_matched = true;
TypeRef _type = ((CrossReference) ele).getType();
EClassifier _classifier = null;
if (_type != null) {
_classifier = _type.getClassifier();
}
boolean _tripleNotEquals = (_classifier != null);
if (_tripleNotEquals) {
String _name = ((CrossReference) ele).getType().getClassifier().getName();
r.add(_name);
}
}
}
if (!_matched) {
if (ele instanceof EnumLiteralDeclaration) {
_matched = true;
String _name = ((EnumLiteralDeclaration) ele).getEnumLiteral().getName();
r.add(_name);
}
}
return r;
}
use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.
the class GrammarAccessExtensions method toStringLiteral.
public CharSequence toStringLiteral(final AbstractElement it) {
CharSequence _switchResult = null;
boolean _matched = false;
if (it instanceof RuleCall) {
AbstractRule _rule = ((RuleCall) it).getRule();
boolean _tripleNotEquals = (_rule != null);
if (_tripleNotEquals) {
_matched = true;
_switchResult = AntlrGrammarGenUtil.getQualifiedNameAsString(((RuleCall) it));
}
}
if (!_matched) {
if (it instanceof Keyword) {
_matched = true;
StringConcatenation _builder = new StringConcatenation();
_builder.append("\"");
String _stringInAntlrAction = AntlrGrammarGenUtil.toStringInAntlrAction(((Keyword) it).getValue());
_builder.append(_stringInAntlrAction);
_builder.append("\"");
_switchResult = _builder;
}
}
if (!_matched) {
_switchResult = "null";
}
return _switchResult;
}
use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.
the class Xtext2EcoreTransformer method deriveFeatures.
private Xtext2EcoreInterpretationContext deriveFeatures(final Xtext2EcoreInterpretationContext context, AbstractElement element) {
XtextSwitch<Xtext2EcoreInterpretationContext> visitor = new XtextSwitch<Xtext2EcoreInterpretationContext>() {
/*
* Used for Alternatives and UnorderedGroups
*/
@Override
public Xtext2EcoreInterpretationContext caseCompoundElement(CompoundElement object) {
List<Xtext2EcoreInterpretationContext> contexts = new ArrayList<Xtext2EcoreInterpretationContext>();
for (AbstractElement group : object.getElements()) {
contexts.add(deriveFeatures(context, group));
}
Xtext2EcoreInterpretationContext result = context;
if (!contexts.isEmpty()) {
if (GrammarUtil.isOptionalCardinality(object)) {
contexts.add(0, result);
} else {
result = contexts.get(0);
}
result = result.mergeSpawnedContexts(contexts);
}
return result;
}
@Override
public Xtext2EcoreInterpretationContext caseAssignment(Assignment object) {
try {
context.addFeature(object);
} catch (TransformationException ex) {
reportError(ex);
}
return context;
}
@Override
public Xtext2EcoreInterpretationContext caseGroup(Group object) {
return visitElements(object, object.getElements());
}
private Xtext2EcoreInterpretationContext visitElements(AbstractElement caller, List<AbstractElement> elementsToProcess) {
Xtext2EcoreInterpretationContext result = deriveFeatures(context.spawnContextForGroup(), elementsToProcess);
if (GrammarUtil.isMultipleCardinality(caller)) {
result = deriveFeatures(result.spawnContextForGroup(), elementsToProcess);
}
if (GrammarUtil.isOptionalCardinality(caller)) {
result = result.mergeSpawnedContexts(Arrays.asList(context, result));
}
return result;
}
@Override
public Xtext2EcoreInterpretationContext caseAlternatives(Alternatives object) {
List<Xtext2EcoreInterpretationContext> contexts = newArrayList();
if (GrammarUtil.isOptionalCardinality(object)) {
contexts.add(context);
}
for (AbstractElement alternative : object.getElements()) {
contexts.add(deriveFeatures(context.spawnContextForGroup(), alternative));
}
Xtext2EcoreInterpretationContext result = context.mergeSpawnedContexts(contexts);
if (GrammarUtil.isMultipleCardinality(object)) {
for (AbstractElement alternative : object.getElements()) {
deriveFeatures(result.spawnContextForGroup(), alternative);
}
}
return result;
}
@Override
public Xtext2EcoreInterpretationContext caseRuleCall(RuleCall object) {
AbstractRule calledRule = object.getRule();
if (isWildcardFragment(calledRule)) {
AbstractElement ruleBody = calledRule.getAlternatives();
if (ruleBody != null) {
return visitElements(object, Collections.singletonList(ruleBody));
}
return context;
}
if (isParserRuleFragment(calledRule)) {
return context;
}
if (!GrammarUtil.isOptionalCardinality(object)) {
// announced during the first iteration
if (calledRule != null && calledRule instanceof ParserRule && !GrammarUtil.isDatatypeRule((ParserRule) calledRule)) {
try {
EClassifierInfo eClassifierInfo = findOrCreateEClassifierInfo(calledRule);
return context.spawnContextWithCalledRule(eClassifierInfo, object);
} catch (TransformationException e) {
reportError(e);
}
}
}
return context;
}
@Override
public Xtext2EcoreInterpretationContext caseAction(Action object) {
try {
TypeRef actionTypeRef = object.getType();
EClassifierInfo actionType = findOrCreateEClassifierInfo(actionTypeRef, null, true);
EClassifierInfo currentCompatibleType = context.getCurrentCompatibleType();
Xtext2EcoreInterpretationContext ctx = context.spawnContextWithReferencedType(actionType, object);
if (object.getFeature() != null) {
ctx.addFeature(object.getFeature(), currentCompatibleType, GrammarUtil.isMultipleAssignment(object), true, object);
}
return ctx;
} catch (TransformationException e) {
reportError(e);
}
return context;
}
@Override
public Xtext2EcoreInterpretationContext defaultCase(EObject object) {
return context;
}
};
return visitor.doSwitch(element);
}
use of org.eclipse.xtext.RuleCall 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.RuleCall in project xtext-core by eclipse.
the class ContextPDAProvider method getFilterableRule.
protected ParserRule getFilterableRule(ISerState state) {
if (state.getType() == SerStateType.PUSH) {
RuleCall rc = (RuleCall) state.getGrammarElement();
AbstractRule rule = rc.getRule();
if (rule instanceof ParserRule) {
ParserRule pr = (ParserRule) rule;
if (pr.isFragment()) {
return null;
}
if (pr.isDefinesHiddenTokens()) {
return null;
}
return pr;
}
}
return null;
}
Aggregations