Search in sources :

Example 11 with Assignment

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

the class GrammarParserTest method testAssignment.

@Test
public void testAssignment() throws Exception {
    Assignment assignment = (Assignment) getModel("name=ID");
    assertNotNull(assignment);
    assertEquals("ID", ((RuleCall) assignment.getTerminal()).getRule().getName());
}
Also used : Assignment(org.eclipse.xtext.Assignment) RuleCall(org.eclipse.xtext.RuleCall) Test(org.junit.Test)

Example 12 with Assignment

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

the class XtextValidationTest method testCheckRuleCallInUnorderedGroup_05.

@Test
public void testCheckRuleCallInUnorderedGroup_05() throws Exception {
    XtextValidator validator = get(XtextValidator.class);
    UnorderedGroup unorderedGroup = XtextFactory.eINSTANCE.createUnorderedGroup();
    RuleCall ruleCall = XtextFactory.eINSTANCE.createRuleCall();
    TypeRef typeRef = XtextFactory.eINSTANCE.createTypeRef();
    typeRef.setClassifier(EcorePackage.Literals.EOBJECT);
    ParserRule parserRule = XtextFactory.eINSTANCE.createParserRule();
    parserRule.setType(typeRef);
    ruleCall.setRule(parserRule);
    Assignment assignment = XtextFactory.eINSTANCE.createAssignment();
    assignment.setTerminal(ruleCall);
    unorderedGroup.getElements().add(assignment);
    ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, false, false);
    validator.setMessageAcceptor(messageAcceptor);
    validator.checkRuleCallInUnorderedGroup(ruleCall);
    messageAcceptor.validate();
}
Also used : Assignment(org.eclipse.xtext.Assignment) ParserRule(org.eclipse.xtext.ParserRule) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) TypeRef(org.eclipse.xtext.TypeRef) RuleCall(org.eclipse.xtext.RuleCall) Test(org.junit.Test)

Example 13 with Assignment

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

the class XtextValidationTest method testKeywordWithSpaces.

@Test
public void testKeywordWithSpaces() throws Exception {
    String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "A: foo='a b c'; B: bar='x\ty'; terminal C: ' ';";
    Grammar grammar = (Grammar) getModel(grammarAsText);
    XtextValidator validator = get(XtextValidator.class);
    ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, false, true);
    Assignment valueAssignment = (Assignment) grammar.getRules().get(0).getAlternatives();
    Assignment valueAssignment2 = (Assignment) grammar.getRules().get(1).getAlternatives();
    messageAcceptor.expectedContext(valueAssignment.getTerminal());
    configureValidator(validator, messageAcceptor, valueAssignment);
    validator.checkKeywordNoSpaces((Keyword) valueAssignment.getTerminal());
    messageAcceptor.validate();
    messageAcceptor = new ValidatingMessageAcceptor(null, false, true);
    messageAcceptor.expectedContext(valueAssignment2.getTerminal());
    configureValidator(validator, messageAcceptor, valueAssignment2);
    validator.checkKeywordNoSpaces((Keyword) valueAssignment2.getTerminal());
    messageAcceptor.validate();
    messageAcceptor = new ValidatingMessageAcceptor(null, false, false);
    configureValidator(validator, messageAcceptor, valueAssignment2);
    validator.checkKeywordNoSpaces((Keyword) grammar.getRules().get(2).getAlternatives());
    messageAcceptor.validate();
}
Also used : Assignment(org.eclipse.xtext.Assignment) Grammar(org.eclipse.xtext.Grammar) Test(org.junit.Test)

Example 14 with Assignment

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

the class NodeModelUtils method findActualSemanticObjectFor.

/**
 * Returns the semantic object that is really associated with the actual container node of the given node. It
 * handles the structural semantics that results from {@link Action Actions} and {@link RuleCall unassigned rule
 * calls}.
 *
 * @return the semantic object that is really associated with the actual container node of the given node.
 */
/* @Nullable */
public static EObject findActualSemanticObjectFor(/* @Nullable */
INode node) {
    if (node == null)
        return null;
    if (node.hasDirectSemanticElement())
        return node.getSemanticElement();
    EObject grammarElement = node.getGrammarElement();
    ICompositeNode parent = node.getParent();
    if (grammarElement == null)
        return findActualSemanticObjectFor(parent);
    Assignment assignment = GrammarUtil.containingAssignment(grammarElement);
    if (assignment != null) {
        if (GrammarUtil.isEObjectFragmentRule(GrammarUtil.containingRule(assignment))) {
            EObject result = findActualSemanticObjectInChildren(node, grammarElement);
            if (result != null)
                return result;
        }
        if (parent.hasDirectSemanticElement())
            return findActualSemanticObjectFor(parent);
        INode sibling = parent.getFirstChild();
        while (!sibling.equals(node)) {
            EObject siblingGrammarElement = sibling.getGrammarElement();
            if (siblingGrammarElement != null && GrammarUtil.containingAssignment(siblingGrammarElement) == null) {
                if (GrammarUtil.isEObjectRuleCall(siblingGrammarElement)) {
                    return findActualSemanticObjectFor(sibling);
                }
                if (siblingGrammarElement.eClass() == XtextPackage.Literals.ACTION) {
                    return findActualSemanticObjectFor(sibling);
                }
            }
            sibling = sibling.getNextSibling();
        }
    } else if (!GrammarUtil.isEObjectFragmentRuleCall(grammarElement)) {
        EObject result = findActualSemanticObjectInChildren(node, grammarElement);
        if (result != null)
            return result;
    }
    return findActualSemanticObjectFor(parent);
}
Also used : Assignment(org.eclipse.xtext.Assignment) INode(org.eclipse.xtext.nodemodel.INode) EObject(org.eclipse.emf.ecore.EObject) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode)

Example 15 with Assignment

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

the class TreeConstructionReportImpl method getDiagnosticMessage.

protected String getDiagnosticMessage(AssignmentToken token) {
    Assignment ass = (Assignment) token.getGrammarElement();
    Object value = token.getEObjectConsumer().getConsumable(ass.getFeature(), false);
    if (value == null) {
        EStructuralFeature f = token.getEObjectConsumer().getEObject().eClass().getEStructuralFeature(ass.getFeature());
        if (f == null)
            return "The current object of type '" + token.getEObjectConsumer().getEObject().eClass().getName() + "' does not have a feature named '" + ass.getFeature() + "'";
        String cls = f.getEContainingClass() == token.getEObjectConsumer().getEObject().eClass() ? f.getEContainingClass().getName() : f.getEContainingClass().getName() + "(" + token.getEObjectConsumer().getEObject().eClass().getName() + ")";
        String feat = cls + "." + f.getName();
        if (f.isMany()) {
            int size = ((List<?>) token.getEObjectConsumer().getEObject().eGet(f)).size();
            return "All " + size + " values of " + feat + " have been consumed. " + "More are needed to continue here.";
        } else
            return feat + " is not set.";
    } else {
        ErrorAcceptor err = new ErrorAcceptor();
        for (RuleCall ruleCall : GrammarUtil.containedRuleCalls(token.getGrammarElement())) {
            if (ruleCall.getRule() instanceof EnumRule) {
                if (enumSerializer.isValid(token.getEObject(), ruleCall, value, err))
                    return null;
            } else if (ruleCall.getRule().getType().getClassifier() instanceof EDataType) {
                if (valueSerializer.isValid(token.getEObject(), ruleCall, value, err))
                    return null;
            }
        }
        return err.getMessage();
    }
}
Also used : Assignment(org.eclipse.xtext.Assignment) EnumRule(org.eclipse.xtext.EnumRule) EDataType(org.eclipse.emf.ecore.EDataType) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) EObject(org.eclipse.emf.ecore.EObject) ArrayList(java.util.ArrayList) List(java.util.List) IErrorAcceptor(org.eclipse.xtext.parsetree.reconstr.ITokenSerializer.IErrorAcceptor) RuleCall(org.eclipse.xtext.RuleCall)

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