Exemple #1
0
        public void ParseMultipleOperators()
        {
            var t_Rule = new RepeatRule(new List <IParseRule>
            {
                new OperatorRule("+"),
                new OperatorRule("-")
            });

            var t_Expression = t_Rule.Match(CreateState(
                                                new Token(TokenType.Operator, "+"),
                                                new Token(TokenType.Operator, "-"),
                                                new Token(TokenType.Operator, "+"),
                                                new Token(TokenType.Operator, "-"),
                                                new Token(TokenType.Operator, "+"),
                                                new Token(TokenType.Operator, "-")));

            t_Expression
            .HasError()
            .Should()
            .BeFalse();

            var t_Expressions = t_Expression.Value.Expressions;

            t_Expressions.Should().HaveCount(6);
        }
        public void RepeatOneCharacter()
        {
            var oneCharRule = new LiteralRule("oneChar", null, "g");
            var rule        = new RepeatRule("Repeat", null, oneCharRule, null, null) as IRule;
            var match       = rule.Match(new ExplorerContext("ggggg")).FirstOrDefault();

            Assert.IsNotNull(match, "Success");
            Assert.AreEqual(rule.RuleName, match.Rule.RuleName, "Rule");
            Assert.AreEqual(5, match.Text.Length, "MatchLength");
            Assert.AreEqual(5, ToList(match.ComputeOutput()).Count(), "Contents");
        }
        public void RepeatWithSequenceAndInterleave()
        {
            var interleave = new RepeatRule("interleave", null, new LiteralRule(null, null, " "), 0, null);
            var seq        = new SequenceRule("seq", null, new[]
            {
                new TaggedRule(new LiteralRule(null, null, "|")),
                new TaggedRule("t", new LiteralRule(null, null, "a"))
            });
            var rule  = new RepeatRule("rep", null, seq, 1, null);
            var text  = "|a  |  a   | a";
            var match = rule.Match(new ExplorerContext(text, interleave)).FirstOrDefault();

            Assert.IsNotNull(match, "Success");
            Assert.AreEqual(rule.RuleName, match.Rule.RuleName, "Rule");
            Assert.AreEqual(text.Length, match.Text.Length, "MatchLength");
            Assert.AreEqual(3, ToList(match.ComputeOutput()).Count(), "Contents");
        }
        public void RepeatCharacterWithCardinality()
        {
            var oneCharRule = new LiteralRule("oneChar", null, "g");
            var testSet     = new[]
            {
                Tuple.Create <string, int?, int?, bool>("gg", 2, 2, true),
                Tuple.Create <string, int?, int?, bool>("gg", 1, 2, true),
                Tuple.Create <string, int?, int?, bool>("gg", null, 2, true),
                Tuple.Create <string, int?, int?, bool>("gg", 2, null, true),
                Tuple.Create <string, int?, int?, bool>("ggg", 2, 2, false),
                Tuple.Create <string, int?, int?, bool>("ggg", 2, 3, true),
                Tuple.Create <string, int?, int?, bool>("g", 2, 2, false),
                Tuple.Create <string, int?, int?, bool>("g", 2, 3, false)
            };

            for (int i = 0; i != testSet.Length; ++i)
            {
                var text      = testSet[i].Item1;
                var min       = testSet[i].Item2;
                var max       = testSet[i].Item3;
                var isSuccess = testSet[i].Item4;
                var rule      = new RepeatRule("Repeat", null, oneCharRule, min, max) as IRule;
                var match     = rule.Match(new ExplorerContext(text)).FirstOrDefault();

                if (!isSuccess)
                {
                    Assert.IsTrue(
                        match == null || match.Text.Length != text.Length,
                        $"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.Length, ToList(match.ComputeOutput()).Count(), $"Contents - {i}");
                }
            }
        }