Exemplo n.º 1
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'));
        }
Exemplo n.º 2
0
        public void ShouldNotBuildRootSituationAndThrowException()
        {
            ISituationSegmentFactory factory;


            factory = new SituationSegmentFactory();
            Assert.ThrowsException <ArgumentNullException>(() => factory.BuildRootSituation(null));
        }
Exemplo n.º 3
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)));
        }
Exemplo n.º 4
0
        public void ShouldNotBuildSituationSegmentUsingInvalidZeroOrMoreTimes()
        {
            IPredicate               predicate;
            SituationSegment         nextSegment;
            ISituationSegmentFactory factory;

            predicate = new ZeroOrMoreTimes();

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

            factory = new SituationSegmentFactory();
            Assert.ThrowsException <InvalidOperationException>(() => factory.BuildSituationSegment(predicate, nextSegment));
        }
Exemplo n.º 5
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));
        }
Exemplo n.º 6
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)));
        }
Exemplo n.º 7
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
        }
Exemplo n.º 8
0
        //private ICharReader reader;

        public Lexer(params IRule[] Rules)
        {
            if (Rules == null)
            {
                throw new ArgumentNullException("Rules");
            }


            IAutomatonTableFactory   automatonTableFactory;
            ISituationSegmentFactory situationSegmentFactory;
            ISituation rootSituation;

            situationSegmentFactory = new SituationSegmentFactory();
            rootSituation           = situationSegmentFactory.BuildRootSituation(Rules);

            automatonTableFactory = new AutomatonTableFactory();
            this.states           = automatonTableFactory.BuildStates(rootSituation);
        }
Exemplo n.º 9
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)));
        }
Exemplo n.º 10
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'));
        }