Exemple #1
0
        public StateMaschine Match(string regularExpression)
        {
            RegularExpressionPart regularExpressionPart = new RegularExpressionPart()
            {
                RegularExpression         = regularExpression,
                RegularExpressionPosition = 0
            };
            StateMaschine stateMaschine = new StateMaschine();
            var           result        = LoopMatch(regularExpressionPart, stateMaschine);

            return(stateMaschine);
        }
 public State Check(char value, StateMaschine stateMaschine)
 {
     foreach (StatusTransition statusTransition in StatusTransitions_)
     {
         if (statusTransition.Status_.Value == value.ToString())
         {
             return(statusTransition.Status_);
         }
         else if (statusTransition.Status_.Value == LexerItems.Point.ToString())
         {
             return(statusTransition.Status_);
         }
         else if (statusTransition.Status_.Value == LexerItems.Star.ToString())
         {
             State returnState = statusTransition.Status_.Check(value, stateMaschine);
             if (!returnState.IsErrorState)
             {
                 return(returnState);
             }
         }
         else if (statusTransition.Status_.Value == LexerItems.QuestionMark.ToString())
         {
             return(statusTransition.Status_);
         }
         else if (statusTransition.Status_.Value == LexerItems.OpenBracket.ToString())
         {
             State returnState = statusTransition.Status_.Check(value, stateMaschine);
             if (!returnState.IsErrorState)
             {
                 return(returnState);
             }
         }
         else if (statusTransition.Status_.Value == LexerItems.CloseBracket.ToString())
         {
             State returnState = statusTransition.Status_.Check(value, stateMaschine);
             if (!returnState.IsErrorState)
             {
                 return(returnState);
             }
         }
         else if (statusTransition.Status_.Value == LexerItems.Pipe.ToString())
         {
             State returnState = statusTransition.Status_.Check(value, stateMaschine);
             if (!returnState.IsErrorState)
             {
                 return(returnState);
             }
         }
     }
     return(new State(-1, "Error", true));
 }
Exemple #3
0
        public bool Recognize(string text, StateMaschine stateMaschine)
        {
            stateMaschine.RecognizeMode();
            var length = text.Length;

            stateMaschine.TextLength = length;
            for (int i = 0; i < length; i++)
            {
                var character  = text.ToArray()[i];
                var recognized = stateMaschine.Recognize(character);
                if (!recognized)
                {
                    return(false);
                }
            }
            return(stateMaschine.ReachedEndeState);
        }
Exemple #4
0
        public StateMaschine LoopMatch(RegularExpressionPart regularExpressionPart, StateMaschine stateMaschine)
        {
            var   length        = regularExpressionPart.RegularExpression.ToArray().Length;
            State previousState = null;
            State jumpState     = null;
            var   minLength     = 0;

            for (int i = 0; i < length; i++)
            {
                var actualIndex = i;
                var character   = regularExpressionPart.RegularExpression.ToArray()[i];
                if (character == LexerItems.Point)
                {
                    previousState = stateMaschine.AddState(actualIndex, character.ToString(), previousState);
                    minLength++;
                    jumpState = null;
                }
                else if (character == LexerItems.Star)
                {
                    State tempPreviousState = previousState;
                    previousState = stateMaschine.AddState(actualIndex, character.ToString(), previousState);
                    if (jumpState != null)
                    {
                        previousState.StatusTransitions_.Add(new StatusTransition(jumpState));
                    }
                    else
                    {
                        previousState.StatusTransitions_.Add(new StatusTransition(tempPreviousState));
                    }
                    jumpState = null;
                }
                else if (character == LexerItems.QuestionMark)
                {
                    previousState = stateMaschine.AddState(actualIndex, character.ToString(), previousState);
                }
                else if (character == LexerItems.OpenBracket)
                {
                    State state = new State(actualIndex, character.ToString());
                    bracketOperations.Push(state);
                    previousState = stateMaschine.AddState(state, previousState);
                }
                else if (character == LexerItems.CloseBracket)
                {
                    State state = new State(actualIndex, character.ToString());
                    previousState = stateMaschine.AddState(state, previousState);
                    var tempState = bracketOperations.Pop();
                    if (tempState.Value == LexerItems.Pipe.ToString())
                    {
                        tempState.StatusTransitions_.Add(new StatusTransition(state));
                        tempState = bracketOperations.Pop();
                        if (tempState.Value != LexerItems.Pipe.ToString())
                        {
                            jumpState = tempState;
                        }
                    }
                    else
                    {
                        jumpState = tempState;
                    }
                }
                else if (character == LexerItems.Pipe)
                {
                    State state            = new State(actualIndex, character.ToString());
                    State bracketOperation = bracketOperations.Peek();
                    bracketOperations.Push(state);
                    previousState = stateMaschine.AddState(state, previousState);
                    var   previewCharacter = regularExpressionPart.RegularExpression.ToArray()[i + 1];
                    State nextCharacter    = new State(actualIndex + 1, previewCharacter.ToString());
                    bracketOperation.StatusTransitions_.Add(new StatusTransition(nextCharacter));
                    if (nextCharacter.Value == LexerItems.OpenBracket.ToString())
                    {
                        bracketOperations.Push(nextCharacter);
                    }
                    stateMaschine.ActualState = nextCharacter;
                    previousState             = nextCharacter;
                    i++;
                    jumpState = null;
                    minLength = 0;
                }
                else
                {
                    previousState = stateMaschine.AddState(actualIndex, character.ToString(), previousState);
                    minLength++;
                    jumpState = null;
                }
                if (stateMaschine.MinLength < minLength)
                {
                    stateMaschine.MinLength = minLength;
                }
            }
            previousState.IsPreviewEndeState = true;
            stateMaschine.AddState(length, "Ende", previousState);
            return(stateMaschine);
        }