Search in sources :

Example 46 with Pair

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;
}
Also used : Set(java.util.Set) ArrayList(java.util.ArrayList) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) List(java.util.List) TerminalRule(org.eclipse.xtext.TerminalRule) Pair(org.eclipse.xtext.xbase.lib.Pair)

Example 47 with Pair

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);
        }
    }
}
Also used : PluginXmlAccess(org.eclipse.xtext.xtext.generator.model.PluginXmlAccess) BundleProjectConfig(org.eclipse.xtext.xtext.generator.model.project.BundleProjectConfig) IXtextGeneratorFileSystemAccess(org.eclipse.xtext.xtext.generator.model.IXtextGeneratorFileSystemAccess) URI(org.eclipse.emf.common.util.URI) Pair(org.eclipse.xtext.xbase.lib.Pair)

Example 48 with Pair

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());
        }
    }
}
Also used : ISyntacticSequencer(org.eclipse.xtext.serializer.sequencer.ISyntacticSequencer) ManifestAccess(org.eclipse.xtext.xtext.generator.model.ManifestAccess) Pair(org.eclipse.xtext.xbase.lib.Pair)

Example 49 with Pair

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());
}
Also used : StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) AbstractDelegatingSemanticSequencer(org.eclipse.xtext.serializer.sequencer.AbstractDelegatingSemanticSequencer) Grammar(org.eclipse.xtext.Grammar) SuppressWarningsAnnotation(org.eclipse.xtext.xtext.generator.model.annotations.SuppressWarningsAnnotation) EClass(org.eclipse.emf.ecore.EClass) IClassAnnotation(org.eclipse.xtext.xtext.generator.model.annotations.IClassAnnotation) IGrammarConstraintProvider(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider) GeneratedJavaFileAccess(org.eclipse.xtext.xtext.generator.model.GeneratedJavaFileAccess) List(java.util.List) TypeReference(org.eclipse.xtext.xtext.generator.model.TypeReference) Pair(org.eclipse.xtext.xbase.lib.Pair)

Example 50 with Pair

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;
}
Also used : ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) SerializationContext(org.eclipse.xtext.serializer.analysis.SerializationContext) LinkedHashMultimap(com.google.common.collect.LinkedHashMultimap) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) IGrammarConstraintProvider(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider) List(java.util.List) SerializationContextMap(org.eclipse.xtext.serializer.analysis.SerializationContextMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Pair(org.eclipse.xtext.xbase.lib.Pair)

Aggregations

Pair (org.eclipse.xtext.xbase.lib.Pair)67 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)22 Test (org.junit.Test)15 Collection (java.util.Collection)11 List (java.util.List)11 AbstractHierarchyBuilderTest (org.eclipse.xtext.junit4.ide.AbstractHierarchyBuilderTest)9 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)9 ArrayList (java.util.ArrayList)8 Map (java.util.Map)7 EObject (org.eclipse.emf.ecore.EObject)6 Resource (org.eclipse.emf.ecore.resource.Resource)6 AbstractElement (org.eclipse.xtext.AbstractElement)6 HashMap (java.util.HashMap)5 ParameterizedTypeRef (org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)5 Type (org.eclipse.n4js.ts.types.Type)5 EList (org.eclipse.emf.common.util.EList)4 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)4 UnorderedGroup (org.eclipse.xtext.UnorderedGroup)4 LinkedHashMap (java.util.LinkedHashMap)3 LinkedList (java.util.LinkedList)3