Java Code Examples for org.eclipse.xtext.Grammar#getRules()

The following examples show how to use org.eclipse.xtext.Grammar#getRules() . 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
private void collectRules(Grammar grammar, Multimap<String, AbstractRule> result, Set<Grammar> visited,
		String name, Set<String> validNames) {
	if (!visited.add(grammar))
		return;

	List<String> allNames = new ArrayList<String>();
	for (AbstractRule rule : grammar.getRules()) {
		if (!validNames.contains(rule.getName())) {
			allNames.add(rule.getName());
			if (rule.getName().equalsIgnoreCase(name))
				result.put(rule.getName(), rule);
		}
	}
	validNames.addAll(allNames);
	for (Grammar usedGrammar : grammar.getUsedGrammars()) {
		collectRules(usedGrammar, result, visited, name, validNames);
	}
}
 
Example 2
protected GrammarElementDeclarationOrder(Grammar grammar) {
	elementIDCache = Maps.newHashMap();
	List<Grammar> grammars = Lists.newArrayList(grammar);
	grammars.addAll(GrammarUtil.allUsedGrammars(grammar));
	int counter = 0;
	for (Grammar g : grammars) {
		elementIDCache.put(g, counter++);
		for (AbstractRule rule : g.getRules()) {
			elementIDCache.put(rule, counter++);
			TreeIterator<EObject> iterator = rule.eAllContents();
			while (iterator.hasNext()) {
				elementIDCache.put(iterator.next(), counter++);
			}
		}
	}
}
 
Example 3
@Test
public void testBug_266440() throws Exception {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("grammar test with org.eclipse.xtext.common.Terminals import \'http://www.eclipse.org/emf/2002/Ecore\' as ecore  generate bugreport \'http://bugreport/266440\'");
  _builder.newLine();
  _builder.append("CompositeModel: (type+=EClassifier)+;");
  _builder.newLine();
  _builder.append("EClassifier: EDataType | EClass;");
  _builder.newLine();
  _builder.append("EClass: \'class\' name=ID;");
  _builder.newLine();
  _builder.append("EDataType: \'dt\' name=ID;");
  String grammar = _builder.toString();
  final XtextResource resource = this.getResourceFromString(grammar);
  Assert.assertTrue(resource.getErrors().isEmpty());
  EObject _head = IterableExtensions.<EObject>head(resource.getContents());
  final Grammar parsedGrammar = ((Grammar) _head);
  EList<AbstractRule> _rules = parsedGrammar.getRules();
  for (final AbstractRule rule : _rules) {
    {
      final EClassifier classifier = rule.getType().getClassifier();
      EPackage pack = classifier.getEPackage();
      Assert.assertEquals("bugreport", pack.getName());
    }
  }
}
 
Example 4
protected Map<String, AbstractRule> collectExistingRules(final Grammar grammar) {
	Map<String, AbstractRule> existingRules = Maps.newHashMap();
	for (AbstractRule rule : grammar.getRules()) {
		existingRules.put(rule.getName(), rule);
	}
	return existingRules;
}
 
Example 5
protected void findOverriddenRule(AbstractRule originalRule, List<Grammar> grammars,
		IAcceptor<AbstractRule> acceptor) {
	for (Grammar grammar : grammars) {
		for (AbstractRule rule : grammar.getRules()) {
			if (rule.eClass() == originalRule.eClass() && Strings.equal(rule.getName(), originalRule.getName())) {
				acceptor.accept(rule);
			}
		}
		findOverriddenRule(originalRule, grammar.getUsedGrammars(), acceptor);
	}
}
 
Example 6
@Override
public Boolean caseGrammar(Grammar grammar) {
	UsedRulesFinder usedRulesFinder = new UsedRulesFinder(usedRules);
	usedRulesFinder.compute(grammar);
	this.inspectedGrammar = grammar;
	for(AbstractRule rule: grammar.getRules()) {
		if (!validatedRules.contains(rule) && usedRules.contains(rule)) {
			doSwitch(rule);
		}
	}
	return Boolean.FALSE;
}
 
Example 7
Source Project: xtext-core   File: XtextLinker.java    License: Eclipse Public License 2.0 5 votes vote down vote up
protected void updateOverriddenRules(Grammar grammar) {
	if (grammar.getUsedGrammars().isEmpty())
		return;
	Map<String, AbstractRule> rulePerName = new HashMap<String, AbstractRule>(grammar.getRules().size());
	for (AbstractRule rule : grammar.getRules())
		rulePerName.put(rule.getName(), rule);
	Set<Grammar> visitedGrammars = new HashSet<Grammar>();
	visitedGrammars.add(grammar);
	for (Grammar usedGrammar : grammar.getUsedGrammars()) {
		updateOverriddenRules(usedGrammar, rulePerName, visitedGrammars);
	}
}
 
Example 8
private boolean containsAnyParserRule(Grammar g, Set<Grammar> visited) {
	if (!visited.add(g))
		return false;
	for (AbstractRule rule : g.getRules()) {
		if (rule instanceof ParserRule && !GrammarUtil.isDatatypeRule((ParserRule) rule))
			return true;
	}
	for (Grammar used : g.getUsedGrammars()) {
		if (containsAnyParserRule(used, visited))
			return true;
	}
	return false;
}
 
Example 9
public void inspect(Grammar grammar) {
	this.inspectedGrammar = grammar;
	for(AbstractRule rule: grammar.getRules()) {
		if (!validatedRules.contains(rule)) {
			// different entry points yield different error messages
			Set<AbstractRule> storedRules = validatedRules;
			validatedRules = Sets.newHashSet();
			doSwitch(rule);
			storedRules.addAll(validatedRules);
			storedRules = validatedRules;
		}
	}
}
 
Example 10
@Test public void testSelf() {
	Grammar g = getGrammarAccess().getGrammar();
	for (AbstractRule r : g.getRules()) {
		// AbstractRule r = GrammarUtil.findRuleForName(g, "GrammarID");
		// System.out.println("serializing :" + r.getName());
		String s = serialize(r);
		assertNotNull(s, r.getName());
		// System.out.println(s);
	}
}
 
Example 11
@Test
public void testBug310122() throws Exception {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("grammar test with org.eclipse.xtext.common.Terminals");
  _builder.newLine();
  _builder.append("generate myDsl \"http://example.xtext.org/MyDsl\"");
  _builder.newLine();
  _builder.append("Model: Sub1 | name=ID \'somekeyword\';");
  _builder.newLine();
  _builder.append("Sub1 returns Model: \'(\' Model \')\';");
  _builder.newLine();
  final String grammarAsString = _builder.toString();
  final XtextResource resource = this.getResourceFromString(grammarAsString);
  EObject _head = IterableExtensions.<EObject>head(resource.getContents());
  final Grammar grammar = ((Grammar) _head);
  AbstractMetamodelDeclaration _head_1 = IterableExtensions.<AbstractMetamodelDeclaration>head(grammar.getMetamodelDeclarations());
  GeneratedMetamodel generatedMetamodel = ((GeneratedMetamodel) _head_1);
  Assert.assertEquals("myDsl", generatedMetamodel.getName());
  Assert.assertEquals(1, generatedMetamodel.getEPackage().getEClassifiers().size());
  EClassifier _type = this.<EClassifier>type(generatedMetamodel.getEPackage(), "Model");
  EClass createdModel = ((EClass) _type);
  Assert.assertEquals(EcorePackage.Literals.ESTRING, this.<EStructuralFeature>feature(createdModel, "name").getEType());
  EList<AbstractRule> _rules = grammar.getRules();
  for (final AbstractRule rule : _rules) {
    Assert.assertEquals(createdModel, rule.getType().getClassifier());
  }
}
 
Example 12
/** Explore the grammar for extracting the key elements.
 *
 * @param grammar the grammar to explore.
 * @param expressionKeywords the SARL keywords, usually within expressions.
 * @param modifiers the modifier keywords.
 * @param primitiveTypes the primitive types.
 * @param punctuation the set of detected punctuation symbols.
 * @param literals the set of detected literals.
 * @param excludedKeywords the set of given excluded keywords.
 * @param ignored the set of ignored tokens that is filled by this function.
 */
@SuppressWarnings("checkstyle:nestedifdepth")
private static void exploreGrammar(Grammar grammar, Set<String> expressionKeywords,
		Set<String> modifiers, Set<String> primitiveTypes, Set<String> punctuation,
		Set<String> literals, Set<String> excludedKeywords, Set<String> ignored) {
	for (final AbstractRule rule : grammar.getRules()) {
		final boolean isModifierRule = MODIFIER_RULE_PATTERN.matcher(rule.getName()).matches();
		final TreeIterator<EObject> iterator = rule.eAllContents();
		while (iterator.hasNext()) {
			final EObject object = iterator.next();
			if (object instanceof Keyword) {
				final Keyword xkeyword = (Keyword) object;
				final String value = xkeyword.getValue();
				if (!Strings.isEmpty(value)) {
					if (KEYWORD_PATTERN.matcher(value).matches()) {
						if (!literals.contains(value) && !primitiveTypes.contains(value)) {
							if (excludedKeywords.contains(value)) {
								ignored.add(value);
							} else {
								if (isModifierRule) {
									modifiers.add(value);
								} else {
									expressionKeywords.add(value);
								}
							}
						}
					} else if (PUNCTUATION_PATTERN.matcher(value).matches()) {
						punctuation.add(value);
					}
				}
			}
		}
	}
}
 
Example 13
protected ParserRule getFirstParserRule(Grammar grammar) {
	for (AbstractRule r : grammar.getRules())
		if (isParserRule(r))
			return (ParserRule) r;
	throw new RuntimeException("Grammar " + grammar.getName() + " contains no parser rules");
}
 
Example 14
public void caseGrammar(Grammar object) {
	for(AbstractRule rule: object.getRules())
		unload(rule);
	unload(object);
}