Пример #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);
        }
Пример #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);
        }
Пример #3
0
        private static CharSetNode CreateWithContents(string contents, bool invert)
        {
            var node = new CharSetNode();

            node.InputCharacters.Value = contents;
            node.InputDoInvert.Checked = invert;
            return(node);
        }
Пример #4
0
        public Piece Literal(QStr str)
        {
            var nodes = str.Text.Select(ch => CharSetNode.Create(IntSet.Of(ch)));

            return(new Piece
            {
                Node = AstNode.Cat(nodes)
            });
        }
Пример #5
0
        public object Visit(CharSetNode node, object value)
        {
            Positions.Add(
                new RegularPositionInfo
            {
                Action     = new Nullable <int>(),
                Characters = node.Characters.Clone(),
            });

            return(this);
        }
Пример #6
0
        public Piece Piece(IntSet charClass)
        {
            if (charClass.IsEmpty)
            {
                return(new Piece {
                    Node = OrNode.Or()
                });
            }

            return(new Piece {
                Node = CharSetNode.Create(charClass)
            });
        }
Пример #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);
        }
Пример #8
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);
        }
Пример #9
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);
        }
Пример #10
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);
        }
Пример #11
0
 public bool Visit(CharSetNode node)
 {
     return(false);
 }
Пример #12
0
        public void CharSetNode_NoSpecials_ReturnsContents(string input, string expectedContents)
        {
            CharSetNode result = ParseCharSet.ParseOrThrow(input);

            Assert.AreEqual(expectedContents, result.InputCharacters.Value);
        }
Пример #13
0
 public int Visit(CharSetNode node)
 {
     return(1);
 }
Пример #14
0
 public SreExpr LiteralMatch(QStr str)
 {
     return(new SreExpr {
         Node = new CatNode(str.Text.Select(ch => CharSetNode.Create(ch)))
     });
 }
Пример #15
0
 private CSetSreExpr CSet(IntSet cset)
 {
     return(new CSetSreExpr {
         Node = CharSetNode.Create(cset)
     });
 }
Пример #16
0
 public CSetSreExpr ComplementOfUnion(CSetSreExpr[] inner)
 {
     return(new CSetSreExpr {
         Node = CharSetNode.Union(inner.Select(expr => expr.Node)).Complement()
     });
 }
Пример #17
0
 public object Visit(CharSetNode node, int posOffset)
 {
     return(this);
 }
Пример #18
0
 public IntSet Visit(CharSetNode node, int posOffset)
 {
     return(SparseIntSetType.Instance.Of(posOffset));
 }