summaryrefslogtreecommitdiffstats
blob: 23cda87315451c351afbf8198a7a70c9f8379136 (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// debug.hpp
// Copyright (c) 2007-2009 Ben Hanson (http://www.benhanson.net/)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file licence_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_LEXER_DEBUG_HPP
#define BOOST_LEXER_DEBUG_HPP

#include <map>
#include <ostream>
#include "rules.hpp"
#include "size_t.hpp"
#include "state_machine.hpp"
#include "string_token.hpp"
#include <vector>

namespace boost
{
namespace lexer
{
template<typename CharT>
class basic_debug
{
public:
    typedef std::basic_ostream<CharT> ostream;
    typedef std::basic_string<CharT> string;
    typedef std::vector<std::size_t> size_t_vector;

    static void escape_control_chars (const string &in_, string &out_)
    {
        const CharT *ptr_ = in_.c_str ();
        std::size_t size_ = in_.size ();

#if defined _MSC_VER && _MSC_VER <= 1200
        out_.erase ();
#else
        out_.clear ();
#endif

        while (size_)
        {
            basic_string_token<CharT>::escape_char (*ptr_, out_);
            ++ptr_;
            --size_;
        }
    }

    static void dump (const basic_state_machine<CharT> &state_machine_,
        basic_rules<CharT> &rules_, ostream &stream_)
    {
        typename basic_state_machine<CharT>::iterator iter_ =
            state_machine_.begin ();
        typename basic_state_machine<CharT>::iterator end_ =
            state_machine_.end ();

        for (std::size_t dfa_ = 0, dfas_ = state_machine_.size ();
            dfa_ < dfas_; ++dfa_)
        {
            lexer_state (stream_);
            stream_ << rules_.state (dfa_) << std::endl << std::endl;

            dump_ex (iter_, stream_);
        }
    }

    static void dump (const basic_state_machine<CharT> &state_machine_,
        ostream &stream_)
    {
        typename basic_state_machine<CharT>::iterator iter_ =
            state_machine_.begin ();
        typename basic_state_machine<CharT>::iterator end_ =
            state_machine_.end ();

        for (std::size_t dfa_ = 0, dfas_ = state_machine_.size ();
            dfa_ < dfas_; ++dfa_)
        {
            lexer_state (stream_);
            stream_ << dfa_ << std::endl << std::endl;

            dump_ex (iter_, stream_);
        }
    }

protected:
    typedef std::basic_stringstream<CharT> stringstream;

    static void dump_ex (typename basic_state_machine<CharT>::iterator &iter_,
        ostream &stream_)
    {
        const std::size_t states_ = iter_->states;

        for (std::size_t i_ = 0; i_ < states_; ++i_)
        {
            state (stream_);
            stream_ << i_ << std::endl;

            if (iter_->end_state)
            {
                end_state (stream_);
                stream_ << iter_->id;
                unique_id (stream_);
                stream_ << iter_->unique_id;
                dfa (stream_);
                stream_ << iter_->goto_dfa;
                stream_ << std::endl;
            }

            if (iter_->bol_index != npos)
            {
                bol (stream_);
                stream_ << iter_->bol_index << std::endl;
            }

            if (iter_->eol_index != npos)
            {
                eol (stream_);
                stream_ << iter_->eol_index << std::endl;
            }

            const std::size_t transitions_ = iter_->transitions;

            if (transitions_ == 0)
            {
                ++iter_;
            }

            for (std::size_t t_ = 0; t_ < transitions_; ++t_)
            {
                std::size_t goto_state_ = iter_->goto_state;

                if (iter_->token.any ())
                {
                    any (stream_);
                }
                else
                {
                    open_bracket (stream_);

                    if (iter_->token._negated)
                    {
                        negated (stream_);
                    }

                    string charset_;
                    CharT c_ = 0;

                    escape_control_chars (iter_->token._charset,
                        charset_);
                    c_ = *charset_.c_str ();

                    if (!iter_->token._negated &&
                        (c_ == '^' || c_ == ']'))
                    {
                        stream_ << '\\';
                    }

                    stream_ << charset_;
                    close_bracket (stream_);
                }

                stream_ << goto_state_ << std::endl;
                ++iter_;
            }

            stream_ << std::endl;
        }
    }

    static void lexer_state (std::ostream &stream_)
    {
        stream_ << "Lexer state: ";
    }

    static void lexer_state (std::wostream &stream_)
    {
        stream_ << L"Lexer state: ";
    }

    static void state (std::ostream &stream_)
    {
        stream_ << "State: ";
    }

    static void state (std::wostream &stream_)
    {
        stream_ << L"State: ";
    }

    static void bol (std::ostream &stream_)
    {
        stream_ << "  BOL -> ";
    }

    static void bol (std::wostream &stream_)
    {
        stream_ << L"  BOL -> ";
    }

    static void eol (std::ostream &stream_)
    {
        stream_ << "  EOL -> ";
    }

    static void eol (std::wostream &stream_)
    {
        stream_ << L"  EOL -> ";
    }

    static void end_state (std::ostream &stream_)
    {
        stream_ << "  END STATE, Id = ";
    }

    static void end_state (std::wostream &stream_)
    {
        stream_ << L"  END STATE, Id = ";
    }

    static void unique_id (std::ostream &stream_)
    {
        stream_ << ", Unique Id = ";
    }

    static void unique_id (std::wostream &stream_)
    {
        stream_ << L", Unique Id = ";
    }

    static void any (std::ostream &stream_)
    {
        stream_ << "  . -> ";
    }

    static void any (std::wostream &stream_)
    {
        stream_ << L"  . -> ";
    }

    static void open_bracket (std::ostream &stream_)
    {
        stream_ << "  [";
    }

    static void open_bracket (std::wostream &stream_)
    {
        stream_ << L"  [";
    }

    static void negated (std::ostream &stream_)
    {
        stream_ << "^";
    }

    static void negated (std::wostream &stream_)
    {
        stream_ << L"^";
    }

    static void close_bracket (std::ostream &stream_)
    {
        stream_ << "] -> ";
    }

    static void close_bracket (std::wostream &stream_)
    {
        stream_ << L"] -> ";
    }

    static void dfa (std::ostream &stream_)
    {
        stream_ << ", dfa = ";
    }

    static void dfa (std::wostream &stream_)
    {
        stream_ << L", dfa = ";
    }
};

typedef basic_debug<char> debug;
typedef basic_debug<wchar_t> wdebug;
}
}

#endif