public void GetToEnd_ReturnsFullDataLength_WhenClassConstructedWithData()
        {
            // ARRANGE
            const string data = Fakes.Literal.BasicLiteral;
            var characterBuffer = new CharacterBuffer(data);

            // ACT
            var actualData = characterBuffer.GetToEnd();
            var actualDataLength = actualData.Length;

            // ASSERT
            Assert.AreEqual(data.Length, actualDataLength);
        }
 public bool Parse(CharacterBuffer buffer)
 {
     string str;
     this.Start = buffer.IndexInOriginalBuffer;
     if (buffer.IsAtEnd)
     {
         return false;
     }
     if (buffer.Next != 'p' && buffer.Next != 'P')
     {
         return false;
     }
     Match match = NamedClass.NamedClassRegex.Match(buffer.GetToEnd());
     if (!match.Success || match.Groups["Type"].Length <= 0)
     {
         this.Description = "Syntax error in Unicode character class";
         this.Literal = "\\p";
         buffer.Move(2);
         this.End = buffer.IndexInOriginalBuffer;
         this.IsValid = false;
         return true;
     }
     if (match.Groups["Type"].Value != "P")
     {
         this.MatchIfAbsent = false;
     }
     else
     {
         this.MatchIfAbsent = true;
     }
     this.ClassName = match.Groups["Name"].Value;
     int length = (int)UnicodeCategories.UnicodeAbbrev.Length;
     this.FriendlyName = "";
     int num = 0;
     while (num < length)
     {
         if (UnicodeCategories.UnicodeAbbrev[num] != this.ClassName)
         {
             num++;
         }
         else
         {
             this.FriendlyName = UnicodeCategories.UnicodeName[num];
             break;
         }
     }
     if (this.ClassName == "")
     {
         str = "Empty Unicode character class";
         this.IsValid = false;
     }
     else if (this.FriendlyName != "")
     {
         str = string.Concat("a Unicode character class: \"", this.FriendlyName, "\"");
     }
     else
     {
         str = string.Concat("Possibly unrecognized Unicode character class: [", this.ClassName, "]");
         this.IsValid = false;
     }
     this.Literal = match.Value;
     if (!this.IsValid)
     {
         this.Description = str;
     }
     else if (!this.MatchIfAbsent)
     {
         this.Description = string.Concat("Any character from ", str);
     }
     else
     {
         this.Description = string.Concat("Any character NOT from ", str);
     }
     buffer.Move(match.Length);
     base.ParseRepetitions(buffer);
     return true;
 }
        public void GetToEnd_ReturnsEmptyString_AfterMoveToEndIsCalled()
        {
            // ARRANGE
            const string data = Fakes.Literal.BasicLiteral;
            var characterBuffer = new CharacterBuffer(data);
            string expectedCharachter = string.Empty;

            // ACT
            characterBuffer.MoveToEnd();
            string actualData = characterBuffer.GetToEnd();

            // ASSERT
            Assert.AreEqual(actualData, expectedCharachter);
        }
 public bool Parse(CharacterBuffer buffer)
 {
     int num;
     this.Start = buffer.IndexInOriginalBuffer;
     if (buffer.CurrentCharacter != '\\')
     {
         return false;
     }
     buffer.MoveNext();
     if (buffer.IsAtEnd)
     {
         return false;
     }
     char current = buffer.CurrentCharacter;
     if (!char.IsDigit(current) && current != 'k')
     {
         buffer.Move(-1);
         return false;
     }
     Match match = BackReference.BackrefRegex.Match(buffer.GetToEnd());
     if (!match.Success)
     {
         if (current != 'k')
         {
             return false;
         }
         this.IsValid = false;
         this.Literal = "\\k";
         this.Description = "Invalid backreference";
         this.contents = "";
         buffer.MoveNext();
         this.End = buffer.IndexInOriginalBuffer;
         return true;
     }
     string value = match.Groups["Backreference"].Value;
     string str = match.Groups["Decimal"].Value;
     string value1 = match.Groups["Octal"].Value;
     string str1 = match.Groups["Named"].Value;
     this.Literal = string.Concat('\\', match.Value);
     if (str1 != "")
     {
         if (BackReference.Names.Contains(str1))
         {
             this.Description = string.Concat(this.Named, str1);
         }
         else if (!int.TryParse(str1, out num) || !BackReference.NumbersContains(num))
         {
             this.Description = string.Concat(this.MissingName, str1);
             this.IsValid = false;
         }
         else
         {
             this.Description = string.Concat(this.Numbered, str1);
         }
         buffer.Move(match.Length);
         base.ParseRepetitions(buffer);
         this.isNamed = true;
         this.contents = str1;
         return true;
     }
     if (value1 != "")
     {
         this.Description = string.Concat(this.ASCII, value1);
         buffer.Move(match.Length);
         base.ParseRepetitions(buffer);
         //this.Image = ImageType.Character;
         this.isOctal = true;
         this.contents = value1;
         return true;
     }
     if (!buffer.IsEcma)
     {
         if (value != "")
         {
             if (!BackReference.NumbersContains(int.Parse(value)))
             {
                 this.Description = string.Concat(this.MissingNumber, value);
                 this.IsValid = false;
             }
             else
             {
                 this.Description = string.Concat(this.Numbered, value);
             }
             buffer.Move(match.Length);
             base.ParseRepetitions(buffer);
             this.contents = value;
             return true;
         }
         num = int.Parse(str);
         if (BackReference.NumbersContains(num))
         {
             this.Description = string.Concat(this.Numbered, str);
             this.contents = str;
             buffer.Move(match.Length);
             base.ParseRepetitions(buffer);
             return true;
         }
         match = BackReference.OctalBackParseRegex.Match(buffer.GetToEnd());
         if (!match.Success)
         {
             return false;
         }
         this.Literal = string.Concat('\\', match.Value);
         this.Description = string.Concat(this.ASCII, match.Groups["Octal"].Value);
         buffer.Move(match.Length);
         base.ParseRepetitions(buffer);
         //this.Image = ImageType.Character;
         this.isOctal = true;
         this.contents = match.Groups["Octal"].Value;
         return true;
     }
     if (value != "")
     {
         num = int.Parse(value);
         if (BackReference.NumbersContains(num))
         {
             this.Description = string.Concat(this.Numbered, value);
             buffer.Move(match.Length);
             this.contents = value;
             base.ParseRepetitions(buffer);
             return true;
         }
         match = BackReference.OctalBackParseRegex.Match(buffer.GetToEnd());
         if (!match.Success)
         {
             return false;
         }
         this.Literal = string.Concat('\\', match.Value);
         this.Description = string.Concat(this.ASCII, match.Groups["Octal"].Value);
         buffer.Move(match.Length);
         base.ParseRepetitions(buffer);
         //this.Image = ImageType.Character;
         this.isOctal = true;
         this.contents = match.Groups["Octal"].Value;
         return true;
     }
     if (str == "")
     {
         return false;
     }
     for (int i = str.Length; i > 0; i--)
     {
         string str2 = str.Substring(0, i);
         num = int.Parse(str2);
         if (BackReference.NumbersContains(num))
         {
             this.Description = string.Concat(this.Numbered, str2);
             this.Literal = string.Concat("\\", str2);
             this.contents = str2;
             buffer.Move(i);
             base.ParseRepetitions(buffer);
             return true;
         }
     }
     match = BackReference.OctalBackParseRegex.Match(buffer.GetToEnd());
     if (!match.Success)
     {
         return false;
     }
     this.Literal = string.Concat('\\', match.Value);
     this.Description = string.Concat(this.ASCII, match.Groups["Octal"].Value);
     buffer.Move(match.Length);
     base.ParseRepetitions(buffer);
     //this.Image = ImageType.Character;
     this.isOctal = true;
     this.contents = match.Groups["Octal"].Value;
     return true;
 }
 public SpecialCharacter(CharacterBuffer buffer)
 {
     //this.Image = ImageType.SpecialCharacter;
     this.Escaped = false;
     this.Start = buffer.IndexInOriginalBuffer;
     if (buffer.IsAtEnd)
     {
         this.CharacterType = CharType.Invalid;
         this.character = "\\";
         this.Literal = "\\";
         this.Description = "Illegal \\ at end of pattern";
         this.IsValid = false;
         this.Start = this.Start - 1;
         this.End = this.Start + 1;
     }
     else if (buffer.CurrentCharacter == '[')
     {
         buffer.MoveNext();
         if (buffer.CurrentCharacter != '\u005E')
         {
             buffer.MoveNext();
             this.Literal = string.Concat("[\\", buffer.CurrentCharacter);
         }
         else
         {
             this.MatchIfAbsent = true;
             buffer.Move(2);
             this.Literal = string.Concat("[^\\", buffer.CurrentCharacter);
         }
         this.S = buffer.CurrentCharacter.ToString();
         if (this.MatchIfAbsent)
         {
             this.Description = string.Concat("Any character other than ", this.Description);
         }
         buffer.MoveNext();
         SpecialCharacter specialCharacter = this;
         specialCharacter.Literal = string.Concat(specialCharacter.Literal, buffer.CurrentCharacter);
         buffer.MoveNext();
     }
     else if (buffer.CurrentCharacter != '\\')
     {
         this.S = buffer.CurrentCharacter.ToString();
         this.Literal = this.S;
         buffer.MoveNext();
     }
     else
     {
         buffer.MoveNext();
         if (!buffer.IsAtEnd)
         {
             Match match = SpecialCharacter.RegNumeric.Match(buffer.GetToEnd());
             if (!match.Success)
             {
                 this.Escaped = true;
                 this.S = buffer.CurrentCharacter.ToString();
                 this.Literal = string.Concat("\\", this.S);
                 buffer.MoveNext();
             }
             else
             {
                 string str = match.Value.Substring(0, 1);
                 string str1 = str;
                 if (str != null)
                 {
                     if (str1 == "x")
                     {
                         this.Description = string.Concat("Hex ", match.Value.Substring(1));
                         this.CharacterType = CharType.Hex;
                         this.character = match.Value.Substring(1);
                         goto Label0;
                     }
                     else if (str1 == "u")
                     {
                         this.Description = string.Concat("Unicode ", match.Value.Substring(1));
                         this.CharacterType = CharType.Unicode;
                         this.character = match.Value.Substring(1);
                         goto Label0;
                     }
                     else
                     {
                         if (str1 != "c")
                         {
                             goto Label2;
                         }
                         this.Description = string.Concat("Control ", match.Value.Substring(1, 1));
                         this.CharacterType = CharType.Control;
                         this.character = match.Value.Substring(1);
                         goto Label0;
                     }
                 }
             Label2:
                 this.Description = string.Concat("Octal ", match.Value);
                 this.CharacterType = CharType.Octal;
                 this.character = match.Value.Substring(2);
             Label0:
                 this.Literal = string.Concat("\\", match.Value);
                 buffer.Move(match.Length);
             }
         }
         else
         {
             Utility.ParseError("Illegal \\ at end of pattern", buffer);
         }
     }
     base.ParseRepetitions(buffer);
 }
Пример #6
0
 private void ParseBrackets(CharacterBuffer buffer)
 {
     try
     {
         Match match = RepeatRegex.Match(buffer.GetToEnd());
         if (match.Success)
         {
             Element element = this;
             element.Literal = string.Concat(element.Literal, match.Value);
             buffer.Move(match.Length);
             string value = match.Groups["N"].Value;
             string str = match.Groups["M"].Value;
             string value1 = match.Groups["Exact"].Value;
             if (value == "" && value1 == "")
             {
                 Utility.ParseError("Error parsing the quantifier!", buffer);
                 this.IsValid = false;
             }
             else if (value1 != "")
             {
                 this.n = int.Parse(value1);
                 this.RepeatType = Repeat.Exact;
             }
             else if (value != "" && str != "")
             {
                 this.n = int.Parse(value);
                 this.m = int.Parse(str);
                 this.RepeatType = Repeat.Between;
                 if (this.n > this.m)
                 {
                     this.IsValid = false;
                     this.Description = "N is greater than M in quantifier!";
                 }
             }
             else if (!(value != "") || !(str == ""))
             {
                 Utility.ParseError("Error parsing the quantifier!", buffer);
                 this.IsValid = false;
             }
             else
             {
                 this.n = int.Parse(value);
                 this.RepeatType = Repeat.AtLeast;
             }
         }
     }
     catch
     {
         Utility.ParseError("Error parsing the quantifier", buffer);
         this.IsValid = false;
     }
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="Expression"/> class.
 /// </summary>
 /// <param name="buffer">The buffer.</param>
 public Expression(CharacterBuffer buffer)
     : this()
 {
     Literal = buffer.GetToEnd();
     Parse();
 }
        public void Parse(int offset, bool skipFirstCaptureNumber)
        {
            Character character;
            CharacterBuffer charBuffer = new CharacterBuffer(Literal)
            {
                Offset = offset,
                IgnoreWhiteSpace = IgnoreWhitespace,
                IsEcma = IsEcma
            };
            //Label2:
            while (!charBuffer.IsAtEnd)
            {
                int indexInOriginalBuffer = charBuffer.IndexInOriginalBuffer;

                HandleWhiteSpace(charBuffer, indexInOriginalBuffer);
                if (charBuffer.IsAtEnd) break; // Exit the loop

                char current = charBuffer.CurrentCharacter;
                if (current > Characters.Dot)
                {
                    if (current == Characters.QuestionMark)
                    {
                        //goto Label0;
                        AddMisplacedQuantifier(charBuffer);
                        continue;
                        // was goto Label2;
                    }
                    switch (current)
                    {
                        case Characters.SquareBracketOpen:
                            {
                                this.Add(new CharacterClass(charBuffer));
                                continue; // Move to next iteration
                            }
                        case Characters.BackSlash:
                            {
                                if (!SpecialCharacter.NextIsWhitespace(charBuffer))
                                {
                                    BackReference backReference = new BackReference();
                                    if (!backReference.Parse(charBuffer))
                                    {
                                        NamedClass namedClass = new NamedClass();
                                        if (!namedClass.Parse(charBuffer))
                                        {
                                            this.Add(new SpecialCharacter(charBuffer));
                                            continue; // Move to next iteration
                                        }
                                        else
                                        {
                                            this.Add(namedClass);
                                            continue; // Move to next iteration
                                        }
                                    }
                                    else
                                    {
                                        BackReference.NeedsSecondPass = true;
                                        if (!backReference.IsOctal)
                                        {
                                            this.Add(backReference);
                                            continue; // Move to next iteration
                                        }
                                        else
                                        {
                                            this.Add(new SpecialCharacter(backReference));
                                            continue; // Move to next iteration
                                        }
                                    }
                                }
                                else
                                {
                                    this.Add(new SpecialCharacter(charBuffer));
                                    continue; // Move to next iteration
                                }
                            }
                        case Characters.SquareBracketClosed:
                            {
                                break;
                            }
                        case Characters.CircumflexAccent:
                            {
                                //goto Label1;
                                AddSpecialCharacter(charBuffer);
                                continue;
                                // was goto Label2;
                            }
                        default:
                            {
                                switch (current)
                                {
                                    case Characters.CurlyBraceOpen:
                                        {
                                            character = new Character(charBuffer, true)
                                            {
                                                //Description = string.Concat(character.Literal, " Misplaced quantifier"),
                                                IsValid = false
                                            };
                                            character.Description = string.Concat(character.Literal,
                                                " Misplaced quantifier");
                                            if (character.RepeatType != Repeat.Once)
                                            {
                                                this.Add(character);
                                                continue;
                                            }
                                            else
                                            {
                                                this.Add(new Character(charBuffer));
                                                continue;
                                            }
                                        }
                                    case Characters.Pipe:
                                        {
                                            SubExpression subExpression = new SubExpression(this.Clone())
                                            {
                                                Literal = charBuffer.Substring(0, charBuffer.CurrentIndex),
                                                Start = charBuffer.Offset,
                                                End = charBuffer.IndexInOriginalBuffer
                                            };
                                            this.alternatives.Add(subExpression);
                                            charBuffer.MoveNext();
                                            int num = charBuffer.IndexInOriginalBuffer;
                                            charBuffer = new CharacterBuffer(charBuffer.GetToEnd())
                                            {
                                                Offset = num,
                                                IgnoreWhiteSpace = IgnoreWhitespace,
                                                IsEcma = IsEcma
                                            };
                                            this.Clear();
                                            continue;
                                        }
                                }
                                break;
                            }
                    }
                }
                else
                {
                    switch (current)
                    {
                        case '\t':
                        case '\n':
                        case '\r':
                            {
                                //goto Label1;
                                AddSpecialCharacter(charBuffer);
                                continue;
                                // was goto Label2;
                            }
                        case '\v':
                        case '\f':
                            {
                                break;
                            }
                        default:
                            {
                                switch (current)
                                {
                                    case ' ':
                                    case '$':
                                    case '.':
                                        {
                                            //goto Label1;
                                            AddSpecialCharacter(charBuffer);
                                            continue;
                                            // was goto Label2;
                                        }
                                    case '#':
                                        {
                                            if (!this.IgnoreWhitespace)
                                            {
                                                this.Add(new Character(charBuffer));
                                                continue;
                                            }
                                            else
                                            {
                                                this.Add(new Comment(charBuffer));
                                                continue;
                                            }
                                        }
                                    case '(':
                                        {
                                            Conditional conditional = new Conditional();
                                            if (!conditional.Parse(charBuffer))
                                            {
                                                Group group = new Group(charBuffer, skipFirstCaptureNumber);
                                                if (group.Type == GroupType.OptionsOutside)
                                                {
                                                    if (group.SetX == CheckState.Checked)
                                                    {
                                                        this.IgnoreWhitespace = true;
                                                    }
                                                    else if (group.SetX == CheckState.Unchecked)
                                                    {
                                                        this.IgnoreWhitespace = false;
                                                    }
                                                    charBuffer.IgnoreWhiteSpace = this.IgnoreWhitespace;
                                                }
                                                this.Add(group);
                                                continue;
                                            }
                                            else
                                            {
                                                this.Add(conditional);
                                                BackReference.NeedsSecondPass = true;
                                                continue;
                                            }
                                        }
                                    case Characters.BracketClosed:
                                        {
                                            character = new Character(charBuffer)
                                            {
                                                IsValid = false,
                                                Description = "Unbalanced parenthesis"
                                            };
                                            this.Add(character);
                                            continue; // Move to next character
                                        }
                                    case Characters.Star:
                                    case Characters.Plus:
                                        {
                                            //goto Label0;
                                            AddMisplacedQuantifier(charBuffer);
                                            continue;
                                            // was goto Label2;
                                        }
                                }
                                break;
                            }
                    }
                }
                Add(new Character(charBuffer));
            }

            HandleAlternatives(charBuffer);

            //Label0:
            //    AddMisplacedQuantifier(charBuffer);
            //    goto Label2;

            //Label1:
            //    AddSpecialCharacter(charBuffer);
            //    goto Label2;
        }