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

The following examples show how to use org.eclipse.xtext.AbstractRule#eAllContents() . 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
protected boolean ruleContainsAssignedAction(AbstractRule rule, Set<AbstractRule> visited) {
	if (!visited.add(rule))
		return false;
	TreeIterator<EObject> i = rule.eAllContents();
	while (i.hasNext()) {
		EObject o = i.next();
		if (o instanceof Action && ((Action) o).getFeature() != null)
			return true;
		else if (o instanceof Assignment)
			i.prune();
		else if (o instanceof RuleCall && isParserRule(((RuleCall) o).getRule())) {
			if (ruleContainsAssignedAction(((RuleCall) o).getRule(), visited))
				return true;
		}
	}
	return false;
}
 
Example 2
@SuppressWarnings("unchecked")
protected <T> List<T> findByNestedRuleCall(Class<T> clazz, AbstractRule... rule) {
	Set<AbstractRule> rls = new HashSet<AbstractRule>(Arrays.asList(rule));
	ArrayList<T> r = new ArrayList<T>();
	for (AbstractRule ar : getRules()) {
		TreeIterator<EObject> i = ar.eAllContents();
		while (i.hasNext()) {
			EObject o = i.next();
			if (clazz.isInstance(o)) {
				TreeIterator<EObject> ct = o.eAllContents();
				while (ct.hasNext()) {
					EObject cto = ct.next();
					if (cto instanceof RuleCall && rls.contains(((RuleCall) cto).getRule())) {
						r.add((T) o);
						break;
					}
				}
				i.prune();
			}
		}
	}
	return r;
}
 
Example 3
public List<CrossReference> findCrossReferences(EClassifier... targetEClassifiers) {
	Set<EClassifier> classifiers = new HashSet<EClassifier>(Arrays.asList(targetEClassifiers));
	Collection<EClass> classes = Lists.newArrayList(Iterables.filter(classifiers, EClass.class));
	ArrayList<CrossReference> r = new ArrayList<CrossReference>();
	for (AbstractRule ar : getRules()) {
		TreeIterator<EObject> i = ar.eAllContents();
		while (i.hasNext()) {
			EObject o = i.next();
			if (o instanceof CrossReference) {
				CrossReference c = (CrossReference) o;
				if (classifiers.contains(c.getType().getClassifier()))
					r.add(c);
				else if (c.getType().getClassifier() instanceof EClass)
					for (EClass cls : classes)
						if (EcoreUtil2.isAssignableFrom(cls,(EClass) c.getType().getClassifier())) {
							r.add(c);
							break;
						}
				i.prune();
			}
		}
	}
	return r;

}
 
Example 4
public List<Pair<Keyword, Keyword>> findKeywordPairs(String leftKw, String rightKw) {
	ArrayList<Pair<Keyword, Keyword>> pairs = new ArrayList<Pair<Keyword, Keyword>>();
	for (AbstractRule ar : getRules())
		if (ar instanceof ParserRule && !GrammarUtil.isDatatypeRule((ParserRule) ar)) {
			Stack<Keyword> openings = new Stack<Keyword>();
			TreeIterator<EObject> i = ar.eAllContents();
			while (i.hasNext()) {
				EObject o = i.next();
				if (o instanceof Keyword) {
					Keyword k = (Keyword) o;
					if (leftKw.equals(k.getValue()))
						openings.push(k);
					else if (rightKw.equals(k.getValue())) {
						if (openings.size() > 0)
							pairs.add(Tuples.create(openings.pop(), k));
					}
				}
			}
		}
	return pairs;
}
 
Example 5
public List<Keyword> findKeywords(String... keywords) {
	Set<String> kwds = new HashSet<String>(Arrays.asList(keywords));
	ArrayList<Keyword> r = new ArrayList<Keyword>();
	for (AbstractRule ar : getRules()) {
		TreeIterator<EObject> i = ar.eAllContents();
		while (i.hasNext()) {
			EObject o = i.next();
			if (o instanceof Keyword) {
				Keyword k = (Keyword) o;
				if (kwds.contains(k.getValue()))
					r.add(k);
			}
		}
	}
	return r;
}
 
Example 6
public List<RuleCall> findRuleCalls(AbstractRule... rules) {
	Set<AbstractRule> rls = new HashSet<AbstractRule>(Arrays.asList(rules));
	ArrayList<RuleCall> r = new ArrayList<RuleCall>();
	for (AbstractRule ar : getRules()) {
		TreeIterator<EObject> i = ar.eAllContents();
		while (i.hasNext()) {
			EObject o = i.next();
			if (o instanceof RuleCall) {
				RuleCall c = (RuleCall) o;
				if (rls.contains(c.getRule()))
					r.add(c);
			}
		}
	}
	return r;
}
 
Example 7
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 8
protected Set<MatcherState> findRuleCallsTo(AbstractRule rule, Set<AbstractRule> visited) {
	if (!visited.add(rule))
		return Collections.emptySet();
	Set<MatcherState> result = Sets.newHashSet();
	Iterator<EObject> i = rule.eAllContents();
	while (i.hasNext()) {
		EObject obj = i.next();
		if (obj instanceof AbstractElement) {
			MatcherState state = nfaProvider.getNFA((AbstractElement) obj);
			if (state.hasTransitions())
				for (MatcherTransition incoming : state.getAllIncoming())
					if (incoming.isRuleCall() && result.add(incoming.getSource())
							&& incoming.getSource().isEndState())
						result.addAll(findRuleCallsTo(
								GrammarUtil.containingRule(incoming.getSource().getGrammarElement()), visited));
		}
	}
	return result;
}
 
Example 9
protected void collectKeywordsFromRule(IGrammarAccess grammarAccess, String ruleName, ImmutableSet.Builder<Keyword> builder) {
	AbstractRule rule = GrammarUtil.findRuleForName(grammarAccess.getGrammar(), ruleName);
	if (!(rule instanceof TerminalRule)) { // if someone decides to override ValidID with a terminal rule
		Iterator<EObject> i = rule.eAllContents();
		while (i.hasNext()) {
			EObject o = i.next();
			if (o instanceof Keyword) {
				builder.add((Keyword) o);
			}
		}
	}
}
 
Example 10
protected boolean ruleContainsRecursiveUnassignedRuleCall(AbstractRule rule, Set<AbstractRule> visited) {
	if (!visited.add(rule))
		return true;
	TreeIterator<EObject> i = rule.eAllContents();
	while (i.hasNext()) {
		EObject o = i.next();
		if (o instanceof Assignment)
			i.prune();
		else if (o instanceof RuleCall && isParserRule(((RuleCall) o).getRule())) {
			if (ruleContainsRecursiveUnassignedRuleCall(((RuleCall) o).getRule(), visited))
				return true;
		}
	}
	return false;
}
 
Example 11
/** 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);
					}
				}
			}
		}
	}
}