]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
2 | Copyright (c) 2002 2004 2006 Joel de Guzman | |
3 | Copyright (c) 2004 Eric Niebler | |
4 | http://spirit.sourceforge.net/ | |
5 | ||
6 | Use, modification and distribution is subject to the Boost Software | |
7 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at | |
8 | http://www.boost.org/LICENSE_1_0.txt) | |
9 | =============================================================================*/ | |
10 | #if !defined(BOOST_SPIRIT_QUICKBOOK_ACTIONS_HPP) | |
11 | #define BOOST_SPIRIT_QUICKBOOK_ACTIONS_HPP | |
12 | ||
13 | #include <string> | |
14 | #include <vector> | |
11fdf7f2 | 15 | #include <boost/spirit/include/classic_parser.hpp> |
7c673cae | 16 | #include "fwd.hpp" |
11fdf7f2 TL |
17 | #include "iterator.hpp" |
18 | #include "scoped.hpp" | |
7c673cae FG |
19 | #include "utils.hpp" |
20 | #include "values.hpp" | |
7c673cae FG |
21 | |
22 | namespace quickbook | |
23 | { | |
24 | namespace cl = boost::spirit::classic; | |
25 | ||
b32b8144 | 26 | // Match if quickbook version is within range |
11fdf7f2 TL |
27 | struct quickbook_range : cl::parser<quickbook_range> |
28 | { | |
b32b8144 | 29 | explicit quickbook_range(unsigned lower_, unsigned upper_) |
11fdf7f2 TL |
30 | : lower(lower_), upper(upper_) |
31 | { | |
32 | } | |
7c673cae FG |
33 | |
34 | bool in_range() const; | |
11fdf7f2 | 35 | |
7c673cae | 36 | template <typename ScannerT> |
11fdf7f2 TL |
37 | typename cl::parser_result<quickbook_range, ScannerT>::type parse( |
38 | ScannerT const& scan) const | |
7c673cae FG |
39 | { |
40 | return in_range() ? scan.empty_match() : scan.no_match(); | |
41 | } | |
42 | ||
43 | unsigned lower, upper; | |
44 | }; | |
11fdf7f2 TL |
45 | |
46 | inline quickbook_range qbk_ver(unsigned lower, unsigned upper = 999u) | |
47 | { | |
7c673cae FG |
48 | return quickbook_range(lower, upper); |
49 | } | |
50 | ||
b32b8144 | 51 | // Match if in strict mode. |
11fdf7f2 TL |
52 | struct quickbook_strict : cl::parser<quickbook_strict> |
53 | { | |
54 | explicit quickbook_strict( | |
55 | quickbook::state& state_, bool positive_ = true) | |
56 | : state(state_), positive(positive_) | |
57 | { | |
58 | } | |
b32b8144 FG |
59 | |
60 | bool is_strict_checking() const; | |
61 | ||
62 | template <typename ScannerT> | |
11fdf7f2 TL |
63 | typename cl::parser_result<quickbook_range, ScannerT>::type parse( |
64 | ScannerT const& scan) const | |
b32b8144 | 65 | { |
11fdf7f2 TL |
66 | return is_strict_checking() == positive ? scan.empty_match() |
67 | : scan.no_match(); | |
b32b8144 FG |
68 | } |
69 | ||
70 | quickbook_strict operator~() const | |
71 | { | |
72 | return quickbook_strict(state, !positive); | |
73 | } | |
74 | ||
75 | quickbook::state& state; | |
76 | bool positive; | |
77 | }; | |
78 | ||
11fdf7f2 TL |
79 | inline quickbook_strict qbk_strict( |
80 | quickbook::state& state, unsigned lower = 999u) | |
81 | { | |
b32b8144 FG |
82 | return quickbook_strict(state, lower); |
83 | } | |
84 | ||
7c673cae | 85 | // Throws load_error |
11fdf7f2 TL |
86 | int load_snippets( |
87 | fs::path const& file, | |
88 | std::vector<template_symbol>& storage, | |
89 | std::string const& extension, | |
90 | value::tag_type load_type); | |
7c673cae FG |
91 | |
92 | struct error_message_action | |
93 | { | |
94 | // Prints an error message to std::cerr | |
95 | ||
11fdf7f2 TL |
96 | explicit error_message_action( |
97 | quickbook::state& state_, std::string const& message_) | |
98 | : state(state_), message(message_) | |
99 | { | |
100 | } | |
7c673cae FG |
101 | |
102 | void operator()(parse_iterator, parse_iterator) const; | |
103 | ||
104 | quickbook::state& state; | |
105 | std::string message; | |
106 | }; | |
107 | ||
108 | struct error_action | |
109 | { | |
110 | // Prints an error message to std::cerr | |
111 | ||
11fdf7f2 | 112 | explicit error_action(quickbook::state& state_) : state(state_) {} |
7c673cae FG |
113 | |
114 | void operator()(parse_iterator first, parse_iterator last) const; | |
115 | ||
116 | error_message_action operator()(std::string const& message) | |
117 | { | |
118 | return error_message_action(state, message); | |
119 | } | |
120 | ||
121 | quickbook::state& state; | |
122 | }; | |
123 | ||
124 | struct element_action | |
125 | { | |
11fdf7f2 | 126 | explicit element_action(quickbook::state& state_) : state(state_) {} |
7c673cae FG |
127 | |
128 | void operator()(parse_iterator, parse_iterator) const; | |
129 | ||
130 | quickbook::state& state; | |
131 | }; | |
132 | ||
133 | struct paragraph_action | |
134 | { | |
135 | // implicit paragraphs | |
136 | // doesn't output the paragraph if it's only whitespace. | |
137 | ||
11fdf7f2 | 138 | explicit paragraph_action(quickbook::state& state_) : state(state_) {} |
7c673cae FG |
139 | |
140 | void operator()() const; | |
141 | void operator()(parse_iterator, parse_iterator) const { (*this)(); } | |
142 | ||
143 | quickbook::state& state; | |
144 | }; | |
145 | ||
146 | struct explicit_list_action | |
147 | { | |
148 | // implicit paragraphs | |
149 | // doesn't output the paragraph if it's only whitespace. | |
150 | ||
11fdf7f2 TL |
151 | explicit explicit_list_action(quickbook::state& state_) : state(state_) |
152 | { | |
153 | } | |
7c673cae FG |
154 | |
155 | void operator()() const; | |
156 | void operator()(parse_iterator, parse_iterator) const { (*this)(); } | |
157 | ||
158 | quickbook::state& state; | |
159 | }; | |
160 | ||
161 | struct phrase_end_action | |
162 | { | |
11fdf7f2 | 163 | explicit phrase_end_action(quickbook::state& state_) : state(state_) {} |
7c673cae FG |
164 | |
165 | void operator()() const; | |
166 | void operator()(parse_iterator, parse_iterator) const { (*this)(); } | |
167 | ||
168 | quickbook::state& state; | |
169 | }; | |
170 | ||
171 | struct simple_phrase_action | |
172 | { | |
173 | // Handles simple text formats | |
174 | ||
11fdf7f2 TL |
175 | explicit simple_phrase_action(quickbook::state& state_) : state(state_) |
176 | { | |
177 | } | |
7c673cae FG |
178 | |
179 | void operator()(char) const; | |
180 | ||
181 | quickbook::state& state; | |
182 | }; | |
183 | ||
184 | struct cond_phrase_push : scoped_action_base | |
185 | { | |
11fdf7f2 | 186 | cond_phrase_push(quickbook::state& x) : state(x) {} |
7c673cae FG |
187 | |
188 | bool start(); | |
189 | void cleanup(); | |
190 | ||
191 | quickbook::state& state; | |
192 | bool saved_conditional; | |
193 | std::vector<std::string> anchors; | |
194 | }; | |
195 | ||
196 | struct do_macro_action | |
197 | { | |
198 | // Handles macro substitutions | |
199 | ||
b32b8144 | 200 | explicit do_macro_action(quickbook::state& state_) : state(state_) {} |
7c673cae FG |
201 | |
202 | void operator()(std::string const& str) const; | |
203 | quickbook::state& state; | |
204 | }; | |
205 | ||
206 | struct raw_char_action | |
207 | { | |
208 | // Prints a space | |
209 | ||
b32b8144 | 210 | explicit raw_char_action(quickbook::state& state_) : state(state_) {} |
7c673cae FG |
211 | |
212 | void operator()(char ch) const; | |
213 | void operator()(parse_iterator first, parse_iterator last) const; | |
214 | ||
215 | quickbook::state& state; | |
216 | }; | |
217 | ||
218 | struct plain_char_action | |
219 | { | |
220 | // Prints a single plain char. | |
221 | // Converts '<' to "<"... etc See utils.hpp | |
222 | ||
b32b8144 | 223 | explicit plain_char_action(quickbook::state& state_) : state(state_) {} |
7c673cae FG |
224 | |
225 | void operator()(char ch) const; | |
226 | void operator()(parse_iterator first, parse_iterator last) const; | |
227 | ||
228 | quickbook::state& state; | |
229 | }; | |
11fdf7f2 | 230 | |
7c673cae FG |
231 | struct escape_unicode_action |
232 | { | |
11fdf7f2 TL |
233 | explicit escape_unicode_action(quickbook::state& state_) : state(state_) |
234 | { | |
235 | } | |
7c673cae FG |
236 | |
237 | void operator()(parse_iterator first, parse_iterator last) const; | |
238 | ||
239 | quickbook::state& state; | |
240 | }; | |
241 | ||
242 | struct break_action | |
243 | { | |
b32b8144 | 244 | explicit break_action(quickbook::state& state_) : state(state_) {} |
7c673cae FG |
245 | |
246 | void operator()(parse_iterator f, parse_iterator) const; | |
247 | ||
248 | quickbook::state& state; | |
249 | }; | |
250 | ||
11fdf7f2 TL |
251 | struct element_id_warning_action |
252 | { | |
b32b8144 | 253 | explicit element_id_warning_action(quickbook::state& state_) |
11fdf7f2 TL |
254 | : state(state_) |
255 | { | |
256 | } | |
7c673cae FG |
257 | |
258 | void operator()(parse_iterator first, parse_iterator last) const; | |
259 | ||
260 | quickbook::state& state; | |
11fdf7f2 | 261 | }; |
7c673cae FG |
262 | |
263 | // Returns the doc_type, or an empty string if there isn't one. | |
11fdf7f2 TL |
264 | std::string pre( |
265 | quickbook::state& state, | |
266 | parse_iterator pos, | |
267 | value include_doc_id, | |
268 | bool nested_file); | |
7c673cae FG |
269 | void post(quickbook::state& state, std::string const& doc_type); |
270 | ||
271 | struct to_value_scoped_action : scoped_action_base | |
272 | { | |
11fdf7f2 | 273 | to_value_scoped_action(quickbook::state& state_) : state(state_) {} |
7c673cae FG |
274 | |
275 | bool start(value::tag_type = value::default_tag); | |
276 | void success(parse_iterator, parse_iterator); | |
277 | void cleanup(); | |
278 | ||
279 | quickbook::state& state; | |
280 | std::vector<std::string> saved_anchors; | |
281 | value::tag_type tag; | |
282 | }; | |
283 | ||
284 | // member_action | |
285 | // | |
286 | // Action for calling a member function taking two parse iterators. | |
287 | ||
11fdf7f2 | 288 | template <typename T> struct member_action |
7c673cae | 289 | { |
11fdf7f2 | 290 | typedef void (T::*member_function)(parse_iterator, parse_iterator); |
7c673cae FG |
291 | |
292 | T& l; | |
293 | member_function mf; | |
294 | ||
b32b8144 | 295 | explicit member_action(T& l_, member_function mf_) : l(l_), mf(mf_) {} |
7c673cae | 296 | |
11fdf7f2 TL |
297 | void operator()(parse_iterator first, parse_iterator last) const |
298 | { | |
7c673cae FG |
299 | (l.*mf)(first, last); |
300 | } | |
301 | }; | |
302 | ||
303 | // member_action1 | |
304 | // | |
11fdf7f2 TL |
305 | // Action for calling a member function taking two parse iterators and a |
306 | // value. | |
7c673cae | 307 | |
11fdf7f2 | 308 | template <typename T, typename Arg1> struct member_action1 |
7c673cae | 309 | { |
11fdf7f2 TL |
310 | typedef void (T::*member_function)( |
311 | parse_iterator, parse_iterator, Arg1); | |
7c673cae FG |
312 | |
313 | T& l; | |
314 | member_function mf; | |
315 | ||
b32b8144 | 316 | explicit member_action1(T& l_, member_function mf_) : l(l_), mf(mf_) {} |
7c673cae FG |
317 | |
318 | struct impl | |
319 | { | |
320 | member_action1 a; | |
321 | Arg1 value; | |
322 | ||
11fdf7f2 TL |
323 | explicit impl(member_action1& a_, Arg1 value_) |
324 | : a(a_), value(value_) | |
325 | { | |
326 | } | |
7c673cae | 327 | |
11fdf7f2 TL |
328 | void operator()(parse_iterator first, parse_iterator last) const |
329 | { | |
7c673cae FG |
330 | (a.l.*a.mf)(first, last, value); |
331 | } | |
332 | }; | |
333 | ||
11fdf7f2 | 334 | impl operator()(Arg1 a1) { return impl(*this, a1); } |
7c673cae FG |
335 | }; |
336 | ||
337 | // member_action_value | |
338 | // | |
339 | // Action for calling a unary member function. | |
340 | ||
11fdf7f2 | 341 | template <typename T, typename Value> struct member_action_value |
7c673cae | 342 | { |
11fdf7f2 | 343 | typedef void (T::*member_function)(Value); |
7c673cae FG |
344 | |
345 | T& l; | |
346 | member_function mf; | |
347 | ||
11fdf7f2 TL |
348 | explicit member_action_value(T& l_, member_function mf_) |
349 | : l(l_), mf(mf_) | |
350 | { | |
7c673cae | 351 | } |
11fdf7f2 TL |
352 | |
353 | void operator()(Value v) const { (l.*mf)(v); } | |
7c673cae FG |
354 | }; |
355 | ||
356 | // member_action_value | |
357 | // | |
358 | // Action for calling a unary member function with a fixed value. | |
359 | ||
11fdf7f2 | 360 | template <typename T, typename Value> struct member_action_fixed_value |
7c673cae | 361 | { |
11fdf7f2 | 362 | typedef void (T::*member_function)(Value); |
7c673cae FG |
363 | |
364 | T& l; | |
365 | member_function mf; | |
366 | Value v; | |
367 | ||
11fdf7f2 TL |
368 | explicit member_action_fixed_value(T& l_, member_function mf_, Value v_) |
369 | : l(l_), mf(mf_), v(v_) | |
370 | { | |
7c673cae FG |
371 | } |
372 | ||
11fdf7f2 TL |
373 | void operator()() const { (l.*mf)(v); } |
374 | ||
375 | void operator()(parse_iterator, parse_iterator) const { (l.*mf)(v); } | |
7c673cae FG |
376 | }; |
377 | } | |
378 | ||
379 | #endif // BOOST_SPIRIT_QUICKBOOK_ACTIONS_HPP |