org.eclipse.xtext.xtext.FlattenedGrammarAccess Java Examples

The following examples show how to use org.eclipse.xtext.xtext.FlattenedGrammarAccess. 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: 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 #2
Source File: AbstractAntlrGrammarGenerator.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
public void generate(final Grammar it, final AntlrOptions options, final IXtextGeneratorFileSystemAccess fsa) {
  this.keywordHelper = KeywordHelper.getHelper(it);
  this.originalGrammar = it;
  final RuleFilter filter = new RuleFilter();
  filter.setDiscardUnreachableRules(options.isSkipUnusedRules());
  final RuleNames ruleNames = RuleNames.getRuleNames(it, true);
  final Grammar flattened = new FlattenedGrammarAccess(ruleNames, filter).getFlattenedGrammar();
  boolean _isCombinedGrammar = this.isCombinedGrammar();
  new CombinedGrammarMarker(_isCombinedGrammar).attachToEmfObject(flattened);
  fsa.generateFile(this.getGrammarNaming().getParserGrammar(it).getGrammarFileName(), this.compileParser(flattened, options));
  boolean _isCombinedGrammar_1 = this.isCombinedGrammar();
  boolean _not = (!_isCombinedGrammar_1);
  if (_not) {
    fsa.generateFile(this.getGrammarNaming().getLexerGrammar(it).getGrammarFileName(), this.compileLexer(flattened, options));
  }
}
 
Example #3
Source File: AbstractAntlrGeneratorFragment.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void generate(Grammar grammar, XpandExecutionContext ctx) {
	checkGrammar(grammar);
	
	RuleFilter filter = new RuleFilter();
	filter.setDiscardUnreachableRules(getOptions().isSkipUnusedRules());
	RuleNames ruleNames = RuleNames.getRuleNames(grammar, true);
	Grammar flattened = new FlattenedGrammarAccess(ruleNames, filter).getFlattenedGrammar();
	super.generate(flattened, ctx);
}
 
Example #4
Source File: GrammarFlatteningTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public Grammar getModel(final String model, final boolean dropUnreachable) throws Exception {
  EObject _model = super.getModel(model);
  Grammar grammar = ((Grammar) _model);
  RuleNames ruleNames = RuleNames.getRuleNames(grammar, false);
  RuleFilter filter = new RuleFilter();
  filter.setDiscardUnreachableRules(dropUnreachable);
  Grammar result = new FlattenedGrammarAccess(ruleNames, filter).getFlattenedGrammar();
  XtextResource resource = this.<XtextResource>get(XtextResource.class);
  resource.getContents().add(result);
  resource.setURI(URI.createURI("synthetic://flattened.xtext"));
  return result;
}