public CharacterClass(CharacterBuffer buffer) { //this.Image = ImageType.CharacterClass; this.Start = buffer.IndexInOriginalBuffer; if (buffer.IsAtEnd) { Utility.ParseError("CharacterClass: Reached end of buffer looking for a character!", buffer); this.IsValid = false; } int currentIndex = buffer.CurrentIndex; ParsedCharacterClass parsedCharacterClass = buffer.GetParsedCharacterClass(); if (parsedCharacterClass.Count == 0) { this.Description = parsedCharacterClass.ErrorMessage; buffer.MoveTo(currentIndex + 1); this.End = buffer.IndexInOriginalBuffer; this.Literal = "["; this.IsValid = false; return; } int num = buffer.CurrentIndex - currentIndex; Match match = CharacterClass.ClassRegex.Match(buffer.Substring(currentIndex, num)); if (!match.Success) { this.Description = "Invalid Character Class"; this.IsValid = false; this.Literal = "["; } else { if (match.Groups["Negate"].Value != "^") { this.Negate = false; this.MatchIfAbsent = false; } else { this.Negate = true; this.MatchIfAbsent = true; } if (match.Groups["Contents"].Value.Length == 0) { this.Description = "Character class is empty"; this.IsValid = false; } else { this.Content = match.Groups["Contents"].Value; } this.Literal = match.Value; } if (this.IsValid) { if (!this.Negate) { this.Description = string.Concat("Any character in this class: ", this.Literal); } else { this.Description = string.Concat("Any character that is NOT in this class: ", this.Literal.Remove(1, 1)); } } base.ParseRepetitions(buffer); }
public void Substring_ReturnsCorrectPortionOfData() { // ARRANGE const string data = Fakes.Literal.BasicLiteral; int expectedStartIndex = 1; int expectedLength = 1; string expected = data.Substring(expectedStartIndex, expectedLength); var characterBuffer = new CharacterBuffer(data); // ACT var actual = characterBuffer.Substring(expectedStartIndex, expectedLength); // ASSERT Assert.AreEqual(expected, actual); }
private void HandleAlternatives(CharacterBuffer charBuffer) { if (this.alternatives.Count != 0) { SubExpression alternative = new SubExpression(this.Clone()); alternative.Exp.alternatives = new Alternatives(); alternative.Start = charBuffer.Offset; alternative.End = charBuffer.IndexInOriginalBuffer; alternative.Literal = charBuffer.Substring(0, charBuffer.CurrentIndex); this.alternatives.Add(alternative); this.alternatives.Start = 0; this.alternatives.End = charBuffer.IndexInOriginalBuffer; } }
public void Substring_ArgumentOutOfRangeExceptionThrown_WhenStartIndexPlusLengthIsGreaterThanLength() { // ARRANGE const string data = Fakes.Literal.BasicLiteral; const int expectedStartIndex = 1; const int expectedLength = 10; var characterBuffer = new CharacterBuffer(data); // ACT characterBuffer.Substring(expectedStartIndex, expectedLength); // ASSERT // Exception thrown by now }
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; }