コード例 #1
0
        public void PotentialInfiniteRecurse()
        {
            //  Equivalent to:
            //  rule A = "a".."z";
            //  rule B = C "," C;
            //  rule C = A | B;
            //  Try to match "a" with C
            var ruleA  = new RangeRule("A", null, 'a', 'z');
            var proxyC = new RuleProxy();
            var ruleB  = new SequenceRule("B", null, new[] {
                new TaggedRule(proxyC),
                new TaggedRule(new LiteralRule(null, null, ",")),
                new TaggedRule(proxyC)
            });
            var ruleC = new DisjunctionRule("C", null, new[] {
                new TaggedRule(ruleA),
                new TaggedRule(ruleB)
            });

            proxyC.ReferencedRule = ruleC;

            var match = ruleC.Match(new ExplorerContext("a")).FirstOrDefault();

            Assert.IsNotNull(match, "Should be a success");
        }
コード例 #2
0
        public void DisjunctionWithoutTags()
        {
            var samples = new[]
            {
                Tuple.Create("Alice", true),
                Tuple.Create("Bob", true),
                Tuple.Create("Charles", true),
                Tuple.Create("Didier", false)
            };
            //  Alice | Bob | Charles
            var rule = new DisjunctionRule("Disjunction", null, new[]
            {
                new TaggedRule(new LiteralRule("Alice", null, "Alice")),
                new TaggedRule(new LiteralRule("Bob", null, "Bob")),
                new TaggedRule(new LiteralRule("Charles", null, "Charles"))
            });

            for (int i = 0; i != samples.Length; ++i)
            {
                var text      = samples[i].Item1;
                var isSuccess = samples[i].Item2;
                var match     = rule.Match(new ExplorerContext(text)).FirstOrDefault();

                if (!isSuccess)
                {
                    Assert.IsNull(match, $"Test case #{i} should have failed");
                }
                else
                {
                    Assert.IsNotNull(match, $"Success - {i}");
                    Assert.AreEqual(rule.RuleName, match.Rule.RuleName, $"Rule - {i}");
                    Assert.AreEqual(text.Length, match.Text.Length, $"MatchLength - {i}");
                    Assert.AreEqual(text, match.Text.ToString(), $"Content - {i}");
                }
            }
        }