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

The following examples show how to use org.eclipse.xtext.ParserRule#getName() . 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: NamedSerializationContextProvider.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
public String getSignificantGrammarElement(final Iterable<ISerializationContext> contexts) {
  ParserRule rule = null;
  int index = Integer.MAX_VALUE;
  for (final ISerializationContext ctx : contexts) {
    {
      ParserRule pr = ctx.getParserRule();
      if ((pr == null)) {
        final Action action = ctx.getAssignedAction();
        if ((action != null)) {
          pr = GrammarUtil.containingParserRule(action);
        }
      }
      if ((pr != null)) {
        final Integer i = this.rules.get(pr);
        if (((i).intValue() < index)) {
          index = (i).intValue();
          rule = pr;
        }
      }
    }
  }
  if ((rule != null)) {
    return rule.getName();
  }
  return "unknown";
}
 
Example 2
Source File: N4JSLinker.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Installs proxies for all non containment references and only if the node representing the EObject that contains
 * the cross reference has got leaf nodes (as a leaf node represents the cross reference).
 *
 * @param resource
 *            the N4JSResource
 * @param obj
 *            the EObject containing the cross reference
 * @param producer
 *            the error/warning producer
 * @param parentNode
 *            the node representing obj inside the node model
 */
private void installProxies(N4JSResource resource, EObject obj, IDiagnosticProducer producer,
		ICompositeNode parentNode, boolean dontCheckParent) {
	final EClass eClass = obj.eClass();
	if (eClass.getEAllReferences().size() - eClass.getEAllContainments().size() == 0)
		return;

	for (INode node = parentNode.getFirstChild(); node != null; node = node.getNextSibling()) {
		EObject grammarElement = node.getGrammarElement();
		if (grammarElement instanceof CrossReference && hasLeafNodes(node)) {
			producer.setNode(node);
			CrossReference crossReference = (CrossReference) grammarElement;
			final EReference eRef = GrammarUtil.getReference(crossReference, eClass);
			if (eRef == null) {
				ParserRule parserRule = GrammarUtil.containingParserRule(crossReference);
				final String feature = GrammarUtil.containingAssignment(crossReference).getFeature();
				throw new IllegalStateException("Couldn't find EReference for crossreference '" + eClass.getName()
						+ "::" + feature + "' in parser rule '" + parserRule.getName() + "'.");
			}
			createAndSetProxy(resource, obj, node, eRef, crossReference, producer);
			afterCreateAndSetProxy(obj, node, eRef, crossReference, producer);
		} else if (grammarElement instanceof RuleCall && node instanceof ICompositeNode) {
			RuleCall ruleCall = (RuleCall) grammarElement;
			AbstractRule calledRule = ruleCall.getRule();
			if (calledRule instanceof ParserRule && ((ParserRule) calledRule).isFragment()) {
				installProxies(resource, obj, producer, (ICompositeNode) node, true);
			}
		}
	}
	if (!dontCheckParent && shouldCheckParentNode(parentNode)) {
		installProxies(resource, obj, producer, parentNode.getParent(), dontCheckParent);
	}
}
 
Example 3
Source File: XtextLabelProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
StyledString text(ParserRule parserRule) {
	if (GrammarUtil.isDatatypeRule(parserRule)) {
		Styler xtextStyleAdapterStyler = stylerFactory.createXtextStyleAdapterStyler(semanticHighlightingConfiguration
				.dataTypeRule());
		return new StyledString(parserRule.getName(), xtextStyleAdapterStyler);
	}
	return convertToStyledString(parserRule.getName());
}
 
Example 4
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 5
Source File: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private void deriveFeatures(ParserRule rule) throws TransformationException {
	EClassifierInfo classInfo = findEClassifierInfo(rule);
	if (classInfo == null)
		throw new TransformationException(TransformationErrorCode.NoSuchTypeAvailable, "No type available for rule " +
				rule.getName(), rule);
	Xtext2EcoreInterpretationContext context = new Xtext2EcoreInterpretationContext(eClassifierInfos, classInfo);
	if (rule.getAlternatives() != null) // might happen due to syntax errors in the document
		deriveFeatures(context, rule.getAlternatives());
}
 
Example 6
Source File: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private void checkParameterLists(ParserRule rule, ParserRule overridden) throws TransformationException {
	int inherited = overridden.getParameters().size();
	if (inherited == rule.getParameters().size()) {
		boolean ok = true;
		for(int i = 0; ok && i < inherited; i++) {
			if (!Strings.equal(rule.getParameters().get(i).getName(), overridden.getParameters().get(i).getName())) {
				ok = false;
			}
		}
		if (ok) {
			return;
		}
	}
	if (inherited == 0) {
		throw new TransformationException(TransformationErrorCode.InvalidRuleOverride,
				"Overridden rule " + rule.getName() + " does not declare any parameters", rule);
	}
	StringBuilder message = new StringBuilder("Parameter list is incompatible with inherited ");
	message.append(rule.getName()).append("<");
	for(int i = 0; i < overridden.getParameters().size(); i++) {
		if (i != 0) {
			message.append(", ");
		}
		message.append(overridden.getParameters().get(i).getName());
	}
	message.append(">");
	throw new TransformationException(TransformationErrorCode.InvalidRuleOverride,
			message.toString(), rule);
}
 
Example 7
Source File: LazyLinker.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private void installProxies(EObject obj, IDiagnosticProducer producer,
		Multimap<EStructuralFeature.Setting, INode> settingsToLink, ICompositeNode parentNode, boolean dontCheckParent) {
	final EClass eClass = obj.eClass();
	if (eClass.getEAllReferences().size() - eClass.getEAllContainments().size() == 0)
		return;

	for (INode node = parentNode.getFirstChild(); node != null; node = node.getNextSibling()) {
		EObject grammarElement = node.getGrammarElement();
		if (grammarElement instanceof CrossReference && hasLeafNodes(node)) {
			producer.setNode(node);
			CrossReference crossReference = (CrossReference) grammarElement;
			final EReference eRef = GrammarUtil.getReference(crossReference, eClass);
			if (eRef == null) {
				ParserRule parserRule = GrammarUtil.containingParserRule(crossReference);
				final String feature = GrammarUtil.containingAssignment(crossReference).getFeature();
				throw new IllegalStateException("Couldn't find EReference for crossreference '"+eClass.getName()+"::"+feature+"' in parser rule '"+parserRule.getName()+"'.");
			}
			if (!eRef.isResolveProxies() /*|| eRef.getEOpposite() != null see https://bugs.eclipse.org/bugs/show_bug.cgi?id=282486*/) {
				final EStructuralFeature.Setting setting = ((InternalEObject) obj).eSetting(eRef);
				settingsToLink.put(new SettingDelegate(setting), node);
			} else {
				createAndSetProxy(obj, node, eRef);
				afterCreateAndSetProxy(obj, node, eRef, crossReference, producer);
			}
		} else if (grammarElement instanceof RuleCall && node instanceof ICompositeNode) {
			RuleCall ruleCall = (RuleCall) grammarElement;
			AbstractRule calledRule = ruleCall.getRule();
			if (calledRule instanceof ParserRule && ((ParserRule) calledRule).isFragment()) {
				installProxies(obj, producer, settingsToLink, (ICompositeNode) node, true);
			}
		}
	}
	if (!dontCheckParent && shouldCheckParentNode(parentNode)) {
		installProxies(obj, producer, settingsToLink, parentNode.getParent(), dontCheckParent);
	}
}
 
Example 8
Source File: Context2NameFunction.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public String getContextName(Grammar grammar, ParserRule ctx) {
	if (grammar == null) {
		return ctx.getName();	
	}
	return RuleNames.getRuleNames(grammar, false).getUniqueRuleName(ctx);
}