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

The following examples show how to use org.eclipse.xtext.AbstractRule#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: KeywordBasedValueConverter.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @throws IllegalArgumentException if the rule is not a datatype rule or does not fulfill
 *   the pattern <pre>RuleName: 'keyword' | 'other';</pre>
 */
@Override
public void setRule(AbstractRule rule) {
	this.rule = rule;
	if (!GrammarUtil.isDatatypeRule(rule))
		throw new IllegalArgumentException(rule.getName() + " is not a data type rule");
	if (!(rule.getAlternatives() instanceof Alternatives) && !(rule.getAlternatives() instanceof Keyword)) {
		throw new IllegalArgumentException(rule.getName() + " is not a simple keyword nor an alternative");
	}
	if (rule.getAlternatives() instanceof Keyword) {
		keywords = ImmutableSet.of(keywordToString((Keyword) rule.getAlternatives()));
	} else {
		Alternatives alternatives = (Alternatives) rule.getAlternatives();
		ImmutableSet.Builder<String> builder = ImmutableSet.builder();
		for(AbstractElement element: alternatives.getElements()) {
			if (!(element instanceof Keyword)) {
				throw new IllegalArgumentException(rule.getName() + "'s body does not contain an alternative of keywords");
			}
			builder.add(keywordToString((Keyword) element));
		}
		keywords = builder.build();
	}
}
 
Example 2
Source File: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private TypeRef getOrComputeReturnType(AbstractRule rule) {
	TypeRef result = rule.getType();
	if (result == null) {
		EClassifier classifier = getClassifierFor(rule);
		if (classifier == null) {
			if (rule.getName() == null)
				return null;
			result = getTypeRef(rule.getName());
		} else
			result = getTypeRef(classifier);
		if (result.getMetamodel() == null) {
			AbstractMetamodelDeclaration bestMatch = null;
			for (AbstractMetamodelDeclaration decl : grammar.getMetamodelDeclarations()) {
				if (decl instanceof GeneratedMetamodel && Strings.isEmpty(decl.getAlias())) {
					bestMatch = decl;
					break;
				}
			}
			if (result.getMetamodel() == null)
				result.setMetamodel(bestMatch);
		}
		rule.setType(result);
	}
	return result;
}
 
Example 3
Source File: DotIDValueConverter.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public ID toValue(String string, INode node)
		throws ValueConverterException {
	if (string == null) {
		return null;
	}
	if (node == null) {
		return ID.fromString(string);
	}

	for (ILeafNode leaf : node.getLeafNodes()) {
		Object grammarElement = leaf.getGrammarElement();
		if (grammarElement instanceof RuleCall) {
			RuleCall lexerRuleCall = (RuleCall) grammarElement;
			AbstractRule nestedLexerRule = lexerRuleCall.getRule();
			String nestedLexerRuleName = nestedLexerRule.getName();
			if ("COMPASS_PT".equals(nestedLexerRuleName)) {
				nestedLexerRuleName = "STRING";
			}
			return ID.fromString(string, Type.valueOf(nestedLexerRuleName));
		}
	}
	throw new IllegalArgumentException("Invalid ID string " + string);
}
 
Example 4
Source File: DotArrowTypeSemanticHighlightingCalculator.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void doProvideHighlightingFor(XtextResource resource,
		IHighlightedPositionAcceptor acceptor,
		CancelIndicator cancelIndicator) {

	// It gets a node model.
	INode root = resource.getParseResult().getRootNode();
	for (INode node : root.getAsTreeIterable()) {
		EObject grammarElement = node.getGrammarElement();
		if (grammarElement instanceof RuleCall) {
			RuleCall rc = (RuleCall) grammarElement;
			AbstractRule r = rc.getRule();
			String ruleName = r.getName();
			switch (ruleName) {
			case "DeprecatedShape": //$NON-NLS-1$
				acceptor.addPosition(node.getOffset(), node.getLength(),
						DotHighlightingConfiguration.DEPRECATED_ATTRIBUTE_VALUE);
				break;
			}
		}
	}
}
 
Example 5
Source File: AssertStructureAcceptor.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected void assertElement(AbstractElement element) {
	AbstractRule expectedRule = null;
	if (stack.isEmpty()) {
		// FIXME: this doesn't work if the serialized EObject is not the model's root. 
		// expectedRule = EcoreUtil2.getContainerOfType(element, Grammar.class).getRules().get(0);
		return;
	} else
		expectedRule = stack.peek().getRule();
	AbstractRule actualRule = EcoreUtil2.getContainerOfType(element, AbstractRule.class);
	if (expectedRule != actualRule) {
		GrammarElementTitleSwitch formatter = new GrammarElementTitleSwitch().showQualified().showRule();
		String elementName = formatter.apply(element);
		String expName = expectedRule.getName();
		String actualName = actualRule.getName();
		String msg = "Element " + elementName + " should be in rule " + expName + " but it is in " + actualName;
		throw new IllegalStateException(msg);
	}
}
 
Example 6
Source File: XtextHyperlinkHelper.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected void createLinksToBase(ITextRegion nameLocation,	AbstractRule rule, IHyperlinkAcceptor acceptor) {
	Set<AbstractRule> visited = Sets.newHashSet();
	Grammar grammar = GrammarUtil.getGrammar(rule);
	for(Grammar used: grammar.getUsedGrammars()) {
		String ruleName = rule.getName();
		AbstractRule overwritten = GrammarUtil.findRuleForName(used, ruleName);
		if (overwritten != null && visited.add(overwritten)) {
			URIConverter uriConverter = rule.eResource().getResourceSet().getURIConverter();
			String hyperlinkText = getLabelProvider().getText(rule) + " - " + GrammarUtil.getGrammar(overwritten).getName();
			URI uri = uriConverter.normalize(EcoreUtil.getURI(overwritten));

			XtextHyperlink result = getHyperlinkProvider().get();
			deprecatedSetRegion(result, toRegion(nameLocation));
			result.setURI(uri);
			result.setHyperlinkText(hyperlinkText);
			result.setTypeLabel("Go To Base");
			acceptor.accept(result);
		}
	}
}
 
Example 7
Source File: AbstractJavaBasedContentProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void completeRuleCall(RuleCall ruleCall, ContentAssistContext contentAssistContext,
		ICompletionProposalAcceptor acceptor) {
	AbstractRule calledRule = ruleCall.getRule();
	String methodName = "complete_" + calledRule.getName();
	invokeMethod(methodName, acceptor, contentAssistContext.getCurrentModel(), ruleCall, contentAssistContext);
}
 
Example 8
Source File: DefaultRenameStrategy.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected String getNameRuleName(EObject targetElement, EAttribute nameAttribute) {
	List<INode> nameNodes = NodeModelUtils.findNodesForFeature(targetElement, nameAttribute);
	if(nameNodes.size() == 1) {
		EObject grammarElement = nameNodes.get(0).getGrammarElement();
		if(grammarElement instanceof RuleCall) {
			AbstractRule nameRule = ((RuleCall) grammarElement).getRule();
			if(nameRule != null)
				return nameRule.getName();
		}
	}
	return null;
}
 
Example 9
Source File: GrammarUtilTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testAllRules() throws Exception {
  this.with(XtextStandaloneSetup.class);
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("grammar myLang with org.eclipse.xtext.common.Terminals");
  _builder.newLine();
  _builder.append("generate g \'http://1\'");
  _builder.newLine();
  _builder.append("Rule:");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("name=super::STRING;");
  _builder.newLine();
  _builder.append("terminal STRING: \'\"\';");
  _builder.newLine();
  String model = _builder.toString();
  final XtextResource r = this.getResourceFromString(model);
  EObject _get = r.getContents().get(0);
  final Grammar grammar = ((Grammar) _get);
  final List<AbstractRule> allRules = GrammarUtil.allRules(grammar);
  final Function1<AbstractRule, String> _function = (AbstractRule it) -> {
    return it.getName();
  };
  Assert.assertEquals(
    Collections.<String>unmodifiableList(CollectionLiterals.<String>newArrayList("Rule", "STRING", "ID", "INT", "STRING", "ML_COMMENT", "SL_COMMENT", "WS", "ANY_OTHER")).toString(), 
    ListExtensions.<AbstractRule, String>map(allRules, _function).toString());
}
 
Example 10
Source File: AntlrGrammarGenUtil.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns the effective rule name for the generated Antlr grammar.
 * Inherited rules may be prefixed by {@code super[0..9]*}. Otherwise the
 * prefix {@code rule or RULE_} is used.
 * @since 2.9
 */
public static String getRuleName(AbstractRule rule) {
	RuleWithParameterValues parameterValues = RuleWithParameterValues.findInEmfObject(rule);
	if (parameterValues != null) {
		return rule.getName();
	}
	RuleNames ruleNames = RuleNames.getRuleNames(rule);
	return ruleNames.getAntlrRuleName(rule);
}
 
Example 11
Source File: ElementMatcherToDot.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected Node newNode(EObject semanticObject, String label) {
	if (semanticObject.eContainer() instanceof AbstractRule) {
		AbstractRule rule = (AbstractRule) semanticObject.eContainer();
		return new Node(semanticObject, rule.getName() + ":\\n" + label, "record");
	} else {
		return new Node(semanticObject, label);
	}
}
 
Example 12
Source File: GrammarElementTitleSwitch.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public String caseAbstractRule(AbstractRule object) {
	String classifier = object.getType().getClassifier().getName();
	if (object.getName().equals(classifier))
		return object.getName() + ":";
	return object.getName() + " returns " + classifier + ":";
}
 
Example 13
Source File: RuleNames.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private String getDefaultAntlrRuleName(AbstractRule rule) {
	if (rule instanceof ParserRule || rule instanceof EnumRule) {
		return "rule" + rule.getName();
	}
	if (rule instanceof TerminalRule) {
		return "RULE_" + rule.getName().toUpperCase();
	}
	throw new IllegalArgumentException(rule.eClass().getName());
}
 
Example 14
Source File: ContextTypeIdHelper.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public String getId(AbstractRule rule) {
	if (!(rule instanceof ParserRule))
		throw new IllegalArgumentException("Unsupported context rule: " + rule);
	return GrammarUtil.getGrammar(rule).getName()+"."+rule.getName();
}
 
Example 15
Source File: TextRegionAccessToString.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected String toString(AbstractRule rule) {
	return rule == null ? "null" : rule.getName();
}
 
Example 16
Source File: ContentAssistFragment2.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
private String getFQFeatureName(final AbstractRule r) {
  String _name = r.getName();
  return ("_" + _name);
}
 
Example 17
Source File: GrammarAccessFragment2.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected String getQualifiedName(final AbstractRule rule) {
  String _name = GrammarUtil.getGrammar(rule).getName();
  String _plus = (_name + ".");
  String _name_1 = rule.getName();
  return (_plus + _name_1);
}
 
Example 18
Source File: XtextFragmentProvider.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public String caseAbstractRule(AbstractRule obj, IFragmentProvider.Fallback fallback) {
	return getFragment(obj.eContainer(), fallback) + "/" + obj.getName();
}
 
Example 19
Source File: KeywordAlternativeConverter.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @since 2.9
 */
protected IllegalArgumentException mismatchedRuleBody(AbstractRule rule) {
	return new IllegalArgumentException(
			rule.getName() + "'s body does not contain an alternative of keywords and a single rule call");
}
 
Example 20
Source File: RuleNames.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public String getQualifiedName(AbstractRule rule) {
	return GrammarUtil.getGrammar(rule).getName() + "." + rule.getName();
}