Merge pull request #1453 from eclipse/cd_genx2j

[eclipse/xtext#1679] converted Xtend code to Java
This commit is contained in:
Christian Dietrich 2020-04-24 14:51:44 +02:00 committed by GitHub
commit 2933c1f7d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 551 additions and 969 deletions

View file

@ -0,0 +1,96 @@
/**
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.xtext.generator.model.annotations;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.eclipse.xtext.util.Strings;
import org.eclipse.xtext.xtext.generator.XtextGenerator;
import org.eclipse.xtext.xtext.generator.model.JavaFileAccess;
import org.eclipse.xtext.xtext.generator.model.TypeReference;
/**
* A class annotation configuration for the <code>@Generated</code> annotation.
*/
public class GeneratedClassAnnotation implements IClassAnnotation {
private boolean includeDate = false;
private String comment;
@Override
public CharSequence generate() {
StringBuilder stringBuilder = new StringBuilder("@Generated(");
if (includeDate || !Strings.isEmpty(comment)) {
stringBuilder.append("value = ");
}
stringBuilder.append("\"");
stringBuilder.append(getGeneratorName());
stringBuilder.append("\"");
if (includeDate) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd\'T\'HH:mmZ");
String date = dateFormat.format(new Date());
stringBuilder.append(", date = \"");
stringBuilder.append(date);
stringBuilder.append("\"");
}
if (!Strings.isEmpty(comment)) {
stringBuilder.append(", comments = \"");
stringBuilder.append(Strings.convertToJavaString(comment));
stringBuilder.append("\"");
}
stringBuilder.append(")");
return stringBuilder;
}
protected String getGeneratorName() {
return XtextGenerator.class.getName();
}
@Override
public boolean appliesTo(JavaFileAccess javaFile) {
return javaFile.isMarkedAsGenerated();
}
@Override
public TypeReference getAnnotationImport() {
return new TypeReference("javax.annotation.Generated");
}
@Override
public String toString() {
return generate().toString();
}
@Override
public boolean equals(Object obj) {
return obj instanceof GeneratedClassAnnotation;
}
@Override
public int hashCode() {
return getClass().getName().hashCode();
}
public boolean isIncludeDate() {
return includeDate;
}
public void setIncludeDate(boolean includeDate) {
this.includeDate = includeDate;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
}

View file

@ -1,82 +0,0 @@
/*******************************************************************************
* Copyright (c) 2015, 2017 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.xtext.generator.model.annotations
import java.text.SimpleDateFormat
import java.util.Date
import org.eclipse.xtend.lib.annotations.Accessors
import org.eclipse.xtext.util.Strings
import org.eclipse.xtext.xtext.generator.XtextGenerator
import org.eclipse.xtext.xtext.generator.model.JavaFileAccess
import org.eclipse.xtext.xtext.generator.model.TypeReference
/**
* A class annotation configuration for the <code>@Generated</code> annotation.
*/
@Accessors
class GeneratedClassAnnotation implements IClassAnnotation {
boolean includeDate = false
String comment
override generate() {
val stringBuilder = new StringBuilder('@Generated(')
if (includeDate || !Strings.isEmpty(comment)) {
stringBuilder += 'value = '
}
stringBuilder += '"'
stringBuilder += generatorName
stringBuilder += '"'
if (includeDate) {
val dateFormat = new SimpleDateFormat('yyyy-MM-dd\'T\'HH:mmZ')
val date = dateFormat.format(new Date)
stringBuilder += ', date = "'
stringBuilder += date
stringBuilder += '"'
}
if (!Strings.isEmpty(comment)) {
val convertedComment = Strings.convertToJavaString(comment)
stringBuilder += ', comments = "'
stringBuilder += convertedComment
stringBuilder += '"'
}
stringBuilder += ')'
return stringBuilder
}
protected def String getGeneratorName() {
return XtextGenerator.name
}
override appliesTo(JavaFileAccess javaFile) {
return javaFile.markedAsGenerated
}
override getAnnotationImport() {
return new TypeReference('javax.annotation.Generated')
}
private def void +=(StringBuilder stringBuilder, String s) {
stringBuilder.append(s)
}
override toString() {
generate.toString
}
override equals(Object obj) {
return obj instanceof GeneratedClassAnnotation
}
override hashCode() {
return class.name.hashCode;
}
}

View file

@ -0,0 +1,72 @@
/**
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.xtext.generator.model.annotations;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.eclipse.xtext.xtext.generator.model.JavaFileAccess;
import org.eclipse.xtext.xtext.generator.model.TypeReference;
/**
* A class annotation configuration for the <code>@SuppressWarnings</code>
* annotation.
*/
public class SuppressWarningsAnnotation implements IClassAnnotation {
private String suppress = "all";
@Override
public CharSequence generate() {
StringBuilder stringBuilder = new StringBuilder("@SuppressWarnings(");
String[] suppressedWarnings = suppress.split("\\s*,\\s*");
if (suppressedWarnings.length != 1) {
stringBuilder.append("{");
}
String value = Arrays.stream(suppressedWarnings).map((s)-> "\"" + s + "\"").collect(Collectors.joining( ", " ));
stringBuilder.append(value);
if (suppressedWarnings.length != 1) {
stringBuilder.append("}");
}
stringBuilder.append(")");
return stringBuilder;
}
@Override
public boolean appliesTo(JavaFileAccess javaFile) {
return true;
}
@Override
public TypeReference getAnnotationImport() {
return new TypeReference(SuppressWarnings.class);
}
@Override
public String toString() {
return generate().toString();
}
@Override
public boolean equals(Object obj) {
return obj instanceof SuppressWarningsAnnotation;
}
@Override
public int hashCode() {
return getClass().getName().hashCode();
}
public String getSuppress() {
return suppress;
}
public void setSuppress(String suppress) {
this.suppress = suppress;
}
}

View file

@ -1,55 +0,0 @@
/*******************************************************************************
* Copyright (c) 2015, 2017 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.xtext.generator.model.annotations
import org.eclipse.xtend.lib.annotations.Accessors
import org.eclipse.xtext.xtext.generator.model.JavaFileAccess
import org.eclipse.xtext.xtext.generator.model.TypeReference
/**
* A class annotation configuration for the <code>@SuppressWarnings</code> annotation.
*/
@Accessors
class SuppressWarningsAnnotation implements IClassAnnotation {
String suppress = 'all'
override generate() {
val stringBuilder = new StringBuilder('@SuppressWarnings(')
val suppressedWarnings = suppress.split("\\s*,\\s*")
if (suppressedWarnings.length != 1)
stringBuilder.append('{')
stringBuilder.append(suppressedWarnings.join('"', '", "', '"', [it]))
if (suppressedWarnings.length != 1)
stringBuilder.append('}')
stringBuilder.append(')')
return stringBuilder
}
override appliesTo(JavaFileAccess javaFile) {
return true
}
override getAnnotationImport() {
return new TypeReference(SuppressWarnings)
}
override toString() {
generate.toString
}
override equals(Object obj) {
return obj instanceof SuppressWarningsAnnotation
}
override hashCode() {
return class.name.hashCode;
}
}

View file

@ -0,0 +1,76 @@
/**
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.xtext.generator.parser.antlr;
public class AntlrGrammar {
private final String packageName;
private final String simpleName;
public String getName() {
return packageName + "." + simpleName;
}
public String getGrammarFileName() {
return getName().replace('.', '/') + ".g";
}
public String getTokensFileName() {
return getName().replace('.', '/') + ".tokens";
}
@Override
public String toString() {
return getName();
}
public AntlrGrammar(String packageName, String simpleName) {
this.packageName = packageName;
this.simpleName = simpleName;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((packageName == null) ? 0 : packageName.hashCode());
result = prime * result + ((simpleName == null) ? 0 : simpleName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AntlrGrammar other = (AntlrGrammar) obj;
if (packageName == null) {
if (other.packageName != null)
return false;
} else if (!packageName.equals(other.packageName))
return false;
if (simpleName == null) {
if (other.simpleName != null)
return false;
} else if (!simpleName.equals(other.simpleName))
return false;
return true;
}
public String getPackageName() {
return packageName;
}
public String getSimpleName() {
return simpleName;
}
}

View file

@ -1,33 +0,0 @@
/*******************************************************************************
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.xtext.generator.parser.antlr
import org.eclipse.xtend.lib.annotations.Data
@Data
class AntlrGrammar {
String packageName
String simpleName
def String getName() {
packageName + "." + simpleName
}
def String getGrammarFileName() {
name.replace('.', '/') + ".g"
}
def String getTokensFileName() {
name.replace('.', '/') + ".tokens"
}
override toString() {
name
}
}

View file

@ -0,0 +1,159 @@
/**
* Copyright (c) 2008, 2020 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.xtext.generator.parser.antlr;
import org.eclipse.xtext.xtext.generator.parser.antlr.splitting.AntlrParserSplitter;
import org.eclipse.xtext.xtext.generator.parser.antlr.splitting.PartialClassExtractor;
import org.eclipse.xtext.xtext.generator.parser.antlr.splitting.internal.LexerSpecialStateTransitionSplitter;
public class AntlrOptions {
private boolean backtrack = false;
private boolean backtrackLexer = false;
private boolean memoize = false;
private int k = -1;
private boolean ignoreCase = false;
private boolean classSplitting = false;
private int fieldsPerClass = AntlrParserSplitter.FIELDS_PER_CLASS;
private int methodsPerClass = PartialClassExtractor.METHODS_PER_CLASS;
private int casesPerSpecialStateSwitch = LexerSpecialStateTransitionSplitter.CASES_PER_SPECIAL_STATE_SWITCH;
private boolean skipUnusedRules = false;
private boolean optimizeCodeQuality = true;
private boolean stripAllComments = false;
private String keptBitSetsPattern;
private String keptBitSetName;
public void setFieldsPerClass(String fieldsPerClass) {
this.fieldsPerClass = Integer.parseInt(fieldsPerClass);
}
public void setMethodsPerClass(String methodsPerClass) {
this.methodsPerClass = Integer.parseInt(methodsPerClass);
}
public void setCasesPerSpecialStateSwitch(String casesPerSpecialStateSwitch) {
this.casesPerSpecialStateSwitch = Integer.parseInt(casesPerSpecialStateSwitch);
}
public void setKAsString(String k) {
this.k = Integer.parseInt(k);
}
public boolean isBacktrack() {
return backtrack;
}
public void setBacktrack(boolean backtrack) {
this.backtrack = backtrack;
}
public boolean isBacktrackLexer() {
return backtrackLexer;
}
public void setBacktrackLexer(boolean backtrackLexer) {
this.backtrackLexer = backtrackLexer;
}
public boolean isMemoize() {
return memoize;
}
public void setMemoize(boolean memoize) {
this.memoize = memoize;
}
public int getK() {
return k;
}
public void setK(int k) {
this.k = k;
}
public boolean isIgnoreCase() {
return ignoreCase;
}
public void setIgnoreCase(boolean ignoreCase) {
this.ignoreCase = ignoreCase;
}
public boolean isClassSplitting() {
return classSplitting;
}
public void setClassSplitting(boolean classSplitting) {
this.classSplitting = classSplitting;
}
public int getFieldsPerClass() {
return fieldsPerClass;
}
public int getMethodsPerClass() {
return methodsPerClass;
}
public int getCasesPerSpecialStateSwitch() {
return casesPerSpecialStateSwitch;
}
public boolean isSkipUnusedRules() {
return skipUnusedRules;
}
public void setSkipUnusedRules(boolean skipUnusedRules) {
this.skipUnusedRules = skipUnusedRules;
}
public boolean isOptimizeCodeQuality() {
return optimizeCodeQuality;
}
public void setOptimizeCodeQuality(boolean optimizeCodeQuality) {
this.optimizeCodeQuality = optimizeCodeQuality;
}
public boolean isStripAllComments() {
return stripAllComments;
}
public void setStripAllComments(boolean stripAllComments) {
this.stripAllComments = stripAllComments;
}
public String getKeptBitSetsPattern() {
return keptBitSetsPattern;
}
public void setKeptBitSetsPattern(String keptBitSetsPattern) {
this.keptBitSetsPattern = keptBitSetsPattern;
}
public String getKeptBitSetName() {
return keptBitSetName;
}
public void setKeptBitSetName(String keptBitSetName) {
this.keptBitSetName = keptBitSetName;
}
}

View file

@ -1,52 +0,0 @@
/**
* Copyright (c) 2008 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.xtext.generator.parser.antlr
import org.eclipse.xtend.lib.annotations.Accessors
import org.eclipse.xtext.xtext.generator.parser.antlr.splitting.AntlrParserSplitter
import org.eclipse.xtext.xtext.generator.parser.antlr.splitting.PartialClassExtractor
import org.eclipse.xtext.xtext.generator.parser.antlr.splitting.internal.LexerSpecialStateTransitionSplitter
@Accessors
class AntlrOptions {
boolean backtrack = false
boolean backtrackLexer = false
boolean memoize = false
int k = -1
boolean ignoreCase = false
boolean classSplitting = false
@Accessors(PUBLIC_GETTER)
int fieldsPerClass = AntlrParserSplitter.FIELDS_PER_CLASS
@Accessors(PUBLIC_GETTER)
int methodsPerClass = PartialClassExtractor.METHODS_PER_CLASS
@Accessors(PUBLIC_GETTER)
int casesPerSpecialStateSwitch = LexerSpecialStateTransitionSplitter.CASES_PER_SPECIAL_STATE_SWITCH
boolean skipUnusedRules = false
boolean optimizeCodeQuality = true
boolean stripAllComments = false
String keptBitSetsPattern
String keptBitSetName
def void setFieldsPerClass(String fieldsPerClass) {
this.fieldsPerClass = Integer.parseInt(fieldsPerClass)
}
def void setMethodsPerClass(String methodsPerClass) {
this.methodsPerClass = Integer.parseInt(methodsPerClass)
}
def void setCasesPerSpecialStateSwitch(String casesPerSpecialStateSwitch) {
this.casesPerSpecialStateSwitch = Integer.parseInt(casesPerSpecialStateSwitch)
}
def void setKAsString(String k) {
this.k = Integer.parseInt(k)
}
}

View file

@ -0,0 +1,39 @@
/**
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.xtext.generator.parser.antlr;
/**
* Static utility that allows to check if the current thread is producing a
* debug grammar or not.
*
* @author Sebastian Zarnekow - Initial contribution and API
* @noreference
*/
public class DebugGrammarToken {
private static ThreadLocal<Boolean> store = new ThreadLocal<>();
public static DebugGrammarToken aquire() {
if (DebugGrammarToken.store.get() != null) {
throw new IllegalStateException(String.valueOf(DebugGrammarToken.store.get()));
}
return new DebugGrammarToken();
}
public static boolean isGeneratingDebugGrammar() {
return Boolean.TRUE.equals(DebugGrammarToken.store.get());
}
private DebugGrammarToken() {
DebugGrammarToken.store.set(Boolean.TRUE);
}
public void done() {
DebugGrammarToken.store.set(null);
}
}

View file

@ -1,41 +0,0 @@
/*******************************************************************************
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.xtext.generator.parser.antlr
/**
* Static utility that allows to check if the current thread is producing
* a debug grammar or not.
*
* @author Sebastian Zarnekow - Initial contribution and API
* @noreference
*/
class DebugGrammarToken {
static ThreadLocal<Boolean> store = new ThreadLocal
static def DebugGrammarToken aquire() {
if (store.get() !== null) {
throw new IllegalStateException(String.valueOf(store.get))
}
return new DebugGrammarToken
}
static def boolean isGeneratingDebugGrammar() {
return store.get == Boolean.TRUE
}
private new() {
store.set(Boolean.TRUE)
}
def void done() {
store.set(null)
}
}

View file

@ -0,0 +1,39 @@
/**
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.xtext.generator.util;
import org.eclipse.xtext.Grammar;
import org.eclipse.xtext.GrammarUtil;
import com.google.common.base.Objects;
public class GrammarUtil2 extends GrammarUtil {
public static final String TERMINALS = "org.eclipse.xtext.common.Terminals";
public static boolean inherits(Grammar grammar, String languageID) {
if (Objects.equal(grammar.getName(), languageID)) {
return true;
}
for (Grammar g : grammar.getUsedGrammars()) {
if (GrammarUtil2.inherits(g, languageID)) {
return true;
}
}
return false;
}
public static Grammar getNonTerminalsSuperGrammar(Grammar grammar) {
for (Grammar g : grammar.getUsedGrammars()) {
if (!TERMINALS.equals(g.getName())) {
return g;
}
}
return null;
}
}

View file

@ -1,33 +0,0 @@
/*******************************************************************************
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.xtext.generator.util
import org.eclipse.xtext.Grammar
import org.eclipse.xtext.GrammarUtil
class GrammarUtil2 extends GrammarUtil {
public static val TERMINALS = 'org.eclipse.xtext.common.Terminals'
static def boolean inherits(Grammar grammar, String languageID) {
if (grammar.name == languageID)
return true
for (grammar2 : grammar.usedGrammars) {
if (grammar2.inherits(languageID)) {
return true
}
}
return false
}
static def Grammar getNonTerminalsSuperGrammar(Grammar grammar) {
grammar.usedGrammars.findFirst[name != TERMINALS]
}
}

View file

@ -0,0 +1,44 @@
/*******************************************************************************
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.xtext.generator.xbase;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.xtext.AbstractRule;
import org.eclipse.xtext.Grammar;
import org.eclipse.xtext.GrammarUtil;
import org.eclipse.xtext.xtext.UsedRulesFinder;
import org.eclipse.xtext.xtext.generator.util.GrammarUtil2;
public class XbaseUsageDetector {
public boolean inheritsXtype(Grammar grammar) {
return GrammarUtil2.inherits(grammar, "org.eclipse.xtext.xbase.Xtype");
}
public boolean inheritsXbase(Grammar grammar) {
return GrammarUtil2.inherits(grammar, "org.eclipse.xtext.xbase.Xbase");
}
public boolean inheritsXbaseWithAnnotations(Grammar grammar) {
return GrammarUtil2.inherits(grammar, "org.eclipse.xtext.xbase.annotations.XbaseWithAnnotations");
}
public boolean usesXImportSection(Grammar grammar) {
Set<AbstractRule> usedRules = new HashSet<>();
new UsedRulesFinder(usedRules).compute(grammar);
for (AbstractRule it : usedRules) {
if ("XImportSection".equals(it.getName())
&& "org.eclipse.xtext.xbase.Xtype".equals(GrammarUtil.getGrammar(it).getName())) {
return true;
}
}
return false;
}
}

View file

@ -1,31 +0,0 @@
package org.eclipse.xtext.xtext.generator.xbase
import java.util.Set
import org.eclipse.xtext.AbstractRule
import org.eclipse.xtext.Grammar
import org.eclipse.xtext.GrammarUtil
import org.eclipse.xtext.xtext.UsedRulesFinder
import static extension org.eclipse.xtext.xtext.generator.util.GrammarUtil2.*
class XbaseUsageDetector {
def boolean inheritsXtype(Grammar grammar) {
grammar.inherits('org.eclipse.xtext.xbase.Xtype')
}
def boolean inheritsXbase(Grammar grammar) {
grammar.inherits('org.eclipse.xtext.xbase.Xbase')
}
def boolean inheritsXbaseWithAnnotations(Grammar grammar) {
grammar.inherits('org.eclipse.xtext.xbase.annotations.XbaseWithAnnotations')
}
def boolean usesXImportSection(Grammar grammar) {
val Set<AbstractRule> usedRules = newHashSet
new UsedRulesFinder(usedRules).compute(grammar)
return usedRules.exists [
name == 'XImportSection' && GrammarUtil.getGrammar(it).name == 'org.eclipse.xtext.xbase.Xtype'
]
}
}

View file

@ -0,0 +1,26 @@
/**
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.xtext.generator.xbase;
import org.eclipse.xtext.xtext.generator.AbstractXtextGeneratorFragment;
import com.google.inject.Inject;
public class XtypeGeneratorFragment2 extends AbstractXtextGeneratorFragment {
@Inject
private XbaseUsageDetector xbaseUsageDetector;
@Override
public void generate() {
if (xbaseUsageDetector.inheritsXtype(getLanguage().getGrammar())
&& getProjectConfig().getEclipsePlugin().getManifest() != null) {
getProjectConfig().getEclipsePlugin().getManifest().getRequiredBundles().add("org.eclipse.xtext.xbase.ui");
}
}
}

View file

@ -1,23 +0,0 @@
/*******************************************************************************
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.xtext.generator.xbase
import com.google.inject.Inject
import org.eclipse.xtext.xtext.generator.AbstractXtextGeneratorFragment
class XtypeGeneratorFragment2 extends AbstractXtextGeneratorFragment {
@Inject extension XbaseUsageDetector
override generate() {
if (language.grammar.inheritsXtype && projectConfig.eclipsePlugin.manifest !== null)
projectConfig.eclipsePlugin.manifest.requiredBundles += 'org.eclipse.xtext.xbase.ui'
}
}

View file

@ -1,111 +0,0 @@
/**
* Copyright (c) 2015, 2017 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.xtext.generator.model.annotations;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.eclipse.xtend.lib.annotations.Accessors;
import org.eclipse.xtext.util.Strings;
import org.eclipse.xtext.xbase.lib.Pure;
import org.eclipse.xtext.xtext.generator.XtextGenerator;
import org.eclipse.xtext.xtext.generator.model.JavaFileAccess;
import org.eclipse.xtext.xtext.generator.model.TypeReference;
import org.eclipse.xtext.xtext.generator.model.annotations.IClassAnnotation;
/**
* A class annotation configuration for the <code>@Generated</code> annotation.
*/
@Accessors
@SuppressWarnings("all")
public class GeneratedClassAnnotation implements IClassAnnotation {
private boolean includeDate = false;
private String comment;
@Override
public CharSequence generate() {
final StringBuilder stringBuilder = new StringBuilder("@Generated(");
if ((this.includeDate || (!Strings.isEmpty(this.comment)))) {
this.operator_add(stringBuilder, "value = ");
}
this.operator_add(stringBuilder, "\"");
String _generatorName = this.getGeneratorName();
this.operator_add(stringBuilder, _generatorName);
this.operator_add(stringBuilder, "\"");
if (this.includeDate) {
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd\'T\'HH:mmZ");
Date _date = new Date();
final String date = dateFormat.format(_date);
this.operator_add(stringBuilder, ", date = \"");
this.operator_add(stringBuilder, date);
this.operator_add(stringBuilder, "\"");
}
boolean _isEmpty = Strings.isEmpty(this.comment);
boolean _not = (!_isEmpty);
if (_not) {
final String convertedComment = Strings.convertToJavaString(this.comment);
this.operator_add(stringBuilder, ", comments = \"");
this.operator_add(stringBuilder, convertedComment);
this.operator_add(stringBuilder, "\"");
}
this.operator_add(stringBuilder, ")");
return stringBuilder;
}
protected String getGeneratorName() {
return XtextGenerator.class.getName();
}
@Override
public boolean appliesTo(final JavaFileAccess javaFile) {
return javaFile.isMarkedAsGenerated();
}
@Override
public TypeReference getAnnotationImport() {
return new TypeReference("javax.annotation.Generated");
}
private void operator_add(final StringBuilder stringBuilder, final String s) {
stringBuilder.append(s);
}
@Override
public String toString() {
return this.generate().toString();
}
@Override
public boolean equals(final Object obj) {
return (obj instanceof GeneratedClassAnnotation);
}
@Override
public int hashCode() {
return this.getClass().getName().hashCode();
}
@Pure
public boolean isIncludeDate() {
return this.includeDate;
}
public void setIncludeDate(final boolean includeDate) {
this.includeDate = includeDate;
}
@Pure
public String getComment() {
return this.comment;
}
public void setComment(final String comment) {
this.comment = comment;
}
}

View file

@ -1,83 +0,0 @@
/**
* Copyright (c) 2015, 2017 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.xtext.generator.model.annotations;
import org.eclipse.xtend.lib.annotations.Accessors;
import org.eclipse.xtext.xbase.lib.Conversions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.Pure;
import org.eclipse.xtext.xtext.generator.model.JavaFileAccess;
import org.eclipse.xtext.xtext.generator.model.TypeReference;
import org.eclipse.xtext.xtext.generator.model.annotations.IClassAnnotation;
/**
* A class annotation configuration for the <code>@SuppressWarnings</code> annotation.
*/
@Accessors
@SuppressWarnings("all")
public class SuppressWarningsAnnotation implements IClassAnnotation {
private String suppress = "all";
@Override
public CharSequence generate() {
final StringBuilder stringBuilder = new StringBuilder("@SuppressWarnings(");
final String[] suppressedWarnings = this.suppress.split("\\s*,\\s*");
int _length = suppressedWarnings.length;
boolean _notEquals = (_length != 1);
if (_notEquals) {
stringBuilder.append("{");
}
final Function1<String, CharSequence> _function = (String it) -> {
return it;
};
stringBuilder.append(IterableExtensions.<String>join(((Iterable<String>)Conversions.doWrapArray(suppressedWarnings)), "\"", "\", \"", "\"", _function));
int _length_1 = suppressedWarnings.length;
boolean _notEquals_1 = (_length_1 != 1);
if (_notEquals_1) {
stringBuilder.append("}");
}
stringBuilder.append(")");
return stringBuilder;
}
@Override
public boolean appliesTo(final JavaFileAccess javaFile) {
return true;
}
@Override
public TypeReference getAnnotationImport() {
return new TypeReference(SuppressWarnings.class);
}
@Override
public String toString() {
return this.generate().toString();
}
@Override
public boolean equals(final Object obj) {
return (obj instanceof SuppressWarningsAnnotation);
}
@Override
public int hashCode() {
return this.getClass().getName().hashCode();
}
@Pure
public String getSuppress() {
return this.suppress;
}
public void setSuppress(final String suppress) {
this.suppress = suppress;
}
}

View file

@ -1,87 +0,0 @@
/**
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.xtext.generator.parser.antlr;
import org.eclipse.xtend.lib.annotations.Data;
import org.eclipse.xtext.xbase.lib.Pure;
@Data
@SuppressWarnings("all")
public class AntlrGrammar {
private final String packageName;
private final String simpleName;
public String getName() {
return ((this.packageName + ".") + this.simpleName);
}
public String getGrammarFileName() {
String _replace = this.getName().replace(".", "/");
return (_replace + ".g");
}
public String getTokensFileName() {
String _replace = this.getName().replace(".", "/");
return (_replace + ".tokens");
}
@Override
public String toString() {
return this.getName();
}
public AntlrGrammar(final String packageName, final String simpleName) {
super();
this.packageName = packageName;
this.simpleName = simpleName;
}
@Override
@Pure
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((this.packageName== null) ? 0 : this.packageName.hashCode());
return prime * result + ((this.simpleName== null) ? 0 : this.simpleName.hashCode());
}
@Override
@Pure
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AntlrGrammar other = (AntlrGrammar) obj;
if (this.packageName == null) {
if (other.packageName != null)
return false;
} else if (!this.packageName.equals(other.packageName))
return false;
if (this.simpleName == null) {
if (other.simpleName != null)
return false;
} else if (!this.simpleName.equals(other.simpleName))
return false;
return true;
}
@Pure
public String getPackageName() {
return this.packageName;
}
@Pure
public String getSimpleName() {
return this.simpleName;
}
}

View file

@ -1,181 +0,0 @@
/**
* Copyright (c) 2008 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.xtext.generator.parser.antlr;
import org.eclipse.xtend.lib.annotations.AccessorType;
import org.eclipse.xtend.lib.annotations.Accessors;
import org.eclipse.xtext.xbase.lib.Pure;
import org.eclipse.xtext.xtext.generator.parser.antlr.splitting.AntlrParserSplitter;
import org.eclipse.xtext.xtext.generator.parser.antlr.splitting.PartialClassExtractor;
import org.eclipse.xtext.xtext.generator.parser.antlr.splitting.internal.LexerSpecialStateTransitionSplitter;
@Accessors
@SuppressWarnings("all")
public class AntlrOptions {
private boolean backtrack = false;
private boolean backtrackLexer = false;
private boolean memoize = false;
private int k = (-1);
private boolean ignoreCase = false;
private boolean classSplitting = false;
@Accessors(AccessorType.PUBLIC_GETTER)
private int fieldsPerClass = AntlrParserSplitter.FIELDS_PER_CLASS;
@Accessors(AccessorType.PUBLIC_GETTER)
private int methodsPerClass = PartialClassExtractor.METHODS_PER_CLASS;
@Accessors(AccessorType.PUBLIC_GETTER)
private int casesPerSpecialStateSwitch = LexerSpecialStateTransitionSplitter.CASES_PER_SPECIAL_STATE_SWITCH;
private boolean skipUnusedRules = false;
private boolean optimizeCodeQuality = true;
private boolean stripAllComments = false;
private String keptBitSetsPattern;
private String keptBitSetName;
public void setFieldsPerClass(final String fieldsPerClass) {
this.fieldsPerClass = Integer.parseInt(fieldsPerClass);
}
public void setMethodsPerClass(final String methodsPerClass) {
this.methodsPerClass = Integer.parseInt(methodsPerClass);
}
public void setCasesPerSpecialStateSwitch(final String casesPerSpecialStateSwitch) {
this.casesPerSpecialStateSwitch = Integer.parseInt(casesPerSpecialStateSwitch);
}
public void setKAsString(final String k) {
this.k = Integer.parseInt(k);
}
@Pure
public boolean isBacktrack() {
return this.backtrack;
}
public void setBacktrack(final boolean backtrack) {
this.backtrack = backtrack;
}
@Pure
public boolean isBacktrackLexer() {
return this.backtrackLexer;
}
public void setBacktrackLexer(final boolean backtrackLexer) {
this.backtrackLexer = backtrackLexer;
}
@Pure
public boolean isMemoize() {
return this.memoize;
}
public void setMemoize(final boolean memoize) {
this.memoize = memoize;
}
@Pure
public int getK() {
return this.k;
}
public void setK(final int k) {
this.k = k;
}
@Pure
public boolean isIgnoreCase() {
return this.ignoreCase;
}
public void setIgnoreCase(final boolean ignoreCase) {
this.ignoreCase = ignoreCase;
}
@Pure
public boolean isClassSplitting() {
return this.classSplitting;
}
public void setClassSplitting(final boolean classSplitting) {
this.classSplitting = classSplitting;
}
@Pure
public int getFieldsPerClass() {
return this.fieldsPerClass;
}
@Pure
public int getMethodsPerClass() {
return this.methodsPerClass;
}
@Pure
public int getCasesPerSpecialStateSwitch() {
return this.casesPerSpecialStateSwitch;
}
@Pure
public boolean isSkipUnusedRules() {
return this.skipUnusedRules;
}
public void setSkipUnusedRules(final boolean skipUnusedRules) {
this.skipUnusedRules = skipUnusedRules;
}
@Pure
public boolean isOptimizeCodeQuality() {
return this.optimizeCodeQuality;
}
public void setOptimizeCodeQuality(final boolean optimizeCodeQuality) {
this.optimizeCodeQuality = optimizeCodeQuality;
}
@Pure
public boolean isStripAllComments() {
return this.stripAllComments;
}
public void setStripAllComments(final boolean stripAllComments) {
this.stripAllComments = stripAllComments;
}
@Pure
public String getKeptBitSetsPattern() {
return this.keptBitSetsPattern;
}
public void setKeptBitSetsPattern(final String keptBitSetsPattern) {
this.keptBitSetsPattern = keptBitSetsPattern;
}
@Pure
public String getKeptBitSetName() {
return this.keptBitSetName;
}
public void setKeptBitSetName(final String keptBitSetName) {
this.keptBitSetName = keptBitSetName;
}
}

View file

@ -1,46 +0,0 @@
/**
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.xtext.generator.parser.antlr;
import com.google.common.base.Objects;
/**
* Static utility that allows to check if the current thread is producing
* a debug grammar or not.
*
* @author Sebastian Zarnekow - Initial contribution and API
* @noreference
*/
@SuppressWarnings("all")
public class DebugGrammarToken {
private static ThreadLocal<Boolean> store = new ThreadLocal<Boolean>();
public static DebugGrammarToken aquire() {
Boolean _get = DebugGrammarToken.store.get();
boolean _tripleNotEquals = (_get != null);
if (_tripleNotEquals) {
String _valueOf = String.valueOf(DebugGrammarToken.store.get());
throw new IllegalStateException(_valueOf);
}
return new DebugGrammarToken();
}
public static boolean isGeneratingDebugGrammar() {
Boolean _get = DebugGrammarToken.store.get();
return Objects.equal(_get, Boolean.TRUE);
}
private DebugGrammarToken() {
DebugGrammarToken.store.set(Boolean.TRUE);
}
public void done() {
DebugGrammarToken.store.set(null);
}
}

View file

@ -1,45 +0,0 @@
/**
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.xtext.generator.util;
import com.google.common.base.Objects;
import org.eclipse.emf.common.util.EList;
import org.eclipse.xtext.Grammar;
import org.eclipse.xtext.GrammarUtil;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
@SuppressWarnings("all")
public class GrammarUtil2 extends GrammarUtil {
public static final String TERMINALS = "org.eclipse.xtext.common.Terminals";
public static boolean inherits(final Grammar grammar, final String languageID) {
String _name = grammar.getName();
boolean _equals = Objects.equal(_name, languageID);
if (_equals) {
return true;
}
EList<Grammar> _usedGrammars = grammar.getUsedGrammars();
for (final Grammar grammar2 : _usedGrammars) {
boolean _inherits = GrammarUtil2.inherits(grammar2, languageID);
if (_inherits) {
return true;
}
}
return false;
}
public static Grammar getNonTerminalsSuperGrammar(final Grammar grammar) {
final Function1<Grammar, Boolean> _function = (Grammar it) -> {
String _name = it.getName();
return Boolean.valueOf((!Objects.equal(_name, GrammarUtil2.TERMINALS)));
};
return IterableExtensions.<Grammar>findFirst(grammar.getUsedGrammars(), _function);
}
}

View file

@ -1,36 +0,0 @@
package org.eclipse.xtext.xtext.generator.xbase;
import com.google.common.base.Objects;
import java.util.Set;
import org.eclipse.xtext.AbstractRule;
import org.eclipse.xtext.Grammar;
import org.eclipse.xtext.GrammarUtil;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xtext.UsedRulesFinder;
import org.eclipse.xtext.xtext.generator.util.GrammarUtil2;
@SuppressWarnings("all")
public class XbaseUsageDetector {
public boolean inheritsXtype(final Grammar grammar) {
return GrammarUtil2.inherits(grammar, "org.eclipse.xtext.xbase.Xtype");
}
public boolean inheritsXbase(final Grammar grammar) {
return GrammarUtil2.inherits(grammar, "org.eclipse.xtext.xbase.Xbase");
}
public boolean inheritsXbaseWithAnnotations(final Grammar grammar) {
return GrammarUtil2.inherits(grammar, "org.eclipse.xtext.xbase.annotations.XbaseWithAnnotations");
}
public boolean usesXImportSection(final Grammar grammar) {
final Set<AbstractRule> usedRules = CollectionLiterals.<AbstractRule>newHashSet();
new UsedRulesFinder(usedRules).compute(grammar);
final Function1<AbstractRule, Boolean> _function = (AbstractRule it) -> {
return Boolean.valueOf((Objects.equal(it.getName(), "XImportSection") && Objects.equal(GrammarUtil.getGrammar(it).getName(), "org.eclipse.xtext.xbase.Xtype")));
};
return IterableExtensions.<AbstractRule>exists(usedRules, _function);
}
}

View file

@ -1,30 +0,0 @@
/**
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.xtext.generator.xbase;
import com.google.inject.Inject;
import java.util.Set;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xtext.generator.AbstractXtextGeneratorFragment;
import org.eclipse.xtext.xtext.generator.xbase.XbaseUsageDetector;
@SuppressWarnings("all")
public class XtypeGeneratorFragment2 extends AbstractXtextGeneratorFragment {
@Inject
@Extension
private XbaseUsageDetector _xbaseUsageDetector;
@Override
public void generate() {
if ((this._xbaseUsageDetector.inheritsXtype(this.getLanguage().getGrammar()) && (this.getProjectConfig().getEclipsePlugin().getManifest() != null))) {
Set<String> _requiredBundles = this.getProjectConfig().getEclipsePlugin().getManifest().getRequiredBundles();
_requiredBundles.add("org.eclipse.xtext.xbase.ui");
}
}
}