Java Code Examples for org.eclipse.xtext.ParserRule#getType()

The following examples show how to use org.eclipse.xtext.ParserRule#getType() . 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: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private void addSuperType(ParserRule rule, TypeRef subTypeRef, EClassifierInfo superTypeInfo) throws TransformationException {
	final EClassifier subType = subTypeRef.getClassifier();
	final EClassifierInfo subTypeInfo = subType == null
	        ? findOrCreateEClassifierInfo(subTypeRef, null, true)
			: eClassifierInfos.getInfoOrNull(subType);
	if (subTypeInfo == null)
		throw new TransformationException(TransformationErrorCode.NoSuchTypeAvailable, "Type '"
				+ superTypeInfo.getEClassifier().getName() + "' is not available.", rule.getType());
	if (superTypeInfo.isAssignableFrom(subTypeInfo))
		return;
	if (subTypeInfo.getEClassifier() instanceof EDataType)
		throw new TransformationException(TransformationErrorCode.InvalidSupertype, "Cannot add supertype '"
				+ superTypeInfo.getEClassifier().getName() + "' to simple datatype '"
				+ subTypeInfo.getEClassifier().getName() + "'.", rule.getType());
	if (!subTypeInfo.isGenerated())
		throw new TransformationException(TransformationErrorCode.CannotCreateTypeInSealedMetamodel,
				"Cannot add supertype '" + superTypeInfo.getEClassifier().getName() + "' to sealed type '"
						+ subTypeInfo.getEClassifier().getName() + "'.", rule.getType());
	subTypeInfo.addSupertype(superTypeInfo);
}
 
Example 2
Source File: DatatypeRuleUtil.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Boolean caseParserRule(ParserRule object) {
	if (visitedRules.isEmpty()) {
		visitedRules.add(object);
		return object.getAlternatives() != null && doSwitch(object.getAlternatives());
	}
	final TypeRef typeRef = object.getType();
	if (typeRef == null || typeRef.getClassifier() == null) {
		throw new IllegalStateException("checks are only allowed for linked grammars. Rule: " + object.getName());
	}
	if (!visitedRules.add(object))
		return true;
	Boolean result = GrammarUtil.isDatatypeRule(object);
	return result; 
}
 
Example 3
Source File: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
private boolean deriveTypeHierarchyFromOverridden(ParserRule rule, Grammar grammar) throws TransformationException {
	AbstractRule parentRule = GrammarUtil.findRuleForName(grammar, rule.getName());
	if (parentRule != null) {
		if (parentRule != rule && parentRule instanceof ParserRule) {
			ParserRule casted = (ParserRule) parentRule;
			if (casted.isFragment() != rule.isFragment()) {
				if (rule.isFragment()) {
					throw new TransformationException(TransformationErrorCode.InvalidFragmentOverride,
							"A fragment rule cannot override a production rule.", rule);
				} else {
					throw new TransformationException(TransformationErrorCode.InvalidFragmentOverride,
							"Only fragment rule can override other fragment rules.", rule);
				}
			}
			if (casted.isWildcard() != rule.isWildcard()) {
				if (rule.isWildcard()) {
					throw new TransformationException(TransformationErrorCode.InvalidFragmentOverride,
							"A wildcard fragment rule cannot override a typed fragment rule.", rule);
				} else {
					throw new TransformationException(TransformationErrorCode.InvalidFragmentOverride,
							"Only wildcard fragment rules can override other wildcard fragments.", rule);
				}
			}
			if (rule.isFragment() && !rule.isWildcard() && parentRule.getType() != null) {
				if (rule.getType().getClassifier() != parentRule.getType().getClassifier()) {
					throw new TransformationException(TransformationErrorCode.InvalidFragmentOverride,
							"Overriding fragment rules cannot redeclare their type.", rule.getType());
				}
			}
			checkParameterLists(rule, casted);
		}
		if (parentRule.getType() != null && parentRule != rule) {			
			if (parentRule.getType().getClassifier() instanceof EDataType)
				throw new TransformationException(TransformationErrorCode.InvalidSupertype,
						"Cannot inherit from datatype rule and return another type.", rule.getType());
			EClassifierInfo parentTypeInfo = eClassifierInfos.getInfoOrNull(parentRule.getType());
			if (parentTypeInfo == null)
				throw new TransformationException(TransformationErrorCode.InvalidSupertype,
						"Cannot determine return type of overridden rule.", rule.getType());
			addSuperType(rule, rule.getType(), parentTypeInfo);
			return true;
		}
	}
	return false;
}