zhangmeng
2021-07-02 056f71f24cefaf88f2a93714c6678c03ed5f1e0e
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*=============================================================================
    Copyright (c) 2001-2008 Hartmut Kaiser
    Copyright (c) 2001-2003 Daniel Nuffer
    http://spirit.sourceforge.net/
 
    Use, modification and distribution is subject to the Boost Software
    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
    http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
 
#ifndef BOOST_SPIRIT_CLASSIC_TREE_IMPL_TREE_TO_XML_IPP
#define BOOST_SPIRIT_CLASSIC_TREE_IMPL_TREE_TO_XML_IPP
 
#include <cstdio>
#include <cstdarg>
#include <locale>
#include <string>
#include <cstring>
 
#include <map>
#include <iostream>
#include <boost/config.hpp>
#include <boost/assert.hpp>
#include <boost/scoped_array.hpp>
 
#ifdef BOOST_NO_STRINGSTREAM
#include <strstream>
#define BOOST_SPIRIT_OSSTREAM std::ostrstream
inline
std::string BOOST_SPIRIT_GETSTRING(std::ostrstream& ss)
{
    ss << std::ends;
    std::string rval = ss.str();
    ss.freeze(false);
    return rval;
}
#else
#include <sstream>
#define BOOST_SPIRIT_GETSTRING(ss) ss.str()
#define BOOST_SPIRIT_OSSTREAM std::basic_ostringstream<CharT>
#endif
 
namespace boost { namespace spirit {
 
BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
 
namespace impl {
 
    ///////////////////////////////////////////////////////////////////////////
    template <typename CharT>
    struct string_lit;
 
    template <>
    struct string_lit<char>
    {
        static char get(char c) { return c; }
        static std::string get(char const* str = "") { return str; }
    };
 
    template <>
    struct string_lit<wchar_t>
    {
        static wchar_t get(char c)
        {
            typedef std::ctype<wchar_t> ctype_t;
            return std::use_facet<ctype_t>(std::locale()).widen(c);
        }
        static std::basic_string<wchar_t> get(char const* source = "")
        {
            using namespace std;        // some systems have size_t in ns std
            size_t len = strlen(source);
            boost::scoped_array<wchar_t> result (new wchar_t[len+1]);
            result.get()[len] = '\0';
 
            // working with wide character streams is supported only if the
            // platform provides the std::ctype<wchar_t> facet
            BOOST_ASSERT(std::has_facet<std::ctype<wchar_t> >(std::locale()));
 
            std::use_facet<std::ctype<wchar_t> >(std::locale())
                .widen(source, source + len, result.get());
            return result.get();
        }
    };
}
 
// xml formatting helper classes
namespace xml {
 
    template <typename CharT>
    inline void
    encode (std::basic_string<CharT> &str, char s, char const *r, int len)
    {
        typedef typename std::basic_string<CharT>::size_type size_type;
 
        size_type pos = 0;
        while ((pos = str.find_first_of (impl::string_lit<CharT>::get(s), pos)) !=
                size_type(std::basic_string<CharT>::npos))
        {
            str.replace (pos, 1, impl::string_lit<CharT>::get(r));
            pos += len;
        }
    }
 
    template <typename CharT>
    inline std::basic_string<CharT>
    encode (std::basic_string<CharT> str)
    {
        encode(str, '&', "&amp;", 3);
        encode(str, '<', "&lt;", 2);
        encode(str, '>', "&gt;", 2);
        encode(str, '\r', "\\r", 1);
        encode(str, '\n', "\\n", 1);
        return str;
    }
 
    template <typename CharT>
    inline std::basic_string<CharT>
    encode (CharT const *text)
    {
        return encode (std::basic_string<CharT>(text));
    }
 
    // format a xml attribute
    template <typename CharT>
    struct attribute
    {
        attribute()
        {
        }
 
        attribute (std::basic_string<CharT> const& key_,
                   std::basic_string<CharT> const& value_)
          : key (key_), value(value_)
        {
        }
 
        bool has_value()
        {
            return value.size() > 0;
        }
 
        std::basic_string<CharT> key;
        std::basic_string<CharT> value;
    };
 
    template <typename CharT>
    inline std::basic_ostream<CharT>&
    operator<< (std::basic_ostream<CharT> &ostrm, attribute<CharT> const &attr)
    {
        if (0 == attr.key.size())
            return ostrm;
        ostrm << impl::string_lit<CharT>::get(" ") << encode(attr.key)
              << impl::string_lit<CharT>::get("=\"") << encode(attr.value)
              << impl::string_lit<CharT>::get("\"");
        return ostrm;
    }
 
    // output a xml element (base class, not used directly)
    template <typename CharT>
    class element
    {
    protected:
        element(std::basic_ostream<CharT> &ostrm_, bool incr_indent_ = true)
        :   ostrm(ostrm_), incr_indent(incr_indent_)
        {
            if (incr_indent) ++get_indent();
        }
        ~element()
        {
            if (incr_indent) --get_indent();
        }
 
    public:
        void output_space ()
        {
            for (int i = 0; i < get_indent(); i++)
                ostrm << impl::string_lit<CharT>::get("    ");
        }
 
    protected:
        int &get_indent()
        {
            static int indent;
 
            return indent;
        }
 
        std::basic_ostream<CharT> &ostrm;
        bool incr_indent;
    };
 
    // a xml node
    template <typename CharT>
    class node : public element<CharT>
    {
    public:
        node (std::basic_ostream<CharT> &ostrm_,
              std::basic_string<CharT> const& tag_, attribute<CharT> &attr)
        :   element<CharT>(ostrm_), tag(tag_)
        {
            this->output_space();
            this->ostrm
                  << impl::string_lit<CharT>::get("<") << tag_ << attr
                  << impl::string_lit<CharT>::get(">\n");
        }
        node (std::basic_ostream<CharT> &ostrm_,
              std::basic_string<CharT> const& tag_)
        :   element<CharT>(ostrm_), tag(tag_)
        {
            this->output_space();
            this->ostrm
                  << impl::string_lit<CharT>::get("<") << tag_
                  << impl::string_lit<CharT>::get(">\n");
        }
        ~node()
        {
            this->output_space();
            this->ostrm
                  << impl::string_lit<CharT>::get("</") << tag
                  << impl::string_lit<CharT>::get(">\n");
        }
 
    private:
        std::basic_string<CharT> tag;
    };
 
    template <typename CharT>
    class text : public element<CharT>
    {
    public:
        text (std::basic_ostream<CharT> &ostrm_,
              std::basic_string<CharT> const& tag,
              std::basic_string<CharT> const& textlit)
        :   element<CharT>(ostrm_)
        {
            this->output_space();
            this->ostrm
                  << impl::string_lit<CharT>::get("<") << tag
                  << impl::string_lit<CharT>::get(">") << encode(textlit)
                  << impl::string_lit<CharT>::get("</") << tag
                  << impl::string_lit<CharT>::get(">\n");
        }
 
        text (std::basic_ostream<CharT> &ostrm_,
              std::basic_string<CharT> const& tag,
              std::basic_string<CharT> const& textlit,
              attribute<CharT> &attr)
        :   element<CharT>(ostrm_)
        {
            this->output_space();
            this->ostrm
                  << impl::string_lit<CharT>::get("<") << tag << attr
                  << impl::string_lit<CharT>::get(">") << encode(textlit)
                  << impl::string_lit<CharT>::get("</") << tag
                  << impl::string_lit<CharT>::get(">\n");
        }
 
        text (std::basic_ostream<CharT> &ostrm_,
              std::basic_string<CharT> const& tag,
              std::basic_string<CharT> const& textlit,
              attribute<CharT> &attr1, attribute<CharT> &attr2)
        :   element<CharT>(ostrm_)
        {
            this->output_space();
            this->ostrm
                  << impl::string_lit<CharT>::get("<") << tag << attr1 << attr2
                  << impl::string_lit<CharT>::get(">") << encode(textlit)
                  << impl::string_lit<CharT>::get("</") << tag
                  << impl::string_lit<CharT>::get(">\n");
        }
    };
 
    // a xml comment
    template <typename CharT>
    class comment : public element<CharT>
    {
    public:
        comment (std::basic_ostream<CharT> &ostrm_,
                 std::basic_string<CharT> const& commentlit)
        :   element<CharT>(ostrm_, false)
        {
            if ('\0' != commentlit[0])
            {
                this->output_space();
                this->ostrm << impl::string_lit<CharT>::get("<!-- ")
                      << encode(commentlit)
                      << impl::string_lit<CharT>::get(" -->\n");
            }
        }
    };
 
    // a xml document
    template <typename CharT>
    class document : public element<CharT>
    {
    public:
        document (std::basic_ostream<CharT> &ostrm_)
        :   element<CharT>(ostrm_)
        {
            this->get_indent() = -1;
            this->ostrm << impl::string_lit<CharT>::get(
                "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
        }
 
        document (std::basic_ostream<CharT> &ostrm_,
                  std::basic_string<CharT> const& mainnode,
                  std::basic_string<CharT> const& dtd)
        :   element<CharT>(ostrm_)
        {
            this->get_indent() = -1;
            this->ostrm << impl::string_lit<CharT>::get(
                "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
 
            this->output_space();
            this->ostrm << impl::string_lit<CharT>::get("<!DOCTYPE ") << mainnode
                  << impl::string_lit<CharT>::get(" SYSTEM \"") << dtd
                  << impl::string_lit<CharT>::get("\">\n");
        }
        ~document()
        {
            BOOST_SPIRIT_ASSERT(-1 == this->get_indent());
        }
    };
 
} // end of namespace xml
 
namespace impl {
 
    ///////////////////////////////////////////////////////////////////////////
    // look up the rule name from the given parser_id
    template <typename AssocContainerT>
    inline typename AssocContainerT::value_type::second_type
    get_rulename (AssocContainerT const &id_to_name_map,
        BOOST_SPIRIT_CLASSIC_NS::parser_id const &id)
    {
        typename AssocContainerT::const_iterator it = id_to_name_map.find(id);
        if (it != id_to_name_map.end())
            return (*it).second;
        typedef typename AssocContainerT::value_type::second_type second_t;
        return second_t();
    }
 
    // dump a parse tree as xml
    template <
        typename CharT, typename IteratorT, typename GetIdT, typename GetValueT
    >
    inline void
    token_to_xml (std::basic_ostream<CharT> &ostrm, IteratorT const &it,
        bool is_root, GetIdT const &get_token_id, GetValueT const &get_token_value)
    {
        BOOST_SPIRIT_OSSTREAM stream;
 
        stream << get_token_id(*it) << std::ends;
        xml::attribute<CharT> token_id (
                impl::string_lit<CharT>::get("id"),
                BOOST_SPIRIT_GETSTRING(stream).c_str());
        xml::attribute<CharT> is_root_attr (
                impl::string_lit<CharT>::get("is_root"),
                impl::string_lit<CharT>::get(is_root ? "1" : ""));
        xml::attribute<CharT> nil;
        xml::text<CharT>(ostrm,
                impl::string_lit<CharT>::get("token"),
                get_token_value(*it).c_str(),
                token_id,
                is_root_attr.has_value() ? is_root_attr : nil);
    }
 
    template <
        typename CharT, typename TreeNodeT, typename AssocContainerT,
        typename GetIdT, typename GetValueT
    >
    inline void
    tree_node_to_xml (std::basic_ostream<CharT> &ostrm, TreeNodeT const &node,
        AssocContainerT const& id_to_name_map, GetIdT const &get_token_id,
        GetValueT const &get_token_value)
    {
        typedef typename TreeNodeT::const_iterator node_iter_t;
        typedef
            typename TreeNodeT::value_type::parse_node_t::const_iterator_t
            value_iter_t;
 
        xml::attribute<CharT> nil;
        node_iter_t end = node.end();
        for (node_iter_t it = node.begin(); it != end; ++it)
        {
            // output a node
            xml::attribute<CharT> id (
                impl::string_lit<CharT>::get("rule"),
                get_rulename(id_to_name_map, (*it).value.id()).c_str());
            xml::node<CharT> currnode (ostrm,
                impl::string_lit<CharT>::get("parsenode"),
                (*it).value.id() != 0 && id.has_value() ? id : nil);
 
            // first dump the value
            std::size_t cnt = std::distance((*it).value.begin(), (*it).value.end());
 
            if (1 == cnt)
            {
                token_to_xml (ostrm, (*it).value.begin(),
                    (*it).value.is_root(), get_token_id, get_token_value);
            }
            else if (cnt > 1)
            {
                xml::node<CharT> value (ostrm,
                        impl::string_lit<CharT>::get("value"));
                bool is_root = (*it).value.is_root();
 
                value_iter_t val_end = (*it).value.end();
                for (value_iter_t val_it = (*it).value.begin();
                val_it != val_end; ++val_it)
                {
                    token_to_xml (ostrm, val_it, is_root, get_token_id,
                        get_token_value);
                }
            }
            tree_node_to_xml(ostrm, (*it).children, id_to_name_map,
                get_token_id, get_token_value);      // dump all subnodes
        }
    }
 
    template <typename CharT, typename TreeNodeT, typename AssocContainerT>
    inline void
    tree_node_to_xml (std::basic_ostream<CharT> &ostrm, TreeNodeT const &node,
            AssocContainerT const& id_to_name_map)
    {
        typedef typename TreeNodeT::const_iterator node_iter_t;
 
        xml::attribute<CharT> nil;
        node_iter_t end = node.end();
        for (node_iter_t it = node.begin(); it != end; ++it)
        {
            // output a node
            xml::attribute<CharT> id (
                impl::string_lit<CharT>::get("rule"),
                get_rulename(id_to_name_map, (*it).value.id()).c_str());
            xml::node<CharT> currnode (ostrm,
                impl::string_lit<CharT>::get("parsenode"),
                (*it).value.id() != parser_id() && id.has_value() ? id : nil);
 
            // first dump the value
            if ((*it).value.begin() != (*it).value.end())
            {
                std::basic_string<CharT> tokens ((*it).value.begin(), (*it).value.end());
 
                if (tokens.size() > 0)
                {
                    // output all subtokens as one string (for better readability)
                    xml::attribute<CharT> is_root (
                        impl::string_lit<CharT>::get("is_root"),
                        impl::string_lit<CharT>::get((*it).value.is_root() ? "1" : ""));
                    xml::text<CharT>(ostrm,
                        impl::string_lit<CharT>::get("value"), tokens.c_str(),
                        is_root.has_value() ? is_root : nil);
                }
 
            }
            // dump all subnodes
            tree_node_to_xml(ostrm, (*it).children, id_to_name_map);
        }
    }
 
} // namespace impl
 
///////////////////////////////////////////////////////////////////////////////
// dump a parse tree as a xml stream (generic variant)
template <
    typename CharT, typename TreeNodeT, typename AssocContainerT,
    typename GetIdT, typename GetValueT
>
inline void
basic_tree_to_xml (std::basic_ostream<CharT> &ostrm, TreeNodeT const &tree,
std::basic_string<CharT> const &input_line, AssocContainerT const& id_to_name,
        GetIdT const &get_token_id, GetValueT const &get_token_value)
{
    // generate xml dump
    xml::document<CharT> doc (ostrm,
            impl::string_lit<CharT>::get("parsetree"),
            impl::string_lit<CharT>::get("parsetree.dtd"));
    xml::comment<CharT> input (ostrm, input_line.c_str());
    xml::attribute<CharT> ver (
            impl::string_lit<CharT>::get("version"),
            impl::string_lit<CharT>::get("1.0"));
    xml::node<CharT> mainnode (ostrm,
            impl::string_lit<CharT>::get("parsetree"), ver);
 
    impl::tree_node_to_xml (ostrm, tree, id_to_name, get_token_id,
        get_token_value);
}
 
// dump a parse tree as a xml steam (for character based parsers)
template <typename CharT, typename TreeNodeT, typename AssocContainerT>
inline void
basic_tree_to_xml (std::basic_ostream<CharT> &ostrm, TreeNodeT const &tree,
        std::basic_string<CharT> const &input_line,
        AssocContainerT const& id_to_name)
{
    // generate xml dump
    xml::document<CharT> doc (ostrm,
            impl::string_lit<CharT>::get("parsetree"),
            impl::string_lit<CharT>::get("parsetree.dtd"));
    xml::comment<CharT> input (ostrm, input_line.c_str());
    xml::attribute<CharT> ver (
            impl::string_lit<CharT>::get("version"),
            impl::string_lit<CharT>::get("1.0"));
    xml::node<CharT> mainnode (ostrm,
            impl::string_lit<CharT>::get("parsetree"), ver);
 
    impl::tree_node_to_xml(ostrm, tree, id_to_name);
}
 
template <typename CharT, typename TreeNodeT>
inline void
basic_tree_to_xml (std::basic_ostream<CharT> &ostrm, TreeNodeT const &tree,
        std::basic_string<CharT> const &input_line)
{
    return basic_tree_to_xml<CharT>(ostrm, tree, input_line,
        std::map<BOOST_SPIRIT_CLASSIC_NS::parser_id, std::basic_string<CharT> >());
}
 
BOOST_SPIRIT_CLASSIC_NAMESPACE_END
 
}} // namespace boost::spirit
 
#undef BOOST_SPIRIT_OSSTREAM
#undef BOOST_SPIRIT_GETSTRING
 
#endif