public void GetNext_Test()
        {
            var parser = new AnyParser <char>();
            var target = new ParseResultSequence <char, char>("abc".ToCharacterSequence(), parser, null);

            target.GetNext().Value.Should().Be('a');
            target.GetNext().Value.Should().Be('b');
            target.GetNext().Value.Should().Be('c');
        }
示例#2
0
 public void SetUp()
 {
     parser = P.Any <string>(
         P.Literal("foo"),
         P.Literal("bar")
         )
              .Process(p => p.GetMatchedString())
              .CreateParser();
 }
示例#3
0
        public void Parse_Test()
        {
            var target = new AnyParser <char>();
            var input  = new StringCharacterSequence("abc");

            target.Parse(input).Value.Should().Be('a');
            target.Parse(input).Value.Should().Be('b');
            target.Parse(input).Value.Should().Be('c');
            target.Parse(input).Success.Should().BeFalse();
        }
        public void IsAtEnd_Test()
        {
            var parser = new AnyParser <char>();
            var target = new ParseResultSequence <char, char>("abc".ToCharacterSequence(), parser, null);

            target.IsAtEnd.Should().BeFalse();
            target.GetNext();
            target.IsAtEnd.Should().BeFalse();
            target.GetNext();
            target.IsAtEnd.Should().BeFalse();
            target.GetNext();
            target.IsAtEnd.Should().BeTrue();
        }
示例#5
0
        public void Should_not_match()
        {
            var values = new[] { 1, 2, 3, 4, 5 };

            var intParser = new AnyParser <int>();

            var parser = from x in intParser.Except(from y in intParser where y == 1 select y)
                         select x;

            var result = parser.ParseArray(values);

            Assert.IsFalse(result.HasValue);
        }
示例#6
0
        public void Should_match()
        {
            var values = new[] { 2, 3, 4, 5 };

            var intParser = new AnyParser <int>();

            var parser = from x in intParser.Except(from y in intParser where y == 1 select y)
                         select x;

            var result = parser.Execute(values);

            Assert.IsTrue(result.HasValue);
        }
示例#7
0
        public void Should_not_match()
        {
            var values = new[] {1, 2, 3, 4, 5};

            var intParser = new AnyParser<int>();

            var parser = from x in intParser.Except(from y in intParser where y == 1 select y)
                         select x;

            var result = parser.ParseArray(values);

            Assert.IsFalse(result.HasValue);
        }
示例#8
0
        public void Should_only_take_that_many_elements()
        {
            var subject = new[] { 1, 2, 3, 4, 5 };

            var anyParser = new AnyParser <int>();

            Parser <int[], int[]> query = (from x in anyParser
                                           select x).Take(2);

            Result <int[], int[]> result = query.ParseArray(subject);

            Assert.IsTrue(result.HasValue);
            Assert.AreEqual(2, result.Value.Length);
        }
示例#9
0
        public void Should_only_take_that_many_elements()
        {
            var subject = new[] { 1, 2, 3, 4, 5 };

            var anyParser = new AnyParser <int>();

            Parser <int, IReadOnlyList <int> > query = (from x in anyParser
                                                        select x).Take(2);

            Result <Cursor <int>, IReadOnlyList <int> > result = query.Execute(subject);

            Assert.IsTrue(result.HasResult);
            Assert.AreEqual(2, result.Result.Count);
        }
示例#10
0
        public void Should_only_take_that_many_elements()
        {
            var subject = new[] {1, 2, 3, 4, 5};

            var anyParser = new AnyParser<int>();

            Parser<int[], int[]> query = (from x in anyParser
                                          select x).Take(2);

            Result<int[], int[]> result = query.ParseArray(subject);

            Assert.IsTrue(result.HasValue);
            Assert.AreEqual(2, result.Value.Length);
        }
示例#11
0
        public void Should_return_a_matching_element()
        {
            var subject = new[] {1, 2, 3, 4, 5};

            var anyParser = new AnyParser<int>();

            Parser<int[], int> query = from x in anyParser
                                       where x == 1
                                       select x;

            Result<int[], int> result = query.ParseArray(subject);

            Assert.IsTrue(result.HasValue);
            Assert.AreEqual(1, result.Value);
        }
示例#12
0
        public void Should_not_return_an_unmatching_element()
        {
            var subject = new[] {1, 2, 3, 4, 5};

            var anyParser = new AnyParser<int>();

            Parser<int[], int> query = from x in anyParser
                                       where x == 2
                                       where x != 1
                                       select x;

            Result<int[], int> result = query.ParseArray(subject);

            Assert.IsFalse(result.HasValue);
        }
示例#13
0
        public void Should_not_return_an_unmatching_element()
        {
            var subject = new[] { 1, 2, 3, 4, 5 };

            var anyParser = new AnyParser <int>();

            IParser <int, int> query = from x in anyParser
                                       where x == 2
                                       where x != 1
                                       select x;

            Result <Cursor <int>, int> result = query.Execute(subject);

            Assert.IsFalse(result.HasResult);
        }
示例#14
0
        public void Should_return_a_matching_element()
        {
            var subject = new[] { 1, 2, 3, 4, 5 };

            var anyParser = new AnyParser <int>();

            Parser <int[], int> query = from x in anyParser
                                        where x == 1
                                        select x;

            Result <int[], int> result = query.ParseArray(subject);

            Assert.IsTrue(result.HasValue);
            Assert.AreEqual(1, result.Value);
        }
示例#15
0
        public void Should_return_a_matching_element()
        {
            var subject = new[] { 1, 2, 3, 4, 5 };

            var anyParser = new AnyParser <int>();

            IParser <int, int> query = from x in anyParser
                                       where x == 1
                                       select x;

            Result <Cursor <int>, int> result = query.Execute(subject);

            Assert.IsTrue(result.HasResult);
            Assert.AreEqual(1, result.Result);
        }
示例#16
0
        public void Should_not_return_an_unmatching_element()
        {
            var subject = new[] { 1, 2, 3, 4, 5 };

            var anyParser = new AnyParser <int>();

            Parser <int[], int> query = from x in anyParser
                                        where x == 2
                                        where x != 1
                                        select x;

            Result <int[], int> result = query.ParseArray(subject);

            Assert.IsFalse(result.HasValue);
        }
示例#17
0
        public void Should_return_the_first_element()
        {
            const string subject = "Hello, World.";

            var anyParser = new AnyParser <char>();

            Parser <char[], char> query = from any in anyParser
                                          select any;

            Result <char[], char> parsed = query.ParseString(subject);

            Assert.IsTrue(parsed.HasValue);
            Assert.AreEqual('H', parsed.Value);

            Assert.AreEqual(1, parsed.Next.Offset);
        }
示例#18
0
        public void Should_return_the_first_element_of_value_type()
        {
            var subject = new[] { 1, 2, 3, 4, 5, 6 };

            var anyParser = new AnyParser <int>();

            Parser <int[], int> query = from any in anyParser
                                        select any;

            Result <int[], int> parsed = query.ParseArray(subject);

            Assert.IsTrue(parsed.HasValue);
            Assert.AreEqual(1, parsed.Value);

            Assert.AreEqual(1, parsed.Next.Offset);
        }
        public void Location_Test()
        {
            var parser = new AnyParser <char>();
            var target = new ParseResultSequence <char, char>("abc".ToCharacterSequence(), parser, null);

            target.CurrentLocation.Line.Should().Be(1);
            target.CurrentLocation.Column.Should().Be(0);
            target.GetNext();
            target.CurrentLocation.Line.Should().Be(1);
            target.CurrentLocation.Column.Should().Be(1);
            target.GetNext();
            target.CurrentLocation.Line.Should().Be(1);
            target.CurrentLocation.Column.Should().Be(2);
            target.GetNext();
            target.CurrentLocation.Line.Should().Be(1);
            target.CurrentLocation.Column.Should().Be(3);
        }
示例#20
0
        public void Should_support_a_combined_parser()
        {
            var anyParser = new AnyParser<int>();

            Parser<int[], int> parser = from x in anyParser
                                        where x == 1
                                        select x;

            var visualizer = new ParserVisualizer();
            parser.Accept(visualizer);
            string text = visualizer.ToString();

            Console.WriteLine(text);

            string expected = @"  (Int32) *
            (Int32) Where x => (x == 1)";
            Assert.AreEqual(expected, text);
        }
        public void Checkpoint_Test()
        {
            var parser = new AnyParser <char>();
            var target = new ParseResultSequence <char, char>("abcde".ToCharacterSequence(), parser, null);

            target.GetNext().Value.Should().Be('a');
            target.GetNext().Value.Should().Be('b');
            var cp = target.Checkpoint();

            target.GetNext().Value.Should().Be('c');
            target.GetNext().Value.Should().Be('d');
            target.GetNext().Value.Should().Be('e');
            target.IsAtEnd.Should().BeTrue();
            cp.Rewind();
            target.GetNext().Value.Should().Be('c');
            target.GetNext().Value.Should().Be('d');
            target.GetNext().Value.Should().Be('e');
            target.IsAtEnd.Should().BeTrue();
        }
示例#22
0
        public void Should_support_a_combined_parser()
        {
            var anyParser = new AnyParser <int>();

            Parser <int[], int> parser = from x in anyParser
                                         where x == 1
                                         select x;

            var visualizer = new ParserVisualizer();

            parser.Accept(visualizer);
            string text = visualizer.ToString();

            Console.WriteLine(text);

            string expected = @"  (Int32) *
(Int32) Where x => (x == 1)";

            Assert.AreEqual(expected, text);
        }
示例#23
0
        public void Should_return_a_series_of_elements()
        {
            var subject = new[] { 1, 2, 3, 4, 5, 6 };

            var anyParser = new AnyParser <int>();

            Parser <int[], int[]> query = from one in anyParser
                                          from two in anyParser
                                          from three in anyParser
                                          from four in anyParser
                                          select new[] { one, two, three, four };

            Result <int[], int[]> parsed = query.ParseArray(subject);

            Assert.IsTrue(parsed.HasValue);
            Assert.AreEqual(4, parsed.Value.Length);
            Assert.AreEqual(1, parsed.Value[0]);
            Assert.AreEqual(2, parsed.Value[1]);
            Assert.AreEqual(3, parsed.Value[2]);
            Assert.AreEqual(4, parsed.Value[3]);

            Assert.AreEqual(4, parsed.Next.Offset);
        }
示例#24
0
        public void Should_only_take_that_many_elements_and_include_the_payload()
        {
            var subject = new[] { 1, 2, 3, 4, 5 };

            var anyParser = new AnyParser <int>();

            Parser <int, IReadOnlyList <int> > query = (from x in anyParser
                                                        select x).Take(2);

            Result <Cursor <int>, IReadOnlyList <int> > result = query.Execute(subject, new MyPayload {
                Value = "Hello"
            });

            Assert.IsTrue(result.HasResult);
            Assert.AreEqual(2, result.Result.Count);

            Assert.That(result.Next.HasContext(typeof(MyPayload)), Is.True);

            MyPayload payload;

            Assert.That(result.Next.TryGetContext(out payload), Is.True);

            Assert.That(payload.Value, Is.EqualTo("Hello"));
        }
示例#25
0
 private bool Accept <TInput>(AnyParser <TInput> p, BnfStringifyVisitor state)
 {
     state.Append('.');
     return(true);
 }