Search in sources :

Example 6 with Pda

use of org.eclipse.xtext.util.formallang.Pda 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 7 with Pda

use of org.eclipse.xtext.util.formallang.Pda in project xtext-core by eclipse.

the class SyntacticSequencerPDAProvider method getSyntacticSequencerPDAs.

@Override
public SerializationContextMap<ISynAbsorberState> getSyntacticSequencerPDAs(Grammar grammar) {
    SerializationContextMap<ISynAbsorberState> cached = cache.get(grammar);
    if (cached != null)
        return cached;
    SerializationContextMap.Builder<ISynAbsorberState> builder = SerializationContextMap.builder();
    SerializationContextMap<Pda<ISerState, RuleCall>> typePDAs = pdaProvider.getContextTypePDAs(grammar);
    for (Entry<Pda<ISerState, RuleCall>> e : typePDAs.values()) {
        Pda<ISerState, RuleCall> pda = e.getValue();
        List<ISerializationContext> contexts = e.getContexts();
        try {
            EClass type = contexts.get(0).getType();
            Map<ISerState, SynAbsorberState> absorbers = Maps.newLinkedHashMap();
            Map<SynAbsorberState, Map<ISerState, SynState>> emitters = Maps.newLinkedHashMap();
            SynAbsorberState state = createAbsorberState(pda.getStart(), absorbers, emitters, type);
            builder.put(contexts, state);
        } catch (Exception x) {
            String ctxs = Joiner.on(", ").join(contexts);
            LOG.error("Error creating PDA for syntactic sequencer for contexts: " + ctxs + ": " + x.getMessage(), x);
        }
    }
    SerializationContextMap<ISynAbsorberState> result = builder.create();
    cache.put(grammar, result);
    return result;
}
Also used : Pda(org.eclipse.xtext.util.formallang.Pda) RuleCall(org.eclipse.xtext.RuleCall) EClass(org.eclipse.emf.ecore.EClass) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) Map(java.util.Map)

Example 8 with Pda

use of org.eclipse.xtext.util.formallang.Pda in project xtext-core by eclipse.

the class DebugGraphGenerator method generateDebugGraphs.

public Iterable<Pair<String, String>> generateDebugGraphs() {
    final ArrayList<Pair<String, String>> result = CollectionLiterals.<Pair<String, String>>newArrayList();
    @Extension final NamedSerializationContextProvider names = new NamedSerializationContextProvider(this.grammar);
    final String dir_context = this.directory("context");
    final String dir_context_type = this.directory("context_type");
    final String dir_syntactic_sequencer = this.directory("syntactic_sequencer");
    final String dir_semantic_sequencer = this.directory("semantic_sequencer");
    List<NamedSerializationContexts<Pda<ISerState, RuleCall>>> _namedContexts = names.<Pda<ISerState, RuleCall>>getNamedContexts(this.contextPDAProvider.getContextPDAs(this.grammar));
    for (final NamedSerializationContexts<Pda<ISerState, RuleCall>> e : _namedContexts) {
        {
            String _name = e.getName();
            String _plus = (dir_context + _name);
            String _plus_1 = (_plus + ".dot");
            String _drawSafe = this.drawSafe(this.pdaToDot, e.getValue());
            Pair<String, String> _mappedTo = Pair.<String, String>of(_plus_1, _drawSafe);
            result.add(_mappedTo);
            String _name_1 = e.getName();
            String _plus_2 = (dir_context + _name_1);
            String _plus_3 = (_plus_2 + ".txt");
            String _join = IterableExtensions.join(e.getContexts(), "\n");
            Pair<String, String> _mappedTo_1 = Pair.<String, String>of(_plus_3, _join);
            result.add(_mappedTo_1);
        }
    }
    List<NamedSerializationContexts<Pda<ISerState, RuleCall>>> _namedContexts_1 = names.<Pda<ISerState, RuleCall>>getNamedContexts(this.contextTypePDAProvider.getContextTypePDAs(this.grammar));
    for (final NamedSerializationContexts<Pda<ISerState, RuleCall>> e_1 : _namedContexts_1) {
        {
            String _name = e_1.getName();
            String _plus = (dir_context_type + _name);
            String _plus_1 = (_plus + ".dot");
            String _drawSafe = this.drawSafe(this.pdaToDot, e_1.getValue());
            Pair<String, String> _mappedTo = Pair.<String, String>of(_plus_1, _drawSafe);
            result.add(_mappedTo);
            String _name_1 = e_1.getName();
            String _plus_2 = (dir_context_type + _name_1);
            String _plus_3 = (_plus_2 + ".txt");
            String _join = IterableExtensions.join(e_1.getContexts(), "\n");
            Pair<String, String> _mappedTo_1 = Pair.<String, String>of(_plus_3, _join);
            result.add(_mappedTo_1);
        }
    }
    List<NamedSerializationContexts<ISyntacticSequencerPDAProvider.ISynAbsorberState>> _namedContexts_2 = names.<ISyntacticSequencerPDAProvider.ISynAbsorberState>getNamedContexts(this.syntacticSequencerPDAProvider.getSyntacticSequencerPDAs(this.grammar));
    for (final NamedSerializationContexts<ISyntacticSequencerPDAProvider.ISynAbsorberState> e_2 : _namedContexts_2) {
        {
            String _name = e_2.getName();
            String _plus = (dir_syntactic_sequencer + _name);
            String _plus_1 = (_plus + ".dot");
            String _drawSafe = this.drawSafe(this.syntacticSequencerPDA2Dot, e_2.getValue());
            Pair<String, String> _mappedTo = Pair.<String, String>of(_plus_1, _drawSafe);
            result.add(_mappedTo);
            String _name_1 = e_2.getName();
            String _plus_2 = (dir_syntactic_sequencer + _name_1);
            String _plus_3 = (_plus_2 + ".txt");
            String _join = IterableExtensions.join(e_2.getContexts(), "\n");
            Pair<String, String> _mappedTo_1 = Pair.<String, String>of(_plus_3, _join);
            result.add(_mappedTo_1);
        }
    }
    List<NamedSerializationContexts<Nfa<ISemanticSequencerNfaProvider.ISemState>>> _namedContexts_3 = names.<Nfa<ISemanticSequencerNfaProvider.ISemState>>getNamedContexts(this.semanticSequencerNFAProvider.getSemanticSequencerNFAs(this.grammar));
    for (final NamedSerializationContexts<Nfa<ISemanticSequencerNfaProvider.ISemState>> e_3 : _namedContexts_3) {
        {
            String _name = e_3.getName();
            String _plus = (dir_semantic_sequencer + _name);
            String _plus_1 = (_plus + ".dot");
            String _drawSafe = this.drawSafe(this.nfaToDot, e_3.getValue());
            Pair<String, String> _mappedTo = Pair.<String, String>of(_plus_1, _drawSafe);
            result.add(_mappedTo);
            String _name_1 = e_3.getName();
            String _plus_2 = (dir_semantic_sequencer + _name_1);
            String _plus_3 = (_plus_2 + ".txt");
            String _join = IterableExtensions.join(e_3.getContexts(), "\n");
            Pair<String, String> _mappedTo_1 = Pair.<String, String>of(_plus_3, _join);
            result.add(_mappedTo_1);
        }
    }
    try {
        int i = 0;
        final HashMap<ISyntacticSequencerPDAProvider.ISynTransition, String> trans2id = CollectionLiterals.<ISyntacticSequencerPDAProvider.ISynTransition, String>newHashMap();
        Set<ISyntacticSequencerPDAProvider.ISynTransition> _allAmbiguousTransitions = this.syntacticSequencerUtil.getAllAmbiguousTransitions();
        for (final ISyntacticSequencerPDAProvider.ISynTransition transition : _allAmbiguousTransitions) {
            {
                final String name = ("ambiguity_" + Integer.valueOf(i));
                String _directory = this.directory("syntactic_sequencer");
                String _plus = (_directory + name);
                String _plus_1 = (_plus + ".dot");
                String _draw = this.nfaToDot.draw(transition.getAmbiguousNfa());
                Pair<String, String> _mappedTo = Pair.<String, String>of(_plus_1, _draw);
                result.add(_mappedTo);
                trans2id.put(transition, name);
                i = (i + 1);
            }
        }
        final StringBuffer ambiguities = new StringBuffer();
        List<EqualAmbiguousTransitions> _allAmbiguousTransitionsBySyntax = this.syntacticSequencerUtil.getAllAmbiguousTransitionsBySyntax();
        for (final EqualAmbiguousTransitions group : _allAmbiguousTransitionsBySyntax) {
            StringConcatenation _builder = new StringConcatenation();
            _builder.append("id: ");
            String _identifier = group.getIdentifier();
            _builder.append(_identifier);
            _builder.newLineIfNotEmpty();
            _builder.append("    ");
            _builder.append("Ambiguous syntax:�:");
            _builder.newLine();
            _builder.append("        ");
            String _replace = group.getElementAlias().toString().replace("\n", "\n        ");
            _builder.append(_replace, "        ");
            _builder.newLineIfNotEmpty();
            _builder.append("    ");
            _builder.append("This ambiguous syntax occurs at:");
            _builder.newLine();
            {
                List<ISyntacticSequencerPDAProvider.ISynTransition> _transitions = group.getTransitions();
                for (final ISyntacticSequencerPDAProvider.ISynTransition trans : _transitions) {
                    _builder.append("\t");
                    String _get = trans2id.get(trans);
                    _builder.append(_get, "\t");
                    _builder.append(":");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("    ");
                    String _replace_1 = group.ambiguityInsideTransition(trans).replace("\n", "\n        ");
                    _builder.append(_replace_1, "\t    ");
                    _builder.newLineIfNotEmpty();
                }
            }
            _builder.append("\t");
            _builder.newLine();
            _builder.newLine();
            ambiguities.append(_builder);
        }
        String _directory = this.directory("syntactic_sequencer");
        String _plus = (_directory + "ambiguities.txt");
        String _string = ambiguities.toString();
        Pair<String, String> _mappedTo = Pair.<String, String>of(_plus, _string);
        result.add(_mappedTo);
    } catch (final Throwable _t) {
        if (_t instanceof Exception) {
            final Exception e_4 = (Exception) _t;
            e_4.printStackTrace();
        } else {
            throw Exceptions.sneakyThrow(_t);
        }
    }
    return result;
}
Also used : Nfa(org.eclipse.xtext.util.formallang.Nfa) Pda(org.eclipse.xtext.util.formallang.Pda) RuleCall(org.eclipse.xtext.RuleCall) ISerState(org.eclipse.xtext.serializer.analysis.ISerState) ISyntacticSequencerPDAProvider(org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider) ArrayList(java.util.ArrayList) List(java.util.List) Pair(org.eclipse.xtext.xbase.lib.Pair) NamedSerializationContextProvider(org.eclipse.xtext.xtext.generator.serializer.NamedSerializationContextProvider) NamedSerializationContexts(org.eclipse.xtext.xtext.generator.serializer.NamedSerializationContexts) EqualAmbiguousTransitions(org.eclipse.xtext.xtext.generator.serializer.EqualAmbiguousTransitions) Extension(org.eclipse.xtext.xbase.lib.Extension) ISemanticSequencerNfaProvider(org.eclipse.xtext.serializer.analysis.ISemanticSequencerNfaProvider) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation)

Example 9 with Pda

use of org.eclipse.xtext.util.formallang.Pda in project xtext-core by eclipse.

the class GrammarConstraintProvider method getConstraints.

@Override
public SerializationContextMap<IConstraint> getConstraints(Grammar grammar) {
    SerializationContextMap<IConstraint> cached = cache.get(grammar);
    if (cached != null)
        return cached;
    SerializationContextMap.Builder<IConstraint> builder = SerializationContextMap.builder();
    GrammarElementDeclarationOrder.get(grammar);
    SerializationContextMap<Nfa<ISemState>> nfas = nfaProvider.getSemanticSequencerNFAs(grammar);
    for (Entry<Nfa<ISemState>> e : nfas.values()) {
        Nfa<ISemState> nfa = e.getValue();
        for (EClass type : e.getTypes()) {
            Constraint constraint = new Constraint(grammar, type, nfa);
            List<ISerializationContext> contexts = e.getContexts(type);
            constraint.contexts.addAll(contexts);
            builder.put(contexts, constraint);
        }
    }
    SerializationContextMap<IConstraint> result = builder.create();
    SerializationContextMap<Pda<ISerState, RuleCall>> typePDAs = typeProvider.getContextTypePDAs(grammar);
    for (Entry<IConstraint> e : result.values()) {
        Constraint constraint = (Constraint) e.getValue();
        constraint.setName(findBestConstraintName(grammar, typePDAs, constraint));
    }
    cache.put(grammar, result);
    return result;
}
Also used : Nfa(org.eclipse.xtext.util.formallang.Nfa) Pda(org.eclipse.xtext.util.formallang.Pda) EClass(org.eclipse.emf.ecore.EClass) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) ISemState(org.eclipse.xtext.serializer.analysis.ISemanticSequencerNfaProvider.ISemState)

Aggregations

Pda (org.eclipse.xtext.util.formallang.Pda)9 RuleCall (org.eclipse.xtext.RuleCall)8 ISerializationContext (org.eclipse.xtext.serializer.ISerializationContext)6 Grammar (org.eclipse.xtext.Grammar)4 ISerState (org.eclipse.xtext.serializer.analysis.ISerState)4 EClass (org.eclipse.emf.ecore.EClass)3 List (java.util.List)2 Map (java.util.Map)2 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)2 ParserRule (org.eclipse.xtext.ParserRule)2 GrammarElementTitleSwitch (org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch)2 Nfa (org.eclipse.xtext.util.formallang.Nfa)2 PdaListFormatter (org.eclipse.xtext.util.formallang.PdaListFormatter)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 AbstractRule (org.eclipse.xtext.AbstractRule)1