Пример #1
0
            // execute test cases for Scanner.FromSetWhile function
            public static void TestFromSetWhile()
            {
                var text   = "_iDenTifier1.test # one line comment\r\n2nd line";
                var source = new ScannerStringSource(text);
                var test   = new ScannerTestClass(source);

                var alpha = new CharSet();

                alpha.Add('a', 'z');
                alpha.Add('A', 'Z');
                alpha.Add('_');

                var alphanum = new CharSet(alpha);

                alphanum.Add('0', '9');

                var hash = new CharSet();

                hash.Add('#');

                var any = new CharSet();

                any.Add('\x1', '\xFFFF');

                TestCase(
                    "FromSetWhile(<ident, no adv., no inner>)",
                    () =>
                {
                    SourceToken token;
                    var result = test.FromSetWhile(
                        alpha, alphanum, false, null,
                        out token, false
                        );
                    var pos = source.Position;
                    return(new { Result = result, Token = token, Position = pos });
                },
                    new { Result = ScanResult.Match, Token = new SourceToken(0, 12), Position = 0 },
                    (a, b) => a.Result == b.Result && a.Token.Equals(b.Token) && a.Position == b.Position,
                    (v) => $"{{{v.Result}, {v.Token}, {v.Position}}}"
                    );

                TestCase(
                    "FromSetWhile(<ident, adv., no inner>)",
                    () =>
                {
                    SourceToken token;
                    var result = test.FromSetWhile(
                        alpha, alphanum, false, null,
                        out token
                        );
                    var pos = source.Position;
                    return(new { Result = result, Token = token, Position = pos });
                },
                    new { Result = ScanResult.Match, Token = new SourceToken(0, 12), Position = 12 },
                    (a, b) => a.Result == b.Result && a.Token.Equals(b.Token) && a.Position == b.Position,
                    (v) => $"{{{v.Result}, {v.Token}, {v.Position}}}"
                    );

                TestCase(
                    "FromSetWhile(<ident, adv., no inner>)",
                    () =>
                {
                    SourceToken token;
                    var result = test.FromSetWhile(
                        alpha, alphanum, false, null,
                        out token
                        );
                    var pos = source.Position;
                    return(new { Result = result, Token = token, Position = pos });
                },

                    // the token here is irrelevant, but it will contain values as if one char
                    // was read
                    new { Result = ScanResult.NoMatch, Token = new SourceToken(12, 1), Position = 12 },

                    (a, b) => a.Result == b.Result && a.Token.Equals(b.Token) && a.Position == b.Position,
                    (v) => $"{{{v.Result}, {v.Token}, {v.Position}}}"
                    );

                TestCase(
                    "FromSetWhile(<ident, adv., inner>)",
                    () =>
                {
                    SourceToken token;
                    var result = test.FromSetWhile(
                        alpha, alphanum, false, () => test.Check('.', false) ? 2 : 0,
                        out token
                        );
                    var pos = source.Position;
                    return(new { Result = result, Token = token, Position = pos });
                },
                    new { Result = ScanResult.Match, Token = new SourceToken(12, 5), Position = 17 },
                    (a, b) => a.Result == b.Result && a.Token.Equals(b.Token) && a.Position == b.Position,
                    (v) => $"{{{v.Result}, {v.Token}, {v.Position}}}"
                    );


                TestCase(
                    "FromSetWhile(<# comment>)",
                    () =>
                {
                    SourceToken token;
                    test.SkipToToken();
                    var result = test.FromSetWhile(
                        hash, any, false, null,
                        out token, false
                        );
                    var pos = source.Position;
                    return(new { Result = result, Token = token, Position = pos });
                },
                    new { Result = ScanResult.Match, Token = new SourceToken(18, 18), Position = 18 },
                    (a, b) => a.Result == b.Result && a.Token.Equals(b.Token) && a.Position == b.Position,
                    (v) => $"{{{v.Result}, {v.Token}, {v.Position}}}"
                    );

                TestCase(
                    "FromSetWhile(<# comment, multiline>)",
                    () =>
                {
                    SourceToken token;
                    var result = test.FromSetWhile(
                        hash, any, true, null,
                        out token
                        );
                    var pos = source.Position;
                    return(new { Result = result, Token = token, Position = pos });
                },
                    new { Result = ScanResult.Match, Token = new SourceToken(18, 28), Position = 46 },
                    (a, b) => a.Result == b.Result && a.Token.Equals(b.Token) && a.Position == b.Position,
                    (v) => $"{{{v.Result}, {v.Token}, {v.Position}}}"
                    );

                Console.WriteLine();
            }
Пример #2
0
            // execute test cases for Scanner.Check functions
            public static void TestCheck()
            {
                var text   = "ABCDEFG";
                var source = new ScannerStringSource(text);
                var test   = new ScannerTestClass(source);

                TestCase(
                    "Check('{0}')",
                    (d) =>
                {
                    var result = test.Check(d);
                    var pos    = source.Position;
                    return(new { Result = result, Position = pos });
                },
                    new { Result = false, Position = 0 },
                    'a',
                    (a, b) => a.Result == b.Result && a.Position == b.Position,
                    (v) => $"{{{v.Result}, {v.Position}}}"
                    );

                TestCase(
                    "Check('{0}', false)",
                    (d) =>
                {
                    var result = test.Check(d, false);
                    var pos    = source.Position;
                    return(new { Result = result, Position = pos });
                },
                    new { Result = true, Position = 0 },
                    'A',
                    (a, b) => a.Result == b.Result && a.Position == b.Position,
                    (v) => $"{{{v.Result}, {v.Position}}}"
                    );

                TestCase(
                    "Check('{0}')",
                    (d) =>
                {
                    var result = test.Check(d);
                    var pos    = source.Position;
                    return(new { Result = result, Position = pos });
                },
                    new { Result = true, Position = 1 },
                    'A',
                    (a, b) => a.Result == b.Result && a.Position == b.Position,
                    (v) => $"{{{v.Result}, {v.Position}}}"
                    );

                TestCase(
                    "Check(\"{0}\")",
                    (d) =>
                {
                    var result = test.Check(d);
                    var pos    = source.Position;
                    return(new { Result = result, Position = pos });
                },
                    new { Result = false, Position = 1 },
                    "ABC",
                    (a, b) => a.Result == b.Result && a.Position == b.Position,
                    (v) => $"{{{v.Result}, {v.Position}}}"
                    );

                TestCase(
                    "Check(\"{0}\", false)",
                    (d) =>
                {
                    var result = test.Check(d, false);
                    var pos    = source.Position;
                    return(new { Result = result, Position = pos });
                },
                    new { Result = true, Position = 1 },
                    "BCD",
                    (a, b) => a.Result == b.Result && a.Position == b.Position,
                    (v) => $"{{{v.Result}, {v.Position}}}"
                    );

                TestCase(
                    "Check(\"{0}\")",
                    (d) =>
                {
                    var result = test.Check(d);
                    var pos    = source.Position;
                    return(new { Result = result, Position = pos });
                },
                    new { Result = true, Position = 4 },
                    "BCD",
                    (a, b) => a.Result == b.Result && a.Position == b.Position,
                    (v) => $"{{{v.Result}, {v.Position}}}"
                    );

                Console.WriteLine();
            }