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

The following examples show how to use org.eclipse.xtext.GrammarUtil#containedAssignments() . 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: GrammarTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private AbstractRule checkConcreteImplRule(Grammar grammar, String ruleName) {
	AbstractRule concreteRule = GrammarUtil.findRuleForName(grammar, ruleName);
	assertNotNull(concreteRule);
	EClassifier returnType = ((ParserRule)concreteRule).getType().getClassifier();
	String returnTypeName = getClassifierName(returnType);
	assertEquals(ruleName, returnTypeName + "_Impl");
	List<Assignment> assignments = GrammarUtil.containedAssignments(concreteRule);
	assertEquals(1, assignments.size());
	assertEquals("name", assignments.get(0).getFeature());
	assertEquals("=", assignments.get(0).getOperator());
	List<Action> containedActions = GrammarUtil.containedActions(concreteRule);
	assertEquals(1, containedActions.size());
	assertEquals(returnTypeName, getClassifierName(containedActions.get(0).getType().getClassifier()));
	List<Keyword> containedKeywords = GrammarUtil.containedKeywords(concreteRule);
	assertEquals(1, containedKeywords.size());
	assertEquals(returnTypeName, containedKeywords.get(0).getValue());
	return concreteRule;
}
 
Example 2
Source File: FollowElementComputer.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
public void collectAbstractElements(Grammar grammar, EStructuralFeature feature, IFollowElementAcceptor followElementAcceptor) {
	for (Grammar superGrammar : grammar.getUsedGrammars()) {
		collectAbstractElements(superGrammar, feature, followElementAcceptor);
	}
	EClass declarator = feature.getEContainingClass();
	for (ParserRule rule : GrammarUtil.allParserRules(grammar)) {
		for (Assignment assignment : GrammarUtil.containedAssignments(rule)) {
			if (assignment.getFeature().equals(feature.getName())) {
				EClassifier classifier = GrammarUtil.findCurrentType(assignment);
				EClassifier compType = EcoreUtil2.getCompatibleType(declarator, classifier);
				if (compType == declarator) {
					followElementAcceptor.accept(assignment);
				}
			}
		}
	}
}
 
Example 3
Source File: GrammarTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testExampleGrammar() throws Exception {
	with(Ecore2XtextTestStandaloneSetup.class);
	Grammar grammar = getGrammarAccess().getGrammar();
	EList<AbstractMetamodelDeclaration> metamodelDeclarations = grammar.getMetamodelDeclarations();
	assertEquals(2, metamodelDeclarations.size());
	
	AbstractRule intDatatypeRule = GrammarUtil.findRuleForName(grammar, "INT0");
	assertNotNull(intDatatypeRule);

	AbstractRule concrete0Rule = GrammarUtil.findRuleForName(grammar, "Concrete0");
	assertNotNull(concrete0Rule);
	
	AbstractRule abstractRule = GrammarUtil.findRuleForName(grammar, "Abstract");
	AbstractElement alternatives = abstractRule.getAlternatives();
	assertTrue(alternatives instanceof Alternatives);
	assertEquals(3, ((Alternatives) alternatives).getElements().size());
	assertTrue(GrammarUtil.containedAssignments(abstractRule).isEmpty());
	
	checkConcreteImplRule(grammar, "Concrete0_Impl");
	checkConcreteImplRule(grammar, "Concrete1_Impl");
	
	ParserRule rootRule = (ParserRule) grammar.getRules().get(0);
	assertEquals("Root", rootRule.getName());
	List<Assignment> assignments = GrammarUtil.containedAssignments(rootRule);
	assertEquals(4, assignments.size());
	assertEquals("name", assignments.get(0).getFeature());
	assertEquals("classes", assignments.get(1).getFeature());
	assertEquals("+=", assignments.get(1).getOperator());
	assertEquals("classes", assignments.get(2).getFeature());
	assertEquals("+=", assignments.get(2).getOperator());
	assertEquals("concrete0", assignments.get(3).getFeature());
	assertEquals("=", assignments.get(3).getOperator());
}
 
Example 4
Source File: AbstractSubCodeBuilderFragment.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies the getter function for accessing to the top element collection of the script.
 *
 * @return the name of the getter function.
 */
@Pure
protected String getLanguageScriptMemberGetter() {
	final Grammar grammar = getGrammar();
	final AbstractRule scriptRule = GrammarUtil.findRuleForName(grammar, getCodeBuilderConfig().getScriptRuleName());
	for (final Assignment assignment : GrammarUtil.containedAssignments(scriptRule)) {
		if ((assignment.getTerminal() instanceof RuleCall)
				&& Objects.equals(((RuleCall) assignment.getTerminal()).getRule().getName(),
				getCodeBuilderConfig().getTopElementRuleName())) {
			return "get" + Strings.toFirstUpper(assignment.getFeature()); //$NON-NLS-1$
		}
	}
	throw new IllegalStateException("member not found"); //$NON-NLS-1$
}
 
Example 5
Source File: AbstractSubCodeBuilderFragment.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies the rule used for defining the members of the given element.
 *
 * @param description description of the container.
 * @return the rule that is defining the members.
 */
protected AbstractRule getMemberRule(CodeElementExtractor.ElementDescription description) {
	for (final Assignment assignment : GrammarUtil.containedAssignments(description.getGrammarComponent())) {
		if (Objects.equals(getCodeBuilderConfig().getMemberCollectionExtensionGrammarName(), assignment.getFeature())) {
			if (assignment.getTerminal() instanceof RuleCall) {
				return ((RuleCall) assignment.getTerminal()).getRule();
			}
		}
	}
	return null;
}