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

The following examples show how to use org.eclipse.xtext.GrammarUtil#allParserRules() . 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: TokenTypeRewriter.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
private static void rewriteTypeReferences(N4JSGrammarAccess ga,
		ImmutableMap.Builder<AbstractElement, Integer> builder) {
	for (ParserRule rule : GrammarUtil.allParserRules(ga.getGrammar())) {
		for (EObject obj : EcoreUtil2.eAllContents(rule.getAlternatives())) {
			if (obj instanceof Assignment) {
				Assignment assignment = (Assignment) obj;
				AbstractElement terminal = assignment.getTerminal();
				if (terminal instanceof RuleCall) {
					AbstractRule calledRule = ((RuleCall) terminal).getRule();
					EClassifier classifier = calledRule.getType().getClassifier();
					if (classifier instanceof EClass
							&& TypeRefsPackage.Literals.TYPE_REF.isSuperTypeOf((EClass) classifier)) {
						builder.put(assignment, TYPE_REF_TOKEN);
					}
				}
			}
		}
	}
}
 
Example 2
Source File: TokenTypeRewriter.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
private static void rewriteIdentifiers(N4JSGrammarAccess ga,
		ImmutableMap.Builder<AbstractElement, Integer> builder) {
	ImmutableSet<AbstractRule> identifierRules = ImmutableSet.of(
			ga.getBindingIdentifierRule(),
			ga.getIdentifierNameRule(),
			ga.getIDENTIFIERRule());
	for (ParserRule rule : GrammarUtil.allParserRules(ga.getGrammar())) {
		for (EObject obj : EcoreUtil2.eAllContents(rule.getAlternatives())) {
			if (obj instanceof Assignment) {
				Assignment assignment = (Assignment) obj;
				AbstractElement terminal = assignment.getTerminal();
				int type = InternalN4JSParser.RULE_IDENTIFIER;
				if (terminal instanceof CrossReference) {
					terminal = ((CrossReference) terminal).getTerminal();
					type = IDENTIFIER_REF_TOKEN;
				}
				if (terminal instanceof RuleCall) {
					AbstractRule calledRule = ((RuleCall) terminal).getRule();
					if (identifierRules.contains(calledRule)) {
						builder.put(assignment, type);
					}
				}
			}
		}
	}
}
 
Example 3
Source File: TokenTypeRewriter.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
private static void rewriteNumberLiterals(N4JSGrammarAccess ga,
		ImmutableMap.Builder<AbstractElement, Integer> builder) {
	for (ParserRule rule : GrammarUtil.allParserRules(ga.getGrammar())) {
		for (EObject obj : EcoreUtil2.eAllContents(rule.getAlternatives())) {
			if (obj instanceof Assignment) {
				Assignment assignment = (Assignment) obj;
				AbstractElement terminal = assignment.getTerminal();
				if (terminal instanceof RuleCall) {
					AbstractRule calledRule = ((RuleCall) terminal).getRule();
					EClassifier classifier = calledRule.getType().getClassifier();
					if (classifier == EcorePackage.Literals.EBIG_DECIMAL) {
						builder.put(assignment, NUMBER_LITERAL_TOKEN);
					}
				}
			}
		}
	}
}
 
Example 4
Source File: JFlexGeneratorFragmentTemplate.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
public String tokens(final Grammar it) {
  ArrayList<String> allKeywords = CollectionLiterals.<String>newArrayList();
  List<ParserRule> _allParserRules = GrammarUtil.allParserRules(it);
  for (final ParserRule r : _allParserRules) {
    this.collectTokens(r, allKeywords);
  }
  List<EnumRule> _allEnumRules = GrammarUtil.allEnumRules(it);
  for (final EnumRule r_1 : _allEnumRules) {
    this.collectTokens(r_1, allKeywords);
  }
  StringConcatenation _builder = new StringConcatenation();
  {
    List<String> _sort = IterableExtensions.<String>sort(allKeywords);
    for(final String k : _sort) {
      CharSequence _state = this.state(k);
      _builder.append(_state);
      _builder.newLineIfNotEmpty();
    }
  }
  return _builder.toString();
}
 
Example 5
Source File: UnorderedGroupHelper.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Inject
public Collector(IGrammarAccess grammarAccess) {
	Grammar grammar = grammarAccess.getGrammar();
	List<ParserRule> parserRules = GrammarUtil.allParserRules(grammar);
	List<UnorderedGroup> groups = Lists.newArrayList();
	for(ParserRule rule: parserRules) {
		Iterator<EObject> iter = rule.eAllContents();
		while(iter.hasNext()) {
			EObject next = iter.next();
			if (next instanceof UnorderedGroup) {
				groups.add((UnorderedGroup) next);
			}
		}
	}
	this.groups = ImmutableList.copyOf(groups);
}
 
Example 6
Source File: GrammarPDAProvider.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public SerializationContextMap<Pda<ISerState, RuleCall>> getGrammarPDAs(Grammar grammar) {
	RuleNames names = RuleNames.getRuleNames(grammar, true);
	RuleFilter filter = new RuleFilter();
	filter.setDiscardTerminalRules(true);
	filter.setDiscardUnreachableRules(false);
	filter.setDiscardRuleTypeRef(false);
	Grammar flattened = new FlattenedGrammarAccess(names, filter).getFlattenedGrammar();
	Builder<Pda<ISerState, RuleCall>> result = SerializationContextMap.<Pda<ISerState, RuleCall>>builder();
	for (ParserRule rule : GrammarUtil.allParserRules(flattened)) {
		RuleWithParameterValues withParams = RuleWithParameterValues.findInEmfObject(rule);
		AbstractRule original = withParams.getOriginal();
		if (original instanceof ParserRule && isValidRule((ParserRule) original)) {
			ISerializationContext context = createContext((ParserRule) original, withParams.getParamValues());
			try {
				Pda<ISerState, RuleCall> pda = createPDA(grammar, rule);
				result.put(context, pda);
			} catch (Exception e) {
				LOG.error("Error creating PDA for context '" + context + "': " + e.getMessage(), e);
			}
		}
	}
	return result.create();
}
 
Example 7
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 8
Source File: AbstractAntlrGeneratorFragment.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.9
 */
protected boolean hasParameterizedRules(Grammar grammar) {
	for(ParserRule rule : GrammarUtil.allParserRules(grammar)) {
		if (!rule.getParameters().isEmpty()) {
			return true;
		}
	}
	return false;
}
 
Example 9
Source File: AbstractAntlrGeneratorFragment.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean containsUnorderedGroup(Grammar grammar) {
	for (ParserRule rule : GrammarUtil.allParserRules(grammar)) {
		if (Iterators.filter(rule.eAllContents(), UnorderedGroup.class).hasNext()) {
			return true;
		}
	}
	return false;
}
 
Example 10
Source File: XtendAntlrGrammarGeneratorHelper.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public LinkedHashSet<String> getTokens(final Grammar it) {
  final LinkedHashSet<String> tokens = CollectionLiterals.<String>newLinkedHashSet();
  List<ParserRule> _allParserRules = GrammarUtil.allParserRules(it);
  List<EnumRule> _allEnumRules = GrammarUtil.allEnumRules(it);
  Iterable<AbstractRule> _plus = Iterables.<AbstractRule>concat(_allParserRules, _allEnumRules);
  for (final AbstractRule rule : _plus) {
    this.collectTokens(rule, tokens);
  }
  return tokens;
}
 
Example 11
Source File: ContextPDAProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected Map<ParserRule, Integer> indexRules(Grammar grammar) {
	List<ParserRule> rules = GrammarUtil.allParserRules(grammar);
	Map<ParserRule, Integer> map = Maps.newHashMap();
	for (int i = 0; i < rules.size(); i++) {
		map.put(rules.get(i), i);
	}
	return map;
}
 
Example 12
Source File: KeywordHelper.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private BiMap<CharSequence, String> createKeywordMap(Grammar grammar) {
	List<ParserRule> parserRules = GrammarUtil.allParserRules(grammar);
	List<EnumRule> enumRules = GrammarUtil.allEnumRules(grammar);
	Iterator<EObject> iter = Iterators.concat(
			EcoreUtil.<EObject>getAllContents(parserRules), EcoreUtil.<EObject>getAllContents(enumRules));
	Iterator<Keyword> filtered = Iterators.filter(iter, Keyword.class);
	Iterator<String> transformed = Iterators.transform(filtered, new Function<Keyword, String>() {
		@Override
		public String apply(Keyword from) {
			return from.getValue();
		}
	});
	TreeSet<String> treeSet = Sets.newTreeSet(new Comparator<String>() {
		@Override
		public int compare(String o1, String o2) {
			if (o1.length() == o2.length())
				return o1.compareTo(o2);
			return Integer.valueOf(o1.length()).compareTo(Integer.valueOf(o2.length()));
		}
	});
	Iterators.addAll(treeSet, transformed);
	BiMap<CharSequence, String> result = HashBiMap.create();
	for(String s: treeSet) {
		CharSequence key = createKey(s);
		String readableName = toAntlrTokenIdentifier(s);
		if (result.containsValue(readableName)) {
			int i = 1;
			String next = readableName + "_" + i;
			while(result.containsValue(next)) {
				i++;
				next = readableName + "_" + i;
			}
			readableName = next;
		}
		result.put(key, readableName);
	}
	return result;
}
 
Example 13
Source File: AntlrContentAssistGrammarGenerator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected CharSequence compileRules(final Grammar g, final AntlrOptions options) {
  StringConcatenation _builder = new StringConcatenation();
  {
    List<ParserRule> _allParserRules = GrammarUtil.allParserRules(g);
    List<EnumRule> _allEnumRules = GrammarUtil.allEnumRules(g);
    Iterable<AbstractRule> _plus = Iterables.<AbstractRule>concat(_allParserRules, _allEnumRules);
    Collection<? extends AbstractElement> _allAlternatives = GrammarUtil.getAllAlternatives(g);
    Iterable<EObject> _plus_1 = Iterables.<EObject>concat(_plus, _allAlternatives);
    Collection<? extends AbstractElement> _allGroups = GrammarUtil.getAllGroups(g);
    Iterable<EObject> _plus_2 = Iterables.<EObject>concat(_plus_1, _allGroups);
    Collection<? extends AbstractElement> _allUnorderedGroups = GrammarUtil.getAllUnorderedGroups(g);
    Iterable<EObject> _plus_3 = Iterables.<EObject>concat(_plus_2, _allUnorderedGroups);
    Collection<? extends AbstractElement> _allAssignments = GrammarUtil.getAllAssignments(g);
    final Function1<EObject, Boolean> _function = (EObject it) -> {
      return Boolean.valueOf(this._grammarAccessExtensions.isCalled(GrammarUtil.containingRule(it), g));
    };
    Iterable<EObject> _filter = IterableExtensions.<EObject>filter(Iterables.<EObject>concat(_plus_3, _allAssignments), _function);
    for(final EObject rule : _filter) {
      _builder.newLine();
      CharSequence _compileRule = this.compileRule(rule, g, options);
      _builder.append(_compileRule);
      _builder.newLineIfNotEmpty();
    }
  }
  {
    boolean _isCombinedGrammar = this.isCombinedGrammar();
    if (_isCombinedGrammar) {
      CharSequence _compileTerminalRules = this.compileTerminalRules(g, options);
      _builder.append(_compileTerminalRules);
      _builder.newLineIfNotEmpty();
    }
  }
  return _builder;
}
 
Example 14
Source File: AbstractAntlrGeneratorFragment2.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean hasParameterizedRules(final Grammar grammar) {
  List<ParserRule> _allParserRules = GrammarUtil.allParserRules(grammar);
  for (final ParserRule rule : _allParserRules) {
    boolean _isEmpty = rule.getParameters().isEmpty();
    boolean _not = (!_isEmpty);
    if (_not) {
      return true;
    }
  }
  return false;
}
 
Example 15
Source File: AbstractAntlrGeneratorFragment2.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean containsUnorderedGroup(final Grammar grammar) {
  List<ParserRule> _allParserRules = GrammarUtil.allParserRules(grammar);
  for (final ParserRule rule : _allParserRules) {
    boolean _hasNext = Iterators.<UnorderedGroup>filter(rule.eAllContents(), UnorderedGroup.class).hasNext();
    if (_hasNext) {
      return true;
    }
  }
  return false;
}