示例#1
0
    public void AlternativeParser()
    {
        var stream = new MemoryStream <char>("a".AsMemory());

        var p1 = Combinator.Alt(Combinator.Char('x'), Combinator.Char('y'), Combinator.Char('z'));
        var p2 = Combinator.Alt(Combinator.Char('a'), Combinator.Char('b'), Combinator.Char('c'));

        var r1 = p1.Parse(stream);
        var r2 = p2.Parse(stream);

        Assert.False(r1.IsOk);
        Assert.True(r2.IsOk);
        Assert.Equal('a', r2.Ok.Value);
    }
示例#2
0
    public void OptParser()
    {
        var stream1 = new MemoryStream <char>("a".AsMemory());
        var stream2 = new MemoryStream <char>("b".AsMemory());

        var p = Combinator.Opt(Combinator.Char('a'));

        var r1 = p.Parse(stream1);
        var r2 = p.Parse(stream2);

        Assert.True(r1.IsOk);
        Assert.True(r2.IsOk);
        Assert.Equal('a', r1.Ok.Value);
        Assert.Equal(default, r2.Ok.Value);
示例#3
0
    public void SequenceParser()
    {
        var stream = new MemoryStream <char>("abc".AsMemory());

        var p1 = Combinator.Seq(Combinator.Char('a'), Combinator.Char('b'), Combinator.Char('d'));
        var p2 = Combinator.Seq(Combinator.Char('a'), Combinator.Char('b'), Combinator.Char('c'));

        var r1 = p1.Parse(stream);
        var r2 = p2.Parse(stream);

        Assert.False(r1.IsOk);
        Assert.True(r2.IsOk);
        Assert.Equal(('a', 'b', 'c'), r2.Ok.Value);
    }
示例#4
0
    public void Repeat1Parser()
    {
        var emptyStream    = new MemoryStream <char>(string.Empty.AsMemory());
        var nonEmptyStream = new MemoryStream <char>("aaa".AsMemory());

        var p = Combinator.Rep1(Combinator.Char('a'));

        var r1 = p.Parse(emptyStream);
        var r2 = p.Parse(nonEmptyStream);

        Assert.False(r1.IsOk);
        Assert.True(r2.IsOk);
        Assert.Equal(3, r2.Ok.Value.Count);
        Assert.Equal('a', r2.Ok.Value[0]);
        Assert.Equal('a', r2.Ok.Value[1]);
        Assert.Equal('a', r2.Ok.Value[2]);
    }