Java Code Examples for org.eclipse.xtext.GrammarUtil#getTypeRefName()

The following examples show how to use org.eclipse.xtext.GrammarUtil#getTypeRefName() . 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
Source File: ElementTypeCalculator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public EClassifier caseTypeRef(TypeRef object) {
	if (object.getClassifier() == null) {
		if (object.getMetamodel() == null || object.getMetamodel().getEPackage() == null)
			return null;
		String name = GrammarUtil.getTypeRefName(object);
		if (Strings.isEmpty(name))
			return null;
		EClassifierInfo info = classifierInfos.getInfo(object.getMetamodel(), name);
		if (info != null)
			object.setClassifier(info.getEClassifier());
	}
	return object.getClassifier();
}
 
Example 2
Source File: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
private EClassifierInfo createEClassifierInfo(TypeRef typeRef, String name) throws TransformationException {
	if (eClassifierInfos.getInfo(typeRef) != null)
		throw new IllegalArgumentException("Cannot create EClass for same type twice "
				+ typeRef.getClassifier().getName());
	//					+ GrammarUtil.getQualifiedName(typeRef));

	String classifierName = GrammarUtil.getTypeRefName(typeRef);
	if (classifierName == null)
		classifierName = name;
	if (classifierName == null)
		throw new TransformationException(TransformationErrorCode.NoSuchTypeAvailable, "Cannot reference unnamed type.", typeRef);

	AbstractMetamodelDeclaration metaModel = typeRef.getMetamodel();
	if (metaModel == null)
		throw new TransformationException(TransformationErrorCode.UnknownMetaModelAlias, "Cannot create type for " + classifierName
				+ " because its EPackage is unknown.", typeRef);
	EPackage generatedEPackage = getGeneratedEPackage(metaModel);
	if (generatedEPackage == null) {
		throw new TransformationException(TransformationErrorCode.CannotCreateTypeInSealedMetamodel,
				"Cannot create type '" + classifierName + "' in alias " + typeRef.getMetamodel().getAlias(), typeRef);
	}

	EClassifier classifier = generatedEPackage.getEClassifier(classifierName);
	if (classifier == null) {
		if (GrammarUtil.containingParserRule(typeRef) != null) {
			classifier = EcoreFactory.eINSTANCE.createEClass();
		} else if (GrammarUtil.containingEnumRule(typeRef) != null) {
			classifier = EcoreFactory.eINSTANCE.createEEnum();
		} else {
			for (AbstractMetamodelDeclaration mmd : grammar.getMetamodelDeclarations()) {
				if (mmd instanceof ReferencedMetamodel && mmd.getEPackage() != null && mmd.getEPackage().getNsURI().equals(EcorePackage.eNS_URI)) {
					throw new TransformationException(TransformationErrorCode.NoSuchTypeAvailable, "Cannot create datatype " + classifierName, typeRef);
				}
			}
			throw new TransformationException(TransformationErrorCode.NoSuchTypeAvailable, "Cannot create datatype " + classifierName + ". If this is supposed to return EString, make sure you have imported '"+EcorePackage.eNS_URI+"'", typeRef);
		}
		classifier.setName(classifierName);
		generatedEPackage.getEClassifiers().add(classifier);
		typeRef.setClassifier(classifier);

		EClassifierInfo result;
		if (classifier instanceof EClass)
			result = EClassifierInfo.createEClassInfo((EClass) classifier, true, getGeneratedEPackageURIs(), GrammarUtil.getGrammar(typeRef));
		else // datatype or enum
			result = EClassifierInfo.createEDataTypeInfo((EDataType) classifier, true);

		if (!eClassifierInfos.addInfo(typeRef, result))
			throw new IllegalStateException("cannot add type for typeRef twice: '" + classifierName + "'");
		SourceAdapter.adapt(classifier, typeRef);
		return result;
	}
	typeRef.setClassifier(classifier);
	SourceAdapter.adapt(classifier, typeRef);
	return eClassifierInfos.getInfo(classifier);
}
 
Example 3
Source File: XtextLinker.java    From xtext-core with 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);
	}
}
 
Example 4
Source File: XtextRuleInspector.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected String getTypeRefName(TypeRef typeRef) {
	String simpleName = GrammarUtil.getTypeRefName(typeRef);
	if (typeRef.getMetamodel() != null && !Strings.isEmpty(typeRef.getMetamodel().getAlias()))
		return typeRef.getMetamodel().getAlias() + "::" + simpleName;
	return simpleName;
}