Пример #1
0
        private Expression CharSetToExpression(BitArray charSet)
        {
            Expression expression = null;

            for (int i = 0; i < charSet.Length; i++)
            {
                if (charSet.Get(i))
                {
                    var io = new Expression.IO {
                        Byte = (byte)i, In = true, Out = true
                    };

                    if (expression == null)
                    {
                        expression = io;
                    }
                    else
                    {
                        expression = new Expression.Union
                        {
                            Left  = expression,
                            Right = io,
                        };
                    }
                }
            }

            if (expression == null)
            {
                expression = new Expression.Nothing();
            }

            return(expression);
        }
Пример #2
0
        private Expression ParseToParenOrEnd()
        {
            Expression unionTree  = null;
            Expression concatTree = null;

            while (_codeIndex < _code.Length && _code[_codeIndex] != ')' && _code[_codeIndex] != '}')
            {
                if (_code[_codeIndex] == '|')
                {
                    _codeIndex++;

                    concatTree = concatTree ?? new Expression.Empty();

                    if (unionTree == null)
                    {
                        unionTree = concatTree;
                    }
                    else
                    {
                        unionTree = new Expression.Union {
                            Left = unionTree, Right = concatTree
                        };
                    }

                    concatTree = null;

                    continue;
                }
                else
                {
                    var term = ParseTerm();

                    if (concatTree == null)
                    {
                        concatTree = term;
                    }
                    else
                    {
                        concatTree = new Expression.Concat {
                            Left = concatTree, Right = term
                        };
                    }
                }
            }

            concatTree = concatTree ?? new Expression.Empty();

            if (unionTree == null)
            {
                return(concatTree);
            }

            unionTree = new Expression.Union {
                Left = unionTree, Right = concatTree
            };

            return(unionTree);
        }
Пример #3
0
        private Expression ParseTerm()
        {
            Expression curTree = ParseUnstarredTerm();

            bool shouldContinue = true;

            while (_codeIndex < _code.Length)
            {
                switch (_code[_codeIndex])
                {
                case (byte)'*':
                    curTree = new Expression.Star {
                        Sub = curTree
                    };
                    break;

                case (byte)'+':
                    curTree = new Expression.Concat
                    {
                        Left  = curTree,
                        Right = new Expression.Star {
                            Sub = curTree
                        },
                    };
                    break;

                case (byte)'?':
                    curTree = new Expression.Union
                    {
                        Left  = new Expression.Empty(),
                        Right = curTree,
                    };
                    break;

                default:
                    shouldContinue = false;
                    break;
                }

                if (!shouldContinue)
                {
                    break;
                }

                _codeIndex++;
            }

            return(curTree);
        }