Beispiel #1
0
        public void DFA_triggers_actions_correctly()
        {
            // Given alphabet
            const int A = 0;
            const int B = 1;

            // Given actions
            const int Action0 = 1001;
            const int Action1 = 1002;
            const int Action2 = 1003;

            GivenRegularExpression(
                AstNode.Or(
                    AstNode.Cat(
                        CharSetNode.Create(A),          // 0
                        ActionNode.Create(Action0)),    // 1
                    AstNode.Cat(                        //
                        CharSetNode.Create(A),          // 2
                        CharSetNode.Create(B),          // 3
                        CharSetNode.Create(B),          // 4
                        ActionNode.Create(Action1)      // 5
                        ),
                    AstNode.Cat(
                        RepeatNode.ZeroOrMore(CharSetNode.Create(A)), // 6
                        RepeatNode.OneOrMore(CharSetNode.Create(B)),  // 7, 8
                        ActionNode.Create(Action2))                   // 9
                    ));

            CreateDfa();

            DfaShouldTriggerAction(Action0, A);
            DfaShouldTriggerAction(Action1, A, B, B);
            DfaShouldTriggerAction(Action2, A, B);
            DfaShouldTriggerAction(Action2, A, B, B, B);
        }
Beispiel #2
0
        public void dfa_provides_correct_serialization_information()
        {
            // Given alphabet
            const int A = 0;
            const int B = 1;

            // Given actions
            const int Action0 = 1001;
            const int Action1 = 1002;
            const int Action2 = 1003;

            GivenRegularExpression(
                AstNode.Or(
                    AstNode.Cat(
                        CharSetNode.Create(A),
                        ActionNode.Create(Action0)),
                    AstNode.Cat(
                        CharSetNode.Create(A),
                        CharSetNode.Create(B),
                        CharSetNode.Create(B),
                        ActionNode.Create(Action1)
                        ),
                    AstNode.Cat(
                        RepeatNode.ZeroOrMore(CharSetNode.Create(A)),
                        RepeatNode.OneOrMore(CharSetNode.Create(B)),
                        ActionNode.Create(Action2))
                    ));

            CreateDfa();
            var output = new StringBuilder();

            serialization.Print(output);
            // DUBUG: Console.WriteLine(output);
        }
Beispiel #3
0
        public Piece Literal(QStr str)
        {
            var nodes = str.Text.Select(ch => CharSetNode.Create(IntSet.Of(ch)));

            return(new Piece
            {
                Node = AstNode.Cat(nodes)
            });
        }
Beispiel #4
0
        public Piece Piece(IntSet charClass)
        {
            if (charClass.IsEmpty)
            {
                return(new Piece {
                    Node = OrNode.Or()
                });
            }

            return(new Piece {
                Node = CharSetNode.Create(charClass)
            });
        }
Beispiel #5
0
        public void DFA_matches_input_correctly()
        {
            // Given alphabet
            const int A = 0;
            const int B = 1;

            GivenRegularExpression(
                new CatNode(new List <AstNode> {
                RepeatNode.ZeroOrMore(
                    new OrNode(new List <AstNode> {
                    CharSetNode.Create(A),
                    CharSetNode.Create(B)
                })),
                CharSetNode.Create(A),
                CharSetNode.Create(B),
                CharSetNode.Create(B)
            })
                );

            CreateDfa();

            DfaShouldMatch(A, B, B);
            DfaShouldMatch(A, A, A, A, B, B);
            DfaShouldMatch(B, B, B, A, B, B);
            DfaShouldMatch(A, B, B, A, B, B);

            DfaShouldNotMatch();
            DfaShouldNotMatch(A, B);
            DfaShouldNotMatch(A, B, B, A);
            DfaShouldNotMatch(A, B, B, B);

            DfaShouldMatchBeginning(A, B, B);
            DfaShouldMatchBeginning(A, A, A, A, B, B);
            DfaShouldMatchBeginning(B, B, B, A, B, B);
            DfaShouldMatchBeginning(A, B, B, A, B, B);
            DfaShouldMatchBeginning(A, B, B, A);
            DfaShouldMatchBeginning(A, B, B, B);

            DfaShouldNotMatchBeginning();
            DfaShouldNotMatchBeginning(A, B, A, B);
            DfaShouldNotMatchBeginning(A, A, A, A);
            DfaShouldNotMatchBeginning(B, B, B, B);
        }
Beispiel #6
0
        public void input_equivalence_classes_are_used()
        {
            // Given alphabet
            int A = 0;
            int B = 0x10FFFF; // Unicode max

            GivenRegularExpression(
                new CatNode(new List <AstNode> {
                RepeatNode.ZeroOrMore(CharSetNode.CreateRange(A, B)),
                CharSetNode.Create(A),
                CharSetNode.Create(B),
                CharSetNode.Create(B)
            })
                );

            CreateDfa();

            DfaShouldMatch(A, B, B);
            DfaShouldMatch(A, A, A, A, B, B);
            DfaShouldMatch(B, B, B, A, B, B);
            DfaShouldMatch(A, B, B, A, B, B);

            DfaShouldNotMatch();
            DfaShouldNotMatch(A, B);
            DfaShouldNotMatch(A, B, B, A);
            DfaShouldNotMatch(A, B, B, B);

            DfaShouldMatchBeginning(A, B, B);
            DfaShouldMatchBeginning(A, A, A, A, B, B);
            DfaShouldMatchBeginning(B, B, B, A, B, B);
            DfaShouldMatchBeginning(A, B, B, A, B, B);
            DfaShouldMatchBeginning(A, B, B, A);
            DfaShouldMatchBeginning(A, B, B, B);

            DfaShouldNotMatchBeginning();
            DfaShouldNotMatchBeginning(A, B, A, B);
            DfaShouldNotMatchBeginning(A, A, A, A);
            DfaShouldNotMatchBeginning(B, B, B, B);
        }
Beispiel #7
0
        public RegularTree(AstNode root)
        {
            this.EoiCharSetNode = CharSetNode.Create(EoiChar);
            this.AugmentedRoot  = new CatNode(new List <AstNode> {
                root, EoiCharSetNode
            });

            var positionBuilder = new PositionBuilder();

            AugmentedRoot.Accept(positionBuilder, null);
            Positions = positionBuilder.Positions;

            EoiPosition = Positions.FindIndex(pos => pos.Characters.Contains(EoiChar));
            Debug.Assert(EoiPosition >= 0);

            var firstPosVisitor = new FirstPosGetter();

            this.FirstPos = AugmentedRoot.Accept(firstPosVisitor, 0);

            var followPosBuilder = new FollowPosBuilder(Positions);

            AugmentedRoot.Accept(followPosBuilder, 0);
        }
Beispiel #8
0
 public SreExpr LiteralMatch(QStr str)
 {
     return(new SreExpr {
         Node = new CatNode(str.Text.Select(ch => CharSetNode.Create(ch)))
     });
 }
Beispiel #9
0
 private CSetSreExpr CSet(IntSet cset)
 {
     return(new CSetSreExpr {
         Node = CharSetNode.Create(cset)
     });
 }