Search in sources :

Example 26 with CrossReference

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

the class SyntacticSequencerTest method getNodeSequence.

@SuppressWarnings("deprecation")
private List<String> getNodeSequence(EObject model) {
    List<String> result = Lists.newArrayList();
    GrammarElementTitleSwitch titleSwitch = new GrammarElementTitleSwitch().showAssignments();
    org.eclipse.xtext.serializer.sequencer.EmitterNodeIterator ni = new org.eclipse.xtext.serializer.sequencer.EmitterNodeIterator(NodeModelUtils.findActualNodeFor(model));
    while (ni.hasNext()) {
        INode next = ni.next();
        EObject ele = next.getGrammarElement() instanceof CrossReference ? ((CrossReference) next.getGrammarElement()).getTerminal() : next.getGrammarElement();
        if (next instanceof ILeafNode || GrammarUtil.isDatatypeRuleCall(ele))
            result.add(titleSwitch.doSwitch(ele) + " -> " + next.getText().trim());
        else if (next instanceof ICompositeNode)
            result.add(titleSwitch.doSwitch(ele));
    }
    return result;
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) GrammarElementTitleSwitch(org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) EObject(org.eclipse.emf.ecore.EObject) CrossReference(org.eclipse.xtext.CrossReference) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode)

Example 27 with CrossReference

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

the class XtextTransientValueService method isTransient.

@Override
public boolean isTransient(EObject owner, EStructuralFeature feature, int index) {
    if (feature == XtextPackage.eINSTANCE.getTypeRef_Metamodel()) {
        final TypeRef typeRef = (TypeRef) owner;
        final AbstractMetamodelDeclaration m = typeRef.getMetamodel();
        if (m == null || Strings.isEmpty(m.getAlias()))
            return true;
    } else if (feature == XtextPackage.eINSTANCE.getAbstractRule_Type()) {
        final AbstractRule rule = (AbstractRule) owner;
        if (rule instanceof ParserRule || rule instanceof EnumRule) {
            final TypeRef returnType = rule.getType();
            if (returnType == null || returnType.getClassifier() == null)
                return true;
            else if (rule.getName().equals(returnType.getClassifier().getName())) {
                return isTransient(returnType, XtextPackage.eINSTANCE.getTypeRef_Metamodel(), -1);
            } else if (GrammarUtil.isDatatypeRule(rule)) {
                return NodeModelUtils.getNode(returnType) == null;
            }
        } else if (rule instanceof TerminalRule) {
            final TypeRef returnType = rule.getType();
            return ((TerminalRule) rule).isFragment() || returnType == null || GrammarUtil.findEString(GrammarUtil.getGrammar(owner)).equals(rule.getType().getClassifier());
        }
    } else if (feature == XtextPackage.eINSTANCE.getCrossReference_Terminal()) {
        final CrossReference ref = (CrossReference) owner;
        if (ref.getTerminal() instanceof RuleCall && ((RuleCall) ref.getTerminal()).getRule() != null && "ID".equals(((RuleCall) ref.getTerminal()).getRule().getName()))
            return true;
    } else if (feature == XtextPackage.eINSTANCE.getEnumLiteralDeclaration_Literal()) {
        final EnumLiteralDeclaration decl = (EnumLiteralDeclaration) owner;
        return decl.getEnumLiteral() != null && decl.getLiteral() != null && Strings.equal(decl.getLiteral().getValue(), decl.getEnumLiteral().getName());
    } else if (feature == XtextPackage.eINSTANCE.getRuleCall_ExplicitlyCalled()) {
        return true;
    } else if (feature == XtextPackage.eINSTANCE.getNamedArgument_Parameter()) {
        return !((NamedArgument) owner).isCalledByName();
    }
    return super.isTransient(owner, feature, index);
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) EnumRule(org.eclipse.xtext.EnumRule) EnumLiteralDeclaration(org.eclipse.xtext.EnumLiteralDeclaration) TypeRef(org.eclipse.xtext.TypeRef) CrossReference(org.eclipse.xtext.CrossReference) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) TerminalRule(org.eclipse.xtext.TerminalRule) NamedArgument(org.eclipse.xtext.NamedArgument) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall)

Example 28 with CrossReference

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

the class XtextValidationTest method testCheckCrossReferenceTerminal_01.

@Test
public void testCheckCrossReferenceTerminal_01() throws Exception {
    XtextValidator validator = get(XtextValidator.class);
    CrossReference reference = XtextFactory.eINSTANCE.createCrossReference();
    RuleCall call = XtextFactory.eINSTANCE.createRuleCall();
    reference.setTerminal(call);
    ParserRule rule = XtextFactory.eINSTANCE.createParserRule();
    call.setRule(rule);
    TypeRef typeRef = XtextFactory.eINSTANCE.createTypeRef();
    rule.setType(typeRef);
    typeRef.setClassifier(EcorePackage.Literals.ESTRING);
    ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, false, false);
    validator.setMessageAcceptor(messageAcceptor);
    validator.checkCrossReferenceTerminal(reference);
    messageAcceptor.validate();
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) TypeRef(org.eclipse.xtext.TypeRef) CrossReference(org.eclipse.xtext.CrossReference) RuleCall(org.eclipse.xtext.RuleCall) Test(org.junit.Test)

Example 29 with CrossReference

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

Aggregations

CrossReference (org.eclipse.xtext.CrossReference)29 RuleCall (org.eclipse.xtext.RuleCall)13 EObject (org.eclipse.emf.ecore.EObject)12 AbstractRule (org.eclipse.xtext.AbstractRule)9 ParserRule (org.eclipse.xtext.ParserRule)8 EReference (org.eclipse.emf.ecore.EReference)7 Assignment (org.eclipse.xtext.Assignment)7 TypeRef (org.eclipse.xtext.TypeRef)7 Test (org.junit.Test)7 AbstractElement (org.eclipse.xtext.AbstractElement)6 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)6 INode (org.eclipse.xtext.nodemodel.INode)6 Action (org.eclipse.xtext.Action)5 EnumLiteralDeclaration (org.eclipse.xtext.EnumLiteralDeclaration)4 TerminalRule (org.eclipse.xtext.TerminalRule)4 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)4 ArrayList (java.util.ArrayList)3 EClass (org.eclipse.emf.ecore.EClass)3 EnumRule (org.eclipse.xtext.EnumRule)3 Keyword (org.eclipse.xtext.Keyword)3