示例#1
0
        public void TokenTrySuccessIsTransparent()
        {
            var tryAb  = Token.EqualTo('a').Then(_ => Token.EqualTo('b')).Try();
            var result = tryAb.TryParse(StringAsCharTokenList.Tokenize("ab"));

            Assert.True(result.HasValue);
            Assert.True(result.Remainder.IsAtEnd);
        }
示例#2
0
        public void TokenTryFailureBacktracks()
        {
            var tryAb  = Token.EqualTo('a').Then(_ => Token.EqualTo('b')).Try();
            var result = tryAb.TryParse(StringAsCharTokenList.Tokenize("ac"));

            Assert.False(result.HasValue);
            Assert.True(result.Backtrack);
        }
示例#3
0
        public void ApplyOnParsedTokenCallsAppliedParser()
        {
            var input  = StringAsCharTokenList.Tokenize("abcd");
            var aAs42  = Token.EqualTo('a').Apply(Character.AnyChar.Value(42));
            var result = aAs42(input);

            Assert.Equal(42, result.Value);
        }
示例#4
0
        public void AnAppliedParserMustConsumeTheWholeTokenSpan()
        {
            var input  = StringAsCharTokenList.Tokenize("abcd");
            var just42 = Token.EqualTo('a').Apply(Parse.Return(42));
            var result = just42(input);

            Assert.False(result.HasValue);
            // The "invalid a" here is the token name, since we're using characters as tokens - in normal use
            // this would read more like "invalid URI: unexpected `:`".
            Assert.Equal("Syntax error (line 1, column 1): invalid a, unexpected `a`.", result.ToString());
        }
示例#5
0
        public void AFailingAppliedParserDoesNotCauseTokenBacktracking2()
        {
            var input  = StringAsCharTokenList.Tokenize("abcd");
            var just42 = Token.EqualTo('a').Apply(Span.EqualTo("b"))
                         .Or(Token.EqualTo('x').Value(TextSpan.Empty));
            var result = just42(input);

            Assert.False(result.HasValue);
            // The "invalid a" here is the token name, since we're using characters as tokens - in normal use
            // this would read more like "invalid URI: expected `:`".
            Assert.Equal("Syntax error (line 1, column 1): invalid a, unexpected `a`, expected `b`.", result.ToString());
        }