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')); }
public void ShouldNotBuildRootSituationAndThrowException() { ISituationSegmentFactory factory; factory = new SituationSegmentFactory(); Assert.ThrowsException <ArgumentNullException>(() => factory.BuildRootSituation(null)); }
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))); }
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)); }
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 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 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 }
//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); }
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))); }
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')); }