Search in sources :

Example 1 with RuleFilter

use of org.eclipse.xtext.xtext.RuleFilter in project xtext-eclipse by eclipse.

the class DefaultAntlrGrammarGenerator method generate.

public void generate(final Grammar it, final AntlrOptions options, final Xtend2ExecutionContext ctx) {
    final RuleFilter filter = new RuleFilter();
    final RuleNames ruleNames = RuleNames.getRuleNames(GrammarUtil.getGrammar(it), true);
    final Grammar flattened = new FlattenedGrammarAccess(ruleNames, filter).getFlattenedGrammar();
    String _name = this._xtextIDEAGeneratorExtensions.getSrcGenOutlet(ctx).getName();
    String _asPath = this._naming.asPath(this.getGrammarFileName(it));
    String _plus = (_asPath + ".g");
    ctx.writeFile(_name, _plus, this.compile(flattened, options));
}
Also used : RuleNames(org.eclipse.xtext.xtext.RuleNames) RuleFilter(org.eclipse.xtext.xtext.RuleFilter) Grammar(org.eclipse.xtext.Grammar) FlattenedGrammarAccess(org.eclipse.xtext.xtext.FlattenedGrammarAccess)

Example 2 with RuleFilter

use of org.eclipse.xtext.xtext.RuleFilter in project xtext-eclipse by eclipse.

the class ParametersContentAssistParserSanityTest method methodNames.

/**
 * Returns test data.
 */
@Parameters(name = "{0}")
public static Collection<Object[]> methodNames() {
    try {
        TwoParametersTestLanguageInjectorProvider injectorProvider = new TwoParametersTestLanguageInjectorProvider();
        try {
            injectorProvider.setupRegistry();
            Injector injector = injectorProvider.getInjector();
            DummyParser dummyParser = new DummyParser();
            TwoParametersTestLanguageGrammarAccess grammarAccess = injector.getInstance(TwoParametersTestLanguageGrammarAccess.class);
            RuleNames ruleNames = RuleNames.getRuleNames(grammarAccess.getGrammar(), false);
            RuleFilter ruleFilter = new RuleFilter();
            ruleFilter.setDiscardUnreachableRules(true);
            FlattenedGrammarAccess flattened = new FlattenedGrammarAccess(ruleNames, ruleFilter);
            Grammar flattenedGrammar = flattened.getFlattenedGrammar();
            dummyParser.setGrammarAccess(grammarAccess);
            dummyParser.setNameMappings(new TwoParametersTestLanguageParser.NameMappings(grammarAccess));
            dummyParser.getRuleName(null);
            Map<AbstractElement, String> nameMappings = new ReflectExtensions().get(dummyParser.getNameMappings(), "mappings");
            List<String> methodNames = Lists.newArrayList(nameMappings.values());
            Collections.sort(methodNames);
            Set<String> flattenedRuleNames = Sets.newHashSet(Lists.transform(flattenedGrammar.getRules(), r -> r.getName()));
            return Lists.transform(methodNames, (s) -> new Object[] { s, flattenedRuleNames });
        } finally {
            injectorProvider.restoreRegistry();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Arrays(java.util.Arrays) AbstractElement(org.eclipse.xtext.AbstractElement) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) FlattenedGrammarAccess(org.eclipse.xtext.xtext.FlattenedGrammarAccess) TwoParametersTestLanguageGrammarAccess(org.eclipse.xtext.ui.tests.editor.contentassist.services.TwoParametersTestLanguageGrammarAccess) Lists(com.google.common.collect.Lists) TwoParametersTestLanguageInjectorProvider(org.eclipse.xtext.ui.tests.editor.contentassist.tests.TwoParametersTestLanguageInjectorProvider) Map(java.util.Map) RuleNames(org.eclipse.xtext.xtext.RuleNames) RuleFilter(org.eclipse.xtext.xtext.RuleFilter) Method(java.lang.reflect.Method) Parameterized(org.junit.runners.Parameterized) InternalTwoParametersTestLanguageParser(org.eclipse.xtext.ui.tests.editor.contentassist.ide.contentassist.antlr.internal.InternalTwoParametersTestLanguageParser) Parameter(org.junit.runners.Parameterized.Parameter) Collection(java.util.Collection) Set(java.util.Set) Test(org.junit.Test) Sets(com.google.common.collect.Sets) TwoParametersTestLanguageParser(org.eclipse.xtext.ui.tests.editor.contentassist.ide.contentassist.antlr.TwoParametersTestLanguageParser) Injector(com.google.inject.Injector) List(java.util.List) Grammar(org.eclipse.xtext.Grammar) ReflectExtensions(org.eclipse.xtext.xbase.lib.util.ReflectExtensions) Assert(org.junit.Assert) Collections(java.util.Collections) ReflectExtensions(org.eclipse.xtext.xbase.lib.util.ReflectExtensions) RuleNames(org.eclipse.xtext.xtext.RuleNames) AbstractElement(org.eclipse.xtext.AbstractElement) InternalTwoParametersTestLanguageParser(org.eclipse.xtext.ui.tests.editor.contentassist.ide.contentassist.antlr.internal.InternalTwoParametersTestLanguageParser) TwoParametersTestLanguageParser(org.eclipse.xtext.ui.tests.editor.contentassist.ide.contentassist.antlr.TwoParametersTestLanguageParser) Grammar(org.eclipse.xtext.Grammar) FlattenedGrammarAccess(org.eclipse.xtext.xtext.FlattenedGrammarAccess) TwoParametersTestLanguageGrammarAccess(org.eclipse.xtext.ui.tests.editor.contentassist.services.TwoParametersTestLanguageGrammarAccess) Injector(com.google.inject.Injector) TwoParametersTestLanguageInjectorProvider(org.eclipse.xtext.ui.tests.editor.contentassist.tests.TwoParametersTestLanguageInjectorProvider) RuleFilter(org.eclipse.xtext.xtext.RuleFilter) Parameters(org.junit.runners.Parameterized.Parameters)

Example 3 with RuleFilter

use of org.eclipse.xtext.xtext.RuleFilter in project n4js by eclipse.

the class ContentAssistParserSanityTest method methodNames.

/**
 * Returns test data.
 */
@Parameters(name = "{0}")
public static Collection<Object[]> methodNames() {
    try {
        N4JSInjectorProvider injectorProvider = new N4JSInjectorProvider();
        Injector injector = injectorProvider.getInjector();
        DummyParser dummyParser = new DummyParser();
        N4JSGrammarAccess grammarAccess = injector.getInstance(N4JSGrammarAccess.class);
        RuleNames ruleNames = RuleNames.getRuleNames(grammarAccess.getGrammar(), false);
        RuleFilter ruleFilter = new RuleFilter();
        ruleFilter.setDiscardUnreachableRules(true);
        FlattenedGrammarAccess flattened = new FlattenedGrammarAccess(ruleNames, ruleFilter);
        Grammar flattenedGrammar = flattened.getFlattenedGrammar();
        dummyParser.setGrammarAccess(grammarAccess);
        injectorProvider.restoreRegistry();
        dummyParser.getRuleName(null);
        Map<AbstractElement, String> nameMappings = new ReflectExtensions().get(dummyParser, "nameMappings");
        List<String> methodNames = Lists.newArrayList(nameMappings.values());
        Collections.sort(methodNames);
        Set<String> flattenedRuleNames = Sets.newHashSet(Lists.transform(flattenedGrammar.getRules(), r -> r.getName()));
        return Lists.transform(methodNames, (s) -> new Object[] { s, flattenedRuleNames });
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : N4JSInjectorProvider(org.eclipse.n4js.N4JSInjectorProvider) Arrays(java.util.Arrays) AbstractElement(org.eclipse.xtext.AbstractElement) N4JSInjectorProvider(org.eclipse.n4js.N4JSInjectorProvider) N4JSParser(org.eclipse.n4js.ui.contentassist.antlr.N4JSParser) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) FlattenedGrammarAccess(org.eclipse.xtext.xtext.FlattenedGrammarAccess) Lists(com.google.common.collect.Lists) InternalN4JSParser(org.eclipse.n4js.ui.contentassist.antlr.internal.InternalN4JSParser) Map(java.util.Map) RuleNames(org.eclipse.xtext.xtext.RuleNames) RuleFilter(org.eclipse.xtext.xtext.RuleFilter) Method(java.lang.reflect.Method) Parameterized(org.junit.runners.Parameterized) Parameter(org.junit.runners.Parameterized.Parameter) Collection(java.util.Collection) Set(java.util.Set) N4JSGrammarAccess(org.eclipse.n4js.services.N4JSGrammarAccess) Test(org.junit.Test) Sets(com.google.common.collect.Sets) Injector(com.google.inject.Injector) List(java.util.List) Grammar(org.eclipse.xtext.Grammar) ReflectExtensions(org.eclipse.xtext.xbase.lib.util.ReflectExtensions) Assert(org.junit.Assert) Collections(java.util.Collections) ReflectExtensions(org.eclipse.xtext.xbase.lib.util.ReflectExtensions) N4JSGrammarAccess(org.eclipse.n4js.services.N4JSGrammarAccess) RuleNames(org.eclipse.xtext.xtext.RuleNames) AbstractElement(org.eclipse.xtext.AbstractElement) Grammar(org.eclipse.xtext.Grammar) FlattenedGrammarAccess(org.eclipse.xtext.xtext.FlattenedGrammarAccess) Injector(com.google.inject.Injector) RuleFilter(org.eclipse.xtext.xtext.RuleFilter) Parameters(org.junit.runners.Parameterized.Parameters)

Example 4 with RuleFilter

use of org.eclipse.xtext.xtext.RuleFilter in project xtext-core by eclipse.

the class XtextAntlrGeneratorFragment2 method initNameMappings.

/**
 * @since 2.14
 */
protected StringConcatenationClient initNameMappings(final Grammar it) {
    StringConcatenationClient _xblockexpression = null;
    {
        final RuleFilter filter = new RuleFilter();
        filter.setDiscardUnreachableRules(this.getOptions().isSkipUnusedRules());
        final RuleNames ruleNames = RuleNames.getRuleNames(it, true);
        final Grammar flattened = new FlattenedGrammarAccess(ruleNames, filter).getFlattenedGrammar();
        final Set<AbstractElement> seenElements = CollectionLiterals.<AbstractElement>newHashSet();
        Collection<? extends AbstractElement> _allAlternatives = GrammarUtil.getAllAlternatives(flattened);
        Collection<? extends AbstractElement> _allGroups = GrammarUtil.getAllGroups(flattened);
        Iterable<AbstractElement> _plus = Iterables.<AbstractElement>concat(_allAlternatives, _allGroups);
        Collection<? extends AbstractElement> _allAssignments = GrammarUtil.getAllAssignments(flattened);
        Iterable<AbstractElement> _plus_1 = Iterables.<AbstractElement>concat(_plus, _allAssignments);
        Collection<? extends AbstractElement> _allUnorderedGroups = GrammarUtil.getAllUnorderedGroups(flattened);
        final Function1<AbstractElement, Boolean> _function = (AbstractElement it_1) -> {
            return Boolean.valueOf(seenElements.add(AntlrGrammarGenUtil.<AbstractElement>getOriginalElement(it_1)));
        };
        final List<AbstractElement> elements = IterableExtensions.<AbstractElement>toList(IterableExtensions.<AbstractElement>filter(Iterables.<AbstractElement>filter((Iterables.<AbstractElement>concat(_plus_1, _allUnorderedGroups)), AbstractElement.class), _function));
        final List<List<AbstractElement>> partitions = Lists.<AbstractElement>partition(elements, 2500);
        StringConcatenationClient _client = new StringConcatenationClient() {

            @Override
            protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                _builder.append("@");
                _builder.append(Singleton.class);
                _builder.newLineIfNotEmpty();
                _builder.append("public static final class NameMappings {");
                _builder.newLine();
                _builder.append("\t");
                _builder.newLine();
                {
                    int _size = partitions.size();
                    boolean _greaterThan = (_size > 1);
                    if (_greaterThan) {
                        {
                            Iterable<Pair<Integer, List<AbstractElement>>> _indexed = IterableExtensions.<List<AbstractElement>>indexed(partitions);
                            for (final Pair<Integer, List<AbstractElement>> partition : _indexed) {
                                _builder.append("\t");
                                _builder.append("private static final class Init");
                                Integer _key = partition.getKey();
                                _builder.append(_key, "\t");
                                _builder.append(" {");
                                _builder.newLineIfNotEmpty();
                                _builder.append("\t");
                                _builder.append("\t");
                                _builder.append("private static void doInit(");
                                _builder.append(ImmutableMap.class, "\t\t");
                                _builder.append(".Builder<");
                                _builder.append(AbstractElement.class, "\t\t");
                                _builder.append(", ");
                                _builder.append(String.class, "\t\t");
                                _builder.append("> builder, ");
                                TypeReference _grammarAccess = XtextAntlrGeneratorFragment2.this.grammarUtil.getGrammarAccess(XtextAntlrGeneratorFragment2.this.getGrammar());
                                _builder.append(_grammarAccess, "\t\t");
                                _builder.append(" grammarAccess) {");
                                _builder.newLineIfNotEmpty();
                                _builder.append("\t");
                                _builder.append("\t\t");
                                StringConcatenationClient _initNameMappings = XtextAntlrGeneratorFragment2.this.initNameMappings(partition.getValue());
                                _builder.append(_initNameMappings, "\t\t\t");
                                _builder.newLineIfNotEmpty();
                                _builder.append("\t");
                                _builder.append("\t");
                                _builder.append("}");
                                _builder.newLine();
                                _builder.append("\t");
                                _builder.append("}");
                                _builder.newLine();
                                _builder.append("\t");
                                _builder.newLine();
                            }
                        }
                    }
                }
                _builder.append("\t");
                _builder.append("private final ");
                _builder.append(Map.class, "\t");
                _builder.append("<");
                _builder.append(AbstractElement.class, "\t");
                _builder.append(", ");
                _builder.append(String.class, "\t");
                _builder.append("> mappings;");
                _builder.newLineIfNotEmpty();
                _builder.append("\t");
                _builder.newLine();
                _builder.append("\t");
                _builder.append("@");
                _builder.append(Inject.class, "\t");
                _builder.newLineIfNotEmpty();
                _builder.append("\t");
                _builder.append("public NameMappings(");
                TypeReference _grammarAccess_1 = XtextAntlrGeneratorFragment2.this.grammarUtil.getGrammarAccess(XtextAntlrGeneratorFragment2.this.getGrammar());
                _builder.append(_grammarAccess_1, "\t");
                _builder.append(" grammarAccess) {");
                _builder.newLineIfNotEmpty();
                _builder.append("\t\t");
                _builder.append(ImmutableMap.class, "\t\t");
                _builder.append(".Builder<");
                _builder.append(AbstractElement.class, "\t\t");
                _builder.append(", ");
                _builder.append(String.class, "\t\t");
                _builder.append("> builder = ");
                _builder.append(ImmutableMap.class, "\t\t");
                _builder.append(".builder();");
                _builder.newLineIfNotEmpty();
                _builder.append("\t\t");
                _builder.append("init(builder, grammarAccess);");
                _builder.newLine();
                _builder.append("\t\t");
                _builder.append("this.mappings = builder.build();");
                _builder.newLine();
                _builder.append("\t");
                _builder.append("}");
                _builder.newLine();
                _builder.append("\t");
                _builder.newLine();
                _builder.append("\t");
                _builder.append("public ");
                _builder.append(String.class, "\t");
                _builder.append(" getRuleName(");
                _builder.append(AbstractElement.class, "\t");
                _builder.append(" element) {");
                _builder.newLineIfNotEmpty();
                _builder.append("\t\t");
                _builder.append("return mappings.get(element);");
                _builder.newLine();
                _builder.append("\t");
                _builder.append("}");
                _builder.newLine();
                _builder.append("\t");
                _builder.newLine();
                _builder.append("\t");
                _builder.append("private static void init(");
                _builder.append(ImmutableMap.class, "\t");
                _builder.append(".Builder<");
                _builder.append(AbstractElement.class, "\t");
                _builder.append(", ");
                _builder.append(String.class, "\t");
                _builder.append("> builder, ");
                TypeReference _grammarAccess_2 = XtextAntlrGeneratorFragment2.this.grammarUtil.getGrammarAccess(XtextAntlrGeneratorFragment2.this.getGrammar());
                _builder.append(_grammarAccess_2, "\t");
                _builder.append(" grammarAccess) {");
                _builder.newLineIfNotEmpty();
                {
                    int _size_1 = partitions.size();
                    boolean _greaterThan_1 = (_size_1 > 1);
                    if (_greaterThan_1) {
                        {
                            Iterable<Pair<Integer, List<AbstractElement>>> _indexed_1 = IterableExtensions.<List<AbstractElement>>indexed(partitions);
                            for (final Pair<Integer, List<AbstractElement>> partition_1 : _indexed_1) {
                                _builder.append("\t\t");
                                _builder.append("Init");
                                Integer _key_1 = partition_1.getKey();
                                _builder.append(_key_1, "\t\t");
                                _builder.append(".doInit(builder, grammarAccess);");
                                _builder.newLineIfNotEmpty();
                            }
                        }
                    } else {
                        {
                            for (final List<AbstractElement> partition_2 : partitions) {
                                _builder.append("\t\t");
                                StringConcatenationClient _initNameMappings_1 = XtextAntlrGeneratorFragment2.this.initNameMappings(partition_2);
                                _builder.append(_initNameMappings_1, "\t\t");
                                _builder.newLineIfNotEmpty();
                            }
                        }
                    }
                }
                _builder.append("\t");
                _builder.append("}");
                _builder.newLine();
                _builder.append("}");
                _builder.newLine();
                _builder.newLine();
                _builder.append("@");
                _builder.append(Inject.class);
                _builder.newLineIfNotEmpty();
                _builder.append("private NameMappings nameMappings;");
                _builder.newLine();
            }
        };
        _xblockexpression = _client;
    }
    return _xblockexpression;
}
Also used : Inject(com.google.inject.Inject) Set(java.util.Set) RuleNames(org.eclipse.xtext.xtext.RuleNames) AbstractElement(org.eclipse.xtext.AbstractElement) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) Grammar(org.eclipse.xtext.Grammar) FlattenedGrammarAccess(org.eclipse.xtext.xtext.FlattenedGrammarAccess) ImmutableMap(com.google.common.collect.ImmutableMap) Singleton(com.google.inject.Singleton) Collection(java.util.Collection) RuleFilter(org.eclipse.xtext.xtext.RuleFilter) List(java.util.List) ArrayList(java.util.ArrayList) TypeReference(org.eclipse.xtext.xtext.generator.model.TypeReference) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Pair(org.eclipse.xtext.xbase.lib.Pair)

Example 5 with RuleFilter

use of org.eclipse.xtext.xtext.RuleFilter in project xtext-core by eclipse.

the class GrammarPDAProvider method getGrammarPDAs.

@Override
public SerializationContextMap<Pda<ISerState, RuleCall>> getGrammarPDAs(Grammar grammar) {
    RuleNames names = RuleNames.getRuleNames(grammar, true);
    RuleFilter filter = new RuleFilter();
    filter.setDiscardTerminalRules(true);
    filter.setDiscardUnreachableRules(false);
    filter.setDiscardRuleTypeRef(false);
    Grammar flattened = new FlattenedGrammarAccess(names, filter).getFlattenedGrammar();
    Builder<Pda<ISerState, RuleCall>> result = SerializationContextMap.<Pda<ISerState, RuleCall>>builder();
    for (ParserRule rule : GrammarUtil.allParserRules(flattened)) {
        RuleWithParameterValues withParams = RuleWithParameterValues.findInEmfObject(rule);
        AbstractRule original = withParams.getOriginal();
        if (original instanceof ParserRule && isValidRule((ParserRule) original)) {
            ISerializationContext context = createContext((ParserRule) original, withParams.getParamValues());
            try {
                Pda<ISerState, RuleCall> pda = createPDA(grammar, rule);
                result.put(context, pda);
            } catch (Exception e) {
                LOG.error("Error creating PDA for context '" + context + "': " + e.getMessage(), e);
            }
        }
    }
    return result.create();
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) RuleNames(org.eclipse.xtext.xtext.RuleNames) RuleWithParameterValues(org.eclipse.xtext.xtext.RuleWithParameterValues) Grammar(org.eclipse.xtext.Grammar) Pda(org.eclipse.xtext.util.formallang.Pda) FlattenedGrammarAccess(org.eclipse.xtext.xtext.FlattenedGrammarAccess) RuleCall(org.eclipse.xtext.RuleCall) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) RuleFilter(org.eclipse.xtext.xtext.RuleFilter) AbstractRule(org.eclipse.xtext.AbstractRule)

Aggregations

Grammar (org.eclipse.xtext.Grammar)6 FlattenedGrammarAccess (org.eclipse.xtext.xtext.FlattenedGrammarAccess)6 RuleFilter (org.eclipse.xtext.xtext.RuleFilter)6 RuleNames (org.eclipse.xtext.xtext.RuleNames)6 Collection (java.util.Collection)3 List (java.util.List)3 Map (java.util.Map)3 Set (java.util.Set)3 AbstractElement (org.eclipse.xtext.AbstractElement)3 Lists (com.google.common.collect.Lists)2 Sets (com.google.common.collect.Sets)2 Injector (com.google.inject.Injector)2 Method (java.lang.reflect.Method)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 ReflectExtensions (org.eclipse.xtext.xbase.lib.util.ReflectExtensions)2 Assert (org.junit.Assert)2 Test (org.junit.Test)2 RunWith (org.junit.runner.RunWith)2 Parameterized (org.junit.runners.Parameterized)2