1 /*=============================================================================
2     Copyright (c) 2010-2011 Daniel James
3 
4     Use, modification and distribution is subject to the Boost Software
5     License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6     http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
8 
9 #if !defined(BOOST_SPIRIT_QUICKBOOK_VALUES_PARSE_HPP)
10 #define BOOST_SPIRIT_QUICKBOOK_VALUES_PARSE_HPP
11 
12 #include <boost/spirit/include/phoenix1_functions.hpp>
13 #include "parsers.hpp"
14 #include "scoped.hpp"
15 #include "values.hpp"
16 
17 #include <iostream>
18 
19 namespace quickbook
20 {
21     namespace ph = phoenix;
22 
23     struct value_builder_save : scoped_action_base
24     {
value_builder_savequickbook::value_builder_save25         explicit value_builder_save(value_builder& builder_) : builder(builder_)
26         {
27         }
28 
startquickbook::value_builder_save29         bool start()
30         {
31             builder.save();
32             return true;
33         }
34 
cleanupquickbook::value_builder_save35         void cleanup() { builder.restore(); }
36 
37         value_builder& builder;
38 
39       private:
40         value_builder_save& operator=(value_builder_save const&);
41     };
42 
43     struct value_builder_list : scoped_action_base
44     {
value_builder_listquickbook::value_builder_list45         explicit value_builder_list(value_builder& builder_) : builder(builder_)
46         {
47         }
48 
startquickbook::value_builder_list49         bool start(value::tag_type tag = value::default_tag)
50         {
51             builder.start_list(tag);
52             return true;
53         }
54 
successquickbook::value_builder_list55         void success(parse_iterator, parse_iterator) { builder.finish_list(); }
failurequickbook::value_builder_list56         void failure() { builder.clear_list(); }
57 
58         value_builder& builder;
59 
60       private:
61         value_builder_list& operator=(value_builder_list const&);
62     };
63 
64     struct value_entry
65     {
66         template <
67             typename Arg1,
68             typename Arg2 = void,
69             typename Arg3 = void,
70             typename Arg4 = void>
71         struct result
72         {
73             typedef void type;
74         };
75 
value_entryquickbook::value_entry76         explicit value_entry(value_builder& builder_, file_ptr* current_file_)
77             : builder(builder_), current_file(current_file_)
78         {
79         }
80 
operator ()quickbook::value_entry81         void operator()(
82             parse_iterator begin,
83             parse_iterator end,
84             value::tag_type tag = value::default_tag) const
85         {
86             builder.insert(
87                 qbk_value(*current_file, begin.base(), end.base(), tag));
88         }
89 
operator ()quickbook::value_entry90         void operator()(int v, value::tag_type tag = value::default_tag) const
91         {
92             builder.insert(int_value(v, tag));
93         }
94 
95         value_builder& builder;
96         file_ptr* current_file;
97 
98       private:
99         value_entry& operator=(value_entry const&);
100     };
101 
102     struct value_sort
103     {
104         typedef void result_type;
105 
value_sortquickbook::value_sort106         explicit value_sort(value_builder& builder_) : builder(builder_) {}
107 
operator ()quickbook::value_sort108         void operator()() const { builder.sort_list(); }
109 
110         value_builder& builder;
111 
112       private:
113         value_sort& operator=(value_sort const&);
114     };
115 
116     struct value_parser
117     {
value_parserquickbook::value_parser118         explicit value_parser(file_ptr* current_file)
119             : builder()
120             , save(value_builder_save(builder))
121             , list(value_builder_list(builder))
122             , entry(value_entry(builder, current_file))
123             , sort(value_sort(builder))
124         {
125         }
126 
releasequickbook::value_parser127         value release() { return builder.release(); }
128 
129         value_builder builder;
130         scoped_parser<value_builder_save> save;
131         scoped_parser<value_builder_list> list;
132         ph::function<value_entry> entry;
133         ph::function<value_sort> sort;
134     };
135 }
136 
137 #endif
138