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

The following examples show how to use org.eclipse.xtext.GrammarUtil#allRules() . 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: ParseTreeConstructorUtil.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public static List<AbstractElement> calcRootFollowers(Grammar g) {
	List<AbstractElement> callees = new ArrayList<AbstractElement>();
	for (AbstractRule r : GrammarUtil.allRules(g))
		if (r instanceof ParserRule) {
			ParserRule pr = (ParserRule) r;
			if (!GrammarUtil.isDatatypeRule(pr) && !pr.isFragment())
				callees.add(pr.getAlternatives());
		}
	return callees;
}
 
Example 2
Source File: AbstractInternalAntlrParser.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.11
 */
protected Map<String, AbstractRule> createAllRules(Grammar grammar) {
	AllRulesCache cache = AllRulesCache.findInEmfObject(grammar);
	if (cache != null) {
		return cache.getAllRules();
	}
	Map<String, AbstractRule> allRules = Maps.newHashMap();
	for (AbstractRule rule: GrammarUtil.allRules(grammar)) {
		if(rule instanceof TerminalRule)
			allRules.put(rule.getName().toUpperCase(), rule);
		else
			allRules.put(rule.getName(), rule);
	}
	return allRules;
}
 
Example 3
Source File: AllRulesCache.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public AllRulesCache(Grammar grammar) {
	allRulesCache = Maps.newHashMap();
	List<AbstractRule> allRules = GrammarUtil.allRules(grammar);
	for (AbstractRule rule : allRules) {
		if (rule instanceof TerminalRule) {
			allRulesCache.put(rule.getName().toUpperCase(), rule);
		} else {
			allRulesCache.put(rule.getName(), rule);
		}
	}
}
 
Example 4
Source File: GrammarAccessExtensions.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public boolean isCalled(final AbstractRule rule, final Grammar grammar) {
  boolean _xblockexpression = false;
  {
    final List<AbstractRule> allRules = GrammarUtil.allRules(grammar);
    _xblockexpression = ((allRules.indexOf(rule) == 0) || IterableExtensions.<RuleCall>exists(Iterables.<RuleCall>concat(ListExtensions.<AbstractRule, List<RuleCall>>map(allRules, ((Function1<AbstractRule, List<RuleCall>>) (AbstractRule it) -> {
      return GrammarUtil.containedRuleCalls(it);
    }))), ((Function1<RuleCall, Boolean>) (RuleCall ruleCall) -> {
      AbstractRule _rule = ruleCall.getRule();
      return Boolean.valueOf(Objects.equal(_rule, rule));
    })));
  }
  return _xblockexpression;
}
 
Example 5
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 6
Source File: GrammarKeywordAccessFragment2.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Returns all grammars from the hierarchy that are used from rules of this grammar.
 *
 * @param grammar the grammar.
 * @return the used grammars.
 */
protected static List<Grammar> getEffectivelyUsedGrammars(final Grammar grammar) {
	final List<AbstractRule> allRules = GrammarUtil.allRules(grammar);
	final List<Grammar> map = ListExtensions.<AbstractRule, Grammar>map(allRules, it -> GrammarUtil.getGrammar(it));
	final Iterable<Grammar> filter = IterableExtensions.<Grammar>filter(map, it -> Boolean.valueOf(it != grammar));
	final Set<Grammar> set = IterableExtensions.<Grammar>toSet(filter);
	return IterableExtensions.<Grammar>toList(set);
}
 
Example 7
Source File: RuleFilter.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public List<AbstractRule> getRules(Grammar grammar) {
	return GrammarUtil.allRules(grammar);
}
 
Example 8
Source File: RuleNames.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public RuleNames(Grammar grammar, boolean installAdapter) {
	this.contextGrammar = grammar;
	Adapter adapter = new Adapter(this);
	if (installAdapter) {
		installAdapterIfMissing(adapter, grammar);
	}
	List<AbstractRule> allRules = GrammarUtil.allRules(grammar);
	ImmutableListMultimap.Builder<String, AbstractRule> simpleNameToRulesBuilder = ImmutableListMultimap.builder();
	ImmutableMap.Builder<String, AbstractRule> qualifiedNameToRuleBuilder = ImmutableMap.builder();
	ImmutableBiMap.Builder<String, AbstractRule> uniqueNameToRuleBuilder = ImmutableBiMap.builder();
	ImmutableBiMap.Builder<String, AbstractRule> antlrNameToRuleBuilder = ImmutableBiMap.builder();
	
	Map<String, AbstractRule> names = Maps.newHashMap();
	Set<String> usedAntlrNames = Sets.newHashSet();
	Set<String> usedUniqueNames = Sets.newHashSet();
	for(AbstractRule rule: allRules) {
		String name = rule.getName();
		simpleNameToRulesBuilder.put(name, rule);
		String qualifiedName = getQualifiedName(rule);
		qualifiedNameToRuleBuilder.put(qualifiedName, rule);
		String uniqueName = name;
		String antlrRuleName;
		if (names.containsKey(name)) {
			name = qualifiedName;
			uniqueName = getInheritedUniqueName(rule, usedUniqueNames);
			antlrRuleName = getInheritedAntlrRuleName(rule, usedAntlrNames);
		} else {
			antlrRuleName = getDefaultAntlrRuleName(rule);
		}
		names.put(name, rule);
		if (!usedUniqueNames.add(uniqueName)) {
			throw new IllegalStateException(uniqueName);
		}
		uniqueNameToRuleBuilder.put(uniqueName, rule);
		if (!usedAntlrNames.add(antlrRuleName)) {
			throw new IllegalStateException(antlrRuleName);
		}
		antlrNameToRuleBuilder.put(antlrRuleName, rule);
		if (installAdapter) {
			installAdapterIfMissing(adapter, rule);
		}
	}
	simpleNameToRules = simpleNameToRulesBuilder.build();
	qualifiedNameToRule = qualifiedNameToRuleBuilder.build();
	nameToRule = ImmutableBiMap.copyOf(names);
	uniqueNameToRule = uniqueNameToRuleBuilder.build();
	antlrNameToRule = antlrNameToRuleBuilder.build();
	this.allRules = ImmutableList.copyOf(allRules);
}
 
Example 9
Source File: AbstractElementFinder.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected Iterable<? extends AbstractRule> getRules() {
	return GrammarUtil.allRules(getGrammar());
}