コード例 #1
0
        public RegexCharClass(RegexBuffer buffer)
        {
            int startLoc = buffer.Offset;

            buffer.MoveNext();

            Regex regex;
            Match match;

            regex = new Regex(@"(?<Negated>\^?)(?<Class>.+?)\]");

            match = regex.Match(buffer.String);
            if (match.Success)
            {
                if (match.Groups["Negated"].ToString() == "^")
                {
                    description = String.Format("Any character not in \"{0}\"",
                                                match.Groups["Class"]);
                }
                else
                {
                    description = String.Format("Any character in \"{0}\"",
                                                match.Groups["Class"]);
                }
                buffer.Offset += match.Groups[0].Length;
            }
            else
            {
                description = "missing ']' in character class";
            }
            buffer.AddLookup(this, startLoc, buffer.Offset - 1);
        }
コード例 #2
0
        public RegexCapture(RegexBuffer buffer)
        {
            startLocation = buffer.Offset;
            buffer.MoveNext();

            // we're not in a series of normal characters, so clear
            buffer.ClearInSeries();

            // if the first character of the capture is a '?',
            // we need to decode what comes after it.
            if (buffer.Current == '?')
            {
                bool decoded = CheckNamed(buffer);

                if (!decoded)
                {
                    decoded = CheckBalancedGroup(buffer);
                }

                if (!decoded)
                {
                    decoded = CheckNonCapturing(buffer);
                }

                if (!decoded)
                {
                    decoded = CheckOptions(buffer);
                }

                if (!decoded)
                {
                    decoded = CheckLookahead(buffer);
                }

                if (!decoded)
                {
                    decoded = CheckNonBacktracking(buffer);
                }

                if (!decoded)
                {
                    decoded = CheckConditional(buffer);
                }
            }
            else
            // plain old capture...
            {
                if (!HandlePlainOldCapture(buffer))
                {
                    throw new Exception(
                              String.Format("Unrecognized capture: {0}", buffer.String));
                }
            }
            buffer.AddLookup(this, startLocation, buffer.Offset - 1);
        }
コード例 #3
0
        // Handle (?(expression)yes|no)
        // when we get called, we're pointing to the first character of the expression
        public RegexConditional(RegexBuffer buffer)
        {
            startLocation = buffer.Offset;

            expression = new RegexExpression(buffer);
            CheckClosingParen(buffer);

            yesNo = new RegexExpression(buffer);
            CheckClosingParen(buffer);

            buffer.AddLookup(this, startLocation, buffer.Offset - 1);
        }
コード例 #4
0
        public RegexQuantifier(RegexBuffer buffer)
        {
            int startLoc = buffer.Offset;

            buffer.MoveNext();

            Regex regex;
            Match match;

            // look for "n}", "n,}", or "n,m}"
            regex = new Regex(@"(?<n>\d+)(?<Comma>,?)(?<m>\d*)\}");

            match = regex.Match(buffer.String);
            if (match.Success)
            {
                if (match.Groups["m"].Length != 0)
                {
                    description = String.Format("At least {0}, but not more than {1} times",
                                                match.Groups["n"], match.Groups["m"]);
                }
                else if (match.Groups["Comma"].Length != 0)
                {
                    description = String.Format("At least {0} times",
                                                match.Groups["n"]);
                }
                else
                {
                    description = String.Format("Exactly {0} times",
                                                match.Groups["n"]);
                }
                buffer.Offset += match.Groups[0].Length;

                if (!buffer.AtEnd && buffer.Current == '?')
                {
                    description += " (non-greedy)";
                    buffer.MoveNext();
                }
            }
            else
            {
                description = "missing '}' in quantifier";
            }
            buffer.AddLookup(this, startLoc, buffer.Offset - 1);
        }
コード例 #5
0
        public RegexCharacter(RegexBuffer buffer)
        {
            int  startLoc   = buffer.Offset;
            bool quantifier = false;

            switch (buffer.Current)
            {
            case '.':
                character = ". (any character)";
                buffer.MoveNext();
                special = true;
                break;

            case '+':
                character = "+ (one or more times)";
                buffer.MoveNext();
                special    = true;
                quantifier = true;
                break;

            case '*':
                character = "* (zero or more times)";
                buffer.MoveNext();
                special    = true;
                quantifier = true;
                break;

            case '?':
                character = "? (zero or one time)";
                buffer.MoveNext();
                special    = true;
                quantifier = true;
                break;

            case '^':
                character = "^ (anchor to start of string)";
                buffer.MoveNext();
                break;

            case '$':
                character = "$ (anchor to end of string)";
                buffer.MoveNext();
                break;

            case ' ':
                character = "' ' (space)";
                buffer.MoveNext();
                break;

            case '\\':
                DecodeEscape(buffer);
                break;

            default:
                character = buffer.Current.ToString();
                buffer.MoveNext();
                special = false;
                break;
            }
            if (quantifier)
            {
                if (!buffer.AtEnd && buffer.Current == '?')
                {
                    character += " (non-greedy)";
                    buffer.MoveNext();
                }
            }
            buffer.AddLookup(this, startLoc, buffer.Offset - 1, (character.Length == 1));
        }
コード例 #6
0
        public RegexAlternate(RegexBuffer buffer)
        {
            buffer.AddLookup(this, buffer.Offset, buffer.Offset);

            buffer.MoveNext();                          // skip "|"
        }