Beispiel #1
0
            public TDfaState BuildPredicateState(int operand, TDfaState currentState, bool predicateResult)
            {
                lock (_syncRoot)
                {
                    var dfaState = operand == -1 ? currentState.TryGetPredicateState(predicateResult) : currentState.TryGetPredicateState(operand, predicateResult);

                    if (dfaState != null)
                    {
                        return(dfaState);
                    }

                    var builderKey = GetBuilderKey();
                    var state      = builderKey.BuildPredicateState(operand, currentState, predicateResult);

                    ReleaseBuilderKey(builderKey);

                    if (operand == -1)
                    {
                        currentState.SetPredicateState(state, predicateResult);
                    }
                    else
                    {
                        currentState.SetPredicateState(operand, state, predicateResult);
                    }

                    if (FastLookup)
                    {
                        BuildFastLookup(state);
                    }

                    return(state);
                }
            }
Beispiel #2
0
            private TDfaState BuildImpl(int operand, TDfaState currentState)
            {
                var builderKey = GetBuilderKey();
                var dfaState   = builderKey.Build(operand, currentState);

                ReleaseBuilderKey(builderKey);

                return(dfaState);
            }
Beispiel #3
0
            public int Register(TDfaState dfaState)
            {
                var stateIndex = StateCount;

                StateRepository[stateIndex] = dfaState;
                StateCount++;

                if (StateCount == StateRepository.Length)
                {
                    Array.Resize(ref StateRepository, StateCount * 2);
                }

                return(stateIndex);
            }
Beispiel #4
0
            public TDfaState Build(int operand, TDfaState currentState)
            {
                lock (_syncRoot)
                {
                    var dfaState = currentState.TryGetState(operand);

                    if (dfaState != null)
                    {
                        return(dfaState);
                    }

                    dfaState = BuildImpl(operand, currentState);

                    currentState.SetState(operand, dfaState);

                    return(dfaState);
                }
            }
Beispiel #5
0
            public TDfaState Build(TDfaState currentState)
            {
                lock (_syncRoot)
                {
                    var dfaState = currentState.EndState;

                    if (dfaState != null)
                    {
                        return(dfaState);
                    }

                    dfaState = BuildImpl(-1, currentState);

                    currentState.EndState = dfaState;

                    return(dfaState);
                }
            }
Beispiel #6
0
            private void BuildFastLookup(TDfaState initialState)
            {
                if (initialState.Break)
                {
                    return;
                }

                var builderKey = GetBuilderKey();
                var hashSet    = new HashSet <TDfaState>();
                var queue      = new Queue <TDfaState>();

                hashSet.Add(initialState);
                queue.Enqueue(initialState);

                while (queue.Count > 0)
                {
                    var state = queue.Dequeue();

                    for (var i = 0; i < DfaState <TDfaState> .ArrayLimit; i++)
                    {
                        var next = state.TryGetState(i);

                        if (next == null)
                        {
                            next = builderKey.Build(i, state);
                            state.SetState(i, next);
                        }

                        if (next.Break)
                        {
                            continue;
                        }

                        if (hashSet.Add(next))
                        {
                            queue.Enqueue(next);
                        }
                    }
                }

                ReleaseBuilderKey(builderKey);
            }
                public TDfaState Build(int operand, TDfaState currentState)
                {
                    _prevSuccessTransition = currentState.PrevSuccessTransition;

                    foreach (var lazyTransition in currentState.LazyTransitions)
                    {
                        AddLazyTransition(lazyTransition);
                    }

                    foreach (var dfaNode in currentState.Nodes)
                    {
                        var transition = dfaNode.Transition;
                        var lazyIndex  = transition.LazyIndex;

                        if (ReferenceEquals(transition, _prevSuccessTransition) && _lazyTransitionsState[lazyIndex])
                        {
                            continue;
                        }

                        var passLazyNode   = _lazyTransitionsState[lazyIndex];
                        var node           = (Node)dfaNode.Node;
                        var executionPaths = operand == -1 ? node.GetExecutionPaths() : node.GetExecutionPaths(operand);

                        foreach (var executionPath in executionPaths)
                        {
                            AddNode(executionPath.Output, transition, executionPath.IsPredicate ? executionPath : null);

                            passLazyNode |= executionPath.PassLazyNode;
                        }

                        if (_lazyTransitionsState[lazyIndex] == passLazyNode)
                        {
                            continue;
                        }

                        AddLazyTransition(transition);
                    }

                    _prevSuccessTransition = _successTransition ?? _prevSuccessTransition;

                    return(BuildState() ?? currentState.NullState);
                }
Beispiel #8
0
            protected DfaBuilder(IEnumerable <FiniteState> states, Automata <TInstruction, TOperand> automata)
            {
                _initialStateNodes     = new List <DfaNode>();
                _noOpInitialStateNodes = new List <DfaNode>();

                foreach (var subGraph in states.Select(automata.EnsureSubGraph))
                {
                    if (subGraph.Graph.CanSimulateDfa == false)
                    {
                        throw new InvalidOperationException($"FiniteState {subGraph.State} can not be simulated as DFA");
                    }

                    if (subGraph.Graph.HasOperandNodes == false)
                    {
                        _noOpInitialStateNodes.Add(CreateDfaNode(subGraph.Graph.BeginNode, new DfaTransition(subGraph, _transitionCount), null));
                    }
                    else
                    {
                        _initialStateNodes.Add(CreateDfaNode(subGraph.Graph.BeginNode, new DfaTransition(subGraph, _transitionCount), null));
                    }

                    _transitionCount++;
                }

                FastLookup = _initialStateNodes.Count < 100;

                var builderKey = GetBuilderKey();

                InitialState     = builderKey.Build(_initialStateNodes);
                NoOpInitialState = builderKey.Build(_noOpInitialStateNodes);

                ReleaseBuilderKey(builderKey);

                if (FastLookup)
                {
                    BuildFastLookup(InitialState);
                }
            }
Beispiel #9
0
 public DfaFrozenStateKey(TDfaState state)
 {
     _state = state;
 }
                public TDfaState BuildPredicateState(int operand, TDfaState state, bool predicateResult)
                {
                    _prevSuccessTransition = state.PrevSuccessTransition;
                    _successTransition     = state.SuccessTransition;

                    foreach (var lazyTransition in state.LazyTransitions)
                    {
                        AddLazyTransition(lazyTransition);
                    }

                    var predicateFound = false;

                    foreach (var dfaNode in state.Nodes)
                    {
                        var node = (Node)dfaNode.Node;
                        var predicateExecutionPath = (ExecutionPath)dfaNode.ExecutionPathObject;

                        if (predicateExecutionPath == null || predicateFound)
                        {
                            AddNode(dfaNode);

                            continue;
                        }

                        predicateFound = true;

                        if (predicateResult == false)
                        {
                            continue;
                        }

                        var transition = dfaNode.Transition;
                        var lazyIndex  = transition.LazyIndex;

                        if (ReferenceEquals(transition, _prevSuccessTransition) && _lazyTransitionsState[lazyIndex])
                        {
                            continue;
                        }

                        if (node.ReturnPath.IsInvalid == false)
                        {
                            AddNode(node.ReturnPath.Output, transition, null);

                            _successTransition = transition;
                        }

                        var passLazyNode   = _lazyTransitionsState[lazyIndex];
                        var executionPaths = operand == -1 ? node.GetExecutionPaths() : node.GetExecutionPaths(operand);

                        foreach (var executionPath in executionPaths)
                        {
                            AddNode(executionPath.Output, transition, executionPath.IsPredicate ? executionPath : null);

                            passLazyNode |= executionPath.PassLazyNode;
                        }

                        if (_lazyTransitionsState[lazyIndex] == passLazyNode)
                        {
                            continue;
                        }

                        AddLazyTransition(transition);
                    }

                    _prevSuccessTransition = _successTransition ?? _prevSuccessTransition;

                    return(BuildState() ?? state.NullState);
                }