예제 #1
0
 private void ReallocateStringBuilderFromCapture()
 {
     this.captureBuilder = ObjectPoolExtensions.Allocate(SharedPools.Default <StringBuilder>());
     if (!string.IsNullOrWhiteSpace(this.capture))
     {
         this.captureBuilder.Append(this.capture);
     }
 }
예제 #2
0
        public override DfaState Transform(Nfa nfa)
        {
            var processOnceQueue = new ProcessOnceQueue <NfaClosure>();

            var set = ObjectPoolExtensions.Allocate(SharedPools.Default <HashSet <NfaState> >());

            foreach (var state in nfa.Start.Closure())
            {
                set.Add(state);
            }

            var start = new NfaClosure(set, nfa.Start.Equals(nfa.End));

            processOnceQueue.Enqueue(start);

            while (processOnceQueue.Count > 0)
            {
                var nfaClosure  = processOnceQueue.Dequeue();
                var transitions = ObjectPoolExtensions.Allocate(SharedPools.Default <Dictionary <AtomTerminal, HashSet <NfaState> > >());

                foreach (var state in nfaClosure.Set)
                {
                    foreach (var transition in state.Transitions)
                    {
                        if (transition is TerminalNfaTransition terminalTransition)
                        {
                            var terminal = terminalTransition.Terminal;

                            if (!transitions.ContainsKey(terminal))
                            {
                                transitions[terminal] = ObjectPoolExtensions.Allocate(SharedPools.Default <HashSet <NfaState> >());
                            }

                            transitions[terminal].Add(transition.Target);
                        }
                    }
                }

                foreach (var terminal in transitions.Keys)
                {
                    var targetStates = transitions[terminal];
                    var closure      = Closure(targetStates, nfa.End);
                    closure = processOnceQueue.EnqueueOrGetExisting(closure);
                    nfaClosure.State.AddTransition(terminal, closure.State);
                    SharedPools.Default <HashSet <NfaState> >().ClearAndFree(targetStates);
                }

                SharedPools
                .Default <HashSet <NfaState> >()
                .ClearAndFree(nfaClosure.Set);
                SharedPools
                .Default <Dictionary <AtomTerminal, HashSet <NfaState> > >()
                .ClearAndFree(transitions);
            }

            return(start.State);
        }
예제 #3
0
        public void AddTrailingTrivia(ITrivia trivia)
        {
            if (this.trailingTrivia == null)
            {
                var pool = SharedPools.Default <List <ITrivia> >();
                this.trailingTrivia = ObjectPoolExtensions.Allocate(pool);
            }

            this.trailingTrivia.Add(trivia);
        }
예제 #4
0
        private static IReadOnlyList <Interval> CreateIntervals(IEnumerable <char> characters)
        {
            var intervalListPool = SharedPools.Default <List <Interval> >();
            var intervalList     = ObjectPoolExtensions.Allocate(intervalListPool);

            // create a initial set of intervals
            foreach (var character in characters)
            {
                intervalList.Add(new Interval(character));
            }

            var groupedIntervals = Interval.Group(intervalList);

            intervalListPool.ClearAndFree(intervalList);

            return(groupedIntervals);
        }
예제 #5
0
        public static IReadOnlyList <Interval> Group(IReadOnlyList <Interval> input)
        {
            var pool            = SharedPools.Default <List <Interval> >();
            var sortedIntervals = ObjectPoolExtensions.Allocate(pool);

            sortedIntervals.AddRange(input);
            sortedIntervals.Sort();

            var intervalList = ObjectPoolExtensions.Allocate(pool);

            Interval?accumulator = null;

            for (var i = 0; i < sortedIntervals.Count; i++)
            {
                var interval = sortedIntervals[i];
                if (accumulator == null)
                {
                    accumulator = interval;
                    continue;
                }

                var joins = Join(accumulator.Value, interval);

                switch (joins.Count)
                {
                case 2:
                    intervalList.Add(joins[0]);
                    accumulator = joins[1];
                    break;

                case 1:
                    accumulator = joins[0];
                    break;
                }
            }

            if (accumulator != null)
            {
                intervalList.Add(accumulator.Value);
            }

            pool.ClearAndFree(sortedIntervals);

            return(intervalList);
        }
예제 #6
0
        private static NfaClosure Closure(IEnumerable <NfaState> states, NfaState endState)
        {
            var set     = ObjectPoolExtensions.Allocate(SharedPools.Default <HashSet <NfaState> >());
            var isFinal = false;

            foreach (var state in states)
            {
                foreach (var item in state.Closure())
                {
                    if (item.Equals(endState))
                    {
                        isFinal = true;
                    }

                    set.Add(item);
                }
            }

            return(new NfaClosure(set, isFinal));
        }
예제 #7
0
 public DfaLexeme(DfaLexerRule lexerRule, int position)
     : base(lexerRule, position)
 {
     this.captureBuilder = ObjectPoolExtensions.Allocate(SharedPools.Default <StringBuilder>());
     this.currentState   = lexerRule.Start;
 }