use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.
the class XtextValidationTest method testRuleCallAllowed_11.
@Test
public void testRuleCallAllowed_11() throws Exception {
String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "Model: name=ID Fragment;\n" + "fragment Fragment: Other;\n" + "Other: name=ID;";
Grammar grammar = (Grammar) getModel(grammarAsText);
ParserRule rule = (ParserRule) grammar.getRules().get(1);
RuleCall ruleCall = (RuleCall) rule.getAlternatives();
XtextValidator validator = get(XtextValidator.class);
ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(ruleCall, true, false);
validator.setMessageAcceptor(messageAcceptor);
try {
validator.checkUnassignedRuleCallAllowed(ruleCall);
fail();
} catch (RuntimeException e) {
assertEquals("org.eclipse.xtext.validation.GuardException", e.getClass().getName());
}
messageAcceptor.validate();
}
use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.
the class AntlrGrammarGenUtil method getPredicatedElement.
/**
* @since 2.9
*/
public static AbstractElement getPredicatedElement(AbstractElement element) {
if (element.isPredicated()) {
return doGetPredicatedElement(element);
}
if (element instanceof Assignment) {
return getPredicatedElement(((Assignment) element).getTerminal());
}
if (element instanceof RuleCall) {
final RuleCall ruleCall = (RuleCall) element;
final AbstractRule calledRule = ruleCall.getRule();
Group group = (Group) calledRule.getAlternatives();
AbstractElement first = group.getElements().get(0);
AbstractElement result = getPredicatedElement(first);
if (ruleCall.getArguments().isEmpty()) {
return result;
}
EcoreUtil.Copier copier = new EcoreUtil.Copier() {
private static final long serialVersionUID = 1L;
@Override
public EObject copy(EObject eObject) {
if (eObject instanceof ParameterReference) {
ParameterReference paramRef = (ParameterReference) eObject;
Parameter parameter = paramRef.getParameter();
if (calledRule == GrammarUtil.containingRule(parameter)) {
for (NamedArgument givenArgument : ruleCall.getArguments()) {
if (givenArgument.getParameter() == parameter) {
EObject result = copy(givenArgument.getValue());
return result;
}
}
throw new IllegalStateException();
}
}
return super.copy(eObject);
}
};
AbstractElement clone = (AbstractElement) copier.copy(result);
copier.copyReferences();
return clone;
}
return doGetPredicatedElement(element);
}
use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.
the class XtextScopingTest method testScope_01.
@Test
public void testScope_01() {
ParserRule rule = (ParserRule) GrammarUtil.findRuleForName(grammar, "AbstractCallOverridenParserRule");
assertNotNull(rule);
Group group = (Group) rule.getAlternatives();
Assignment ass = (Assignment) group.getElements().get(1);
assertNotNull(ass);
RuleCall call = (RuleCall) ass.getTerminal();
AbstractRule calledRule = call.getRule();
assertNotNull(calledRule);
XtextGrammarAccess ga = (XtextGrammarAccess) get(IGrammarAccess.class);
String text = get(ICrossReferenceSerializer.class).serializeCrossRef(call, ga.getRuleCallAccess().getRuleAbstractRuleCrossReference_0_0(), calledRule, null);
assertNotNull(text);
assertEquals(calledRule.getName(), text);
}
use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.
the class XtextValidationTest method testRuleCalledSuper.
@Test
public void testRuleCalledSuper() throws Exception {
XtextResource resource = getResourceFromString("grammar com.acme.Bar with org.eclipse.xtext.common.Terminals\n" + "generate metamodel 'myURI'\n" + "Model: super=super;\n" + "super: name=ID;");
IResourceValidator validator = get(IResourceValidator.class);
List<Issue> issues = validator.validate(resource, CheckMode.FAST_ONLY, CancelIndicator.NullImpl);
assertEquals(issues.toString(), 1, issues.size());
assertEquals("Discouraged rule name 'super'", issues.get(0).getMessage());
Grammar grammar = (Grammar) resource.getContents().get(0);
AbstractRule model = grammar.getRules().get(0);
Assignment assignment = (Assignment) model.getAlternatives();
RuleCall ruleCall = (RuleCall) assignment.getTerminal();
assertSame(grammar.getRules().get(1), ruleCall.getRule());
}
use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.
the class XtextValidationTest method testRuleCallAllowed_12.
@Test
public void testRuleCallAllowed_12() throws Exception {
String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "Model: name=ID Fragment;\n" + "fragment Fragment: Other;\n" + "fragment Other: name=ID;";
Grammar grammar = (Grammar) getModel(grammarAsText);
ParserRule rule = (ParserRule) grammar.getRules().get(1);
RuleCall ruleCall = (RuleCall) rule.getAlternatives();
XtextValidator validator = get(XtextValidator.class);
ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, false, false);
validator.setMessageAcceptor(messageAcceptor);
validator.checkUnassignedRuleCallAllowed(ruleCall);
messageAcceptor.validate();
}
Aggregations