public RegexQuantifier(RegexBuffer buffer)
        {
            int startLocation = buffer.Offset;

            buffer.MoveNext();
            Match match = new Regex(@"(?<n>\d+)(?<Comma>,?)(?<m>\d*)\}").Match(buffer.String);

            if (match.Success)
            {
                if (match.Groups["m"].Length != 0)
                {
                    this.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)
                {
                    this.description = string.Format("At least {0} times", match.Groups["n"]);
                }
                else
                {
                    this.description = string.Format("Exactly {0} times", match.Groups["n"]);
                }
                buffer.Offset += match.Groups[0].Length;
                if (!buffer.AtEnd && (buffer.Current == '?'))
                {
                    this.description = this.description + " (non-greedy)";
                    buffer.MoveNext();
                }
            }
            else
            {
                this.description = "missing '}' in quantifier";
            }
            buffer.AddLookup(this, startLocation, buffer.Offset - 1);
        }
        public RegexCharClass(RegexBuffer buffer)
        {
            int startLocation = buffer.Offset;

            buffer.MoveNext();
            Match match = new Regex(@"(?<Negated>\^?)(?<Class>.+?)\]").Match(buffer.String);

            if (match.Success)
            {
                if (match.Groups["Negated"].ToString() == "^")
                {
                    this.description = string.Format("Any character not in \"{0}\"", match.Groups["Class"]);
                }
                else
                {
                    this.description = string.Format("Any character in \"{0}\"", match.Groups["Class"]);
                }
                buffer.Offset += match.Groups[0].Length;
            }
            else
            {
                this.description = "missing ']' in character class";
            }
            buffer.AddLookup(this, startLocation, buffer.Offset - 1);
        }
 private void EatComment(RegexBuffer buffer)
 {
     while (buffer.Current != '\r')
     {
         buffer.MoveNext();
     }
 }
Exemplo n.º 4
0
        private void DecodeEscape(RegexBuffer buffer)
        {
            buffer.MoveNext();
            this.character = (string)escaped[buffer.Current];
            if (this.character == null)
            {
                if (!this.CheckBackReference(buffer))
                {
                    switch (buffer.Current)
                    {
                    case 'u':
                    {
                        buffer.MoveNext();
                        string text = buffer.String.Substring(0, 4);
                        this.character = "Unicode " + text;
                        buffer.Offset += 4;
                        return;
                    }

                    case 'x':
                    {
                        buffer.MoveNext();
                        string text2 = buffer.String.Substring(0, 2);
                        this.character = "Hex " + text2;
                        buffer.Offset += 2;
                        return;
                    }

                    case ' ':
                        this.character = "' ' (space)";
                        this.special   = false;
                        buffer.MoveNext();
                        return;

                    case 'c':
                        buffer.MoveNext();
                        this.character = "CTRL-" + buffer.Current;
                        buffer.MoveNext();
                        return;
                    }
                    this.character = new string(buffer.Current, 1);
                    this.special   = false;
                    buffer.MoveNext();
                }
            }
            else
            {
                this.special = true;
                buffer.MoveNext();
            }
        }
Exemplo n.º 5
0
 public RegexCapture(RegexBuffer buffer)
 {
     this.startLocation = buffer.Offset;
     buffer.MoveNext();
     buffer.ClearInSeries();
     if (buffer.Current == '?')
     {
         bool flag = this.CheckNamed(buffer);
         if (!flag)
         {
             flag = this.CheckBalancedGroup(buffer);
         }
         if (!flag)
         {
             flag = this.CheckNonCapturing(buffer);
         }
         if (!flag)
         {
             flag = this.CheckOptions(buffer);
         }
         if (!flag)
         {
             flag = this.CheckLookahead(buffer);
         }
         if (!flag)
         {
             flag = this.CheckNonBacktracking(buffer);
         }
         if (!flag)
         {
             flag = this.CheckConditional(buffer);
         }
     }
     else if (!this.HandlePlainOldCapture(buffer))
     {
         throw new Exception(string.Format("Unrecognized capture: {0}", buffer.String));
     }
     buffer.AddLookup(this, this.startLocation, buffer.Offset - 1);
 }
 public RegexAlternate(RegexBuffer buffer)
 {
     buffer.AddLookup(this, buffer.Offset, buffer.Offset);
     buffer.MoveNext();
 }
Exemplo n.º 7
0
        public RegexCharacter(RegexBuffer buffer)
        {
            int  startLocation = buffer.Offset;
            bool flag          = false;

            switch (buffer.Current)
            {
            case ' ':
                this.character = "' ' (space)";
                buffer.MoveNext();
                break;

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

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

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

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

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

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

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

            default:
                this.character = buffer.Current.ToString();
                buffer.MoveNext();
                this.special = false;
                break;
            }
            if ((flag && !buffer.AtEnd) && (buffer.Current == '?'))
            {
                this.character = this.character + " (non-greedy)";
                buffer.MoveNext();
            }
            buffer.AddLookup(this, startLocation, buffer.Offset - 1, this.character.Length == 1);
        }
        private void Parse(RegexBuffer buffer)
        {
            while (!buffer.AtEnd)
            {
                if (buffer.IgnorePatternWhitespace && (((buffer.Current == ' ') || (buffer.Current == '\r')) || ((buffer.Current == '\n') || (buffer.Current == '\t'))))
                {
                    buffer.MoveNext();
                }
                else
                {
                    switch (buffer.Current)
                    {
                    case '(':
                    {
                        this.items.Add(new RegexCapture(buffer));
                        continue;
                    }

                    case ')':
                        return;

                    case '#':
                    {
                        if (buffer.IgnorePatternWhitespace)
                        {
                            this.EatComment(buffer);
                        }
                        else
                        {
                            this.items.Add(new RegexCharacter(buffer));
                        }
                        continue;
                    }

                    case '[':
                    {
                        this.items.Add(new TeamAgile.RegexKit.Common.RegexParser.RegexCharClass(buffer));
                        continue;
                    }

                    case '\\':
                    {
                        this.items.Add(new RegexCharacter(buffer));
                        continue;
                    }

                    case '{':
                    {
                        this.items.Add(new RegexQuantifier(buffer));
                        continue;
                    }

                    case '|':
                    {
                        this.items.Add(new RegexAlternate(buffer));
                        continue;
                    }
                    }
                    this.items.Add(new RegexCharacter(buffer));
                }
            }
        }