Esempio n. 1
0
        /// <summary>
        /// Returns <c>true</c> if <see cref="Helper"/> is at the start of a line comment.
        /// </summary>
        /// <returns>True if <see cref="Helper"/> is at the start of a line comment.</returns>
        private bool IsLineCommentStart()
        {
            Debug.Assert(Helper != null);

            if (LineComments != null)
            {
                foreach (string lineComment in LineComments)
                {
                    if (Helper.MatchesCurrentPosition(lineComment))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
            public void RegExTests()
            {
                string        text   = "summer side creature toothpaste dime wind harbor cake nail attention opinion railway horses garden alley quicksand knot servant fight form park polish toad rub hall";
                ParsingHelper helper = new ParsingHelper(text);
                string        s      = helper.ParseTokenRegEx(@"\b[d]\w+");

                Assert.AreEqual("dime", s);
                Assert.AreEqual(36, helper.Index);

                helper.Reset();
                IEnumerable <string> results = helper.ParseTokensRegEx(@"\b[s]\w+");

                CollectionAssert.AreEqual(new[] {
                    "summer",
                    "side",
                    "servant"
                }, results.ToList());
                Assert.AreEqual(127, helper.Index);

                helper.Reset();
                s = helper.ParseTokenRegEx(@"\b[x]\w+");
                Assert.AreEqual(string.Empty, s);
                Assert.AreEqual(text.Length, helper.Index);

                helper.Reset();
                results = helper.ParseTokensRegEx(@"\b[x]\w+");
                CollectionAssert.AreEqual(new List <string>(), results.ToList());
                Assert.AreEqual(text.Length, helper.Index);

                helper.Reset();
                helper.SkipToRegEx(@"\b[a]\w+");
                Assert.IsTrue(helper.MatchesCurrentPosition("attention"));

                s = helper.ParseToRegEx(@"\b[r]\w+");
                Assert.AreEqual("attention opinion ", s);
                Assert.IsTrue(helper.MatchesCurrentPosition("railway "));

                helper.Reset();
                helper.SkipToRegEx(@"\b[a]\w+", true);
                Assert.IsTrue(helper.MatchesCurrentPosition(" opinion"));

                helper.Reset("Abc1234def5678ghi");
                Assert.AreEqual(true, helper.SkipTo("123"));
                helper.SkipRegEx(@"\d+");
                Assert.AreEqual('d', helper.Peek());
                helper.SkipRegEx(@"[a-z]+");
                Assert.AreEqual('5', helper.Peek());

                // Test overloads that accept a Regex object

                Regex regex;

                helper.Reset(text);

                regex = new Regex(@"\b[d]\w+");
                s     = helper.ParseTokenRegEx(regex);
                Assert.AreEqual("dime", s);
                Assert.AreEqual(36, helper.Index);

                regex = new Regex(@"\b[s]\w+");
                helper.Reset();
                results = helper.ParseTokensRegEx(regex);
                CollectionAssert.AreEqual(new[] {
                    "summer",
                    "side",
                    "servant"
                }, results.ToList());
                Assert.AreEqual(127, helper.Index);

                regex = new Regex(@"\b[x]\w+");
                helper.Reset();
                s = helper.ParseTokenRegEx(regex);
                Assert.AreEqual(string.Empty, s);
                Assert.AreEqual(text.Length, helper.Index);

                regex = new Regex(@"\b[x]\w+");
                helper.Reset();
                results = helper.ParseTokensRegEx(regex);
                CollectionAssert.AreEqual(new List <string>(), results.ToList());
                Assert.AreEqual(text.Length, helper.Index);

                regex = new Regex(@"\b[a]\w+");
                helper.Reset();
                helper.SkipToRegEx(regex);
                Assert.IsTrue(helper.MatchesCurrentPosition("attention"));

                regex = new Regex(@"\b[r]\w+");
                s     = helper.ParseToRegEx(regex);
                Assert.AreEqual("attention opinion ", s);
                Assert.IsTrue(helper.MatchesCurrentPosition("railway "));

                regex = new Regex(@"\b[a]\w+");
                helper.Reset();
                helper.SkipToRegEx(regex, true);
                Assert.IsTrue(helper.MatchesCurrentPosition(" opinion"));

                helper.Reset("Abc1234def5678ghi");
                Assert.AreEqual(true, helper.SkipTo("123"));
                regex = new Regex(@"\d+");
                helper.SkipRegEx(regex);
                Assert.AreEqual('d', helper.Peek());
                regex = new Regex(@"[a-z]+");
                helper.SkipRegEx(regex);
                Assert.AreEqual('5', helper.Peek());
            }
            public void MatchesCurrentPositionTests()
            {
                ParsingHelper helper = new ParsingHelper(LongTest);

                Assert.IsTrue(helper.SkipTo("consecrated it"));
                Assert.AreEqual(true, helper.MatchesCurrentPosition("consecrated it"));
                Assert.AreEqual(true, helper.MatchesCurrentPosition("CONSECRATED IT", StringComparison.OrdinalIgnoreCase));
                Assert.AreEqual(false, helper.MatchesCurrentPosition(string.Empty));
                Assert.AreEqual(false, helper.MatchesCurrentPosition(string.Empty, StringComparison.OrdinalIgnoreCase));
                Assert.AreEqual(false, helper.MatchesCurrentPosition("consecrated_it"));
                Assert.AreEqual(false, helper.MatchesCurrentPosition("CONSECRATED_IT", StringComparison.OrdinalIgnoreCase));
                Assert.AreEqual(true, helper.MatchesCurrentPosition(new[] { 'c', 'o', 'n', 's', 'e', 'c', 'r', 'a', 't', 'e', 'd', ' ', 'i', 't' }));
                Assert.AreEqual(false, helper.MatchesCurrentPosition(new[] { 'o', 'n', 's', 'e', 'c', 'r', 'a', 't', 'e', 'd', ' ', 'i', 't' }));

                helper.Index = LongTest.Length - 1;
                Assert.AreEqual(false, helper.MatchesCurrentPosition("consecrated it"));
                Assert.AreEqual(false, helper.MatchesCurrentPosition("CONSECRATED IT", StringComparison.OrdinalIgnoreCase));
                helper.Index = LongTest.Length;
                Assert.AreEqual(false, helper.MatchesCurrentPosition("consecrated it"));
                Assert.AreEqual(false, helper.MatchesCurrentPosition("CONSECRATED IT", StringComparison.OrdinalIgnoreCase));
            }
            public void SkipTests()
            {
                ParsingHelper helper = new ParsingHelper(LongTest);

                // SkipTo
                Assert.IsTrue(helper.SkipTo("score"));
                Assert.AreEqual('s', helper.Peek());
                Assert.AreEqual('c', helper.Peek(1));
                helper.Reset();
                Assert.IsTrue(helper.SkipTo("score", includeToken: true));
                Assert.AreEqual(' ', helper.Peek());
                Assert.AreEqual('a', helper.Peek(1));
                helper.Reset();
                Assert.IsTrue(helper.SkipTo("SCORE", StringComparison.OrdinalIgnoreCase));
                Assert.AreEqual('s', helper.Peek());
                Assert.AreEqual('c', helper.Peek(1));
                helper.Reset();
                Assert.IsTrue(helper.SkipTo('v'));
                Assert.AreEqual('v', helper.Peek());
                Assert.AreEqual('e', helper.Peek(1));
                Assert.IsFalse(helper.SkipTo("XxXxXxX"));
                Assert.AreEqual(LongTest.Length, helper.Index);
                Assert.AreEqual(true, helper.EndOfText);
                Assert.AreEqual(0, helper.Remaining);

                // SkipWhiteSpace
                helper.Reset();
                Assert.IsTrue(helper.SkipTo(' '));
                helper.SkipWhiteSpace();
                Assert.AreEqual('s', helper.Peek());

                // SkipWhiteSpace with options
                ParsingHelper helper2 = new ParsingHelper("    \r\nxyz");

                helper2.SkipWhiteSpace(SkipWhiteSpaceOption.StopAtEol);
                Assert.IsTrue(helper2.MatchesCurrentPosition("\r\nxyz"));
                helper2.Reset();
                helper2.SkipWhiteSpace(SkipWhiteSpaceOption.StopAtNextLine);
                Assert.IsTrue(helper2.MatchesCurrentPosition("xyz"));
                helper2.Reset("    \rxyz ");
                helper2.SkipWhiteSpace(SkipWhiteSpaceOption.StopAtEol);
                Assert.IsTrue(helper2.MatchesCurrentPosition("\rxyz"));
                helper2.Reset("    \nxyz ");
                helper2.SkipWhiteSpace(SkipWhiteSpaceOption.StopAtNextLine);
                Assert.IsTrue(helper2.MatchesCurrentPosition("xyz"));
                helper2.Reset("    xyz");
                helper2.SkipWhiteSpace(SkipWhiteSpaceOption.StopAtEol);
                Assert.IsTrue(helper2.MatchesCurrentPosition("xyz"));
                helper2.Reset();
                helper2.SkipWhiteSpace(SkipWhiteSpaceOption.StopAtNextLine);
                Assert.IsTrue(helper2.MatchesCurrentPosition("xyz"));

                // SkipWhile
                helper.SkipWhile(c => "score".Contains(c));
                Assert.AreEqual(' ', helper.Peek());
                Assert.AreEqual('a', helper.Peek(1));

                // SkipToNextLine/SkipToEndOfLine
                helper.Reset();
                helper.SkipToEndOfLine();
                Assert.AreEqual('\r', helper.Peek());
                Assert.AreEqual('\n', helper.Peek(1));
                helper.SkipToNextLine();
                Assert.AreEqual('a', helper.Peek());
                Assert.AreEqual(' ', helper.Peek(1));
                helper.SkipToNextLine();
                helper.SkipToNextLine();
                Assert.AreEqual('N', helper.Peek());
                Assert.AreEqual('o', helper.Peek(1));

                // Skip
                helper.Skip('N', 'o', 'w', ' ', 'e');
                Assert.AreEqual('a', helper.Peek());
                Assert.AreEqual('r', helper.Peek(1));
            }