org.eclipse.xtext.generator.Generator Java Examples

The following examples show how to use org.eclipse.xtext.generator.Generator. 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: XtextAntlrGeneratorFragment.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void generate(Grammar grammar, XpandExecutionContext ctx) {
	super.generate(grammar, ctx);
	String srcGenPath = ctx.getOutput().getOutlet(Generator.SRC_GEN).getPath();
	final String encoding = getEncoding(ctx, Generator.SRC_GEN);
	String absoluteGrammarFileName = srcGenPath+"/"+getGrammarFileName(grammar, getNaming()).replace('.', '/')+".g";
	addAntlrParam("-fo");
	addAntlrParam(absoluteGrammarFileName.substring(0, absoluteGrammarFileName.lastIndexOf('/')));
	getAntlrTool().runWithEncodingAndParams(absoluteGrammarFileName, encoding, getAntlrParams());
	Charset charset = Charset.forName(encoding);
	simplifyUnorderedGroupPredicatesIfRequired(grammar, absoluteGrammarFileName, charset);
	splitParserAndLexerIfEnabled(absoluteGrammarFileName, charset);
	suppressWarnings(absoluteGrammarFileName, charset);
	normalizeLineDelimiters(absoluteGrammarFileName, charset);
	normalizeTokens(absoluteGrammarFileName, charset);
}
 
Example #2
Source File: XtextAntlrUiGeneratorFragment.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void generate(Grammar grammar, XpandExecutionContext ctx) {
	super.generate(grammar, ctx);
	String srcUiGenPath = ctx.getOutput().getOutlet(Generator.SRC_GEN_IDE).getPath();
	final String encoding = getEncoding(ctx, Generator.SRC_GEN_IDE);
	String absoluteGrammarFileName = srcUiGenPath + "/" + getGrammarFileName(grammar, getNaming()).replace('.', '/') + ".g";
	addAntlrParam("-fo");
	addAntlrParam(absoluteGrammarFileName.substring(0, absoluteGrammarFileName.lastIndexOf('/')));
	getAntlrTool().runWithEncodingAndParams(absoluteGrammarFileName, encoding, getAntlrParams());
	Charset charset = Charset.forName(getEncoding(ctx, Generator.SRC_GEN_IDE));
	simplifyUnorderedGroupPredicatesIfRequired(grammar, absoluteGrammarFileName, charset);
	splitParserAndLexerIfEnabled(absoluteGrammarFileName, charset);
	suppressWarnings(absoluteGrammarFileName, charset);
	removeBacktrackingGuardsIfEnabled(absoluteGrammarFileName, charset);
	normalizeLineDelimiters(absoluteGrammarFileName, charset);
	normalizeTokens(absoluteGrammarFileName, charset);
}
 
Example #3
Source File: ExternalAntlrLexerFragment.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void generate(Grammar grammar, XpandExecutionContext ctx) {
	super.generate(grammar, ctx);
	String srcGen = Generator.SRC_GEN;
	String src = Generator.SRC;
	if (contentAssist || highlighting) {
		srcGen = Generator.SRC_GEN_IDE;
		src = Generator.SRC_IDE;
	}
	String srcGenPath = ctx.getOutput().getOutlet(srcGen).getPath();
	String srcPath = ctx.getOutput().getOutlet(src).getPath();
	String grammarFile = srcPath + "/" + getLexerGrammar().replace('.', '/') + ".g";
	String generateTo = "";
	if (lexerGrammar.lastIndexOf('.') != -1) {
		generateTo = lexerGrammar.substring(0, lexerGrammar.lastIndexOf('.'));
	}
	generateTo = srcGenPath + "/" + generateTo.replace('.', '/');
	addAntlrParam("-fo");
	addAntlrParam(generateTo);
	final String encoding = getEncoding(ctx, srcGen);
	getAntlrTool().runWithEncodingAndParams(grammarFile, encoding, getAntlrParams());
	Charset charset = Charset.forName(encoding);
	String javaFile = srcGenPath+"/"+getLexerGrammar().replace('.', '/')+".java";
	splitLexerIfEnabled(javaFile, charset);
	suppressWarningsImpl(javaFile, charset);
	normalizeLineDelimiters(javaFile, charset);
	normalizeTokens(javaFile, charset);
}
 
Example #4
Source File: ContentAssistParserGeneratorFragment.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void generate(Grammar grammar, XpandExecutionContext ctx) {
	KeywordHelper helper = new KeywordHelper(grammar, getOptions().isIgnoreCase());
	super.generate(grammar, ctx);
	final String srcGenPath = ctx.getOutput().getOutlet(Generator.SRC_GEN_IDE).getPath();
	final String encoding = getEncoding(ctx, Generator.SRC_GEN_IDE);
	String libPath = srcGenPath + "/" + getFragmentHelper().getContentAssistLexerGrammarFileName(grammar).replace('.', '/');
	libPath = libPath.substring(0, libPath.lastIndexOf('/'));
	String absoluteLexerFileName = srcGenPath+"/"+getFragmentHelper().getContentAssistLexerGrammarFileName(grammar).replace('.', '/')+".g";
	String absoluteParserFileName = srcGenPath+"/"+getFragmentHelper().getContentAssistParserGrammarFileName(grammar).replace('.', '/')+".g";
	addAntlrParam("-fo");
	addAntlrParam(absoluteParserFileName.substring(0, absoluteParserFileName.lastIndexOf('/')));
	String[] lexerAntlrParams = getAntlrParams();
	lexerAntlrParams[lexerAntlrParams.length - 1] = absoluteLexerFileName.substring(0, absoluteLexerFileName.lastIndexOf('/'));
	getAntlrTool().runWithEncodingAndParams(absoluteLexerFileName, encoding, lexerAntlrParams);
	addAntlrParam("-lib");
	addAntlrParam(libPath);
	getAntlrTool().runWithEncodingAndParams(absoluteParserFileName, encoding, getAntlrParams());
	Charset charset = Charset.forName(encoding);
	simplifyUnorderedGroupPredicatesIfRequired(grammar, absoluteParserFileName, charset);
	splitParserAndLexerIfEnabled(absoluteLexerFileName, absoluteParserFileName, charset);
	suppressWarnings(absoluteLexerFileName, absoluteParserFileName, charset);
	normalizeLineDelimiters(absoluteLexerFileName, absoluteParserFileName, charset);
	normalizeTokens(absoluteLexerFileName, charset);
	normalizeTokens(absoluteParserFileName, charset);
	helper.discardHelper(grammar);
}
 
Example #5
Source File: DebugAntlrGeneratorFragment.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void generate(Grammar grammar, XpandExecutionContext ctx) {
	DebugGrammarToken token = DebugGrammarToken.aquire();
	try {
		super.generate(grammar, ctx);
		String srcGenPath = ctx.getOutput().getOutlet(Generator.SRC_GEN).getPath();
		String absoluteGrammarFileName = srcGenPath+"/"+getGrammarFileName(grammar, getNaming()).replace('.', '/')+".g";
		prettyPrint(absoluteGrammarFileName, Charset.forName(getEncoding(ctx, Generator.SRC_GEN)));
	} finally {
		token.done();
	}
}
 
Example #6
Source File: GrammarAccessFragment.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void generate(Grammar grammar, XpandExecutionContext ctx) {
	RuleNames.ensureAdapterInstalled(grammar);
	super.generate(grammar, ctx);

	final ResourceSaveIndicator isSaving = new ResourceSaveIndicator();
	// create a defensive clone
	Grammar copy = deepCopy(grammar, isSaving);
	ResourceSet set = copy.eResource().getResourceSet();

	// save grammar model
	String path;
	if (xmlVersion == null) {
		path = GrammarUtil.getClasspathRelativePathToBinGrammar(copy);
	} else {
		log.warn("'xmlVersion' has been specified for this "
				+ GrammarAccessFragment.class.getSimpleName()
				+ ". Therefore, the grammar is persisted as XMI and not as binary. This can be a performance drawback.");
		path = GrammarUtil.getClasspathRelativePathToXmi(copy);
	}
	URI uri = URI.createURI(ctx.getOutput().getOutlet(Generator.SRC_GEN).getPath() + "/" + path);
	Resource resource = set.createResource(uri, ContentHandler.UNSPECIFIED_CONTENT_TYPE);
	addAllGrammarsToResource(resource, copy, new HashSet<Grammar>());
	isSaving.set(Boolean.TRUE);
	Map<String, Object> saveOptions = Maps.newHashMap();
	if (resource instanceof XMLResource) {
		((XMLResource) resource).setXMLVersion(getXmlVersion());
	} else if (resource instanceof BinaryResourceImpl){
		saveOptions.put(BinaryResourceImpl.OPTION_VERSION, BinaryResourceImpl.BinaryIO.Version.VERSION_1_1);
		saveOptions.put(BinaryResourceImpl.OPTION_STYLE_DATA_CONVERTER, Boolean.TRUE);
	}
	try {
		resource.save(saveOptions);
	} catch (IOException e) {
		log.error(e.getMessage(), e);
	} finally {
		isSaving.set(Boolean.FALSE);
	}
}
 
Example #7
Source File: Generator2AdapterSetup.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private XtextProjectConfig createProjectConfig() {
	StandardProjectConfig cfg = new StandardProjectConfig();
	cfg.setCreateEclipseMetaData(true);
	cfg.setBaseName(naming.getProjectNameRt());
	String runtimeRoot = xpandContext.getOutput().getOutlet(Generator.PLUGIN_RT).getPath();
	int projectNameIndex = runtimeRoot.lastIndexOf(cfg.getBaseName());
	if (projectNameIndex >= 0) {
		cfg.setRootPath(runtimeRoot.substring(0, projectNameIndex));
	} else {
		cfg.setRootPath(runtimeRoot);
	}
	cfg.getRuntime().setName(cfg.getBaseName());
	cfg.getRuntime().setRoot(runtimeRoot);
	cfg.getRuntime().setSrc(xpandContext.getOutput().getOutlet(Generator.SRC).getPath());
	cfg.getRuntime().setSrcGen(xpandContext.getOutput().getOutlet(Generator.SRC_GEN).getPath());
	cfg.getRuntime().setEcoreModel(xpandContext.getOutput().getOutlet(Generator.MODEL).getPath());
	cfg.getEclipsePlugin().setEnabled(true);
	cfg.getEclipsePlugin().setName(naming.getProjectNameUi());
	cfg.getEclipsePlugin().setRoot(xpandContext.getOutput().getOutlet(Generator.PLUGIN_UI).getPath());
	cfg.getEclipsePlugin().setSrc(xpandContext.getOutput().getOutlet(Generator.SRC_UI).getPath());
	cfg.getEclipsePlugin().setSrcGen(xpandContext.getOutput().getOutlet(Generator.SRC_GEN_UI).getPath());
	cfg.getGenericIde().setEnabled(true);
	cfg.getGenericIde().setName(naming.getProjectNameIde());
	cfg.getGenericIde().setRoot(xpandContext.getOutput().getOutlet(Generator.PLUGIN_IDE).getPath());
	cfg.getGenericIde().setSrc(xpandContext.getOutput().getOutlet(Generator.SRC_IDE).getPath());
	cfg.getGenericIde().setSrcGen(xpandContext.getOutput().getOutlet(Generator.SRC_GEN_IDE).getPath());
	return cfg;
}
 
Example #8
Source File: SimpleProjectWizardFragment.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void generate(final Grammar grammar, XpandExecutionContext ctx) {
	final String templateName = getNewProjectTemplateName(grammar, getNaming());
	final Outlet outlet = ctx.getOutput().getOutlet(Generator.SRC_UI);
	final File templateFile = new File(new File(outlet.getPath()), templateName.replaceAll("::", "/") + '.' + XpandUtil.TEMPLATE_EXTENSION);
	final boolean templateExisted = templateFile.exists();
	List<Object> parameters = getParameters(grammar);
	parameters.add(outlet.getFileEncoding());
	XpandFacade.create(ctx).evaluate2(getTemplate()+"::generate", grammar, parameters);
	if (!templateExisted && templateFile.exists()) {
		LOG.info("A new template " + templateName + " has been generated into " + outlet.getPath() + ".");
		LOG.info("  Make sure to enable the Xtend/Xpand nature and JavaBeans metamodel to edit this template.");
	}
}
 
Example #9
Source File: XtextAntlrGeneratorComparisonFragment.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Tweaks the generation of the {@link Generator#SRC_GEN Generator.SRC_GEN},
 * {@link Generator#SRC_GEN_IDE Generator.SRC_GEN_IDE}, and
 * {@link Generator#SRC_GEN_UI Generator.SRC_GEN_UI} outlets
 * and injects the {@link #getTmpFolder()}.
 */
@Override
protected Outlet createOutlet(final boolean append, final String encoding, final String name, final boolean overwrite, final String path) {
  Outlet _xifexpression = null;
  if (((Objects.equal(name, Generator.SRC_GEN) || Objects.equal(name, Generator.SRC_GEN_IDE)) || Objects.equal(name, Generator.SRC_GEN_UI))) {
    _xifexpression = super.createOutlet(append, encoding, name, overwrite, this.getTmpFolder().getAbsolutePath());
  } else {
    _xifexpression = super.createOutlet(append, encoding, name, overwrite, path);
  }
  return _xifexpression;
}
 
Example #10
Source File: AntlrGeneratorFragment.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void generate(final Grammar grammar, XpandExecutionContext ctx) {
	KeywordHelper helper = new KeywordHelper(grammar, getOptions().isIgnoreCase());
	super.generate(grammar, ctx);
	final String srcGenPath = ctx.getOutput().getOutlet(Generator.SRC_GEN).getPath();
	final String encoding = getEncoding(ctx, Generator.SRC_GEN);
	final String lexerBaseFileName = srcGenPath+"/"+getFragmentHelper().getLexerGrammarFileName(grammar).replace('.', '/');
	String libPath = lexerBaseFileName;
	libPath = libPath.substring(0, libPath.lastIndexOf('/'));
	String absoluteLexerFileName = lexerBaseFileName + ".g";
	String absoluteParserFileName = srcGenPath+"/"+getFragmentHelper().getParserGrammarFileName(grammar).replace('.', '/')+".g";
	addAntlrParam("-fo");
	addAntlrParam(absoluteParserFileName.substring(0, absoluteParserFileName.lastIndexOf('/')));
	String[] lexerAntlrParams = getAntlrParams();
	lexerAntlrParams[lexerAntlrParams.length - 1] = absoluteLexerFileName.substring(0, absoluteLexerFileName.lastIndexOf('/'));
	getAntlrTool().runWithEncodingAndParams(absoluteLexerFileName, encoding, lexerAntlrParams);
	cleanupLexerTokensFile(lexerBaseFileName, helper, encoding);
	addAntlrParam("-lib");
	addAntlrParam(libPath);
	getAntlrTool().runWithEncodingAndParams(absoluteParserFileName, encoding, getAntlrParams());
	Charset charset = Charset.forName(encoding);
	simplifyUnorderedGroupPredicatesIfRequired(grammar, absoluteParserFileName, charset);
	splitParserAndLexerIfEnabled(absoluteLexerFileName, absoluteParserFileName, charset);
	suppressWarnings(absoluteLexerFileName, absoluteParserFileName, charset);
	normalizeLineDelimiters(absoluteLexerFileName, absoluteParserFileName, charset);
	normalizeTokens(absoluteParserFileName, charset);
	MutableTokenDefProvider provider = createLexerTokensProvider(lexerBaseFileName, helper, charset);
	for(Map.Entry<Integer, String> entry: provider.getTokenDefMap().entrySet()) {
		String value = entry.getValue();
		if(helper.isKeywordRule(value)) {
			String keywordAsAntlrString = AntlrGrammarGenUtil.toAntlrString(helper.getKeywordValue(value));
			entry.setValue("'" + keywordAsAntlrString + "'");
		} else if (value.startsWith("'")) {
			value = AntlrGrammarGenUtil.toAntlrString(value);
			entry.setValue("'" + value + "'");
		}
	}
	try {
		provider.writeTokenFile(new PrintWriter(new File(srcGenPath+"/"+getFragmentHelper().getParserGrammarFileName(grammar).replace('.', '/') + ".tokens"), encoding));
	}
	catch (IOException e) {
		throw new RuntimeException(e);
	}
	normalizeTokens(absoluteLexerFileName, charset);
	helper.discardHelper(grammar);
}
 
Example #11
Source File: Formatter2Fragment.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void generate(Xtend2ExecutionContext ctx) {
	ctx.writeFile(Generator.SRC, this.stubGenerator.getStubFileName(), stubGenerator.generateStubFileContents());
}
 
Example #12
Source File: XtextAntlrGeneratorComparisonFragment.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void generate() {
  Naming _naming = this.getNaming();
  boolean _tripleEquals = (_naming == null);
  if (_tripleEquals) {
    this.setNaming(this.createNaming());
  }
  if ((this.options == null)) {
    AntlrOptions _antlrOptions = new AntlrOptions();
    this.options = _antlrOptions;
  }
  final AntlrGrammarComparator.IErrorHandler errorHandler = this.createErrorHandler();
  RuntimeException exception = null;
  IRuntimeProjectConfig _runtime = this.getProjectConfig().getRuntime();
  IXtextGeneratorFileSystemAccess _srcGen = null;
  if (_runtime!=null) {
    _srcGen=_runtime.getSrcGen();
  }
  boolean _tripleNotEquals = (_srcGen != null);
  if (_tripleNotEquals) {
    exception = this.loadAndCompareGrammars(this.getProjectConfig().getRuntime().getSrcGen(), Generator.SRC_GEN, errorHandler);
  }
  boolean _and = false;
  if (!(((!this.failOnError) || (exception == null)) && (!this.skipContentAssistGrammarComparison))) {
    _and = false;
  } else {
    IBundleProjectConfig _genericIde = this.getProjectConfig().getGenericIde();
    IXtextGeneratorFileSystemAccess _srcGen_1 = null;
    if (_genericIde!=null) {
      _srcGen_1=_genericIde.getSrcGen();
    }
    boolean _tripleNotEquals_1 = (_srcGen_1 != null);
    _and = _tripleNotEquals_1;
  }
  if (_and) {
    exception = this.loadAndCompareGrammars(this.getProjectConfig().getGenericIde().getSrcGen(), Generator.SRC_GEN_IDE, errorHandler);
  }
  XtextAntlrGeneratorComparisonFragment.deleteDir(this.getTmpFolder());
  if ((this.failOnError && (exception != null))) {
    throw exception;
  }
}
 
Example #13
Source File: ContentAssistFragment.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void generate(final Xtend2ExecutionContext ctx) {
  if (this.generateStub) {
    String _asPath = this._naming.asPath(this.getProposalProviderName(this.grammar));
    String _plus = (_asPath + ".xtend");
    StringConcatenation _builder = new StringConcatenation();
    String _fileHeader = this._naming.fileHeader();
    _builder.append(_fileHeader);
    _builder.newLineIfNotEmpty();
    _builder.append("package ");
    String _packageName = this._naming.packageName(this.getProposalProviderName(this.grammar));
    _builder.append(_packageName);
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("import ");
    String _genProposalProviderName = this.getGenProposalProviderName();
    _builder.append(_genProposalProviderName);
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("/**");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("* See https://www.eclipse.org/Xtext/documentation/310_eclipse_support.html#content-assist");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("* on how to customize the content assistant.");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("class ");
    String _simpleName = this._naming.toSimpleName(this.getProposalProviderName(this.grammar));
    _builder.append(_simpleName);
    _builder.append(" extends ");
    String _simpleName_1 = this._naming.toSimpleName(this.getGenProposalProviderName());
    _builder.append(_simpleName_1);
    _builder.append(" {");
    _builder.newLineIfNotEmpty();
    _builder.append("}");
    _builder.newLine();
    ctx.writeFile(Generator.SRC_UI, _plus, _builder);
  }
  XpandFacade.create(ctx.getXpandExecutionContext()).evaluate2(
    "org::eclipse::xtext::ui::generator::contentAssist::JavaBasedContentAssistFragment::GenProposalProvider", 
    this.grammar, 
    Collections.<Object>singletonList(this.getSuperClassName()));
}
 
Example #14
Source File: FormatFragmentUtil.java    From dsl-devkit with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Returns a URI corresponding to the default location for format files. This is in the SRC outlet where the Xtext grammar file usually is.
 *
 * @param grammar
 *          Xtext grammar to get format file URI for
 * @param ctx
 *          xpand execution context (defines required SRC outlet)
 * @return the file URI to the default format file location
 */
private static URI getDefaultFormatLocation(final Grammar grammar, final XpandExecutionContext ctx) {
  final String xmiPath = GrammarUtil.getClasspathRelativePathToXmi(grammar);
  return URI.createFileURI(new File(ctx.getOutput().getOutlet(Generator.SRC).getPath(), xmiPath).getAbsolutePath()).trimFileExtension().appendFileExtension(FormatConstants.FILE_EXTENSION);
}