Ejemplo n.º 1
0
        public override StateRange AddTo <T>(NFA <T> nfa, CodePointEquivalenceClasses equivalenceClasses)
        {
            var startState = nfa.AddState();
            var endState   = nfa.AddState();

            foreach (var states in Expressions.Select(exp => exp.AddTo(nfa, equivalenceClasses)))
            {
                nfa.AddEpsilonTransition(startState, states.Start);
                nfa.AddEpsilonTransition(states.End, endState);
            }
            return(new StateRange(startState, endState));
        }
Ejemplo n.º 2
0
 public override StateRange AddTo <T>(NFA <T> nfa, CodePointEquivalenceClasses equivalenceClasses)
 {
     return(Expressions.Select(exp => exp.AddTo(nfa, equivalenceClasses)).Aggregate((states1, states2) =>
     {
         nfa.AddEpsilonTransition(states1.End, states2.Start);
         return new StateRange(states1.End, states2.Start);
     }));
 }
        public override StateRange AddTo <T>(NFA <T> nfa, CodePointEquivalenceClasses equivalenceClasses)
        {
            var reps = AddReps(nfa, equivalenceClasses).ToList();

            if (MaxRepititions == null)
            {
                // Repeat last rep as many times as needed
                var lastRep = reps.Last();
                nfa.AddEpsilonTransition(lastRep.End, lastRep.Start);
            }

            return(reps.Aggregate((states1, states2) =>
            {
                nfa.AddEpsilonTransition(states1.End, states2.Start);
                return new StateRange(states1.Start, states2.End);
            }));
        }
        public override StateRange AddTo <T>(NFA <T> nfa, CodePointEquivalenceClasses equivalenceClasses)
        {
            // Create a separate NFA
            var tempNFA = new NFA <State?>(equivalenceClasses.Count);
            var states  = Expression.AddTo(tempNFA, equivalenceClasses);

            tempNFA.SetStart(states.Start);
            tempNFA.SetFinal(states.End);

            // Convert to a DFA
            var dfa = tempNFA.ToDFA(stateData => null).Item2;

            dfa = dfa.Minimize().Item2;
            dfa.MakeComplete();             // Can only do complement on complete DFA

            // Now add the complement to this nfa
            var startState = nfa.AddState();
            var endState   = nfa.AddState();

            foreach (var dfaState in dfa.States)
            {
                var nfaState = GetMatchingState(nfa, dfa, dfaState);
                if (dfa.IsStart(dfaState))
                {
                    nfa.AddEpsilonTransition(startState, nfaState);
                }
                if (!dfa.IsFinal(dfaState))
                {
                    nfa.AddEpsilonTransition(nfaState, endState);
                }
                // Copy transitions
                foreach (var input in nfa.Inputs)
                {
                    var toDFAState = dfa.GetTransition(dfaState, input);
                    if (toDFAState != null)
                    {
                        var toNFAState = GetMatchingState(nfa, dfa, toDFAState.Value);
                        nfa.AddTransition(nfaState, input, toNFAState);
                    }
                }
            }

            return(new StateRange(startState, endState));
        }
        public void AddStates(IDictionary<Mode, State> modeMap, CodePointEquivalenceClasses equivalenceClasses, NFA<LexerAction> nfa, int priority, Channel defaultChannel)
        {
            var states = Expression.AddTo(nfa, equivalenceClasses);

            foreach(var mode in Modes)
                nfa.AddEpsilonTransition(modeMap[mode], states.Start);

            nfa.SetFinal(states.End);

            // Set Action
            nfa.SetData(states.End, new LexerAction(priority, GetValueAction(), GetModeActions(), GetEmitAction(defaultChannel), GetCodeAction()));
        }
        public void AddStates(IDictionary <Mode, State> modeMap, CodePointEquivalenceClasses equivalenceClasses, NFA <LexerAction> nfa, int priority, Channel defaultChannel)
        {
            var states = Expression.AddTo(nfa, equivalenceClasses);

            foreach (var mode in Modes)
            {
                nfa.AddEpsilonTransition(modeMap[mode], states.Start);
            }

            nfa.SetFinal(states.End);

            // Set Action
            nfa.SetData(states.End, new LexerAction(priority, GetValueAction(), GetModeActions(), GetEmitAction(defaultChannel), GetCodeAction()));
        }
        private IEnumerable <StateRange> AddReps <T>(NFA <T> nfa, CodePointEquivalenceClasses equivalenceClasses)
        {
            // Add required reps
            for (var i = 0; i < MinRepititions; i++)
            {
                yield return(Expression.AddTo(nfa, equivalenceClasses));
            }

            // Add optional reps
            // If min=0 and max=*, then we want to make one optional rep to use for the * hence `MaxRepititions ?? 1`
            var optionalReps = (MaxRepititions ?? 1) - MinRepititions;

            for (var i = 0; i < optionalReps; i++)
            {
                var states = Expression.AddTo(nfa, equivalenceClasses);
                // Can skip these reps
                nfa.AddEpsilonTransition(states.Start, states.End);
                yield return(states);
            }
        }