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

The following examples show how to use org.eclipse.xtext.GrammarUtil#containingRule() . 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: XtextDiagnosticConverter.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected Severity getSeverity(Diagnostic diagnostic) {
	Severity result = super.getSeverity(diagnostic);
	String issueCode = getIssueCode(diagnostic);
	if (result == Severity.WARNING && issueCode != null) {
		// only warnings can be suppressed
		EObject causer = getCauser(diagnostic);
		if (causer != null) {
			if (isMarkedAsIgnored(causer, issueCode)) {
				return null;
			}
			if (!(causer instanceof AbstractRule)) {
				AbstractRule rule = GrammarUtil.containingRule(causer);
				if (rule != null && isMarkedAsIgnored(rule, issueCode)) {
					return null;
				}
			}
			Grammar grammar = GrammarUtil.getGrammar(causer);
			if (grammar != null && isMarkedAsIgnored(grammar, issueCode)) {
				return null;
			}
		}
	}
	return result;
}
 
Example 2
Source File: SuperCallScope.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Iterable<IEObjectDescription> getElements(EObject object) {
	if (object instanceof AbstractRule) {
		Grammar grammar = GrammarUtil.getGrammar(context);
		AbstractRule rule = (AbstractRule) object;
		if (GrammarUtil.getGrammar(rule) == grammar) {
			return Lists.newArrayList(
					EObjectDescription.create(GrammarUtil.getSimpleName(grammar) + "." + rule.getName(), rule),
					EObjectDescription.create(grammar.getName() + "." + rule.getName(), rule));
		}
		List<IEObjectDescription> result = Lists.newArrayList(
				EObjectDescription.create(SUPER + "." + rule.getName(), rule),
				EObjectDescription.create(GrammarUtil.getSimpleName(grammar) + "." + rule.getName(), rule),
				EObjectDescription.create(grammar.getName() + "." + rule.getName(), rule));
		AbstractRule contextRule = GrammarUtil.containingRule(context);
		if (contextRule != null && contextRule.getName().equals(rule.getName())) {
			result.add(0, EObjectDescription.create(SUPER, rule));
		}
		return result;
	}
	return Collections.emptyList();
}
 
Example 3
Source File: XtextValidator.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Check
public void checkTerminalFragmentCalledFromTerminalRule(final RuleCall call) {
	if (call.getRule() != null && !call.getRule().eIsProxy()) {
		if (call.getRule() instanceof TerminalRule && ((TerminalRule) call.getRule()).isFragment()) {
			AbstractRule container = GrammarUtil.containingRule(call);
			if (!(container instanceof TerminalRule)) {
				getMessageAcceptor().acceptError(
						"Only terminal rules may use terminal fragments.", 
						call, 
						XtextPackage.Literals.RULE_CALL__RULE,
						ValidationMessageAcceptor.INSIGNIFICANT_INDEX,
						null);
			}
		}
	}
}
 
Example 4
Source File: KeywordInspector.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
public void inspectKeywordHidesTerminalRule(Keyword keyword) {
	AbstractRule container = GrammarUtil.containingRule(keyword);
	if (container instanceof TerminalRule)
		return;
	Grammar grammar = GrammarUtil.getGrammar(container);
	List<TerminalRule> rules = cache.get(KeywordInspector.class, grammar.eResource(), ()->GrammarUtil.allTerminalRules(grammar));
	for(TerminalRule rule: rules) {
		if (!rule.isFragment()) {
			AbstractElement element = rule.getAlternatives();
			if (element instanceof Keyword && Strings.isEmpty(element.getCardinality())) {
				String value = ((Keyword) element).getValue();
				if (value.equals(keyword.getValue()))
					acceptor.acceptError(
						"The keyword '" + value + "' hides the terminal rule " + rule.getName()+ ".", 
						keyword,
						XtextPackage.Literals.KEYWORD__VALUE,
						ValidationMessageAcceptor.INSIGNIFICANT_INDEX, null);
			}
		}
	}
}
 
Example 5
Source File: SemanticNodeIterator.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean isEObjectNode(INode node) {
	if (node.getGrammarElement() instanceof AbstractRule)
		return true;
	if (node.getGrammarElement() instanceof Action)
		return true;
	if (GrammarUtil.isAssignedEObjectRuleCall(node.getGrammarElement())) {
		if (node.hasDirectSemanticElement())
			return true;
		AbstractRule rule = ((RuleCall) node.getGrammarElement()).getRule();
		node = node.getParent();
		while (node != null) {
			if (GrammarUtil.isAssigned(node.getGrammarElement()))
				return true;
			if (node.getGrammarElement() instanceof Action
					&& GrammarUtil.containingRule(node.getGrammarElement()) == rule)
				return false;
			node = node.getParent();
		}
		return true;
	}
	return false;
}
 
Example 6
Source File: XtextElementSelectionListener.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Gets the parser rule at current selection.
 *
 * @return AbstractRule or null if no selection or no active XtextEditor
 */
public AbstractRule getRule() {
  INode node = nodeAtTextSelection();
  if (node != null && node.getGrammarElement() != null) {
    return GrammarUtil.containingRule(node.getGrammarElement());
  }
  return null;
}
 
Example 7
Source File: XtendProposalProvider.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void completeMember_ReturnType(EObject model, Assignment assignment, ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	if (context.getPrefix().length() == 0) {
		EObject previousGrammarElement = context.getLastCompleteNode().getGrammarElement();
		AbstractRule rule = GrammarUtil.containingRule(previousGrammarElement);
		if (rule != grammarAccess.getValidIDRule()) {
			completeJavaTypes(context, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE, true,
					getQualifiedNameValueConverter(), createVisibilityFilter(context), acceptor);
		}
	}
}
 
Example 8
Source File: FormatResourceDescriptionStrategy.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Checks whether given EObject represents a Xbase local variable.
 *
 * @param eObject
 *          to be checked
 * @return true if the given object does not represent a xbase local variable
 */
public boolean isXbaseLocalVariableName(final EObject eObject) {
  INode semanticNode = NodeModelUtils.getNode(eObject);
  if (semanticNode != null) {
    INode leafNode = NodeModelUtils.findLeafNodeAtOffset(semanticNode, semanticNode.getTotalOffset());
    AbstractRule containingRule = GrammarUtil.containingRule(leafNode.getGrammarElement());
    if (leafNode != null && containingRule != null && "ValidID".equals(containingRule.getName())) {
      return true;
    }
  }
  return false;
}
 
Example 9
Source File: AntlrContentAssistGrammarGenerator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected CharSequence paramConfig(final AbstractElement it) {
  StringConcatenation _builder = new StringConcatenation();
  {
    if ((((GrammarUtil.containingRule(it).getAlternatives() == it) && ParserRule.class.isInstance(GrammarUtil.containingRule(it))) && (!((ParserRule) AntlrGrammarGenUtil.<AbstractRule>getOriginalElement(GrammarUtil.containingRule(it))).getParameters().isEmpty()))) {
      _builder.append(", ");
      AbstractRule _containingRule = GrammarUtil.containingRule(it);
      int _parameterConfig = AntlrGrammarGenUtil.getParameterConfig(((ParserRule) _containingRule));
      _builder.append(_parameterConfig);
      _builder.newLineIfNotEmpty();
    }
  }
  return _builder;
}
 
Example 10
Source File: ElementMatcherProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected void installAfter(IAfterElement pattern) {
	Set<MatcherState> states = getAllStates(pattern.matchAfter());
	AbstractRule rule = GrammarUtil.containingRule(pattern.matchAfter());
	for (MatcherState state : states) {
		state.getAfterPatterns().add(pattern);
		for (MatcherTransition out : state.isParserRuleCall() ? state.getOutgoingAfterReturn() : state
				.getOutgoing())
			if (pattern.matchAfter() == out.getLoopCenter() || !states.contains(out.getTarget()))
				out.addPattern(pattern);
		if (state.isEndState())
			for (MatcherState caller : findRuleCallsTo(rule, Sets.<AbstractRule> newHashSet()))
				for (MatcherTransition afterReturn : caller.getOutgoingAfterReturn())
					afterReturn.addPattern(state, pattern);
	}
}
 
Example 11
Source File: AbstractSemanticRegionsFinder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public List<Pair<ISemanticRegion, ISemanticRegion>> keywordPairs(String kw1, String kw2) {
	Preconditions.checkNotNull(kw1);
	Preconditions.checkNotNull(kw2);
	Preconditions.checkArgument(!kw1.equals(kw2));
	Predicate<ISemanticRegion> p1 = new KeywordPredicate(kw1);
	Predicate<ISemanticRegion> p2 = new KeywordPredicate(kw2);
	List<ISemanticRegion> all = findAll(Predicates.or(p1, p2));
	Builder<Pair<ISemanticRegion, ISemanticRegion>> result = ImmutableList.builder();
	LinkedList<ISemanticRegion> stack = new LinkedList<ISemanticRegion>();
	for (ISemanticRegion region : all) {
		if (p1.apply(region))
			stack.push(region);
		else {
			AbstractRule regionRule = GrammarUtil.containingRule(region.getGrammarElement());
			while (!stack.isEmpty()) {
				ISemanticRegion candidate = stack.pop();
				if (region.getSemanticElement() == candidate.getSemanticElement()) {
					AbstractRule candidateRule = GrammarUtil.containingRule(candidate.getGrammarElement());
					if (regionRule == candidateRule) {
						result.add(Pair.of(candidate, region));
						break;
					}
				}
			}
		}
	}
	return result.build();
}
 
Example 12
Source File: GrammarElementTitleSwitch.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected String addQualified(String result, AbstractElement ele) {
	if (!showQualified && !showRule)
		return result;
	AbstractRule rule = GrammarUtil.containingRule(ele);
	if (rule == null)
		return "<AbstractElement not contained in AbstractRule!>:" + result;
	if (!showQualified)
		return result + ":" + rule.getName();
	GrammarElementTitleSwitch others = copy();
	others.showQualified = false;
	others.showRule = false;
	List<AbstractElement> elementsWithSameName = Lists.newArrayList();
	for (AbstractElement candidate : EcoreUtil2.getAllContentsOfType(rule, ele.getClass()))
		if (candidate == ele || result.equals(others.doSwitch(candidate)))
			elementsWithSameName.add(candidate);
	if (elementsWithSameName.size() < 2) {
		if (showRule)
			return rule.getName() + ":" + result;
		return result;
	}
	Map<CompoundElement, Node> nodes = Maps.newHashMap();
	for (AbstractElement collision : elementsWithSameName) {
		EObject current = EcoreUtil2.getContainerOfType(collision, CompoundElement.class);
		Node node = new Node(null, collision == ele ? result : "");
		while (current instanceof CompoundElement) {
			CompoundElement container = (CompoundElement) current;
			Node cntNode = nodes.get(container);
			if (cntNode == null)
				nodes.put(container, cntNode = new Node(container, null));
			if (!cntNode.children.contains(node))
				cntNode.children.add(node);
			node = cntNode;
			current = current.eContainer();
		}
	}
	if (showRule)
		return rule.getName() + ":" + nodes.get(rule.getAlternatives());
	return nodes.get(rule.getAlternatives()).toString();
}
 
Example 13
Source File: TreeConstructionReportImpl.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public Set<EObject> collectConsumedEObjects() {
	Set<EObject> result = new HashSet<EObject>();
	for (AbstractToken endToken : getDeadends()) {
		AbstractToken currentToken = endToken;
		while (currentToken.getNext() != null && currentToken.getNext().getLastRuleCallOrigin() != null
				&& currentToken.getNext().getLastRuleCallOrigin().getGrammarElement() != null) {
			if (GrammarUtil.containingRule(currentToken.getNext().getLastRuleCallOrigin().getGrammarElement()) == GrammarUtil
					.containingRule(currentToken.getGrammarElement()))
				result.add(currentToken.getNext().getEObjectConsumer().getEObject());
			currentToken = currentToken.getNext();
		}
	}
	return result;
}
 
Example 14
Source File: XtextValidator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Check
public void checkKeywordNoSpaces(final Keyword keyword) {
	if (keyword.getValue() != null && !(keyword.eContainer() instanceof EnumLiteralDeclaration) 
			&& !(GrammarUtil.containingRule(keyword) instanceof TerminalRule)) {
		if (keyword.getValue().contains(" ") || keyword.getValue().contains("\t")) {
			addIssue("A keyword should not contain spaces.", 
					keyword, 
					null,
					SPACES_IN_KEYWORD);
		}
	}
}
 
Example 15
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 16
Source File: AbstractNFAState.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected boolean filter(AbstractElement ele) {
	AbstractRule rule = GrammarUtil.containingRule(ele);
	if (rule == null || !GrammarUtil.isEObjectRule(rule))
		return true;
	return builder.filter(ele);
}
 
Example 17
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();
}
 
Example 18
Source File: CurrentTypeFinder.java    From xtext-core with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Find the type of the current variable after an AbstractElement was consumed.
 * If the AbstractElement will instantiate a new type (e.g. is an Action), 
 * the instantiated type will be returned. If the element may occur in a 
 * path that leads to an unassigned current element, <code>null</code> may 
 * be returned. If the element is optional and changes the type, this method 
 * will return the changed type as if the element were consumed. 
 */
public EClassifier findCurrentTypeAfter(AbstractElement element) {
	final AbstractRule rule = GrammarUtil.containingRule(element);
	Implementation implementation = new Implementation();
	implementation.computeResult(rule, element);
	return implementation.getResult();
}