예제 #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.FromTokenWhile functions
            public static void TestFromTokenWhile()
            {
                var text   = "..12345 0x256 0X126 // one line comment\r\n2nd line";
                var source = new ScannerStringSource(text);
                var test   = new ScannerTestClass(source);

                var num = new CharSet();

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

                var any = new CharSet();

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

                TestCase(
                    "FromTokenWhile(<real, not empty while>)",
                    () =>
                {
                    SourceToken token;
                    var result = test.FromTokenWhile(
                        ".", num, false, null, true,
                        out token
                        );
                    var pos = source.Position;
                    return(new { Result = result, Token = token, Position = pos });
                },
                    new { Result = ScanResult.NoMatch, Token = new SourceToken(0, 1), 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(
                    "FromTokenWhile(<real>)",
                    () =>
                {
                    SourceToken token;
                    var result = test.FromTokenWhile(
                        ".", num, false, null, false,
                        out token
                        );
                    var pos = source.Position;
                    return(new { Result = result, Token = token, Position = pos });
                },
                    new { Result = ScanResult.Match, Token = new SourceToken(0, 1), Position = 1 },
                    (a, b) => a.Result == b.Result && a.Token.Equals(b.Token) && a.Position == b.Position,
                    (v) => $"{{{v.Result}, {v.Token}, {v.Position}}}"
                    );

                TestCase(
                    "FromTokenWhile(<real>)",
                    () =>
                {
                    SourceToken token;
                    var result = test.FromTokenWhile(
                        ".", num, false, null, false,
                        out token
                        );
                    var pos = source.Position;
                    return(new { Result = result, Token = token, Position = pos });
                },
                    new { Result = ScanResult.Match, Token = new SourceToken(1, 6), Position = 7 },
                    (a, b) => a.Result == b.Result && a.Token.Equals(b.Token) && a.Position == b.Position,
                    (v) => $"{{{v.Result}, {v.Token}, {v.Position}}}"
                    );

                TestCase(
                    "FromTokenWhile(<hex>)",
                    () =>
                {
                    SourceToken token;
                    test.SkipToToken();
                    var result = test.FromTokenWhile(
                        new string[] { "0X", "0x" }, num, false, null, false,
                        out token
                        );
                    var pos = source.Position;
                    return(new { Result = result, Token = token, Position = pos });
                },
                    new { Result = ScanResult.Match, Token = new SourceToken(8, 5), Position = 13 },
                    (a, b) => a.Result == b.Result && a.Token.Equals(b.Token) && a.Position == b.Position,
                    (v) => $"{{{v.Result}, {v.Token}, {v.Position}}}"
                    );

                TestCase(
                    "FromTokenWhile(<hex>)",
                    () =>
                {
                    SourceToken token;
                    test.SkipToToken();
                    var result = test.FromTokenWhile(
                        new string[] { "0X", "0x" }, num, false, null, false,
                        out token
                        );
                    var pos = source.Position;
                    return(new { Result = result, Token = token, Position = pos });
                },
                    new { Result = ScanResult.Match, Token = new SourceToken(14, 5), Position = 19 },
                    (a, b) => a.Result == b.Result && a.Token.Equals(b.Token) && a.Position == b.Position,
                    (v) => $"{{{v.Result}, {v.Token}, {v.Position}}}"
                    );

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

                TestCase(
                    "FromTokenWhile(<// comment multi>)",
                    () =>
                {
                    SourceToken token;
                    test.SkipToToken();
                    var result = test.FromTokenWhile(
                        "//", any, true, null, false,
                        out token
                        );
                    var pos = source.Position;
                    return(new { Result = result, Token = token, Position = pos });
                },
                    new { Result = ScanResult.Match, Token = new SourceToken(20, 29), Position = 49 },
                    (a, b) => a.Result == b.Result && a.Token.Equals(b.Token) && a.Position == b.Position,
                    (v) => $"{{{v.Result}, {v.Token}, {v.Position}}}"
                    );

                Console.WriteLine();
            }
예제 #3
0
            // execute test cases for Scanner.SkipToToken functions
            public static void TestSkipToToken()
            {
                var text   = " \t\bA\r\nB";
                var source = new ScannerStringSource(text);

                var test = new ScannerTestClass(source);

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

                source.Advance(1);

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

                TestCase(
                    "SkipToToken(true)",
                    (d) =>
                {
                    var result = test.SkipToToken();
                    return(new { Result = result, Current = test.CharCurrent });
                },
                    new { Result = true, Current = 'B' },
                    0,
                    (a, b) => a.Result == b.Result && a.Current == b.Current,
                    (v) => $"{{{v.Result}, '{v.Current}'}}"
                    );

                source.Advance(1);

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

                Console.WriteLine();
            }