public void Peek_Test()
        {
            var target = new StringCharacterSequence("abc");

            target.Peek().Should().Be('a');
            target.Peek().Should().Be('a');
            target.Peek().Should().Be('a');
        }
        public void Peek_OldMacNewlines()
        {
            var target = new StringCharacterSequence("\ra\r");

            target.Peek().Should().Be('\n');
            target.GetNext();
            target.Peek().Should().Be('a');
            target.GetNext();
            target.Peek().Should().Be('\n');
            target.GetNext();
            target.Peek().Should().Be('\0');
        }
        public void Peek_WindowsNewlines()
        {
            var target = new StringCharacterSequence("\r\na\r\n");

            target.Peek().Should().Be('\n');
            target.GetNext();
            target.Peek().Should().Be('a');
            target.GetNext();
            target.Peek().Should().Be('\n');
            target.GetNext();
            target.Peek().Should().Be('\0');
        }
예제 #4
0
        public void Parse_Success_ThenFail()
        {
            var parser = If(_successParser, _failParser, Produce(() => true));

            var input  = new StringCharacterSequence("abc");
            var result = parser.Parse(input);

            result.Success.Should().BeFalse();
            input.Peek().Should().Be('a');
        }
예제 #5
0
        public void FollowedBy_Success()
        {
            var parser = Match('[').FollowedBy(Match('~'));
            var input  = new StringCharacterSequence("[~test]");
            var result = parser.Parse(input);

            result.Value.Should().Be('[');
            result.Consumed.Should().Be(1);
            input.Peek().Should().Be('~');
        }
예제 #6
0
        public void FollowedBy_Fail()
        {
            var parser = Match('[').FollowedBy(Match('~'));
            var input  = new StringCharacterSequence("[test]");
            var result = parser.Parse(input);

            result.Success.Should().BeFalse();
            result.Consumed.Should().Be(0);
            input.Peek().Should().Be('[');
        }
예제 #7
0
        public void Parse_Fail()
        {
            var target = And(Any(), Fail <char>()).None();
            var input  = new StringCharacterSequence("abc");
            var result = target.Parse(input);

            result.Success.Should().BeFalse();
            result.Consumed.Should().Be(0);
            input.Peek().Should().Be('a');
        }
예제 #8
0
        public void Parse_Output_Test()
        {
            var target = Any().None();
            var input  = new StringCharacterSequence("abc");
            var result = target.Parse(input);

            result.Success.Should().BeTrue();
            result.Value.Should().Be('a');
            result.Consumed.Should().Be(0);
            input.Peek().Should().Be('a');
        }
예제 #9
0
        public void RewindInput_Test()
        {
            var parser = Rule(
                Match('a'),
                Match('b'),
                Match('c'),
                (a, b, c) => "ok"
                );
            var input  = new StringCharacterSequence("abd");
            var result = parser.Parse(input);

            result.Success.Should().BeFalse();
            input.Peek().Should().Be('a');
        }
        public void Indexer_Test()
        {
            var sequence = new StringCharacterSequence("abcdefg");
            var buffer   = sequence.CreateBuffer();

            buffer[0].Should().Be('a');
            buffer[1].Should().Be('b');
            buffer[2].Should().Be('c');
            buffer[3].Should().Be('d');
            buffer[4].Should().Be('e');

            var result = buffer.Capture(3);
            var str    = new string(result);

            str.Should().Be("abc");

            sequence.Peek().Should().Be('d');
        }
예제 #11
0
        public void Parse_Fail_Rewind()
        {
            var parser = Sequential(s =>
            {
                var first  = s.Parse(Any());
                var second = s.Parse(Any());
                var third  = s.Parse(Any());
                var fail   = s.Parse(Fail <char>());

                return($"{first}{second}{third}".ToUpper());
            });

            var input  = new StringCharacterSequence("abc");
            var result = parser.Parse("abc");

            result.Success.Should().BeFalse();
            input.Peek().Should().Be('a');
        }
예제 #12
0
        public void ZeroOrMore_MissingRight_Recursed_Rewind()
        {
            // We match <first> and <middle>, recurse on <right>, but the recursed rule
            // fails on <recursed.Right>. Rewind back to a success and leave the
            // unmatched second <middle> on the input sequence
            var numberParser = Match(char.IsNumber).Transform(c => c.ToString());
            var letterParser = Match(char.IsLetter).Transform(c => c.ToString());
            var parser       = RightApply(
                numberParser,
                letterParser,
                (l, m, r) => $"({l}{m}{r})"
                );

            var input  = new StringCharacterSequence("1a2b");
            var result = parser.Parse(input);

            result.Value.Should().Be("(1a2)");
            input.Peek().Should().Be('b');
            result.Consumed.Should().Be(3);
        }
예제 #13
0
        public void ZeroOrMore_MissingRight_Rewind()
        {
            // If we match <first> and <middle> but fail to parse <right> and there is
            // no synthetic option specified, we should rewind <middle> and only return
            // <first>
            var numberParser = Match(char.IsNumber).Transform(c => c.ToString());
            var letterParser = Match(char.IsLetter).Transform(c => c.ToString());
            var parser       = RightApply(
                numberParser,
                letterParser,
                (l, m, r) => $"({l}{m}{r})"
                );

            var input  = new StringCharacterSequence("1a");
            var result = parser.Parse(input);

            result.Value.Should().Be("1");
            input.Peek().Should().Be('a');
            result.Consumed.Should().Be(1);
        }
        public void Peek_End()
        {
            var target = new StringCharacterSequence("");

            target.Peek().Should().Be('\0');
        }