File: sexpr_generator.hpp

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (128 lines) | stat: -rw-r--r-- 3,323 bytes parent folder | download | duplicates (16)
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
/*==============================================================================
    Copyright (c) 2001-2011 Hartmut Kaiser
    Copyright (c) 2010-2011 Bryce Lelbach

    Distributed under the Boost Software License, Version 1.0. (See accompanying
    file BOOST_LICENSE_1_0.rst or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/

#if !defined(BOOST_SPIRIT_UTREE_EXAMPLE_SEXPR_GENERATOR_HPP)
#define BOOST_SPIRIT_UTREE_EXAMPLE_SEXPR_GENERATOR_HPP

#include <boost/spirit/include/support_utree.hpp>
#include <boost/spirit/include/karma.hpp>

namespace boost {
namespace spirit {
namespace traits { 

template<>
struct transform_attribute<utree::nil_type, unused_type, karma::domain> {
  typedef unused_type type;

  static unused_type pre (utree::nil_type&) { return unused_type(); }
};

} // traits
} // spirit
} // boost

namespace sexpr
{

namespace karma = boost::spirit::karma;
namespace standard = boost::spirit::standard;

using boost::spirit::utree;
using boost::spirit::utf8_symbol_range_type;
using boost::spirit::utf8_string_range_type;
using boost::spirit::binary_range_type;

struct bool_output_policies : karma::bool_policies<>
{
    template <typename CharEncoding, typename Tag, typename Iterator>
    static bool generate_true(Iterator& sink, bool)
    {
        return string_inserter<CharEncoding, Tag>::call(sink, "#t");
    }

    template <typename CharEncoding, typename Tag, typename Iterator>
    static bool generate_false(Iterator& sink, bool)
    {
        return string_inserter<CharEncoding, Tag>::call(sink, "#f");
    }
};

template <typename Iterator>
struct generator : karma::grammar<Iterator, utree()>
{
    typedef boost::iterator_range<utree::const_iterator> utree_list;

    karma::rule<Iterator, utree()>
        start, ref_;

    karma::rule<Iterator, utree_list()>
        list;

    karma::rule<Iterator, utf8_symbol_range_type()>
        symbol;

    karma::rule<Iterator, utree::nil_type()>
        nil_;

    karma::rule<Iterator, utf8_string_range_type()>
        utf8;

    karma::rule<Iterator, binary_range_type()>
        binary;

    generator() : generator::base_type(start)
    {
        using standard::char_;
        using standard::string;
        using karma::bool_generator;
        using karma::uint_generator;
        using karma::double_;
        using karma::int_;
        using karma::lit;
        using karma::right_align;

        uint_generator<unsigned char, 16> hex2;
        bool_generator<bool, bool_output_policies> boolean;

        start = nil_
              | double_
              | int_
              | boolean
              | utf8
              | symbol
              | binary
              | list
              | ref_;
  
        ref_ = start;

        list = '(' << -(start % ' ') << ')';

        utf8 = '"' << *(&char_('"') << "\\\"" | char_) << '"';

        symbol = string;

        binary = '#' << *right_align(2, '0')[hex2] << '#';

        nil_ = karma::attr_cast(lit("nil"));

        start.name("sexpr");
        ref_.name("ref");
        list.name("list");
        utf8.name("string");
        symbol.name("symbol");
        binary.name("binary");
        nil_.name("nil");
    }
};

} // sexpr

#endif // BOOST_SPIRIT_UTREE_EXAMPLE_SEXPR_GENERATOR_HPP