Search in sources :

Example 86 with RuleCall

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();
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Grammar(org.eclipse.xtext.Grammar) RuleCall(org.eclipse.xtext.RuleCall) Test(org.junit.Test)

Example 87 with RuleCall

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);
}
Also used : Group(org.eclipse.xtext.Group) AbstractElement(org.eclipse.xtext.AbstractElement) ParameterReference(org.eclipse.xtext.ParameterReference) RuleCall(org.eclipse.xtext.RuleCall) Assignment(org.eclipse.xtext.Assignment) EObject(org.eclipse.emf.ecore.EObject) Parameter(org.eclipse.xtext.Parameter) EcoreUtil(org.eclipse.emf.ecore.util.EcoreUtil) NamedArgument(org.eclipse.xtext.NamedArgument) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 88 with RuleCall

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);
}
Also used : Assignment(org.eclipse.xtext.Assignment) ParserRule(org.eclipse.xtext.ParserRule) Group(org.eclipse.xtext.Group) ICrossReferenceSerializer(org.eclipse.xtext.parsetree.reconstr.ITokenSerializer.ICrossReferenceSerializer) IGrammarAccess(org.eclipse.xtext.IGrammarAccess) AbstractRule(org.eclipse.xtext.AbstractRule) XtextGrammarAccess(org.eclipse.xtext.services.XtextGrammarAccess) RuleCall(org.eclipse.xtext.RuleCall) Test(org.junit.Test)

Example 89 with RuleCall

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());
}
Also used : Assignment(org.eclipse.xtext.Assignment) Issue(org.eclipse.xtext.validation.Issue) IResourceValidator(org.eclipse.xtext.validation.IResourceValidator) XtextResource(org.eclipse.xtext.resource.XtextResource) Grammar(org.eclipse.xtext.Grammar) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall) Test(org.junit.Test)

Example 90 with RuleCall

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();
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Grammar(org.eclipse.xtext.Grammar) RuleCall(org.eclipse.xtext.RuleCall) Test(org.junit.Test)

Aggregations

RuleCall (org.eclipse.xtext.RuleCall)95 ParserRule (org.eclipse.xtext.ParserRule)41 AbstractRule (org.eclipse.xtext.AbstractRule)36 EObject (org.eclipse.emf.ecore.EObject)33 Test (org.junit.Test)33 Grammar (org.eclipse.xtext.Grammar)28 AbstractElement (org.eclipse.xtext.AbstractElement)26 Assignment (org.eclipse.xtext.Assignment)22 TerminalRule (org.eclipse.xtext.TerminalRule)19 Action (org.eclipse.xtext.Action)17 CrossReference (org.eclipse.xtext.CrossReference)13 Group (org.eclipse.xtext.Group)13 TypeRef (org.eclipse.xtext.TypeRef)13 UnorderedGroup (org.eclipse.xtext.UnorderedGroup)13 ArrayList (java.util.ArrayList)11 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)11 InternalEObject (org.eclipse.emf.ecore.InternalEObject)10 Keyword (org.eclipse.xtext.Keyword)10 EClass (org.eclipse.emf.ecore.EClass)9 EnumRule (org.eclipse.xtext.EnumRule)9