Example #1
0
        private NFAState ParseExpr(NFAState start)
        {
            NFAState end = new NFAState();

            do
            {
                if (PeekChar(0) == '|')
                {
                    ReadChar('|');
                }
                var subStart = new NFAState();
                var subEnd   = ParseTerm(subStart);
                if (subStart.Incoming.Length == 0)
                {
                    subStart.MergeInto(start);
                }
                else
                {
                    start.AddOut(new NFAEpsilonTransition(subStart));
                }
                if (subEnd.Outgoing.Length == 0 ||
                    (!end.HasTransitions() && PeekChar(0) != '|'))
                {
                    subEnd.MergeInto(end);
                }
                else
                {
                    subEnd.AddOut(new NFAEpsilonTransition(end));
                }
            } while (PeekChar(0) == '|');
            return(end);
        }
Example #2
0
        private NFAState ParseFact(NFAState start)
        {
            NFAState placeholder = new NFAState();

            var end = ParseAtom(placeholder);

            switch (PeekChar(0))
            {
            case '?':
            case '*':
            case '+':
            case '{':
                end = ParseAtomModifier(placeholder, end);
                break;
            }
            if (placeholder.Incoming.Length > 0 && start.Outgoing.Length > 0)
            {
                start.AddOut(new NFAEpsilonTransition(placeholder));
                return(end);
            }
            else
            {
                placeholder.MergeInto(start);
                return((end == placeholder) ? start : end);
            }
        }
Example #3
0
        private NFAState ParseAtomModifier(NFAState start, NFAState end)
        {
            int min      = 0;
            int max      = -1;
            int firstPos = _pos;

            // Read min and max
            switch (ReadChar())
            {
            case '?':
                min = 0;
                max = 1;
                break;

            case '*':
                min = 0;
                max = -1;
                break;

            case '+':
                min = 1;
                max = -1;
                break;

            case '{':
                min = ReadNumber();
                max = min;
                if (PeekChar(0) == ',')
                {
                    ReadChar(',');
                    max = -1;
                    if (PeekChar(0) != '}')
                    {
                        max = ReadNumber();
                    }
                }
                ReadChar('}');
                if (max == 0 || (max > 0 && min > max))
                {
                    throw new RegExpException(
                              RegExpException.ErrorType.INVALID_REPEAT_COUNT,
                              firstPos,
                              _pattern);
                }
                break;

            default:
                throw new RegExpException(
                          RegExpException.ErrorType.UNEXPECTED_CHARACTER,
                          _pos - 1,
                          _pattern);
            }

            // Read possessive or reluctant modifiers
            if (PeekChar(0) == '?')
            {
                throw new RegExpException(
                          RegExpException.ErrorType.UNSUPPORTED_SPECIAL_CHARACTER,
                          _pos,
                          _pattern);
            }
            else if (PeekChar(0) == '+')
            {
                throw new RegExpException(
                          RegExpException.ErrorType.UNSUPPORTED_SPECIAL_CHARACTER,
                          _pos,
                          _pattern);
            }

            // Handle supported repeaters
            if (min == 0 && max == 1)
            {
                return(start.AddOut(new NFAEpsilonTransition(end)));
            }
            else if (min == 0 && max == -1)
            {
                if (end.Outgoing.Length == 0)
                {
                    end.MergeInto(start);
                }
                else
                {
                    end.AddOut(new NFAEpsilonTransition(start));
                }
                return(start);
            }
            else if (min == 1 && max == -1)
            {
                if (start.Outgoing.Length == 1 &&
                    end.Outgoing.Length == 0 &&
                    end.Incoming.Length == 1 &&
                    start.Outgoing[0] == end.Incoming[0])
                {
                    end.AddOut(start.Outgoing[0].Copy(end));
                }
                else
                {
                    end.AddOut(new NFAEpsilonTransition(start));
                }
                return(end);
            }
            else
            {
                throw new RegExpException(
                          RegExpException.ErrorType.INVALID_REPEAT_COUNT,
                          firstPos,
                          _pattern);
            }
        }