public static Expression <DfaStateMachine <LetterId, TLetter> > CreateStateMachine <TLetter>(this LexerBuilder <TLetter> that, out Id <DfaState <LetterId> > dfaStartState, RangeSet <TLetter>?validRanges = default, LetterId?defaultLetter = null)
            where TLetter : struct, IEquatable <TLetter>, IComparable <TLetter>
        {
            var dfa = that.ComputeDfa(out var alphabetBuilder, validRanges ?? that.ValidRange);

            dfaStartState = dfa.StartState.Id;
            var inputToLetter = AlphabetMapperEmitter <TLetter> .CreateExpression(alphabetBuilder, defaultLetter);

            return(DfaStateMachineEmitter.CreateExpression(dfa, inputToLetter));
        }
Ejemplo n.º 2
0
        private CharsetGrammar()
        {
            var provider       = new UnicodeCharSetProvider();
            var mapper         = new UnicodeUtf16Mapper(false, false);
            var rx             = RegexLexer.CreateRx(mapper);
            var rxWhitespace   = new RxAccept <char>(RxOfSymbol <char> .Extract(rx, RegexLexer.SymWhitespace), SymWhitespace, 0);
            var rxCharset      = new RxAccept <char>(RxOfSymbol <char> .Extract(rx, RegexLexer.SymCharset), SymCharset, 0);
            var rxRegexCharset = new RxAccept <char>(RxOfSymbol <char> .Extract(rx, RegexLexer.SymRegexCharset), SymRegexCharset, 0);
            var rxUnion        = new RxAccept <char>(RegexMatchSet.FromChars('|', '+').ToInvariant(mapper, provider, true), SymUnion, 0);
            var rxSubtract     = new RxAccept <char>(RegexMatchSet.FromChars('-').ToInvariant(mapper, provider, true), SymSubtract, 0);
            var rxIntersect    = new RxAccept <char>(RegexMatchSet.FromChars('&').ToInvariant(mapper, provider, true), SymIntersect, 0);
            var rxDifference   = new RxAccept <char>(RegexMatchSet.FromChars('^').ToInvariant(mapper, provider, true), SymDifference, 0);
            var rxNegate       = new RxAccept <char>(RegexMatchSet.FromChars('~').ToInvariant(mapper, provider, true), SymNegate, 0);
            var rxParensOpen   = new RxAccept <char>(RegexMatchSet.FromChars('(').ToInvariant(mapper, provider, true), SymParensOpen, 0);
            var rxParensClose  = new RxAccept <char>(RegexMatchSet.FromChars(')').ToInvariant(mapper, provider, true), SymParensClose, 0);
            var alpha          = new AlphabetBuilder <char>(
                new RxAlternation <char>(rxWhitespace,
                                         new RxAlternation <char>(rxCharset,
                                                                  new RxAlternation <char>(rxRegexCharset,
                                                                                           new RxAlternation <char>(rxUnion,
                                                                                                                    new RxAlternation <char>(rxSubtract,
                                                                                                                                             new RxAlternation <char>(rxIntersect,
                                                                                                                                                                      new RxAlternation <char>(rxDifference,
                                                                                                                                                                                               new RxAlternation <char>(rxNegate,
                                                                                                                                                                                                                        new RxAlternation <char>(rxParensOpen, rxParensClose))))))))),
                Utf16Chars.EOF,
                Utf16Chars.ValidBmp);
            var nfa = NfaBuilder <LetterId> .Build(alpha.Expression);

            var dfa = DfaBuilder <LetterId> .Build(nfa, LetterId.Eof);

            if (dfa.StartState.Id != default(Id <DfaState <LetterId> >))
            {
                throw new InvalidOperationException($"Internal error: Unexpected DFA start state {dfa.StartState.Id}");
            }
            this.stateMachine = DfaStateMachineEmitter.CreateExpression(dfa, AlphabetMapperEmitter <char> .CreateExpression(alpha)).Compile();
            this.table        = new LalrTableGenerator(new GrammarBuilder(-2, -1, SymExpression)
            {
                { SymUnionExpression, SymExpression, SymUnion, SymNegateExpression },
                { SymExpression, SymUnionExpression },
                { SymSubtractExpression, SymExpression, SymSubtract, SymNegateExpression },
                { SymExpression, SymSubtractExpression },
                { SymIntersectExpression, SymExpression, SymIntersect, SymNegateExpression },
                { SymExpression, SymIntersectExpression },
                { SymDifferenceExpression, SymExpression, SymDifference, SymNegateExpression },
                { SymExpression, SymDifferenceExpression },
                { SymExpression, SymNegateExpression },
                { SymNegateExpression, SymNegate, SymValueExpression },
                { SymNegateExpression, SymValueExpression },
                { SymValueExpression, SymParensOpen, SymExpression, SymParensClose },
                { SymValueExpression, SymCharset },
                { SymValueExpression, SymRegexCharset }
            })
                                .ComputeTable();
        }