Exemple #1
0
        public void ShouldReadWithTags()
        {
            ICharReader reader;
            ILexer      lexer;
            Rule        number, word;
            TokenMatch  tokenMatch;

            reader = new MockedCharReader("12345abc");

            number = new Rule("Number", Parse.Character('0').OrCharacter('1').OrCharacter('2').OrCharacter('3').OrCharacter('4').OrCharacter('5').OrCharacter('6').OrCharacter('7').OrCharacter('8').OrCharacter('9').OneOrMoreTimes());
            number.Tags.Add(new Tag("Number", "Value"));
            word = new Rule("Word", Parse.Character('a').OrCharacter('b').OrCharacter('c').OrCharacter('d').OneOrMoreTimes());
            word.Tags.Add(new Tag("Word", "Value"));

            lexer = new Lexer(word, number);

            tokenMatch = lexer.Read(reader);
            Assert.IsTrue(tokenMatch.Success);
            Assert.AreEqual("Number", tokenMatch.Token.Class);
            Assert.AreEqual("12345", tokenMatch.Token.Value);
            Assert.IsNotNull(tokenMatch.Tags);
            Assert.AreEqual(1, tokenMatch.Tags.Length);
            Assert.AreEqual("Number", tokenMatch.Tags[0].Name);

            tokenMatch = lexer.Read(reader);
            Assert.AreEqual("Word", tokenMatch.Token.Class);
            Assert.AreEqual("abc", tokenMatch.Token.Value);
            Assert.IsNotNull(tokenMatch.Tags);
            Assert.AreEqual(1, tokenMatch.Tags.Length);
            Assert.AreEqual("Word", tokenMatch.Tags[0].Name);
        }
        public void ShouldCreateSequencePredicateAddingZeroOrMoreTimes()
        {
            ISequence predicate;

            predicate = Parse.Character('a').ThenZeroOrMoreTimes('b').ThenCharacter('c');
            Assert.IsNotNull(predicate);
            Assert.AreEqual(3, predicate.Items.Count());
            Assert.IsInstanceOfType(predicate.Items.ElementAt(0), typeof(Character));
            Assert.IsInstanceOfType(predicate.Items.ElementAt(1), typeof(ZeroOrMoreTimes));
            Assert.IsInstanceOfType(predicate.Items.ElementAt(2), typeof(Character));

            predicate = Parse.Character('a').ThenCharacter('c').ThenZeroOrMoreTimes('b');
            Assert.IsNotNull(predicate);
            Assert.AreEqual(3, predicate.Items.Count());
            Assert.IsInstanceOfType(predicate.Items.ElementAt(0), typeof(Character));
            Assert.IsInstanceOfType(predicate.Items.ElementAt(1), typeof(Character));
            Assert.IsInstanceOfType(predicate.Items.ElementAt(2), typeof(ZeroOrMoreTimes));

            predicate = Parse.Character('a').ThenZeroOrMoreTimes(Parse.AnyCharacter()).ThenCharacter('c');
            Assert.IsNotNull(predicate);
            Assert.AreEqual(3, predicate.Items.Count());
            Assert.IsInstanceOfType(predicate.Items.ElementAt(0), typeof(Character));
            Assert.IsInstanceOfType(predicate.Items.ElementAt(1), typeof(ZeroOrMoreTimes));
            Assert.IsInstanceOfType(predicate.Items.ElementAt(2), typeof(Character));
        }
Exemple #3
0
        public void ShouldConvertToString()
        {
            IPredicate predicate;

            predicate = Parse.Character('a').OrCharacter('b').OrCharacter('c').OrCharacter('d');
            Assert.AreEqual("(a|b|c|d)", predicate.ToString());
        }
Exemple #4
0
        public void ShouldBuildStates3()
        {
            AutomatonTableFactory factory;

            State[] states;
            SituationSegmentFactory segmentFactory;
            ISituation rootSituation;


            segmentFactory = new SituationSegmentFactory();

            rootSituation = segmentFactory.BuildRootSituation(
                new Rule("A", Parse.Character('a').Then(Parse.Character('b').ThenCharacter('b').ThenCharacter('b').Perhaps()).ThenCharacter('c')),
                new Rule("B", Parse.Character('a').ThenCharacter('b').ThenCharacter('e'))
                );

            factory = new AutomatonTableFactory();
            states  = factory.BuildStates(rootSituation);

            Assert.AreEqual(7, states.Length);
            Assert.AreEqual("A", ParseStates(states, 'a', 'b', 'b', 'b', 'c'));
            Assert.AreEqual("A", ParseStates(states, 'a', 'c'));
            Assert.ThrowsException <InvalidOperationException>(() => ParseStates(states, 'a', 'b', 'b', 'b', 'e'));
            Assert.AreEqual("B", ParseStates(states, 'a', 'b', 'e'));
        }
Exemple #5
0
        public void ShouldConvertToString()
        {
            IPredicate predicate;

            predicate = Parse.Character('a').OneOrMoreTimes();
            Assert.AreEqual("a+", predicate.ToString());
        }
Exemple #6
0
        public void ShouldConvertToString()
        {
            IPredicate predicate;

            predicate = Parse.Character('a');
            Assert.AreEqual("a", predicate.ToString());
        }
Exemple #7
0
        public void ShouldAccept()
        {
            Character predicate;

            predicate = Parse.Character('a');
            Assert.IsTrue(predicate.Accept('a'));
        }
        public void ShouldCreateOrPredicateAddingPerhaps()
        {
            IOr predicate;

            predicate = Parse.Character('a').OrPerhaps('b').OrCharacter('c');
            Assert.IsNotNull(predicate);
            Assert.AreEqual(3, predicate.Items.Count());
            Assert.IsInstanceOfType(predicate.Items.ElementAt(0), typeof(Character));
            Assert.IsInstanceOfType(predicate.Items.ElementAt(1), typeof(Perhaps));
            Assert.IsInstanceOfType(predicate.Items.ElementAt(2), typeof(Character));

            predicate = Parse.Character('a').OrCharacter('c').OrPerhaps('b');
            Assert.IsNotNull(predicate);
            Assert.AreEqual(3, predicate.Items.Count());
            Assert.IsInstanceOfType(predicate.Items.ElementAt(0), typeof(Character));
            Assert.IsInstanceOfType(predicate.Items.ElementAt(1), typeof(Character));
            Assert.IsInstanceOfType(predicate.Items.ElementAt(2), typeof(Perhaps));

            predicate = Parse.Character('a').OrPerhaps(Parse.AnyCharacter()).OrCharacter('c');
            Assert.IsNotNull(predicate);
            Assert.AreEqual(3, predicate.Items.Count());
            Assert.IsInstanceOfType(predicate.Items.ElementAt(0), typeof(Character));
            Assert.IsInstanceOfType(predicate.Items.ElementAt(1), typeof(Perhaps));
            Assert.IsInstanceOfType(predicate.Items.ElementAt(2), typeof(Character));
        }
Exemple #9
0
        public void ShouldNotAccept()
        {
            Character predicate;

            predicate = Parse.Character('a');
            Assert.IsFalse(predicate.Accept('b'));
        }
Exemple #10
0
        public void ShouldCreateCharacterPredicate()
        {
            ICharacter predicate;

            predicate = Parse.Character('a');
            Assert.IsNotNull(predicate);
            Assert.AreEqual('a', predicate.Value);
        }
Exemple #11
0
        public void ShouldNotTryRead()
        {
            ILexer     lexer;
            Rule       number, word;
            TokenMatch tokenMatch;

            number = new Rule("Number", Parse.Character('0').OrCharacter('1').OrCharacter('2').OrCharacter('3').OrCharacter('4').OrCharacter('5').OrCharacter('6').OrCharacter('7').OrCharacter('8').OrCharacter('9').OneOrMoreTimes());
            word   = new Rule("Word", Parse.Character('a').OrCharacter('b').OrCharacter('c').OrCharacter('d').OneOrMoreTimes());

            lexer = new Lexer(word, number);
            Assert.ThrowsException <ArgumentNullException>(() => tokenMatch = lexer.TryRead(null));
        }
Exemple #12
0
        public void ShouldBuildRootSituation()
        {
            IRule      rule;
            ISituation situation;
            ISituationSegmentFactory factory;

            rule = new Rule("A", Parse.Character('a'));

            factory   = new SituationSegmentFactory();
            situation = factory.BuildRootSituation(rule);
            Assert.IsNotNull(situation);
            Assert.IsTrue(ParseSegment(situation.Transitions, typeof(Character)));
        }
Exemple #13
0
        public void ShouldNotBuildSituationSegmentAndThrowException()
        {
            IPredicate               predicate;
            SituationSegment         nextSegment;
            ISituationSegmentFactory factory;

            predicate = Parse.Character('a');

            nextSegment = new SituationSegment();
            nextSegment.InputTransitions.Add(new ReductionTransition());

            factory = new SituationSegmentFactory();
            Assert.ThrowsException <ArgumentNullException>(() => factory.BuildSituationSegment(predicate, null));
            Assert.ThrowsException <ArgumentNullException>(() => factory.BuildSituationSegment(null, nextSegment));
        }
        public void ShouldCreateOrPredicateAddingAnyCharacter()
        {
            IOr predicate;

            predicate = Parse.Character('a').OrAnyCharacter();
            Assert.IsNotNull(predicate);
            Assert.AreEqual(2, predicate.Items.Count());
            Assert.IsInstanceOfType(predicate.Items.ElementAt(0), typeof(Character));
            Assert.IsInstanceOfType(predicate.Items.ElementAt(1), typeof(AnyCharacter));

            predicate = Parse.Characters("ab").OrAnyCharacter();
            Assert.IsNotNull(predicate);
            Assert.AreEqual(2, predicate.Items.Count());
            Assert.IsInstanceOfType(predicate.Items.ElementAt(0), typeof(Sequence));
            Assert.IsInstanceOfType(predicate.Items.ElementAt(1), typeof(AnyCharacter));
        }
        public void ShouldCreateOrPredicateAddingAbstractPredicate()
        {
            IOr predicate;

            predicate = Parse.Character('a').Or(Parse.AnyCharacter());
            Assert.IsNotNull(predicate);
            Assert.AreEqual(2, predicate.Items.Count());
            Assert.IsInstanceOfType(predicate.Items.ElementAt(0), typeof(Character));
            Assert.IsInstanceOfType(predicate.Items.ElementAt(1), typeof(AnyCharacter));

            predicate = Parse.Character('a').Or('b'.OneOrMoreTimes());
            Assert.IsNotNull(predicate);
            Assert.AreEqual(2, predicate.Items.Count());
            Assert.IsInstanceOfType(predicate.Items.ElementAt(0), typeof(Character));
            Assert.IsInstanceOfType(predicate.Items.ElementAt(1), typeof(OneOrMoreTimes));
        }
Exemple #16
0
        public void ShouldBuildSituationSegmentUsingCharacterPredicate()
        {
            IPredicate               predicate;
            SituationSegment         nextSegment;
            ISituationSegmentFactory factory;
            ISituationSegment        segment;

            predicate = Parse.Character('a');

            nextSegment = new SituationSegment();
            nextSegment.InputTransitions.Add(new ReductionTransition());

            factory = new SituationSegmentFactory();
            segment = factory.BuildSituationSegment(predicate, nextSegment);

            Assert.IsTrue(ParseSegment(segment, typeof(Character)));
        }
        public void ShouldCreateSequencePredicateAddingExceptCharacterRange()
        {
            ISequence predicate;

            predicate = Parse.Character('a').ThenExceptCharacterRange('b', 'z');
            Assert.IsNotNull(predicate);
            Assert.AreEqual(2, predicate.Items.Count());
            Assert.IsInstanceOfType(predicate.Items.ElementAt(0), typeof(Character));
            Assert.IsInstanceOfType(predicate.Items.ElementAt(1), typeof(ExceptCharacterRange));

            predicate = Parse.Characters("ab").ThenExceptCharacterRange('c', 'z');
            Assert.IsNotNull(predicate);
            Assert.AreEqual(3, predicate.Items.Count());
            Assert.IsInstanceOfType(predicate.Items.ElementAt(0), typeof(Character));
            Assert.IsInstanceOfType(predicate.Items.ElementAt(1), typeof(Character));
            Assert.IsInstanceOfType(predicate.Items.ElementAt(2), typeof(ExceptCharacterRange));
        }
Exemple #18
0
        public void ShouldBuildSituationSegmentUsingComplexPredicate()
        {
            IPredicate               predicate;
            SituationSegment         nextSegment;
            ISituationSegmentFactory factory;
            ISituationSegment        segment;

            #region or inside a sequence
            predicate = Parse.AnyCharacter().Then(Parse.Character('a').OrCharacter('b')).ThenAnyCharacter();

            nextSegment = new SituationSegment();
            nextSegment.InputTransitions.Add(new ReductionTransition());

            factory = new SituationSegmentFactory();
            segment = factory.BuildSituationSegment(predicate, nextSegment);

            Assert.IsTrue(ParseSegment(segment, typeof(AnyCharacter), typeof(Character), typeof(AnyCharacter)));
            #endregion

            #region optional sequence in sequence
            predicate = Parse.AnyCharacter().Then(Parse.Character('a').ThenCharacter('b').Perhaps()).ThenAnyCharacter();

            nextSegment = new SituationSegment();
            nextSegment.InputTransitions.Add(new ReductionTransition());

            factory = new SituationSegmentFactory();
            segment = factory.BuildSituationSegment(predicate, nextSegment);

            Assert.IsTrue(ParseSegment(segment, typeof(AnyCharacter), typeof(Character), typeof(Character), typeof(AnyCharacter)));
            Assert.IsTrue(ParseSegment(segment, typeof(AnyCharacter), typeof(AnyCharacter)));
            #endregion

            #region one or more inside a sequence
            predicate = Parse.AnyCharacter().Then(Parse.Character('b').OneOrMoreTimes()).ThenAnyCharacter();

            nextSegment = new SituationSegment();
            nextSegment.InputTransitions.Add(new ReductionTransition());

            factory = new SituationSegmentFactory();
            segment = factory.BuildSituationSegment(predicate, nextSegment);

            Assert.IsTrue(ParseSegment(segment, typeof(AnyCharacter), typeof(Character), typeof(AnyCharacter)));
            Assert.IsTrue(ParseSegment(segment, typeof(AnyCharacter), typeof(Character), typeof(Character), typeof(Character), typeof(AnyCharacter)));
            #endregion
        }
Exemple #19
0
        public void ShouldBuildSituationSegmentUsingOneOrMoreTimes()
        {
            IPredicate               predicate;
            SituationSegment         nextSegment;
            ISituationSegmentFactory factory;
            ISituationSegment        segment;

            predicate = Parse.Character('a').OneOrMoreTimes();

            nextSegment = new SituationSegment();
            nextSegment.InputTransitions.Add(new ReductionTransition());

            factory = new SituationSegmentFactory();
            segment = factory.BuildSituationSegment(predicate, nextSegment);

            Assert.ThrowsException <Exception>(() => ParseSegment(segment));
            Assert.IsTrue(ParseSegment(segment, typeof(Character), typeof(Character), typeof(Character)));
        }
Exemple #20
0
        public void ShouldCreateOneOrMoreTimesPredicate()
        {
            IOneOrMoreTimes predicate;

            predicate = Parse.OneOrMoreTimes('a');
            Assert.IsNotNull(predicate);
            Assert.IsNotNull(predicate.Item);
            Assert.IsInstanceOfType(predicate.Item, typeof(Character));

            predicate = Parse.OneOrMoreTimes(Parse.AnyCharacter());
            Assert.IsNotNull(predicate);
            Assert.IsNotNull(predicate.Item);
            Assert.IsInstanceOfType(predicate.Item, typeof(AnyCharacter));

            predicate = Parse.OneOrMoreTimes(Parse.Character('a').ThenAnyCharacter());
            Assert.IsNotNull(predicate);
            Assert.IsNotNull(predicate.Item);
            Assert.IsInstanceOfType(predicate.Item, typeof(Sequence));
        }
Exemple #21
0
        public void ShouldBuildStates()
        {
            AutomatonTableFactory factory;

            State[] states;
            SituationSegmentFactory segmentFactory;
            ISituation rootSituation;


            segmentFactory = new SituationSegmentFactory();

            rootSituation = segmentFactory.BuildRootSituation(
                new Rule("A", Parse.Character('a').ThenCharacter('b').ThenCharacter('c')),
                new Rule("B", Parse.Character('a').ThenCharacter('b').ThenAnyCharacter())
                );

            factory = new AutomatonTableFactory();
            states  = factory.BuildStates(rootSituation);

            Assert.AreEqual(5, states.Length);
            Assert.AreEqual("A", ParseStates(states, 'a', 'b', 'c'));
            Assert.AreEqual("B", ParseStates(states, 'a', 'b', 'd'));
        }
        public void ShouldCreatePerhapsPredicate()
        {
            IPerhaps predicate;

            predicate = 'a'.Perhaps();
            Assert.IsNotNull(predicate);
            Assert.IsNotNull(predicate.Item);
            Assert.IsInstanceOfType(predicate.Item, typeof(Character));

            predicate = Parse.Character('a').Perhaps();
            Assert.IsNotNull(predicate);
            Assert.IsNotNull(predicate.Item);
            Assert.IsInstanceOfType(predicate.Item, typeof(Character));

            predicate = Parse.AnyCharacter().Perhaps();
            Assert.IsNotNull(predicate);
            Assert.IsNotNull(predicate.Item);
            Assert.IsInstanceOfType(predicate.Item, typeof(AnyCharacter));

            predicate = Parse.Character('a').ThenCharacter('b').Perhaps();
            Assert.IsNotNull(predicate);
            Assert.IsNotNull(predicate.Item);
            Assert.IsInstanceOfType(predicate.Item, typeof(Sequence));
        }
Exemple #23
0
        public void ShouldSerializeAndDeserialize()
        {
            XmlSerializer serializer;
            Rule          ruleA, ruleB;
            MemoryStream  stream;

            ruleA           = new Rule();
            ruleA.Name      = "RuleA";
            ruleA.Predicate = Parse.Character('a').OneOrMoreTimes();
            ruleA.Tags.Add(new Tag("A", "B"));

            using (stream = new MemoryStream())
            {
                serializer = new XmlSerializer(typeof(Rule));
                serializer.Serialize(stream, ruleA);
                stream.Seek(0, SeekOrigin.Begin);
                ruleB = serializer.Deserialize(stream) as Rule;
            }

            Assert.IsNotNull(ruleB);
            Assert.AreEqual(ruleA.Name, ruleB.Name);
            Assert.IsNotNull(ruleB.Predicate);
            Assert.AreEqual(1, ruleB.Tags.Count);
        }
        public void ShouldCreateZeroOrMoreTimesPredicate()
        {
            IZeroOrMoreTimes predicate;

            predicate = 'a'.ZeroOrMoreTimes();
            Assert.IsNotNull(predicate);
            Assert.IsNotNull(predicate.Item);
            Assert.IsInstanceOfType(predicate.Item, typeof(Character));

            predicate = Parse.Character('a').ZeroOrMoreTimes();
            Assert.IsNotNull(predicate);
            Assert.IsNotNull(predicate.Item);
            Assert.IsInstanceOfType(predicate.Item, typeof(Character));

            predicate = Parse.AnyCharacter().ZeroOrMoreTimes();
            Assert.IsNotNull(predicate);
            Assert.IsNotNull(predicate.Item);
            Assert.IsInstanceOfType(predicate.Item, typeof(AnyCharacter));

            predicate = Parse.Character('a').ThenCharacter('b').ZeroOrMoreTimes();
            Assert.IsNotNull(predicate);
            Assert.IsNotNull(predicate.Item);
            Assert.IsInstanceOfType(predicate.Item, typeof(Sequence));
        }