Search in sources :

Example 1 with FlattenedGrammarAccess

use of org.eclipse.xtext.xtext.FlattenedGrammarAccess 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)

Example 2 with FlattenedGrammarAccess

use of org.eclipse.xtext.xtext.FlattenedGrammarAccess 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) AntlrGrammar(org.eclipse.xtext.xtext.generator.parser.antlr.AntlrGrammar) 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 3 with FlattenedGrammarAccess

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

the class AbstractAntlrGrammarGenerator method generate.

public void generate(final Grammar it, final AntlrOptions options, final IXtextGeneratorFileSystemAccess fsa) {
    this.keywordHelper = KeywordHelper.getHelper(it);
    this.originalGrammar = it;
    final RuleFilter filter = new RuleFilter();
    filter.setDiscardUnreachableRules(options.isSkipUnusedRules());
    final RuleNames ruleNames = RuleNames.getRuleNames(it, true);
    final Grammar flattened = new FlattenedGrammarAccess(ruleNames, filter).getFlattenedGrammar();
    boolean _isCombinedGrammar = this.isCombinedGrammar();
    new CombinedGrammarMarker(_isCombinedGrammar).attachToEmfObject(flattened);
    fsa.generateFile(this.getGrammarNaming().getParserGrammar(it).getGrammarFileName(), this.compileParser(flattened, options));
    boolean _isCombinedGrammar_1 = this.isCombinedGrammar();
    boolean _not = (!_isCombinedGrammar_1);
    if (_not) {
        fsa.generateFile(this.getGrammarNaming().getLexerGrammar(it).getGrammarFileName(), this.compileLexer(flattened, options));
    }
}
Also used : RuleNames(org.eclipse.xtext.xtext.RuleNames) RuleFilter(org.eclipse.xtext.xtext.RuleFilter) Grammar(org.eclipse.xtext.Grammar) CombinedGrammarMarker(org.eclipse.xtext.xtext.generator.parser.antlr.CombinedGrammarMarker) FlattenedGrammarAccess(org.eclipse.xtext.xtext.FlattenedGrammarAccess)

Example 4 with FlattenedGrammarAccess

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

the class GrammarFlatteningTest method getModel.

public Grammar getModel(final String model, final boolean dropUnreachable) throws Exception {
    EObject _model = super.getModel(model);
    Grammar grammar = ((Grammar) _model);
    RuleNames ruleNames = RuleNames.getRuleNames(grammar, false);
    RuleFilter filter = new RuleFilter();
    filter.setDiscardUnreachableRules(dropUnreachable);
    Grammar result = new FlattenedGrammarAccess(ruleNames, filter).getFlattenedGrammar();
    XtextResource resource = this.<XtextResource>get(XtextResource.class);
    resource.getContents().add(result);
    resource.setURI(URI.createURI("synthetic://flattened.xtext"));
    return result;
}
Also used : RuleNames(org.eclipse.xtext.xtext.RuleNames) EObject(org.eclipse.emf.ecore.EObject) RuleFilter(org.eclipse.xtext.xtext.RuleFilter) XtextResource(org.eclipse.xtext.resource.XtextResource) Grammar(org.eclipse.xtext.Grammar) FlattenedGrammarAccess(org.eclipse.xtext.xtext.FlattenedGrammarAccess)

Aggregations

Grammar (org.eclipse.xtext.Grammar)4 FlattenedGrammarAccess (org.eclipse.xtext.xtext.FlattenedGrammarAccess)4 RuleFilter (org.eclipse.xtext.xtext.RuleFilter)4 RuleNames (org.eclipse.xtext.xtext.RuleNames)4 ImmutableMap (com.google.common.collect.ImmutableMap)1 Inject (com.google.inject.Inject)1 Singleton (com.google.inject.Singleton)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 EObject (org.eclipse.emf.ecore.EObject)1 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)1 AbstractElement (org.eclipse.xtext.AbstractElement)1 AbstractRule (org.eclipse.xtext.AbstractRule)1 ParserRule (org.eclipse.xtext.ParserRule)1 RuleCall (org.eclipse.xtext.RuleCall)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 ISerializationContext (org.eclipse.xtext.serializer.ISerializationContext)1