示例#1
1
        public void Constructor_WhenGivenANullExpression_ThrowsException()
        {
            var start = new Cursor("OK");
            var end = start.Advance(2);
            var codeSpan = new CodeSpan("OK", start, end);

            Assert.That(() => new TypedExpression(codeSpan, null), Throws.InstanceOf<ArgumentNullException>());
        }
示例#2
1
        public void ToString_WhenConstructedWithANullValue_ReturnsTheCodeVerbatim()
        {
            var start = new Cursor("OK");
            var end = start.Advance(2);
            var codeSpan = new CodeSpan("OK", start, end);

            Assert.That(codeSpan.ToString(), Is.EqualTo("OK"));
        }
示例#3
0
        protected override IEnumerable<Token> Tokenize(string source)
        {
            Cursor c = new Cursor(source);
            while (c.Offset < source.Length)
            {
                var currentChar = source[c.Offset];

                if (operators.Contains(currentChar))
                {
                    yield return new Token(c.Offset, c.Offset + 1, "Operator.Symbol");
                    c.Advance(1);
                    continue;
                }

                if (grouping.Contains(currentChar))
                {
                    yield return new Token(c.Offset, c.Offset + 1, "Grouping.Statements");
                    c.Advance(1);
                    continue;
                }

                var match = whitespace.Match(source, c.Offset);
                if (match.Success && match.Index == c.Offset)
                {
                    yield return new Token(c.Offset, c.Offset + match.Length, "Whitespace.Insignificant");
                    c.Advance(match.Length);
                    continue;
                }

                match = nonOperator.Match(source, c.Offset);
                yield return new Token(c.Offset, c.Offset + match.Length, "Comment.Block");
                c.Advance(match.Length);
            }
        }
示例#4
0
        public void Advance_WhenAdvancingPastTheEndOfTheText_ThrowsException()
        {
            var subject = "OK_OK_OK_OK_OK";
            var cursor = new Cursor(subject);

            cursor.Advance(subject.Length);

            Assert.That(() => cursor.Advance(1), Throws.InstanceOf<InvalidOperationException>());
        }
示例#5
0
        public void Constructor_WhenGivenANullSettingsCollection_DoesNotThrow()
        {
            var start = new Cursor("OK");
            var end = start.Advance(2);

            Assert.That(() => new Grammar(new Rule[0], null, end), Throws.Nothing);
        }
示例#6
0
        public void Constructor_WhenGivenANullCollectionOfRules_ThrowsException()
        {
            var start = new Cursor("OK");
            var end = start.Advance(2);

            Assert.That(() => new Grammar(null, new Dictionary<Identifier, object>(), end), Throws.InstanceOf<ArgumentNullException>());
        }
示例#7
0
        public void Constructor_WhenGivenANullValue_ThrowsException(bool ignoreCase, bool fromResource)
        {
            var start = new Cursor("OK");
            var end = start.Advance(2);

            Assert.That(() => new LiteralExpression(start, end, null, ignoreCase, fromResource), Throws.InstanceOf<ArgumentNullException>());
        }
示例#8
0
        public void GetHashCode_WithEqualSubjectsAndIndexesAndStateKey_ReturnsSameValue([Values(0, 1, 2)] int index)
        {
            var subjectA = new Cursor("OK", index);
            var subjectB = subjectA.Advance(0);

            Assert.That(subjectB.GetHashCode(), Is.EqualTo(subjectA.GetHashCode()));
        }
示例#9
0
        public void OpEquality_WithEqualSubjectAndIndex_ReturnsTrue()
        {
            var subjectA = new Cursor("OK");
            var subjectB = subjectA.Advance(0);

            Assert.That(subjectA == subjectB, Is.True);
        }
示例#10
0
        public void OpInequality_WithEqualSubjectAndIndex_ReturnsFalse()
        {
            var subjectA = new Cursor("OK");
            var subjectB = subjectA.Advance(0);

            Assert.That(subjectA != subjectB, Is.False);
        }
示例#11
0
        public void Constructor_WhenGivenANullFlagsCollection_DoesNotThrow()
        {
            var start = new Cursor("OK");
            var end = start.Advance(2);

            Assert.That(() => new Rule(new Identifier("OK", start, end), new WildcardExpression(), null), Throws.Nothing);
        }
示例#12
0
        public void GetHashCode_WithEqualSubjectsAndIndexesAndStateKey_ReturnsSameValue([Values(0, 1, 2)] int index)
        {
            var subjectA = new Cursor("OK", index);
            var subjectB = subjectA.Advance(0);

            Assert.That(subjectB.GetHashCode(), Is.EqualTo(subjectA.GetHashCode()));
        }
示例#13
0
        public void Constructor_WhenGivenANullCollectionOfRules_ThrowsException()
        {
            var start = new Cursor("OK");
            var end   = start.Advance(2);

            Assert.That(() => new Grammar(null, new Dictionary <Identifier, object>(), end), Throws.InstanceOf <ArgumentNullException>());
        }
示例#14
0
        public void Constructor_WhenGivenANullExpression_ThrowsException()
        {
            var start = new Cursor("OK");
            var end = start.Advance(2);

            Assert.That(() => new Identifier(null, start, end), Throws.InstanceOf<ArgumentNullException>());
        }
示例#15
0
        public void Constructor_WhenGivenANullFlagsCollection_DoesNotThrow()
        {
            var start = new Cursor("OK");
            var end   = start.Advance(2);

            Assert.That(() => new Rule(new Identifier("OK", start, end), new WildcardExpression(), null), Throws.Nothing);
        }
示例#16
0
        public void Constructor_WhenGivenNullStartCursor_ThrowsException()
        {
            var start = new Cursor("OK");
            var end   = start.Advance(2);

            Assert.That(() => new CodeSpan("OK", null, end), Throws.InstanceOf <ArgumentNullException>());
        }
示例#17
0
        public void Constructor_WhenGivenNullStartCursor_ThrowsException()
        {
            var start = new Cursor("OK");
            var end = start.Advance(2);

            Assert.That(() => new CodeSpan("OK", null, end), Throws.InstanceOf<ArgumentNullException>());
        }
示例#18
0
        public void Constructor_WhenGivenANullValue_ThrowsException(bool ignoreCase, bool fromResource)
        {
            var start = new Cursor("OK");
            var end   = start.Advance(2);

            Assert.That(() => new LiteralExpression(start, end, null, ignoreCase, fromResource), Throws.InstanceOf <ArgumentNullException>());
        }
示例#19
0
        public void Constructor_WhenGivenANullSettingsCollection_DoesNotThrow()
        {
            var start = new Cursor("OK");
            var end   = start.Advance(2);

            Assert.That(() => new Grammar(new Rule[0], null, end), Throws.Nothing);
        }
示例#20
0
 private IParseResult <string> ParseClass(ref Cursor cursor, string characterRanges, bool negated = false, bool ignoreCase = false)
 {
     if (cursor.Location + 1 <= cursor.Subject.Length)
     {
         var  c     = cursor.Subject[cursor.Location];
         bool match = false;
         for (int i = 0; !match && i < characterRanges.Length; i += 2)
         {
             match = c >= characterRanges[i] && c <= characterRanges[i + 1];
         }
         if (!match && ignoreCase && (char.IsUpper(c) || char.IsLower(c)))
         {
             var cs = c.ToString();
             for (int i = 0; !match && i < characterRanges.Length; i += 2)
             {
                 var min = characterRanges[i];
                 var max = characterRanges[i + 1];
                 for (char o = min; !match && o <= max; o++)
                 {
                     match = (char.IsUpper(o) || char.IsLower(o)) && cs.Equals(o.ToString(), StringComparison.CurrentCultureIgnoreCase);
                 }
             }
         }
         if (match ^ negated)
         {
             var endCursor = cursor.Advance(1);
             var substr    = cursor.Subject.Substring(cursor.Location, 1);
             var result    = this.ReturnHelper <string>(cursor, ref endCursor, state => substr);
             cursor = endCursor;
             return(result);
         }
     }
     return(null);
 }
示例#21
0
        public void Constructor_WhenGivenANullExpression_ThrowsException()
        {
            var start = new Cursor("OK");
            var end   = start.Advance(2);

            Assert.That(() => new Rule(new Identifier("OK", start, end), null, new Identifier[0]), Throws.InstanceOf <ArgumentNullException>());
        }
        public void Constructor_WhenGivenANullExpression_ThrowsException()
        {
            var start      = new Cursor("OK");
            var end        = start.Advance(2);
            var quantifier = new Quantifier(start, end, 0);

            Assert.That(() => new RepetitionExpression(null, quantifier), Throws.InstanceOf <ArgumentNullException>());
        }
示例#23
0
        public void Constructor_WhenGivenANullStartCursor_ThrowsException(int min, int? max, bool nullDelimiter)
        {
            var start = new Cursor("OK");
            var end = start.Advance(2);
            var delimiter = nullDelimiter ? null : new WildcardExpression();

            Assert.That(() => new Quantifier(null, end, min, max, delimiter), Throws.InstanceOf<ArgumentNullException>());
        }
示例#24
0
        public void Constructor_WhenGivenANullStartCursor_ThrowsException(int min, int?max, bool nullDelimiter)
        {
            var start     = new Cursor("OK");
            var end       = start.Advance(2);
            var delimiter = nullDelimiter ? null : new WildcardExpression();

            Assert.That(() => new Quantifier(null, end, min, max, delimiter), Throws.InstanceOf <ArgumentNullException>());
        }
示例#25
0
        public void ToString_WhenConstructedWithANullValue_ReturnsTheCodeVerbatim()
        {
            var start    = new Cursor("OK");
            var end      = start.Advance(2);
            var codeSpan = new CodeSpan("OK", start, end);

            Assert.That(codeSpan.ToString(), Is.EqualTo("OK"));
        }
示例#26
0
        public void Constructor_WhenGivenANullExpression_ThrowsException()
        {
            var start    = new Cursor("OK");
            var end      = start.Advance(2);
            var codeSpan = new CodeSpan("OK", start, end);

            Assert.That(() => new TypedExpression(codeSpan, null), Throws.InstanceOf <ArgumentNullException>());
        }
示例#27
0
        public void OpEquality_WithEqualValuesAndCursors_ReturnsTrue()
        {
            var start = new Cursor("OK", 0);
            var end = start.Advance(1);
            var subjectA = new ParseResult<int>(start, end, 0);
            var subjectB = new ParseResult<int>(start, end, 0);

            Assert.That(subjectA == subjectB, Is.True);
        }
示例#28
0
        public void Equals_WithOtherObject_ReturnsFalse()
        {
            var start = new Cursor("OK", 0);
            var end = start.Advance(1);
            var subjectA = new ParseResult<int>(start, end, 0);
            var subjectB = new object();

            Assert.That(subjectA.Equals(subjectB), Is.False);
        }
示例#29
0
        public void OpEquality_WithNullReferenceOnRight_ReturnsFalse()
        {
            var start = new Cursor("OK", 0);
            var end = start.Advance(1);
            var subjectA = new ParseResult<int>(start, end, 0);
            var subjectB = (ParseResult<int>)null;

            Assert.That(subjectA == subjectB, Is.False);
        }
示例#30
0
        public void OpEquality_WithUnequalValues_ReturnsFalse()
        {
            var start    = new Cursor("OK");
            var end      = start.Advance(1);
            var subjectA = new ParseResult <int>(start, end, 1);
            var subjectB = new ParseResult <int>(end, end, 0);

            Assert.That(subjectA == subjectB, Is.False);
        }
示例#31
0
        public void OpEquality_WithEqualValuesAndCursors_ReturnsTrue()
        {
            var start    = new Cursor("OK");
            var end      = start.Advance(1);
            var subjectA = new ParseResult <int>(start, end, 0);
            var subjectB = new ParseResult <int>(start, end, 0);

            Assert.That(subjectA == subjectB, Is.True);
        }
示例#32
0
        public void OpInequality_WithNullReferenceOnLeft_ReturnsTrue()
        {
            var start    = new Cursor("OK");
            var end      = start.Advance(1);
            var subjectA = (ParseResult <int>)null;
            var subjectB = new ParseResult <int>(start, end, 0);

            Assert.That(subjectA != subjectB, Is.True);
        }
示例#33
0
        public void GetHashCode_WithEqualValuesAndCursors_ReturnsSameValue([Values(0, 1, 2)] int index)
        {
            var start    = new Cursor("OK");
            var end      = start.Advance(index);
            var subjectA = new ParseResult <int>(start, end, 0);
            var subjectB = new ParseResult <int>(start, end, 0);

            Assert.That(subjectB.GetHashCode(), Is.EqualTo(subjectA.GetHashCode()));
        }
示例#34
0
        public void OpEquality_WithNullReferenceOnRight_ReturnsFalse()
        {
            var start    = new Cursor("OK");
            var end      = start.Advance(1);
            var subjectA = new ParseResult <int>(start, end, 0);
            var subjectB = (ParseResult <int>)null;

            Assert.That(subjectA == subjectB, Is.False);
        }
示例#35
0
        public void Equals_WithOtherObject_ReturnsFalse()
        {
            var start    = new Cursor("OK");
            var end      = start.Advance(1);
            var subjectA = new ParseResult <int>(start, end, 0);
            var subjectB = new object();

            Assert.That(subjectA.Equals(subjectB), Is.False);
        }
示例#36
0
        public void GetHashCode_WithEqualValuesAndCursors_ReturnsSameValue([Values(0, 1, 2)] int index)
        {
            var start = new Cursor("OK");
            var end = start.Advance(index);
            var subjectA = new ParseResult<int>(start, end, 0);
            var subjectB = new ParseResult<int>(start, end, 0);

            Assert.That(subjectB.GetHashCode(), Is.EqualTo(subjectA.GetHashCode()));
        }
示例#37
0
        public void OpInequality_WithUnequalEndCursors_ReturnsTrue()
        {
            var start    = new Cursor("OK");
            var one      = start.Advance(1);
            var two      = one.Advance(1);
            var subjectA = new ParseResult <int>(start, one, 0);
            var subjectB = new ParseResult <int>(start, two, 0);

            Assert.That(subjectA != subjectB, Is.True);
        }
示例#38
0
        public void OpEquality_WithUnequalStartCursors_ReturnsFalse()
        {
            var start = new Cursor("OK", 0);
            var one = start.Advance(1);
            var two = one.Advance(1);
            var subjectA = new ParseResult<int>(start, two, 0);
            var subjectB = new ParseResult<int>(one, two, 0);

            Assert.That(subjectA == subjectB, Is.False);
        }
示例#39
0
        public void OpEquality_WithUnequalStartCursors_ReturnsFalse()
        {
            var start    = new Cursor("OK");
            var one      = start.Advance(1);
            var two      = one.Advance(1);
            var subjectA = new ParseResult <int>(start, two, 0);
            var subjectB = new ParseResult <int>(one, two, 0);

            Assert.That(subjectA == subjectB, Is.False);
        }
示例#40
0
        public void AdvanceShouldReturnOnEof()
        {
            var c = new Cursor("Lorem ipsum");

            for (var i = 0; i < c.Buffer.Length - 1; i++)
            {
                c.Advance();
                Assert.False(c.Eof);
            }

            Assert.Equal('m', c.Current);

            c.Advance();
            Assert.True(c.Eof);
            Assert.Equal(Cursor.NullChar, c.Current);

            c.Advance();
            Assert.True(c.Eof);
            Assert.Equal(Cursor.NullChar, c.Current);
        }
示例#41
0
        public void Advance_IncrementsTheOffset()
        {
            var subject = "OK_OK_OK_OK_OK";
            var cursor = new Cursor(subject);

            for (int i = 1; i <= subject.Length; i++)
            {
                cursor.Advance(1);
                Assert.That(cursor.Offset, Is.EqualTo(i));
            }
        }
示例#42
0
        public void Advance_WhenOnASingleLine_IncrementsTheColumnNumber()
        {
            var subject = "OK_OK_OK_OK_OK";
            var cursor = new Cursor(subject);

            for (int i = 1; i <= subject.Length; i++)
            {
                cursor.Advance(1);
                Assert.That(cursor.Column, Is.EqualTo(i + 1));
            }
        }
示例#43
0
 private IParseResult <string> ParseAny(ref Cursor cursor)
 {
     if (cursor.Location + 1 <= cursor.Subject.Length)
     {
         var substr    = cursor.Subject.Substring(cursor.Location, 1);
         var endCursor = cursor.Advance(1);
         var result    = this.ReturnHelper <string>(cursor, ref endCursor, state => substr);
         cursor = endCursor;
         return(result);
     }
     return(null);
 }
示例#44
0
 private IParseResult <string> ParseLiteral(ref Cursor cursor, string literal, bool ignoreCase = false)
 {
     if (cursor.Location + literal.Length <= cursor.Subject.Length)
     {
         var substr = cursor.Subject.Substring(cursor.Location, literal.Length);
         if (ignoreCase ? substr.Equals(literal, StringComparison.OrdinalIgnoreCase) : substr == literal)
         {
             var endCursor = cursor.Advance(substr.Length);
             var result    = this.ReturnHelper <string>(cursor, ref endCursor, state => substr);
             cursor = endCursor;
             return(result);
         }
     }
     return(null);
 }
示例#45
0
        public override void HandleMessage(object message)
        {
            if (message is KeyPressedMessage msg)
            {
                char c = msg.Key.KeyChar;

                if (c == BackspaceChar)
                {
                    cursor.Retract();
                }
                var drawMsg = new DrawMessage((cursor.Pos.X, cursor.Pos.Y), new DrawInfo(c));
                MessageBus.PostMessage(drawMsg);
                //matrix[cursor.Pos.X, cursor.Pos.Y] = c;
                if (c != BackspaceChar)
                {
                    cursor.Advance();
                }
            }
        }
示例#46
0
        public void OpInequality_WithUnequalEndCursors_ReturnsTrue()
        {
            var start = new Cursor("OK", 0);
            var one = start.Advance(1);
            var two = one.Advance(1);
            var subjectA = new ParseResult<int>(start, one, 0);
            var subjectB = new ParseResult<int>(start, two, 0);

            Assert.That(subjectA != subjectB, Is.True);
        }
示例#47
0
 public void Advance_WhenTheCursorIsMutable_ThrowsException()
 {
     var cursor = new Cursor("OK", 0).WithMutability(mutable: true);
     Assert.That(() => cursor.Advance(0), Throws.InstanceOf<InvalidOperationException>());
 }
示例#48
0
        public void OpInequality_WithEqualSubjectAndIndex_ReturnsFalse()
        {
            var subjectA = new Cursor("OK");
            var subjectB = subjectA.Advance(0);

            Assert.That(subjectA != subjectB, Is.False);
        }
示例#49
0
        public void OpInequality_WithNullReferenceOnLeft_ReturnsTrue()
        {
            var start = new Cursor("OK", 0);
            var end = start.Advance(1);
            var subjectA = (ParseResult<int>)null;
            var subjectB = new ParseResult<int>(start, end, 0);

            Assert.That(subjectA != subjectB, Is.True);
        }
示例#50
0
        public void OpEquality_WithEqualSubjectAndIndex_ReturnsTrue()
        {
            var subjectA = new Cursor("OK");
            var subjectB = subjectA.Advance(0);

            Assert.That(subjectA == subjectB, Is.True);
        }
示例#51
0
        public void Advance_WhenPassingALineTerminator_ResetsTheColumnNumberToOne(string lineTerminator)
        {
            var subject = "OK" + lineTerminator + "OK";
            var cursor = new Cursor(subject);

            cursor.Advance("OK".Length + lineTerminator.Length);

            Assert.That(cursor.Column, Is.EqualTo(1));
        }
示例#52
0
        public void Advance_WhenTheCursorIsMutable_ThrowsException()
        {
            var cursor = new Cursor("OK", 0).WithMutability(mutable: true);

            Assert.That(() => cursor.Advance(0), Throws.InstanceOf <InvalidOperationException>());
        }
示例#53
0
        public void Advance_WhenPassingLFWhichIsFollowedByCRLF_IncrementsTheColumnNumber()
        {
            var subject = "OK\n\r\n";
            var cursor = new Cursor(subject);

            cursor.Advance("OK".Length + 1);

            Assert.That(cursor.Line, Is.EqualTo(2));
        }
示例#54
0
        public void OpEquality_WithUnequalValues_ReturnsFalse()
        {
            var start = new Cursor("OK", 0);
            var end = start.Advance(1);
            var subjectA = new ParseResult<int>(start, end, 1);
            var subjectB = new ParseResult<int>(end, end, 0);

            Assert.That(subjectA == subjectB, Is.False);
        }
示例#55
0
        public void AdvanceShouldCountLinesAndColumns()
        {
            var c = new Cursor("123\n456\r\n789");

            Assert.Equal('1', c.Current);
            Assert.Equal(0, c.Position.Offset);
            Assert.Equal(1, c.Position.Column);
            Assert.Equal(1, c.Position.Line);

            c.Advance();

            Assert.Equal('2', c.Current);
            Assert.Equal(1, c.Position.Offset);
            Assert.Equal(2, c.Position.Column);
            Assert.Equal(1, c.Position.Line);

            c.Advance();

            Assert.Equal('3', c.Current);
            Assert.Equal(2, c.Position.Offset);
            Assert.Equal(3, c.Position.Column);
            Assert.Equal(1, c.Position.Line);
            c.Advance();

            Assert.Equal('\n', c.Current);
            Assert.Equal(3, c.Position.Offset);
            Assert.Equal(4, c.Position.Column);
            Assert.Equal(1, c.Position.Line);
            c.Advance();

            Assert.Equal('4', c.Current);
            Assert.Equal(4, c.Position.Offset);
            Assert.Equal(1, c.Position.Column);
            Assert.Equal(2, c.Position.Line);
            c.Advance();

            Assert.Equal('5', c.Current);
            Assert.Equal(5, c.Position.Offset);
            Assert.Equal(2, c.Position.Column);
            Assert.Equal(2, c.Position.Line);
            c.Advance();

            Assert.Equal('6', c.Current);
            Assert.Equal(6, c.Position.Offset);
            Assert.Equal(3, c.Position.Column);
            Assert.Equal(2, c.Position.Line);
            c.Advance();

            Assert.Equal('\n', c.Current);
            Assert.Equal(8, c.Position.Offset);
            Assert.Equal(3, c.Position.Column);
            Assert.Equal(2, c.Position.Line);
            c.Advance();

            Assert.Equal('7', c.Current);
            Assert.Equal(9, c.Position.Offset);
            Assert.Equal(1, c.Position.Column);
            Assert.Equal(3, c.Position.Line);
            c.Advance();

            Assert.Equal('8', c.Current);
            Assert.Equal(10, c.Position.Offset);
            Assert.Equal(2, c.Position.Column);
            Assert.Equal(3, c.Position.Line);
            c.Advance();

            Assert.Equal('9', c.Current);
            Assert.Equal(11, c.Position.Offset);
            Assert.Equal(3, c.Position.Column);
            Assert.Equal(3, c.Position.Line);
            c.Advance();

            Assert.Equal(Cursor.NullChar, c.Current);
            Assert.Equal(12, c.Position.Offset);
            Assert.Equal(4, c.Position.Column);
            Assert.Equal(3, c.Position.Line);
        }
示例#56
0
        public void Advance_WhenPassingTheTheFirstCharacterOfCRLF_DoesNotIncrementTheLineNumber()
        {
            var subject = "OK\r\n";
            var cursor = new Cursor(subject);

            cursor.Advance("OK".Length + 1);

            Assert.That(cursor.Line, Is.EqualTo(1));
        }
示例#57
0
        public void Advance_WhenPassingALineTerminator_IncrementsTheLineNumber(string lineTerminator)
        {
            int count = 10;

            var subject = string.Join(string.Empty, from i in Enumerable.Range(0, count) select lineTerminator); // Create a subject containing 10 copies of the line terminator.
            var cursor = new Cursor(subject);

            for (int i = 1; i <= count; i++)
            {
                cursor.Advance(lineTerminator.Length);
                Assert.That(cursor.Line, Is.EqualTo(i + 1));
            }
        }