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); }
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); }
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); }
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()); }
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()); }