Ejemplo n.º 1
0
        private bool GeneratePatternNfa(State <TData, TOffset> startState, Pattern <TData, TOffset> pattern, string parentName,
                                        Func <Match <TData, TOffset>, bool>[] acceptables, ref int nextPriority)
        {
            bool   deterministic = true;
            string name          = parentName == null ? pattern.Name : parentName + "*" + pattern.Name;

            if (pattern.Acceptable != null)
            {
                acceptables = acceptables.Concat(pattern.Acceptable).ToArray();
            }
            if (pattern.Children.All(node => node is Pattern <TData, TOffset>))
            {
                foreach (Pattern <TData, TOffset> childExpr in pattern.Children)
                {
                    if (!GeneratePatternNfa(startState, childExpr, name, acceptables, ref nextPriority))
                    {
                        deterministic = false;
                    }
                }
            }
            else
            {
                startState = _fsa.CreateTag(startState, _fsa.CreateState(), EntireMatch, true);
                bool hasVariables;
                startState = pattern.GenerateNfa(_fsa, startState, out hasVariables);
                if (hasVariables)
                {
                    deterministic = false;
                }
                startState = _fsa.CreateTag(startState, _fsa.CreateState(), EntireMatch, false);


                Func <TData, FstResult <TData, TOffset>, bool> acceptable = null;
                if (acceptables.Length > 0)
                {
                    acceptable = (input, match) =>
                    {
                        Match <TData, TOffset> patMatch = CreatePatternMatch(input, match);
                        return(acceptables.All(a => a(patMatch)));
                    };
                }
                State <TData, TOffset> acceptingState = _fsa.CreateAcceptingState(name, acceptable, nextPriority++);
                startState.Arcs.Add(acceptingState);
            }

            return(deterministic);
        }
Ejemplo n.º 2
0
        internal override State <TData, TOffset> GenerateNfa(Fst <TData, TOffset> fsa, State <TData, TOffset> startState, out bool hasVariables)
        {
            if (IsLeaf)
            {
                hasVariables = false;
                return(startState);
            }

            if (_name != null)
            {
                startState = fsa.CreateTag(startState, fsa.CreateState(), _name, true);
            }
            startState = base.GenerateNfa(fsa, startState, out hasVariables);
            startState = _name != null?fsa.CreateTag(startState, fsa.CreateState(), _name, false) : startState.Arcs.Add(fsa.CreateState());

            return(startState);
        }