Java Code Examples for org.eclipse.xtext.RuleCall#setRule()

The following examples show how to use org.eclipse.xtext.RuleCall#setRule() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@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();
}
 
Example 2
protected void assertFirstSet(String expectation, AbstractRule rule) {
	RuleCall ruleCall = XtextFactory.eINSTANCE.createRuleCall();
	ruleCall.setRule(rule);
	List<AbstractElement> firstSet = AntlrGrammarGenUtil.getFirstSet(ruleCall);
	StringBuilder actual = new StringBuilder();
	GrammarElementTitleSwitch stringifier = new GrammarElementTitleSwitch();
	for(int i = 0; i < firstSet.size(); i++) {
		if (i != 0)
			actual.append(", ");
		actual.append(stringifier.apply(firstSet.get(i)));
	}
	assertEquals(expectation, actual.toString());
}
 
Example 3
@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();
}
 
Example 4
@Test public void testCheckCrossReferenceTerminal_02() 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.EINT);
	ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(call, true, false);
	validator.setMessageAcceptor(messageAcceptor);
	validator.checkCrossReferenceTerminal(reference);
	messageAcceptor.validate();
}
 
Example 5
@Test public void testCheckRuleCallInUnorderedGroup_01() 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);
	unorderedGroup.getElements().add(ruleCall);
	ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(ruleCall, true, false);
	validator.setMessageAcceptor(messageAcceptor);
	validator.checkRuleCallInUnorderedGroup(ruleCall);
	messageAcceptor.validate();
}
 
Example 6
@Test public void testCheckRuleCallInUnorderedGroup_02() 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.EBIG_DECIMAL);
	ParserRule parserRule = XtextFactory.eINSTANCE.createParserRule();
	parserRule.setType(typeRef);
	ruleCall.setRule(parserRule);
	unorderedGroup.getElements().add(ruleCall);
	ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, false, false);
	validator.setMessageAcceptor(messageAcceptor);
	validator.checkRuleCallInUnorderedGroup(ruleCall);
	messageAcceptor.validate();
}
 
Example 7
@Test public void testCheckRuleCallInUnorderedGroup_03() throws Exception {
	XtextValidator validator = get(XtextValidator.class);
	UnorderedGroup unorderedGroup = XtextFactory.eINSTANCE.createUnorderedGroup();
	RuleCall ruleCall = XtextFactory.eINSTANCE.createRuleCall();
	EnumRule enumRule = XtextFactory.eINSTANCE.createEnumRule();
	ruleCall.setRule(enumRule);
	unorderedGroup.getElements().add(ruleCall);
	ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, false, false);
	validator.setMessageAcceptor(messageAcceptor);
	validator.checkRuleCallInUnorderedGroup(ruleCall);
	messageAcceptor.validate();
}
 
Example 8
@Test public void testCheckRuleCallInUnorderedGroup_04() throws Exception {
	XtextValidator validator = get(XtextValidator.class);
	UnorderedGroup unorderedGroup = XtextFactory.eINSTANCE.createUnorderedGroup();
	RuleCall ruleCall = XtextFactory.eINSTANCE.createRuleCall();
	TerminalRule terminalRule = XtextFactory.eINSTANCE.createTerminalRule();
	ruleCall.setRule(terminalRule);
	unorderedGroup.getElements().add(ruleCall);
	ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, false, false);
	validator.setMessageAcceptor(messageAcceptor);
	validator.checkRuleCallInUnorderedGroup(ruleCall);
	messageAcceptor.validate();
}
 
Example 9
Source Project: xtext-core   File: XtextLinker.java    License: Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected void setDefaultValueImpl(EObject obj, EReference ref, IDiagnosticProducer producer) {
	if (XtextPackage.eINSTANCE.getTypeRef_Metamodel() == ref) {
		final TypeRef typeRef = (TypeRef) obj;
		final String typeRefName = GrammarUtil.getTypeRefName(typeRef);
		final List<EObject> metamodels = XtextMetamodelReferenceHelper.findBestMetamodelForType(typeRef, "",
				typeRefName, scopeProvider.getScope(typeRef, ref));
		if (metamodels.isEmpty() || metamodels.size() > 1)
			producer.addDiagnostic(new DiagnosticMessage("Cannot find meta model for type '" + typeRefName + "'", Severity.ERROR, null));
		else
			typeRef.setMetamodel((AbstractMetamodelDeclaration) metamodels.get(0));
	} else if (XtextPackage.eINSTANCE.getCrossReference_Terminal() == ref) {
		AbstractRule rule = GrammarUtil.findRuleForName(GrammarUtil.getGrammar(obj), "ID");
		if (rule == null)
			producer.addDiagnostic(new DiagnosticMessage("Cannot resolve implicit reference to rule 'ID'", Severity.ERROR, null));
		else {
			RuleCall call = XtextFactory.eINSTANCE.createRuleCall();
			call.setRule(rule);
			((CrossReference) obj).setTerminal(call);
		}
	} else if (XtextPackage.eINSTANCE.getNamedArgument_Parameter() == ref) {
		final NamedArgument argument = (NamedArgument) obj;
		if (!argument.isCalledByName()) {
			RuleCall ruleCall = EcoreUtil2.getContainerOfType(argument, RuleCall.class);
			AbstractRule calledRule = ruleCall.getRule();
			if (!(calledRule instanceof ParserRule)) {
				producer.addDiagnostic(new DiagnosticMessage("Arguments can only be used with parser rules.", Severity.ERROR, null));	
				return;
			}
			if (!calledRule.eIsProxy()) {
				ParserRule casted = (ParserRule) calledRule;
				int idx = ruleCall.getArguments().indexOf(argument);
				if (idx < casted.getParameters().size()) {
					argument.setParameter(casted.getParameters().get(idx));
					return;
				} else if (casted.getParameters().size() == 0) {
					producer.addDiagnostic(new DiagnosticMessage(
							"Rule " + calledRule.getName() + " has no arguments.", Severity.ERROR, null));
				} else {
					String message = "Invalid number of arguments for rule " + calledRule.getName() + ", expecting "
							+ casted.getParameters().size() + " but was " + (idx+1);
					producer.addDiagnostic(new DiagnosticMessage(message, Severity.ERROR, null));
				}
			}
		}
	} else {
		super.setDefaultValueImpl(obj, ref, producer);
	}
}