| 1 | /*============================================================================= |
|---|
| 2 | Copyright (c) 2002 2004 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_BLOCK_HPP) |
|---|
| 11 | #define BOOST_SPIRIT_QUICKBOOK_BLOCK_HPP |
|---|
| 12 | |
|---|
| 13 | #include "./detail/utils.hpp" |
|---|
| 14 | #include "./phrase.hpp" |
|---|
| 15 | #include <boost/spirit/core.hpp> |
|---|
| 16 | #include <boost/spirit/utility/confix.hpp> |
|---|
| 17 | #include <boost/spirit/utility/chset.hpp> |
|---|
| 18 | #include <boost/spirit/actor/assign_actor.hpp> |
|---|
| 19 | #include <boost/spirit/dynamic/if.hpp> |
|---|
| 20 | #include <boost/spirit/symbols/symbols.hpp> |
|---|
| 21 | |
|---|
| 22 | namespace quickbook |
|---|
| 23 | { |
|---|
| 24 | using namespace boost::spirit; |
|---|
| 25 | |
|---|
| 26 | template <typename Actions> |
|---|
| 27 | struct block_grammar : grammar<block_grammar<Actions> > |
|---|
| 28 | { |
|---|
| 29 | block_grammar(Actions& actions_) |
|---|
| 30 | : actions(actions_) {} |
|---|
| 31 | |
|---|
| 32 | template <typename Scanner> |
|---|
| 33 | struct definition |
|---|
| 34 | { |
|---|
| 35 | definition(block_grammar const& self) |
|---|
| 36 | : is_not_preformatted(true) |
|---|
| 37 | , common(self.actions, is_not_preformatted) |
|---|
| 38 | { |
|---|
| 39 | using detail::var; |
|---|
| 40 | Actions& actions = self.actions; |
|---|
| 41 | |
|---|
| 42 | start_ = |
|---|
| 43 | *(space_p | comment) >> blocks >> blank |
|---|
| 44 | ; |
|---|
| 45 | |
|---|
| 46 | blocks = |
|---|
| 47 | +( block_markup |
|---|
| 48 | | code |
|---|
| 49 | | list [actions.list] |
|---|
| 50 | | hr [actions.hr] |
|---|
| 51 | | comment >> *eol |
|---|
| 52 | | paragraph [actions.paragraph] |
|---|
| 53 | | eol |
|---|
| 54 | ) |
|---|
| 55 | ; |
|---|
| 56 | |
|---|
| 57 | space = |
|---|
| 58 | *(space_p | comment) |
|---|
| 59 | ; |
|---|
| 60 | |
|---|
| 61 | blank = |
|---|
| 62 | *(blank_p | comment) |
|---|
| 63 | ; |
|---|
| 64 | |
|---|
| 65 | eol = blank >> eol_p |
|---|
| 66 | ; |
|---|
| 67 | |
|---|
| 68 | close_bracket = |
|---|
| 69 | ']' | |
|---|
| 70 | if_p(var(is_not_preformatted)) |
|---|
| 71 | [ |
|---|
| 72 | eol_p >> eol_p // Make sure that we don't go |
|---|
| 73 | ] // past a single block, except |
|---|
| 74 | ; // when preformatted. |
|---|
| 75 | |
|---|
| 76 | hard_space = |
|---|
| 77 | (eps_p - (alnum_p | '_')) >> space // must not be followed by |
|---|
| 78 | ; // alpha-numeric or underscore |
|---|
| 79 | |
|---|
| 80 | comment = |
|---|
| 81 | "[/" >> *(anychar_p - ']') >> ']' |
|---|
| 82 | ; |
|---|
| 83 | |
|---|
| 84 | hr = |
|---|
| 85 | str_p("----") |
|---|
| 86 | >> *(anychar_p - eol) |
|---|
| 87 | >> +eol |
|---|
| 88 | ; |
|---|
| 89 | |
|---|
| 90 | block_markup = |
|---|
| 91 | '[' >> space |
|---|
| 92 | >> ( begin_section |
|---|
| 93 | | end_section |
|---|
| 94 | | headings |
|---|
| 95 | | blurb |
|---|
| 96 | | blockquote |
|---|
| 97 | | admonition |
|---|
| 98 | | preformatted |
|---|
| 99 | | def_macro |
|---|
| 100 | | table |
|---|
| 101 | | variablelist |
|---|
| 102 | | xinclude |
|---|
| 103 | | include |
|---|
| 104 | ) |
|---|
| 105 | >> ( (space >> ']' >> +eol) |
|---|
| 106 | | eps_p [actions.error] |
|---|
| 107 | ) |
|---|
| 108 | ; |
|---|
| 109 | |
|---|
| 110 | begin_section = |
|---|
| 111 | "section" |
|---|
| 112 | >> hard_space |
|---|
| 113 | >> (':' >> (*(alnum_p | '_')) [assign_a(actions.section_id)] |
|---|
| 114 | | eps_p [assign_a(actions.section_id)] |
|---|
| 115 | ) |
|---|
| 116 | >> phrase [actions.begin_section] |
|---|
| 117 | ; |
|---|
| 118 | |
|---|
| 119 | end_section = |
|---|
| 120 | str_p("endsect") [actions.end_section] |
|---|
| 121 | ; |
|---|
| 122 | |
|---|
| 123 | headings = |
|---|
| 124 | h1 | h2 | h3 | h4 | h5 | h6 |
|---|
| 125 | ; |
|---|
| 126 | |
|---|
| 127 | h1 = "h1" >> hard_space >> phrase [actions.h1]; |
|---|
| 128 | h2 = "h2" >> hard_space >> phrase [actions.h2]; |
|---|
| 129 | h3 = "h3" >> hard_space >> phrase [actions.h3]; |
|---|
| 130 | h4 = "h4" >> hard_space >> phrase [actions.h4]; |
|---|
| 131 | h5 = "h5" >> hard_space >> phrase [actions.h5]; |
|---|
| 132 | h6 = "h6" >> hard_space >> phrase [actions.h6]; |
|---|
| 133 | |
|---|
| 134 | blurb = |
|---|
| 135 | "blurb" >> hard_space |
|---|
| 136 | >> phrase [actions.blurb] |
|---|
| 137 | ; |
|---|
| 138 | |
|---|
| 139 | blockquote = |
|---|
| 140 | ':' >> blank >> |
|---|
| 141 | phrase [actions.blockquote] |
|---|
| 142 | ; |
|---|
| 143 | |
|---|
| 144 | admonition = |
|---|
| 145 | "warning" >> blank >> |
|---|
| 146 | phrase [self.actions.warning] |
|---|
| 147 | | |
|---|
| 148 | "caution" >> blank >> |
|---|
| 149 | phrase [self.actions.caution] |
|---|
| 150 | | |
|---|
| 151 | "important" >> blank >> |
|---|
| 152 | phrase [self.actions.important] |
|---|
| 153 | | |
|---|
| 154 | "note" >> blank >> |
|---|
| 155 | phrase [self.actions.note] |
|---|
| 156 | | |
|---|
| 157 | "tip" >> blank >> |
|---|
| 158 | phrase [self.actions.tip] |
|---|
| 159 | ; |
|---|
| 160 | |
|---|
| 161 | { |
|---|
| 162 | static const bool true_ = true; |
|---|
| 163 | static const bool false_ = false; |
|---|
| 164 | |
|---|
| 165 | preformatted = |
|---|
| 166 | "pre" >> hard_space [assign_a(is_not_preformatted, false_)] |
|---|
| 167 | >> !eol >> phrase [actions.preformatted] |
|---|
| 168 | >> eps_p [assign_a(is_not_preformatted, true_)] |
|---|
| 169 | ; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | def_macro = |
|---|
| 173 | "def" >> hard_space |
|---|
| 174 | >> identifier [actions.identifier] |
|---|
| 175 | >> blank >> phrase [actions.macro_def] |
|---|
| 176 | ; |
|---|
| 177 | |
|---|
| 178 | variablelist = |
|---|
| 179 | "variablelist" |
|---|
| 180 | >> (eps_p(*blank_p >> eol_p) | hard_space) |
|---|
| 181 | >> (*(anychar_p - eol)) [assign_a(actions.table_title)] |
|---|
| 182 | >> +eol |
|---|
| 183 | >> *varlistentry |
|---|
| 184 | >> eps_p [actions.variablelist] |
|---|
| 185 | ; |
|---|
| 186 | |
|---|
| 187 | varlistentry = |
|---|
| 188 | space |
|---|
| 189 | >> ch_p('[') [actions.start_varlistentry] |
|---|
| 190 | >> |
|---|
| 191 | ( |
|---|
| 192 | ( |
|---|
| 193 | varlistterm |
|---|
| 194 | >> +varlistitem |
|---|
| 195 | >> ch_p(']') [actions.end_varlistentry] |
|---|
| 196 | >> space |
|---|
| 197 | ) |
|---|
| 198 | | eps_p [actions.error] |
|---|
| 199 | ) |
|---|
| 200 | ; |
|---|
| 201 | |
|---|
| 202 | varlistterm = |
|---|
| 203 | space |
|---|
| 204 | >> ch_p('[') [actions.start_varlistterm] |
|---|
| 205 | >> |
|---|
| 206 | ( |
|---|
| 207 | ( |
|---|
| 208 | phrase |
|---|
| 209 | >> ch_p(']') [actions.end_varlistterm] |
|---|
| 210 | >> space |
|---|
| 211 | ) |
|---|
| 212 | | eps_p [actions.error] |
|---|
| 213 | ) |
|---|
| 214 | ; |
|---|
| 215 | |
|---|
| 216 | varlistitem = |
|---|
| 217 | space |
|---|
| 218 | >> ch_p('[') [actions.start_varlistitem] |
|---|
| 219 | >> |
|---|
| 220 | ( |
|---|
| 221 | ( |
|---|
| 222 | phrase [actions.end_varlistitem] |
|---|
| 223 | >> ch_p(']') |
|---|
| 224 | >> space |
|---|
| 225 | ) |
|---|
| 226 | | eps_p [actions.error] |
|---|
| 227 | ) |
|---|
| 228 | ; |
|---|
| 229 | |
|---|
| 230 | table = |
|---|
| 231 | "table" |
|---|
| 232 | >> (eps_p(*blank_p >> eol_p) | hard_space) |
|---|
| 233 | >> (*(anychar_p - eol)) [assign_a(actions.table_title)] |
|---|
| 234 | >> +eol |
|---|
| 235 | >> *table_row |
|---|
| 236 | >> eps_p [actions.table] |
|---|
| 237 | ; |
|---|
| 238 | |
|---|
| 239 | table_row = |
|---|
| 240 | space |
|---|
| 241 | >> ch_p('[') [actions.start_row] |
|---|
| 242 | >> |
|---|
| 243 | ( |
|---|
| 244 | ( |
|---|
| 245 | *table_cell |
|---|
| 246 | >> ch_p(']') [actions.end_row] |
|---|
| 247 | >> space |
|---|
| 248 | ) |
|---|
| 249 | | eps_p [actions.error] |
|---|
| 250 | ) |
|---|
| 251 | ; |
|---|
| 252 | |
|---|
| 253 | table_cell = |
|---|
| 254 | space |
|---|
| 255 | >> ch_p('[') [actions.start_cell] |
|---|
| 256 | >> |
|---|
| 257 | ( |
|---|
| 258 | ( |
|---|
| 259 | phrase |
|---|
| 260 | >> ch_p(']') [actions.end_cell] |
|---|
| 261 | >> space |
|---|
| 262 | ) |
|---|
| 263 | | eps_p [actions.error] |
|---|
| 264 | ) |
|---|
| 265 | ; |
|---|
| 266 | |
|---|
| 267 | xinclude = |
|---|
| 268 | "xinclude" |
|---|
| 269 | >> hard_space |
|---|
| 270 | >> (*(anychar_p - |
|---|
| 271 | close_bracket)) [actions.xinclude] |
|---|
| 272 | ; |
|---|
| 273 | |
|---|
| 274 | include = |
|---|
| 275 | "include" |
|---|
| 276 | >> hard_space |
|---|
| 277 | >> |
|---|
| 278 | !( |
|---|
| 279 | ':' |
|---|
| 280 | >> (*((alnum_p | '_') - space_p))[assign_a(actions.include_doc_id)] |
|---|
| 281 | >> space |
|---|
| 282 | ) |
|---|
| 283 | >> (*(anychar_p - |
|---|
| 284 | close_bracket)) [actions.include] |
|---|
| 285 | ; |
|---|
| 286 | |
|---|
| 287 | identifier = |
|---|
| 288 | +(anychar_p - (space_p | ']')) |
|---|
| 289 | ; |
|---|
| 290 | |
|---|
| 291 | code = |
|---|
| 292 | ( |
|---|
| 293 | code_line |
|---|
| 294 | >> *(*eol >> code_line) |
|---|
| 295 | ) [actions.code] |
|---|
| 296 | >> +eol |
|---|
| 297 | ; |
|---|
| 298 | |
|---|
| 299 | code_line = |
|---|
| 300 | ((ch_p(' ') | '\t')) |
|---|
| 301 | >> *(anychar_p - eol) >> eol |
|---|
| 302 | ; |
|---|
| 303 | |
|---|
| 304 | list = |
|---|
| 305 | eps_p(ch_p('*') | '#') >> |
|---|
| 306 | +( |
|---|
| 307 | (*blank_p |
|---|
| 308 | >> (ch_p('*') | '#')) [actions.list_format] |
|---|
| 309 | >> *blank_p |
|---|
| 310 | >> list_item |
|---|
| 311 | ) [actions.list_item] |
|---|
| 312 | ; |
|---|
| 313 | |
|---|
| 314 | list_item = |
|---|
| 315 | *( common |
|---|
| 316 | | (anychar_p - |
|---|
| 317 | ( eol_p >> *blank_p >> eps_p(ch_p('*') | '#') |
|---|
| 318 | | (eol >> eol) |
|---|
| 319 | ) |
|---|
| 320 | ) [actions.plain_char] |
|---|
| 321 | ) |
|---|
| 322 | >> +eol |
|---|
| 323 | ; |
|---|
| 324 | |
|---|
| 325 | paragraph_end_markups = |
|---|
| 326 | "section", "endsect", "h1", "h2", "h3", "h4", "h5", "h6", |
|---|
| 327 | "blurb", ":", "pre", "def", "table", "include" |
|---|
| 328 | ; |
|---|
| 329 | |
|---|
| 330 | paragraph_end = |
|---|
| 331 | '[' >> space >> paragraph_end_markups | eol >> eol |
|---|
| 332 | ; |
|---|
| 333 | |
|---|
| 334 | paragraph = |
|---|
| 335 | *( common |
|---|
| 336 | | (anychar_p - // Make sure we don't go past |
|---|
| 337 | paragraph_end // a single block. |
|---|
| 338 | ) [actions.plain_char] |
|---|
| 339 | ) |
|---|
| 340 | >> (eps_p('[') | +eol) |
|---|
| 341 | ; |
|---|
| 342 | |
|---|
| 343 | phrase = |
|---|
| 344 | *( common |
|---|
| 345 | | comment |
|---|
| 346 | | (anychar_p - |
|---|
| 347 | close_bracket) [actions.plain_char] |
|---|
| 348 | ) |
|---|
| 349 | ; |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | bool is_not_preformatted; |
|---|
| 353 | |
|---|
| 354 | rule<Scanner> start_, blocks, block_markup, code, code_line, |
|---|
| 355 | paragraph, space, blank, comment, headings, h1, h2, |
|---|
| 356 | h3, h4, h5, h6, hr, blurb, blockquote, admonition, |
|---|
| 357 | phrase, list, close_bracket, ordered_list, def_macro, |
|---|
| 358 | identifier, table, table_row, variablelist, |
|---|
| 359 | varlistentry, varlistterm, varlistitem, table_cell, |
|---|
| 360 | preformatted, list_item, begin_section, end_section, |
|---|
| 361 | xinclude, include, hard_space, eol, paragraph_end; |
|---|
| 362 | symbols<> paragraph_end_markups; |
|---|
| 363 | |
|---|
| 364 | phrase_grammar<Actions> common; |
|---|
| 365 | |
|---|
| 366 | rule<Scanner> const& |
|---|
| 367 | start() const { return start_; } |
|---|
| 368 | }; |
|---|
| 369 | |
|---|
| 370 | Actions& actions; |
|---|
| 371 | }; |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| 374 | #endif // BOOST_SPIRIT_QUICKBOOK_BLOCK_HPP |
|---|
| 375 | |
|---|
| 376 | |
|---|