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

The following examples show how to use org.eclipse.xtext.ParserRule#isFragment() . 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: OverriddenValueInspector.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Boolean caseRuleCall(RuleCall object) {
	AbstractRule calledRule = object.getRule();
	if (calledRule == null || calledRule.eIsProxy() || calledRule instanceof TerminalRule || calledRule instanceof EnumRule)
		return Boolean.FALSE;
	ParserRule parserRule = (ParserRule) calledRule;
	if (GrammarUtil.isDatatypeRule(parserRule))
		return Boolean.FALSE;
	if (parserRule.isFragment()) {
		visitFragment(object);
		if (GrammarUtil.isMultipleCardinality(object))
			visitFragment(object);
	}
	if (!addVisited(parserRule))
		return Boolean.FALSE;
	Multimap<String, AbstractElement> prevAssignedFeatures = assignedFeatures;
	assignedFeatures = newMultimap();
	doSwitch(parserRule.getAlternatives());
	for (String feature : assignedFeatures.keySet())
		prevAssignedFeatures.put(feature, object);
	assignedFeatures = prevAssignedFeatures;
	removeVisited(parserRule);
	return Boolean.FALSE;
}
 
Example 2
Source File: ContextPDAProvider.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected ParserRule getFilterableRule(ISerState state) {
	if (state.getType() == SerStateType.PUSH) {
		RuleCall rc = (RuleCall) state.getGrammarElement();
		AbstractRule rule = rc.getRule();
		if (rule instanceof ParserRule) {
			ParserRule pr = (ParserRule) rule;
			if (pr.isFragment()) {
				return null;
			}
			if (pr.isDefinesHiddenTokens()) {
				return null;
			}
			return pr;
		}
	}
	return null;
}
 
Example 3
Source File: AntlrGrammarGenUtil.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @since 2.9
 */
public static String getParameterList(ParserRule rule, Boolean skipCurrent, String currentType) {
	boolean currentAsParam = rule.isFragment() && !GrammarUtil.isDatatypeRule(getOriginalElement(rule));
	if ((skipCurrent || !currentAsParam) && rule.getParameters().isEmpty()) {
		return "";
	}
	StringBuilder result = new StringBuilder();
	result.append("[");
	if (!skipCurrent) {
		if (currentAsParam) {
			result.append(currentType).append(" in_current");
			if (!rule.getParameters().isEmpty()) {
				result.append(", ");
			}
		}
	}
	Joiner.on(", ").appendTo(result, Iterables.transform(rule.getParameters(), new Function<Parameter, String>() {
		@Override
		public String apply(Parameter input) {
			return "boolean p_" + input.getName();
		}
	}));
	result.append("] ");
	return result.toString();
}
 
Example 4
Source File: ParseTreeConstructorUtil.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public static List<AbstractElement> calcRootFollowers(Grammar g) {
	List<AbstractElement> callees = new ArrayList<AbstractElement>();
	for (AbstractRule r : GrammarUtil.allRules(g))
		if (r instanceof ParserRule) {
			ParserRule pr = (ParserRule) r;
			if (!GrammarUtil.isDatatypeRule(pr) && !pr.isFragment())
				callees.add(pr.getAlternatives());
		}
	return callees;
}
 
Example 5
Source File: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isWildcardFragment(AbstractRule rule) {
	if (rule instanceof ParserRule) {
		ParserRule casted = (ParserRule) rule;
		return casted.isFragment() && casted.isWildcard();
	}
	return false;
}
 
Example 6
Source File: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isParserRuleFragment(AbstractRule rule) {
	if (rule instanceof ParserRule) {
		ParserRule casted = (ParserRule) rule;
		return casted.isFragment() && !casted.isWildcard();
	}
	return false;
}
 
Example 7
Source File: CurrentTypeFinder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Boolean caseRuleCall(RuleCall object) {
	EClassifier wasType = currentType;
	AbstractRule calledRule = object.getRule();
	if (currentType == null) {
		if (calledRule instanceof ParserRule && !GrammarUtil.isDatatypeRule((ParserRule) calledRule)) {
			ParserRule parserRule = (ParserRule) calledRule;
			if (parserRule.isFragment()) {
				if (context.getType() != null)
					currentType = context.getType().getClassifier();
				if (!parserRule.isWildcard()) {
					doSwitch(parserRule);
				}
			} else {
				TypeRef returnType = calledRule.getType();
				if (returnType != null) {
					currentType = returnType.getClassifier();
				}
			}
		}
	} else if (isFragmentButNotWildcard(calledRule)) {
		doSwitch(calledRule);
	}
	if (object == stopElement)
		return true;
	if (GrammarUtil.isOptionalCardinality(object))
		currentType = getCompatibleType(currentType, wasType, object);
	return false;
}
 
Example 8
Source File: CurrentTypeFinder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isFragmentButNotWildcard(AbstractRule calledRule) {
	if (calledRule instanceof ParserRule) {
		ParserRule casted = (ParserRule) calledRule;
		return casted.isFragment() && !casted.isWildcard();
	}
	return false;
}
 
Example 9
Source File: XtextValidator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Check
public void checkUnassignedRuleCallAllowed(final RuleCall call) {
	if (call.getRule() != null && !call.getRule().eIsProxy() && GrammarUtil.containingAssignment(call) == null) {
		AbstractRule container = GrammarUtil.containingRule(call);
		if (call.getRule() instanceof ParserRule) {
			if (container instanceof TerminalRule) {
				getMessageAcceptor().acceptError(
						"Cannot call parser rule from terminal rule.", 
						call, 
						XtextPackage.Literals.RULE_CALL__RULE,
						ValidationMessageAcceptor.INSIGNIFICANT_INDEX,
						null);
			} else {
				ParserRule parserRule = (ParserRule) call.getRule();
				if (!GrammarUtil.isDatatypeRule(parserRule) && !parserRule.isFragment()) {
					checkCurrentMustBeUnassigned(call);
				}
			}
		}
		if (call.getRule() instanceof EnumRule) {
			if (container instanceof TerminalRule) {
				getMessageAcceptor().acceptError(
						"Cannot call enum rule from terminal rule.", 
						call, 
						XtextPackage.Literals.RULE_CALL__RULE,
						ValidationMessageAcceptor.INSIGNIFICANT_INDEX,
						null);
			}
		}
	}
}
 
Example 10
Source File: RuleWithoutInstantiationInspector.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected boolean canInspect(ParserRule rule) {
	// special treatment of first rule
	if (GrammarUtil.getGrammar(rule).getRules().get(0) == rule)
		return false;
	if (GrammarUtil.isDatatypeRule(rule) || rule.getAlternatives() == null || rule.isFragment())
		return false;
	return super.canInspect(rule);
}
 
Example 11
Source File: PartialParsingHelper.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isInvalidRootNode(ICompositeNode rootNode, ICompositeNode candidate) {
	int endOffset = candidate.getTotalEndOffset();
	if (candidate instanceof SyntheticCompositeNode)
		return true;
	if (candidate.getGrammarElement() instanceof RuleCall) {
		AbstractRule rule = ((RuleCall) candidate.getGrammarElement()).getRule();
		if (!(rule instanceof ParserRule))
			return true;
		ParserRule casted = (ParserRule) rule;
		if (GrammarUtil.isDatatypeRule(casted) || casted.isFragment() || !casted.getParameters().isEmpty()) {
			return true;
		}
		if (isInvalidDueToPredicates((AbstractElement) candidate.getGrammarElement()))
			return true;
	}
	if (candidate.getGrammarElement() instanceof Action) {
		return true;
	}
	if (endOffset == rootNode.getTotalEndOffset()) {
		INode lastChild = getLastChild(candidate);
		if (lastChild instanceof ICompositeNode) {
			INode lastLeaf = getLastLeaf(candidate);
			if (isInvalidLastChildNode(candidate, lastLeaf)) {
				return true;
			}
		}
		if (isInvalidLastChildNode(candidate, lastChild)) {
			return true;
		}
	}
	return false;
}
 
Example 12
Source File: AntlrGrammarGenUtil.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.9
 */
public static boolean isValidEntryRule(ParserRule rule) {
	if (rule.isFragment()) {
		return false;
	}
	RuleWithParameterValues parameterValues = RuleWithParameterValues.findInEmfObject(rule);
	if (parameterValues.getParamValues().isEmpty()) {
		return true;
	}
	return false;
}
 
Example 13
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;
}
 
Example 14
Source File: GrammarPDAProvider.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected boolean isValidRule(ParserRule rule) {
	return GrammarUtil.isEObjectRule(rule) && !rule.isFragment();
}
 
Example 15
Source File: AbstractCleaningLinker.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
private boolean isContainedInFragmentRule(EObject grammarElement) {
	ParserRule rule = (ParserRule) GrammarUtil.containingRule(grammarElement);
	return rule.isFragment();
}