use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.
the class ConcreteSyntaxConstraintProvider method createElement.
protected ISyntaxConstraint createElement(EObject obj) {
if (!(obj instanceof AbstractElement))
return null;
AbstractElement ele = (AbstractElement) obj;
boolean multiple = false;
boolean optional = false;
EClass semanticType = null;
while (true) {
multiple = multiple || isMultipleCardinality(ele);
optional = optional || isOptionalCardinality(ele);
if (ele.eContainer() instanceof ParserRule && ((ParserRule) ele.eContainer()).getType().getClassifier() instanceof EClass)
semanticType = (EClass) ((ParserRule) ele.eContainer()).getType().getClassifier();
if (ele instanceof Assignment) {
return createElement(ConstraintType.ASSIGNMENT, ele, semanticType, multiple, optional);
} else if (ele instanceof Group || ele instanceof UnorderedGroup) {
CompoundElement comp = (CompoundElement) ele;
AbstractElement lastChild = null;
for (AbstractElement o : comp.getElements()) if (containsRelevantElement(o)) {
if (lastChild == null)
lastChild = o;
else {
List<AbstractElement> c = new ArrayList<AbstractElement>(comp.getElements());
List<ISyntaxConstraint> e = createSummarizedAssignments(comp, c, semanticType, optional);
if (e.size() == 1 && c.size() == 0)
return e.get(0);
return createElement(ConstraintType.GROUP, ele, c, e, semanticType, multiple, optional);
}
}
if (lastChild == null)
return null;
ele = lastChild;
continue;
} else if (ele instanceof Alternatives) {
int relevantChildren = 0;
AbstractElement lastChild = null;
for (AbstractElement o : ((CompoundElement) ele).getElements()) if (containsRelevantElement(o)) {
relevantChildren++;
lastChild = o;
}
if (relevantChildren < ((CompoundElement) ele).getElements().size())
optional = true;
if (relevantChildren > 1)
return createElement(ConstraintType.ALTERNATIVE, ele, semanticType, multiple, optional);
if (lastChild == null)
return null;
ele = lastChild;
continue;
} else if (ele instanceof Action) {
semanticType = (EClass) ((Action) ele).getType().getClassifier();
return createElement(ConstraintType.ACTION, ele, semanticType, multiple, optional);
} else if (ele instanceof RuleCall) {
AbstractRule rule = ((RuleCall) ele).getRule();
if (rule.getType().getClassifier() instanceof EClass) {
ele = rule.getAlternatives();
continue;
}
}
return null;
}
}
use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.
the class GrammarWithoutLeftRecursionInspector method createErrorMessages.
public void createErrorMessages(RuleCall object) {
acceptError("The rule '" + object.getRule().getName() + "' is left recursive.", object.getRule(), XtextPackage.Literals.ABSTRACT_RULE__NAME, null);
acceptError("This rule call is part of a left recursive call graph.", object, XtextPackage.Literals.RULE_CALL__RULE, null);
for (int i = callStack.size() - 1; i >= 0; i--) {
RuleCall call = callStack.get(i);
if (call.getRule() == object.getRule()) {
return;
}
acceptError("This rule call is part of a left recursive call graph.", call, XtextPackage.Literals.RULE_CALL__RULE, null);
}
}
use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.
the class XtextTransientValueService2 method isValueTransient.
@Override
public ValueTransient isValueTransient(EObject owner, EStructuralFeature feature) {
if (feature == XtextPackage.eINSTANCE.getTypeRef_Metamodel()) {
final TypeRef typeRef = (TypeRef) owner;
final AbstractMetamodelDeclaration m = typeRef.getMetamodel();
if (m == null || Strings.isEmpty(m.getAlias()))
return YES;
} else if (feature == XtextPackage.eINSTANCE.getAbstractRule_Type()) {
final AbstractRule rule = (AbstractRule) owner;
if (!GrammarUtil.isDatatypeRule(rule) && (rule instanceof ParserRule || rule instanceof EnumRule)) {
final TypeRef returnType = rule.getType();
if (returnType == null || returnType.getClassifier() == null)
return YES;
else if (rule.getName().equals(returnType.getClassifier().getName())) {
if (isValueTransient(returnType, XtextPackage.eINSTANCE.getTypeRef_Metamodel()) == YES)
return PREFERABLY;
else
return NO;
} else if (GrammarUtil.isDatatypeRule(rule)) {
return NodeModelUtils.getNode(returnType) == null ? YES : NO;
}
} else if (rule instanceof TerminalRule || GrammarUtil.isDatatypeRule(rule)) {
final TypeRef returnType = rule.getType();
if (returnType == null)
return YES;
if (rule instanceof TerminalRule) {
if (((TerminalRule) rule).isFragment()) {
return YES;
}
}
if (GrammarUtil.findEString(GrammarUtil.getGrammar(owner)).equals(rule.getType().getClassifier()))
return PREFERABLY;
return NO;
}
} else if (feature == XtextPackage.eINSTANCE.getCrossReference_Terminal()) {
final CrossReference ref = (CrossReference) owner;
if (ref.getTerminal() instanceof RuleCall && ((RuleCall) ref.getTerminal()).getRule() != null) {
if ("ID".equals(((RuleCall) ref.getTerminal()).getRule().getName()))
return PREFERABLY;
else
return NO;
}
return YES;
} else if (feature == XtextPackage.eINSTANCE.getEnumLiteralDeclaration_Literal()) {
final EnumLiteralDeclaration decl = (EnumLiteralDeclaration) owner;
if (decl.getEnumLiteral() != null && decl.getLiteral() != null) {
if (Strings.equal(decl.getLiteral().getValue(), decl.getEnumLiteral().getName()))
return PREFERABLY;
return NO;
}
return YES;
} else if (feature == XtextPackage.eINSTANCE.getRuleCall_ExplicitlyCalled()) {
return YES;
} else if (feature == XtextPackage.eINSTANCE.getNamedArgument_Parameter()) {
if (((NamedArgument) owner).isCalledByName()) {
return NO;
}
return YES;
}
return super.isValueTransient(owner, feature);
}
use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.
the class XtextValidator method checkCrossReferenceTerminal.
@Check
public void checkCrossReferenceTerminal(CrossReference reference) {
if (reference.getTerminal() != null) {
if (reference.getTerminal() instanceof RuleCall) {
RuleCall call = (RuleCall) reference.getTerminal();
checkCrossReferenceTerminal(call);
}
}
}
use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.
the class XtextLinkerTest method testQualifiedRuleCall_01.
@Test
public void testQualifiedRuleCall_01() throws Exception {
StringConcatenation _builder = new StringConcatenation();
_builder.append("grammar test.Lang with org.eclipse.xtext.common.Terminals");
_builder.newLine();
_builder.append("generate test \'http://test\'");
_builder.newLine();
_builder.append("RuleA returns Type:");
_builder.newLine();
_builder.append(" ");
_builder.append("name+=ID name+=super::ID name+=Terminals::ID name+=org::eclipse::xtext::common::Terminals::ID;");
_builder.newLine();
_builder.append("RuleB returns Type:");
_builder.newLine();
_builder.append(" ");
_builder.append("name+=STRING name+=Lang::STRING name+=test::Lang::STRING;");
_builder.newLine();
_builder.append("RuleC returns Type:");
_builder.newLine();
_builder.append(" ");
_builder.append("name+=super::STRING name+=Terminals::STRING name+=org::eclipse::xtext::common::Terminals::STRING;");
_builder.newLine();
_builder.append("terminal STRING: super::STRING;");
_builder.newLine();
final String grammarAsString = _builder.toString();
final XtextResource resource = this.getResourceFromString(grammarAsString);
EObject _get = resource.getContents().get(0);
Grammar grammar = ((Grammar) _get);
final AbstractRule firstRule = IterableExtensions.<AbstractRule>head(grammar.getRules());
final AbstractRule idRule = GrammarUtil.findRuleForName(IterableExtensions.<Grammar>head(grammar.getUsedGrammars()), "ID");
final Function1<RuleCall, Boolean> _function = (RuleCall it) -> {
AbstractRule _rule = it.getRule();
return Boolean.valueOf(Objects.equal(_rule, idRule));
};
Assert.assertTrue(IterableExtensions.<RuleCall>forall(GrammarUtil.containedRuleCalls(firstRule), _function));
final AbstractRule secondRule = IterableExtensions.<AbstractRule>head(IterableExtensions.<AbstractRule>tail(grammar.getRules()));
final AbstractRule stringRule = IterableExtensions.<AbstractRule>last(grammar.getRules());
final Function1<RuleCall, Boolean> _function_1 = (RuleCall it) -> {
AbstractRule _rule = it.getRule();
return Boolean.valueOf(Objects.equal(_rule, stringRule));
};
Assert.assertTrue(IterableExtensions.<RuleCall>forall(GrammarUtil.containedRuleCalls(secondRule), _function_1));
final AbstractRule thirdRule = IterableExtensions.<AbstractRule>head(IterableExtensions.<AbstractRule>drop(grammar.getRules(), 2));
final AbstractRule inheritedString = GrammarUtil.findRuleForName(IterableExtensions.<Grammar>head(grammar.getUsedGrammars()), "STRING");
final Function1<RuleCall, Boolean> _function_2 = (RuleCall it) -> {
AbstractRule _rule = it.getRule();
return Boolean.valueOf(Objects.equal(_rule, inheritedString));
};
Assert.assertTrue(IterableExtensions.<RuleCall>forall(GrammarUtil.containedRuleCalls(thirdRule), _function_2));
}
Aggregations