public void ValueTuple_Combine_6_Test()
        {
            var target = (_any, _any, _any, _any, _any, _any).Combine();

            var input = new StringCharacterSequence("abcdefghi");

            target.Parse(input).Value.Count.Should().Be(6);
        }
        public void ValueTuple_Produce_6_Test()
        {
            var target = (_any, _any, _any, _any, _any, _any).Rule((a, b, c, d, e, f) => $"{a}{b}{c}{d}{e}{f}");

            var input = new StringCharacterSequence("abcdefghijklmn");

            target.Parse(input).Value.Should().Be("abcdef");
        }
        public void GetRemainder_End()
        {
            var target = new StringCharacterSequence("abc");

            target.GetNext();
            target.GetNext();
            target.GetNext();
            target.GetRemainder().Should().Be("");
        }
        public void GetNext_Test()
        {
            var target = new StringCharacterSequence("abc");

            target.GetNext().Should().Be('a');
            target.GetNext().Should().Be('b');
            target.GetNext().Should().Be('c');
            target.GetNext().Should().Be('\0');
        }
        public void GetNext_OldMacNewlines()
        {
            var target = new StringCharacterSequence("\ra\r");

            target.GetNext().Should().Be('\n');
            target.GetNext().Should().Be('a');
            target.GetNext().Should().Be('\n');
            target.GetNext().Should().Be('\0');
        }
        public void GetNext_UnixNewlines()
        {
            var target = new StringCharacterSequence("\na\n");

            target.GetNext().Should().Be('\n');
            target.GetNext().Should().Be('a');
            target.GetNext().Should().Be('\n');
            target.GetNext().Should().Be('\0');
        }
Beispiel #7
0
        public void Parse_Test()
        {
            var target = Produce(() => 5);
            var input  = new StringCharacterSequence("abc");
            var result = target.Parse(input);

            result.Value.Should().Be(5);
            result.Consumed.Should().Be(0);
        }
        public void Parse_Test()
        {
            var parser = Empty();
            var input  = new StringCharacterSequence("abc");
            var result = parser.Parse(input);

            result.Success.Should().BeTrue();
            result.Value.Should().NotBeNull();
        }
        public void NotFollowedBy_Fail()
        {
            var parser = Match('[').NotFollowedBy(Match("~"));
            var input  = new StringCharacterSequence("[~test]");
            var result = parser.Parse(input);

            result.Success.Should().BeFalse();
            result.Consumed.Should().Be(0);
        }
Beispiel #10
0
        public void Line_Test()
        {
            var parser = Line();
            var input  = new StringCharacterSequence(@"line
NOT LINE");
            var result = parser.Parse(input);

            result.Value.Should().Be("line");
        }
        public void Parse_Test()
        {
            var parser = Match(char.IsNumber);
            var input  = new StringCharacterSequence("123");
            var result = parser.Parse(input);

            result.Value.Should().Be('1');
            result.Consumed.Should().Be(1);
        }
        public void ParseNext_Word()
        {
            var target = LexicalGrammar.GetParser();
            var source = new StringCharacterSequence("test class @class");

            target.Parse(source).Value.Should().Match(TokenType.Word, "test");
            target.Parse(source).Value.Should().Match(TokenType.Word, "class");
            target.Parse(source).Value.Should().Match(TokenType.Word, "@class");
        }
        public void Parse_Fail_Third()
        {
            var parser = Match("abc");
            var input  = new StringCharacterSequence("abXcd");
            var result = parser.Parse(input);

            result.Success.Should().BeFalse();
            result.Consumed.Should().Be(0);
        }
        public void Parse_Fail()
        {
            var parser = Match(char.IsLetter);
            var input  = new StringCharacterSequence("123");
            var result = parser.Parse(input);

            result.Success.Should().BeFalse();
            result.Consumed.Should().Be(0);
        }
        public void Output_Callback_Throw()
        {
            var    parser = Any().Chain <char, char, object>(c => throw new System.Exception());
            var    input  = new StringCharacterSequence("abc");
            Action act    = () => parser.Parse(input);

            act.Should().Throw <Exception>();
            input.GetNext().Should().Be('a');
        }
        public void ParseNext_IntegerDivideInteger()
        {
            var target = LexicalGrammar.GetParser();
            var source = new StringCharacterSequence(@"1/2");

            target.Parse(source).Value.Should().Match(TokenType.Integer, "1");
            target.Parse(source).Value.Should().Match(TokenType.Operator, "/");
            target.Parse(source).Value.Should().Match(TokenType.Integer, "2");
        }
Beispiel #17
0
        public void Parse_Exactly_TooFew()
        {
            var parser = List(Any(), 4, 4);
            var input  = new StringCharacterSequence("abc");
            var result = parser.Parse(input);

            result.Success.Should().BeFalse();
            result.Consumed.Should().Be(0);
        }
        public void NotFollowedBy_Success()
        {
            var parser = Match('[').NotFollowedBy(Match("~"));
            var input  = new StringCharacterSequence("[test]");
            var result = parser.Parse(input);

            result.Value.Should().Be('[');
            result.Consumed.Should().Be(1);
        }
Beispiel #19
0
        public void Peek_Test()
        {
            var target = Peek();
            var input  = new StringCharacterSequence("abc");

            target.Parse(input).Value.Should().Be('a');
            target.Parse(input).Value.Should().Be('a');
            target.Parse(input).Value.Should().Be('a');
        }
        public void CharacterString_Test()
        {
            var parser = ParserMethods.CharacterString("abc");
            var input  = new StringCharacterSequence("abcd");
            var result = parser.Parse(input);

            result.Success.Should().BeTrue();
            result.Value.Should().Be("abc");
            result.Consumed.Should().Be(3);
        }
        public void Parse_Empty()
        {
            var parser = Match("");
            var input  = new StringCharacterSequence("abc");
            var result = parser.Parse(input);

            result.Success.Should().BeTrue();
            result.Value.Count.Should().Be(0);
            result.Consumed.Should().Be(0);
        }
        public void Parse_Success_ThenFail()
        {
            var parser = If(_successParser, _failParser, Produce(() => true));

            var input  = new StringCharacterSequence("abc");
            var result = parser.Parse(input);

            result.Success.Should().BeFalse();
            input.Peek().Should().Be('a');
        }
        public void Parse_Fail_ThenSuccess()
        {
            var parser = If(_failParser, Any());

            var input  = new StringCharacterSequence("abc");
            var result = parser.Parse(input);

            result.Success.Should().BeFalse();
            result.Consumed.Should().Be(0);
        }
Beispiel #24
0
        public void Parse_Success_Fail()
        {
            var parser = And(_anyParser, _failParser);

            var input  = new StringCharacterSequence("abc");
            var result = parser.Parse(input);

            result.Success.Should().BeFalse();
            result.Consumed.Should().Be(0);
        }
        public IJsonValue Deserialize(string json)
        {
            var input  = new StringCharacterSequence(json);
            var lexer  = JsonLexer.CreateParser();
            var tokens = lexer.ToSequence(input).Select(r => r.Value);
            var parser = JsonGrammar.CreateParser();
            var result = parser.Parse(tokens);

            return(result.Success ? result.Value : null);
        }
Beispiel #26
0
        public void NotExtension_Parse_Fail()
        {
            var parser = _failParser.Not();

            var input  = new StringCharacterSequence("abc");
            var result = parser.Parse(input);

            result.Success.Should().BeTrue();
            result.Consumed.Should().Be(0);
        }
Beispiel #27
0
        private void RegexTestFail(string pattern, string input)
        {
            var sequence = new StringCharacterSequence(input);
            var parser   = Regex(pattern);
            var result   = parser.Parse(sequence);

            result.Success.Should().BeFalse();
            result.Consumed.Should().Be(0);
            sequence.Consumed.Should().Be(0);
        }
Beispiel #28
0
    /// <summary>
    /// Convenience method for parsers which act on character sequences. Attempts a parse but
    /// does not consume any input. Returns true if the parse would succeed, false otherwise.
    /// </summary>
    /// <param name="parser"></param>
    /// <param name="input"></param>
    /// <param name="options"></param>
    /// <returns></returns>
    public static bool CanMatch(this IMultiParser <char> parser, string input, StringCharacterSequence.Options options = default)
    {
        // Don't need to .Checkpoint()/.Rewind() because the sequence is private and we don't
        // reuse it
        var sequence = new StringCharacterSequence(input, options);
        var state    = new ParseState <char>(sequence, Defaults.LogMethod);
        var result   = parser.Parse(state);

        return(result.Success);
    }
Beispiel #29
0
        public void Parse_None()
        {
            var parser = List(Match(char.IsNumber));
            var input  = new StringCharacterSequence("abc");
            var result = parser.Parse(input);

            result.Success.Should().BeTrue();
            result.Value.Count().Should().Be(0);
            result.Consumed.Should().Be(0);
        }
Beispiel #30
0
        public void Any_Test()
        {
            var target = Any();
            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();
        }