use of org.eclipse.xtext.xbase.lib.Pair in project xtext-core by eclipse.
the class AbstractAntlrGrammarGenerator method compileKeywordRules.
protected CharSequence compileKeywordRules(final Grammar it, final AntlrOptions options) {
CharSequence _xblockexpression = null;
{
final Set<String> allKeywords = this.keywordHelper.getAllKeywords();
final List<TerminalRule> allTerminalRules = GrammarUtil.allTerminalRules(it);
final ArrayList<String> synthetic_kw_alternatives = CollectionLiterals.<String>newArrayList();
final Function1<Pair<Integer, String>, String> _function = (Pair<Integer, String> it_1) -> {
final String ruleName = this.keywordHelper.getRuleName(it_1.getValue());
StringConcatenation _builder = new StringConcatenation();
_builder.append("(FRAGMENT_");
_builder.append(ruleName);
_builder.append(")=> FRAGMENT_");
_builder.append(ruleName);
_builder.append(" {$type = ");
_builder.append(ruleName);
_builder.append("; }");
return _builder.toString();
};
Iterables.<String>addAll(synthetic_kw_alternatives, IterableExtensions.<Pair<Integer, String>, String>map(IterableExtensions.<String>indexed(allKeywords), _function));
final Function1<Pair<Integer, TerminalRule>, String> _function_1 = (Pair<Integer, TerminalRule> it_1) -> {
if (((!this._syntheticTerminalDetector.isSyntheticTerminalRule(it_1.getValue())) && (!it_1.getValue().isFragment()))) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("(FRAGMENT_");
String _ruleName = this._grammarAccessExtensions.ruleName(it_1.getValue());
_builder.append(_ruleName);
_builder.append(")=> FRAGMENT_");
String _ruleName_1 = this._grammarAccessExtensions.ruleName(it_1.getValue());
_builder.append(_ruleName_1);
_builder.append(" {$type = ");
String _ruleName_2 = this._grammarAccessExtensions.ruleName(it_1.getValue());
_builder.append(_ruleName_2);
_builder.append("; }");
return _builder.toString();
}
return null;
};
synthetic_kw_alternatives.addAll(IterableExtensions.<String>toList(IterableExtensions.<String>filterNull(IterableExtensions.<Pair<Integer, TerminalRule>, String>map(IterableExtensions.<TerminalRule>indexed(allTerminalRules), _function_1))));
StringConcatenation _builder = new StringConcatenation();
{
boolean _isBacktrackLexer = options.isBacktrackLexer();
if (_isBacktrackLexer) {
_builder.append("SYNTHETIC_ALL_KEYWORDS :");
_builder.newLine();
{
boolean _hasElements = false;
for (final String kw : synthetic_kw_alternatives) {
if (!_hasElements) {
_hasElements = true;
} else {
_builder.appendImmediate(" |", "\t");
}
_builder.append("\t");
_builder.append(kw, "\t");
_builder.newLineIfNotEmpty();
}
}
_builder.append(";");
_builder.newLine();
_builder.newLine();
{
for (final String kw_1 : allKeywords) {
_builder.append("fragment FRAGMENT_");
String _ruleName = this.keywordHelper.getRuleName(kw_1);
_builder.append(_ruleName);
_builder.append(" : \'");
String _antlrString = AntlrGrammarGenUtil.toAntlrString(kw_1);
_builder.append(_antlrString);
_builder.append("\';");
_builder.newLineIfNotEmpty();
_builder.newLine();
}
}
} else {
{
for (final String rule : allKeywords) {
_builder.newLine();
CharSequence _compileRule = this.compileRule(rule, it, options);
_builder.append(_compileRule);
_builder.newLineIfNotEmpty();
}
}
}
}
_xblockexpression = _builder;
}
return _xblockexpression;
}
use of org.eclipse.xtext.xbase.lib.Pair in project xtext-core by eclipse.
the class XtextGenerator method generatePluginXmls.
protected void generatePluginXmls() {
// Filter null values and merge duplicate entries
Iterable<BundleProjectConfig> bundleProjects = Iterables.filter(projectConfig.getEnabledProjects(), BundleProjectConfig.class);
List<Pair<PluginXmlAccess, IXtextGeneratorFileSystemAccess>> pluginXmls = IterableExtensions.toList(Iterables.transform(bundleProjects, (BundleProjectConfig it) -> {
return Pair.of(it.getPluginXml(), it.getRoot());
}));
HashMap<URI, PluginXmlAccess> uri2PluginXml = Maps.newHashMapWithExpectedSize(pluginXmls.size());
ListIterator<Pair<PluginXmlAccess, IXtextGeneratorFileSystemAccess>> pluginXmlIter = pluginXmls.listIterator();
while (pluginXmlIter.hasNext()) {
Pair<PluginXmlAccess, IXtextGeneratorFileSystemAccess> entry = pluginXmlIter.next();
PluginXmlAccess pluginXml = entry.getKey();
IXtextGeneratorFileSystemAccess root = entry.getValue();
if (pluginXml == null || root == null) {
pluginXmlIter.remove();
} else {
URI uri = root.getURI(pluginXml.getPath());
if (uri2PluginXml.containsKey(uri)) {
uri2PluginXml.get(uri).merge(pluginXml);
pluginXmlIter.remove();
} else {
uri2PluginXml.put(uri, pluginXml);
}
}
}
for (Pair<PluginXmlAccess, IXtextGeneratorFileSystemAccess> entry : pluginXmls) {
PluginXmlAccess pluginXml = entry.getKey();
IXtextGeneratorFileSystemAccess root = entry.getValue();
if (root.isFile(pluginXml.getPath())) {
// differs
if (!pluginXml.getEntries().isEmpty()) {
String textFileContent = null;
CharSequence textFile = root.readTextFile(pluginXml.getPath());
if (textFile != null) {
textFileContent = textFile.toString();
}
if (!Objects.equal(textFileContent, pluginXml.getContentString())) {
if (pluginXml.getPath().endsWith(".xml")) {
pluginXml.setPath(pluginXml.getPath() + "_gen");
pluginXml.writeTo(root);
}
}
}
} else {
pluginXml.writeTo(root);
}
}
}
use of org.eclipse.xtext.xbase.lib.Pair in project xtext-core by eclipse.
the class SerializerFragment2 method generate.
@Override
public void generate() {
new GuiceModuleAccess.BindingFactory().addTypeToType(TypeReference.typeRef(ISemanticSequencer.class), this.getSemanticSequencerClass(this.getGrammar())).addTypeToType(TypeReference.typeRef(ISyntacticSequencer.class), this.getSyntacticSequencerClass(this.getGrammar())).addTypeToType(TypeReference.typeRef(ISerializer.class), TypeReference.typeRef(Serializer.class)).contributeTo(this.getLanguage().getRuntimeGenModule());
ManifestAccess _manifest = this.getProjectConfig().getRuntime().getManifest();
boolean _tripleNotEquals = (_manifest != null);
if (_tripleNotEquals) {
Set<String> _exportedPackages = this.getProjectConfig().getRuntime().getManifest().getExportedPackages();
String _serializerBasePackage = this.getSerializerBasePackage(this.getGrammar());
_exportedPackages.add(_serializerBasePackage);
Set<String> _requiredBundles = this.getProjectConfig().getRuntime().getManifest().getRequiredBundles();
String _xbaseLibVersionLowerBound = this.getProjectConfig().getRuntime().getXbaseLibVersionLowerBound();
String _plus = ("org.eclipse.xtext.xbase.lib;bundle-version=\"" + _xbaseLibVersionLowerBound);
String _plus_1 = (_plus + "\"");
_requiredBundles.add(_plus_1);
}
this.generateAbstractSemanticSequencer();
this.generateAbstractSyntacticSequencer();
boolean _isGenerateStub = this.isGenerateStub();
if (_isGenerateStub) {
this.generateSemanticSequencer();
this.generateSyntacticSequencer();
}
if (this.generateDebugData) {
this.generateGrammarConstraints();
Iterable<Pair<String, String>> _generateDebugGraphs = this.debugGraphGenerator.generateDebugGraphs();
for (final Pair<String, String> fileToContent : _generateDebugGraphs) {
this.getProjectConfig().getRuntime().getSrcGen().generateFile(fileToContent.getKey(), fileToContent.getValue());
}
}
}
use of org.eclipse.xtext.xbase.lib.Pair in project xtext-core by eclipse.
the class SerializerFragment2 method generateAbstractSemanticSequencer.
protected void generateAbstractSemanticSequencer() {
final Collection<IGrammarConstraintProvider.IConstraint> localConstraints = this._semanticSequencerExtensions.getGrammarConstraints(this.getGrammar());
final Collection<IGrammarConstraintProvider.IConstraint> superConstraints = this._semanticSequencerExtensions.getGrammarConstraints(this._semanticSequencerExtensions.getSuperGrammar(this.getGrammar()));
final Function1<IGrammarConstraintProvider.IConstraint, Boolean> _function = (IGrammarConstraintProvider.IConstraint it) -> {
return Boolean.valueOf(((it.getType() != null) && (!superConstraints.contains(it))));
};
final Set<IGrammarConstraintProvider.IConstraint> newLocalConstraints = IterableExtensions.<IGrammarConstraintProvider.IConstraint>toSet(IterableExtensions.<IGrammarConstraintProvider.IConstraint>filter(localConstraints, _function));
TypeReference _xifexpression = null;
boolean _isGenerateStub = this.isGenerateStub();
if (_isGenerateStub) {
_xifexpression = this.getAbstractSemanticSequencerClass(this.getGrammar());
} else {
_xifexpression = this.getSemanticSequencerClass(this.getGrammar());
}
final TypeReference clazz = _xifexpression;
TypeReference _xifexpression_1 = null;
final Function1<IGrammarConstraintProvider.IConstraint, Boolean> _function_1 = (IGrammarConstraintProvider.IConstraint it) -> {
return Boolean.valueOf(superConstraints.contains(it));
};
boolean _exists = IterableExtensions.<IGrammarConstraintProvider.IConstraint>exists(localConstraints, _function_1);
if (_exists) {
_xifexpression_1 = this.getSemanticSequencerClass(IterableExtensions.<Grammar>head(this.getGrammar().getUsedGrammars()));
} else {
_xifexpression_1 = TypeReference.typeRef(AbstractDelegatingSemanticSequencer.class);
}
final TypeReference superClazz = _xifexpression_1;
final GeneratedJavaFileAccess javaFile = this.fileAccessFactory.createGeneratedJavaFile(clazz);
javaFile.setResourceSet(this.getLanguage().getResourceSet());
final HashSet<Pair<String, EClass>> methodSignatures = CollectionLiterals.<Pair<String, EClass>>newHashSet();
StringConcatenationClient _client = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
_builder.append("public ");
{
boolean _isGenerateStub = SerializerFragment2.this.isGenerateStub();
if (_isGenerateStub) {
_builder.append("abstract ");
}
}
_builder.append("class ");
String _simpleName = clazz.getSimpleName();
_builder.append(_simpleName);
_builder.append(" extends ");
_builder.append(superClazz);
_builder.append(" {");
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("\t");
_builder.append("@");
_builder.append(Inject.class, "\t");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("private ");
TypeReference _grammarAccess = SerializerFragment2.this._grammarAccessExtensions.getGrammarAccess(SerializerFragment2.this.getGrammar());
_builder.append(_grammarAccess, "\t");
_builder.append(" grammarAccess;");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
StringConcatenationClient _genMethodCreateSequence = SerializerFragment2.this.genMethodCreateSequence();
_builder.append(_genMethodCreateSequence, "\t");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.newLine();
{
List<IGrammarConstraintProvider.IConstraint> _sort = IterableExtensions.<IGrammarConstraintProvider.IConstraint>sort(newLocalConstraints);
for (final IGrammarConstraintProvider.IConstraint c : _sort) {
{
String _simpleName_1 = c.getSimpleName();
EClass _type = c.getType();
Pair<String, EClass> _mappedTo = Pair.<String, EClass>of(_simpleName_1, _type);
boolean _add = methodSignatures.add(_mappedTo);
if (_add) {
_builder.append("\t");
StringConcatenationClient _genMethodSequence = SerializerFragment2.this.genMethodSequence(c);
_builder.append(_genMethodSequence, "\t");
_builder.newLineIfNotEmpty();
} else {
_builder.append("\t");
String _simpleName_2 = clazz.getSimpleName();
String _plus = ("Skipped generating duplicate method in " + _simpleName_2);
SerializerFragment2.LOG.warn(_plus);
_builder.newLineIfNotEmpty();
_builder.append("\t");
StringConcatenationClient _genMethodSequenceComment = SerializerFragment2.this.genMethodSequenceComment(c);
_builder.append(_genMethodSequenceComment, "\t");
_builder.newLineIfNotEmpty();
}
}
_builder.append("\t");
_builder.newLine();
}
}
_builder.append("}");
_builder.newLine();
}
};
javaFile.setContent(_client);
List<IClassAnnotation> _annotations = javaFile.getAnnotations();
SuppressWarningsAnnotation _suppressWarningsAnnotation = new SuppressWarningsAnnotation();
_annotations.add(_suppressWarningsAnnotation);
javaFile.writeTo(this.getProjectConfig().getRuntime().getSrcGen());
}
use of org.eclipse.xtext.xbase.lib.Pair in project xtext-core by eclipse.
the class SerializerFragment2 method genMethodCreateSequenceCaseBody.
private StringConcatenationClient genMethodCreateSequenceCaseBody(final Map<IGrammarConstraintProvider.IConstraint, IGrammarConstraintProvider.IConstraint> superConstraints, final EClass type) {
StringConcatenationClient _xblockexpression = null;
{
final Function1<Map.Entry<IGrammarConstraintProvider.IConstraint, List<ISerializationContext>>, String> _function = (Map.Entry<IGrammarConstraintProvider.IConstraint, List<ISerializationContext>> it) -> {
return it.getKey().getName();
};
final List<Map.Entry<IGrammarConstraintProvider.IConstraint, List<ISerializationContext>>> contexts = IterableExtensions.<Map.Entry<IGrammarConstraintProvider.IConstraint, List<ISerializationContext>>, String>sortBy(this._semanticSequencerExtensions.getGrammarConstraints(this.getGrammar(), type).entrySet(), _function);
final LinkedHashMultimap<EObject, IGrammarConstraintProvider.IConstraint> context2constraint = LinkedHashMultimap.<EObject, IGrammarConstraintProvider.IConstraint>create();
for (final Map.Entry<IGrammarConstraintProvider.IConstraint, List<ISerializationContext>> e : contexts) {
List<ISerializationContext> _value = e.getValue();
for (final ISerializationContext ctx : _value) {
context2constraint.put(((SerializationContext) ctx).getActionOrRule(), e.getKey());
}
}
StringConcatenationClient _client = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
{
int _size = contexts.size();
boolean _greaterThan = (_size > 1);
if (_greaterThan) {
{
Iterable<Pair<Integer, Map.Entry<IGrammarConstraintProvider.IConstraint, List<ISerializationContext>>>> _indexed = IterableExtensions.<Map.Entry<IGrammarConstraintProvider.IConstraint, List<ISerializationContext>>>indexed(contexts);
for (final Pair<Integer, Map.Entry<IGrammarConstraintProvider.IConstraint, List<ISerializationContext>>> ctx : _indexed) {
{
Integer _key = ctx.getKey();
boolean _greaterThan_1 = ((_key).intValue() > 0);
if (_greaterThan_1) {
_builder.append("else ");
}
}
_builder.append("if (");
StringConcatenationClient _genCondition = SerializerFragment2.this.genCondition(ctx.getValue().getValue(), ctx.getValue().getKey(), context2constraint);
_builder.append(_genCondition);
_builder.append(") {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
StringConcatenationClient _genMethodCreateSequenceCall = SerializerFragment2.this.genMethodCreateSequenceCall(superConstraints, type, ctx.getValue().getKey());
_builder.append(_genMethodCreateSequenceCall, "\t");
_builder.newLineIfNotEmpty();
_builder.append("}");
_builder.newLine();
}
}
_builder.append("else break;");
_builder.newLine();
} else {
int _size_1 = contexts.size();
boolean _equals = (_size_1 == 1);
if (_equals) {
StringConcatenationClient _genMethodCreateSequenceCall_1 = SerializerFragment2.this.genMethodCreateSequenceCall(superConstraints, type, IterableExtensions.<Map.Entry<IGrammarConstraintProvider.IConstraint, List<ISerializationContext>>>head(contexts).getKey());
_builder.append(_genMethodCreateSequenceCall_1);
_builder.newLineIfNotEmpty();
} else {
_builder.append("// error, no contexts. ");
_builder.newLine();
}
}
}
}
};
_xblockexpression = _client;
}
return _xblockexpression;
}
Aggregations