File: pattern.ih

package info (click to toggle)
bobcat 1.11.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,344 kB
  • ctags: 473
  • sloc: makefile: 12,078; cpp: 5,121; ansic: 63; sh: 14
file content (131 lines) | stat: -rw-r--r-- 3,102 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "pattern"

/*
    The PerlSetFSA is a Finite State Automaton converting the special
character set characters as used by Perl to their corresponding
set-notations. 

    The special perl-set characters may be used inside and outside of
character classes. When used in a class, it adds the set to the current
set. If the class starts with a caret, then the meaning of the set is
reversed:
            \s <-> \S
            \d <-> \D
(etc).

    The character sets s, d, and w are currently supported. \b and \B are 
already part of the current regex implementation. 

*/

#include <iostream>
#include <vector>
#include <algorithm>

namespace FBB
{

    class PerlSetFSA
    {
        class Validator;
        friend class Validator;

        enum State
        {
            Start,
            Bs,
            DQuote,
            Apostrophy,
            Set,
            DQuoteBs,
            ApostrophyBs,
            NotSet,
            SetBs,
            SetNested,
            InSet,
            NotSetBs,
            NotSetNested,
            InNotSet,

            _nStates
        };
     
        struct TransitionMatrix
        {
            State   d_state;
            int     d_input;
            State   d_next;
            void (PerlSetFSA::*d_action)();
        };

        static TransitionMatrix s_stateTransitions[];
        static TransitionMatrix *s_stateTransitions_end;

        class Validator
        {
            std::vector<bool> d_used;
            int     d_last;
            State   d_state;
            bool    d_valid;
            size_t d_element;

            public:
                Validator()
                :
                    d_used(_nStates),
                    d_last(0),
                    d_state(_nStates),
                    d_valid(true)
                {}
                void operator()(TransitionMatrix const &state);
                operator bool() const
                {
                    return d_valid;
                }
        };

            // s_transition holds as many elements as there are states,
            // first: points to first element of corresponding state
            //          in s_transition
            // second: points just beyond the last element of corresponding
            //          state in s_transition

        typedef std::pair<TransitionMatrix *, 
                          TransitionMatrix *> statePair;

        static std::vector<statePair> s_transition;

        std::string d_target;
        std::string::iterator d_next;

        public:
            PerlSetFSA();

            void convert(std::string &pattern);

        private:
            static void initialize(TransitionMatrix &stateDescription);

            void nop() 
            {};
            void copy();
            void copybs();
            void w_Set();
            void W_Set();
            void d_Set();
            void D_Set();
            void s_Set();
            void S_Set();
            void w_Nest();
            void d_Nest();
            void s_Nest();
    };
};

using namespace std;
using namespace FBB;