示例#1
0
        public void ImprovingDefaultMessagesWithAKnownExpectation()
        {
            var labeled = Label(AB, "'A' followed by 'B'");

            //When p succeeds after consuming input, Label(p) is the same as p.
            AB.Parses(Tokenize("AB")).WithNoMessage().WithValue("AB");
            labeled.Parses(Tokenize("AB")).WithNoMessage().WithValue("AB");

            //When p fails after consuming input, Label(p) is the same as p.
            AB.FailsToParse(Tokenize("A!")).LeavingUnparsedTokens("!").WithMessage("(1, 2): B expected");
            labeled.FailsToParse(Tokenize("A!")).LeavingUnparsedTokens("!").WithMessage("(1, 2): B expected");

            //When p succeeds but does not consume input, Label(p) still succeeds but the potential failure is included.
            var succeedWithoutConsuming = new MonadicUnitParser <Token>(new Token(null, new Position(0, 0), "$"));

            succeedWithoutConsuming
            .PartiallyParses(Tokenize("!"))
            .LeavingUnparsedTokens("!")
            .WithNoMessage()
            .WithValue(Lexeme("$"));
            Label(succeedWithoutConsuming, "nothing")
            .PartiallyParses(Tokenize("!"))
            .LeavingUnparsedTokens("!")
            .WithMessage("(1, 1): nothing expected")
            .WithValue(Lexeme("$"));

            //When p fails but does not consume input, Label(p) fails with the given expectation.
            AB.FailsToParse(Tokenize("!")).LeavingUnparsedTokens("!").WithMessage("(1, 1): A expected");
            labeled.FailsToParse(Tokenize("!")).LeavingUnparsedTokens("!").WithMessage("(1, 1): 'A' followed by 'B' expected");
        }
示例#2
0
        public void CanBuildParserWhichSimulatesSuccessfulParsingOfGivenValueWithoutConsumingInput()
        {
            var parser = new MonadicUnitParser <int>(1);

            parser.PartiallyParses(Tokenize("input")).LeavingUnparsedTokens("i", "n", "p", "u", "t").WithValue(1);
        }