Search in sources :

Example 56 with Assignment

use of org.eclipse.xtext.Assignment in project xtext-core by eclipse.

the class NodeModelUtilsTest method testFindActualSemanticObjectFor_03.

@Test
public void testFindActualSemanticObjectFor_03() throws Exception {
    String grammarString = "grammar foo.Bar with org.eclipse.xtext.common.Terminals generate foo 'bar' Model:name=ID;";
    Grammar grammar = (Grammar) getModel(grammarString);
    ILeafNode leafNode = NodeModelUtils.findLeafNodeAtOffset(NodeModelUtils.getNode(grammar), grammarString.indexOf('='));
    EObject object = NodeModelUtils.findActualSemanticObjectFor(leafNode);
    assertTrue(object instanceof Assignment);
}
Also used : Assignment(org.eclipse.xtext.Assignment) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) EObject(org.eclipse.emf.ecore.EObject) Grammar(org.eclipse.xtext.Grammar) Test(org.junit.Test)

Example 57 with Assignment

use of org.eclipse.xtext.Assignment in project xtext-core by eclipse.

the class XtextParserTest method testParseCrossRef.

@Test
public void testParseCrossRef() throws Exception {
    Grammar model = (Grammar) getModel("grammar foo with org.eclipse.xtext.common.Terminals " + "generate foo 'bar' as boo " + "Model returns boo::Model : 'a' stuff+=Stuff*; " + "Stuff returns boo::Stuff : 'stuff' name=ID refersTo=[boo::Stuff];");
    AbstractRule rule = model.getRules().get(1);
    Group group = (Group) rule.getAlternatives();
    Assignment assignment = (Assignment) group.getElements().get(2);
    CrossReference reference = (CrossReference) assignment.getTerminal();
    assertEquals("boo", reference.getType().getMetamodel().getAlias());
    // assertWithXtend("'boo'", "eAllContents.typeSelect(xtext::CrossReference).first().type.metamodel.alias", model);
    assertEquals("Stuff", reference.getType().getClassifier().getName());
// assertWithXtend("'Stuff'", "eAllContents.typeSelect(xtext::CrossReference).first().type.classifier.name", model);
}
Also used : Assignment(org.eclipse.xtext.Assignment) Group(org.eclipse.xtext.Group) CrossReference(org.eclipse.xtext.CrossReference) Grammar(org.eclipse.xtext.Grammar) AbstractRule(org.eclipse.xtext.AbstractRule) Test(org.junit.Test)

Example 58 with Assignment

use of org.eclipse.xtext.Assignment 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 59 with Assignment

use of org.eclipse.xtext.Assignment 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 60 with Assignment

use of org.eclipse.xtext.Assignment 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)

Aggregations

Assignment (org.eclipse.xtext.Assignment)60 EObject (org.eclipse.emf.ecore.EObject)27 Test (org.junit.Test)25 RuleCall (org.eclipse.xtext.RuleCall)22 Grammar (org.eclipse.xtext.Grammar)21 AbstractElement (org.eclipse.xtext.AbstractElement)15 ParserRule (org.eclipse.xtext.ParserRule)15 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)14 Action (org.eclipse.xtext.Action)13 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)12 AbstractRule (org.eclipse.xtext.AbstractRule)12 Group (org.eclipse.xtext.Group)11 UnorderedGroup (org.eclipse.xtext.UnorderedGroup)9 Alternatives (org.eclipse.xtext.Alternatives)8 CrossReference (org.eclipse.xtext.CrossReference)7 ArrayList (java.util.ArrayList)6 EClass (org.eclipse.emf.ecore.EClass)6 InternalEObject (org.eclipse.emf.ecore.InternalEObject)6 NamedArgument (org.eclipse.xtext.NamedArgument)6 LiteralCondition (org.eclipse.xtext.LiteralCondition)5