Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r65418 - trunk/tools/quickbook/src
From: dnljms_at_[hidden]
Date: 2010-09-14 16:44:25


Author: danieljames
Date: 2010-09-14 16:44:22 EDT (Tue, 14 Sep 2010)
New Revision: 65418
URL: http://svn.boost.org/trac/boost/changeset/65418

Log:
Create a parser to extract block as an attribute.

This lets us get rid of temp_para, a source of nasty bugs. Also should
make it easier to nest blocks in the future.
Added:
   trunk/tools/quickbook/src/scoped_block.hpp (contents, props changed)
Text files modified:
   trunk/tools/quickbook/src/actions.cpp | 42 ++++-----------------
   trunk/tools/quickbook/src/actions.hpp | 79 ++++++++++-----------------------------
   trunk/tools/quickbook/src/actions_class.cpp | 25 +++++-------
   trunk/tools/quickbook/src/actions_class.hpp | 13 ++----
   trunk/tools/quickbook/src/block_grammar.cpp | 49 +++++++++++++-----------
   trunk/tools/quickbook/src/code_snippet.cpp | 2
   trunk/tools/quickbook/src/doc_info_grammar.cpp | 18 ++++----
   7 files changed, 80 insertions(+), 148 deletions(-)

Modified: trunk/tools/quickbook/src/actions.cpp
==============================================================================
--- trunk/tools/quickbook/src/actions.cpp (original)
+++ trunk/tools/quickbook/src/actions.cpp 2010-09-14 16:44:22 EDT (Tue, 14 Sep 2010)
@@ -61,6 +61,11 @@
         ++error_count;
     }
 
+ void tagged_action::operator()(std::string const& str) const
+ {
+ out << pre << str << post;
+ }
+
     void phrase_action::operator()(iterator first, iterator last) const
     {
         std::string str;
@@ -936,7 +941,6 @@
             }
         }
 
- actions.write_paragraphs(); // Deal with any content in 'temp_para'
         actions.out.swap(block);
         actions.phrase.swap(phrase);
         actions.pop(); // restore the actions' states
@@ -979,7 +983,7 @@
 
         if(symbol->body.is_block || !block.empty()) {
             actions.inside_paragraph();
- actions.temp_para << block;
+ actions.out << block;
             actions.phrase << phrase;
         }
         else {
@@ -1030,20 +1034,6 @@
         actions.table_title.clear();
     }
 
- void start_varlistitem_action::operator()() const
- {
- phrase << start_varlistitem_;
- phrase.push();
- }
-
- void end_varlistitem_action::operator()() const
- {
- std::string str;
- temp_para.swap(str);
- phrase.pop();
- phrase << str << end_varlistitem_;
- }
-
     void table_action::operator()(iterator, iterator) const
     {
         std::string::iterator first = actions.table_title.begin();
@@ -1129,21 +1119,12 @@
         (*this)(*f);
     }
 
- void start_col_action::operator()(char) const
+ void col_action::operator()(std::string const& contents) const
     {
- phrase << start_cell_;
- phrase.push();
+ phrase << start_cell_ << contents << end_cell_;
         ++span;
     }
 
- void end_col_action::operator()(char) const
- {
- std::string str;
- temp_para.swap(str);
- phrase.pop();
- phrase << str << end_cell_;
- }
-
     void begin_section_action::operator()(iterator first, iterator last) const
     {
         section_id = element_id.empty() ?
@@ -1396,11 +1377,4 @@
         phrase.swap(out.encoded);
         out.raw = std::string(first, last);
     }
-
- void copy_stream_action::operator()() const
- {
- std::string str;
- phrase.swap(str);
- out << str;
- }
 }

Modified: trunk/tools/quickbook/src/actions.hpp
==============================================================================
--- trunk/tools/quickbook/src/actions.hpp (original)
+++ trunk/tools/quickbook/src/actions.hpp 2010-09-14 16:44:22 EDT (Tue, 14 Sep 2010)
@@ -86,6 +86,23 @@
         int& error_count;
     };
 
+ struct tagged_action
+ {
+ tagged_action(
+ collector& out,
+ std::string const& pre,
+ std::string const& post)
+ : out(out)
+ , pre(pre)
+ , post(post) {}
+
+ void operator()(std::string const&) const;
+
+ collector& out;
+ std::string pre;
+ std::string post;
+ };
+
     struct phrase_action
     {
         // blurb, blockquote, preformatted, list_item,
@@ -530,37 +547,6 @@
         quickbook::actions& actions;
     };
 
- struct start_varlistitem_action
- {
- start_varlistitem_action(collector& phrase)
- : phrase(phrase) {}
-
- void operator()() const;
-
- template <typename T1>
- void operator()(T1 const&) const { return (*this)(); }
- template <typename T1, typename T2>
- void operator()(T1 const&, T2 const&) const { return (*this)(); }
-
- collector& phrase;
- };
-
- struct end_varlistitem_action
- {
- end_varlistitem_action(collector& phrase, collector& temp_para)
- : phrase(phrase), temp_para(temp_para) {}
-
- void operator()() const;
-
- template <typename T1>
- void operator()(T1 const&) const { return (*this)(); }
- template <typename T1, typename T2>
- void operator()(T1 const&, T2 const&) const { return (*this)(); }
-
- collector& phrase;
- collector& temp_para;
- };
-
     struct break_action
     {
         // Handles line-breaks (DEPRECATED!!!)
@@ -685,30 +671,17 @@
         std::string& header;
     };
 
- struct start_col_action
+ struct col_action
     {
- // Handles table columns
-
- start_col_action(collector& phrase, unsigned& span)
+ col_action(collector& phrase, unsigned& span)
         : phrase(phrase), span(span) {}
 
- void operator()(char) const;
+ void operator()(std::string const&) const;
 
         collector& phrase;
         unsigned& span;
     };
 
- struct end_col_action
- {
- end_col_action(collector& phrase, collector& temp_para)
- : phrase(phrase), temp_para(temp_para) {}
-
- void operator()(char) const;
-
- collector& phrase;
- collector& temp_para;
- };
-
     struct begin_section_action
     {
         // Handles begin page
@@ -852,18 +825,6 @@
         docinfo_string& out;
         collector& phrase;
     };
-
- struct copy_stream_action
- {
- copy_stream_action(collector& out, collector& phrase)
- : out(out) , phrase(phrase) {}
-
- void operator()(iterator, iterator) const { (*this)(); }
- void operator()() const;
-
- collector& out;
- collector& phrase;
- };
 }
 
 #ifdef BOOST_MSVC

Modified: trunk/tools/quickbook/src/actions_class.cpp
==============================================================================
--- trunk/tools/quickbook/src/actions_class.cpp (original)
+++ trunk/tools/quickbook/src/actions_class.cpp 2010-09-14 16:44:22 EDT (Tue, 14 Sep 2010)
@@ -86,8 +86,7 @@
         , code(out, phrase, *this)
         , code_block(phrase, phrase, *this)
         , inline_code(phrase, *this)
- , inside_paragraph(temp_para, phrase, paragraph_pre, paragraph_post)
- , write_paragraphs(out, temp_para)
+ , inside_paragraph(out, phrase, paragraph_pre, paragraph_post)
         , h(out, phrase, element_id, doc_id, section_id, qualified_section_id, section_level)
         , h1(out, phrase, element_id, doc_id, section_id, qualified_section_id, h1_pre, h1_post)
         , h2(out, phrase, element_id, doc_id, section_id, qualified_section_id, h2_pre, h2_post)
@@ -96,14 +95,14 @@
         , h5(out, phrase, element_id, doc_id, section_id, qualified_section_id, h5_pre, h5_post)
         , h6(out, phrase, element_id, doc_id, section_id, qualified_section_id, h6_pre, h6_post)
         , hr(out, hr_)
- , blurb(out, temp_para, blurb_pre, blurb_post)
- , blockquote(out, temp_para, blockquote_pre, blockquote_post)
+ , blurb(out, blurb_pre, blurb_post)
+ , blockquote(out, blockquote_pre, blockquote_post)
         , preformatted(out, phrase, preformatted_pre, preformatted_post)
- , warning(out, temp_para, warning_pre, warning_post)
- , caution(out, temp_para, caution_pre, caution_post)
- , important(out, temp_para, important_pre, important_post)
- , note(out, temp_para, note_pre, note_post)
- , tip(out, temp_para, tip_pre, tip_post)
+ , warning(out, warning_pre, warning_post)
+ , caution(out, caution_pre, caution_post)
+ , important(out, important_pre, important_post)
+ , note(out, note_pre, note_post)
+ , tip(out, tip_pre, tip_post)
         , plain_char(phrase)
         , raw_char(phrase)
         , escape_unicode(phrase)
@@ -161,8 +160,7 @@
         , end_varlistentry(phrase, end_varlistentry_)
         , start_varlistterm(phrase, start_varlistterm_)
         , end_varlistterm(phrase, end_varlistterm_)
- , start_varlistitem(phrase)
- , end_varlistitem(phrase, temp_para)
+ , varlistitem(phrase, start_varlistitem_, end_varlistitem_)
 
         , break_(phrase)
         , macro_identifier(*this)
@@ -178,8 +176,7 @@
         , table(*this)
         , start_row(phrase, table_span, table_header)
         , end_row(phrase, end_row_)
- , start_cell(phrase, table_span)
- , end_cell(phrase, temp_para)
+ , cell(phrase, table_span)
         , anchor(out)
 
         , begin_section(out, phrase, doc_id, section_id, section_level, qualified_section_id, element_id)
@@ -221,7 +218,6 @@
 
         out.push();
         phrase.push();
- temp_para.push();
         list_buffer.push();
         templates.push();
     }
@@ -262,7 +258,6 @@
 
         out.pop();
         phrase.pop();
- temp_para.pop();
         list_buffer.pop();
         templates.pop();
     }

Modified: trunk/tools/quickbook/src/actions_class.hpp
==============================================================================
--- trunk/tools/quickbook/src/actions_class.hpp (original)
+++ trunk/tools/quickbook/src/actions_class.hpp 2010-09-14 16:44:22 EDT (Tue, 14 Sep 2010)
@@ -65,7 +65,6 @@
 
     // auxilliary streams
         collector phrase;
- collector temp_para;
         collector list_buffer;
 
     // state
@@ -142,12 +141,12 @@
         code_action code_block;
         inline_code_action inline_code;
         implicit_paragraph_action inside_paragraph;
- copy_stream_action write_paragraphs;
         generic_header_action h;
         header_action h1, h2, h3, h4, h5, h6;
         markup_action hr;
- phrase_action blurb, blockquote, preformatted;
- phrase_action warning, caution, important, note, tip;
+ tagged_action blurb, blockquote;
+ phrase_action preformatted;
+ tagged_action warning, caution, important, note, tip;
         plain_char_action plain_char;
         raw_char_action raw_char;
         escape_unicode_action escape_unicode;
@@ -205,8 +204,7 @@
         markup_action end_varlistentry;
         markup_action start_varlistterm;
         markup_action end_varlistterm;
- start_varlistitem_action start_varlistitem;
- end_varlistitem_action end_varlistitem;
+ tagged_action varlistitem;
 
         break_action break_;
         macro_identifier_action macro_identifier;
@@ -222,8 +220,7 @@
         table_action table;
         start_row_action start_row;
         markup_action end_row;
- start_col_action start_cell;
- end_col_action end_cell;
+ col_action cell;
         anchor_action anchor;
 
         begin_section_action begin_section;

Modified: trunk/tools/quickbook/src/block_grammar.cpp
==============================================================================
--- trunk/tools/quickbook/src/block_grammar.cpp (original)
+++ trunk/tools/quickbook/src/block_grammar.cpp 2010-09-14 16:44:22 EDT (Tue, 14 Sep 2010)
@@ -11,6 +11,7 @@
 #include "phrase_grammar.hpp"
 #include "utils.hpp"
 #include "actions_class.hpp"
+#include "scoped_block.hpp"
 #include <boost/spirit/include/classic_confix.hpp>
 #include <boost/spirit/include/classic_chset.hpp>
 #include <boost/spirit/include/classic_assign_actor.hpp>
@@ -79,7 +80,6 @@
             | hr [actions.hr]
             | +eol
             | paragraph [actions.inside_paragraph]
- [actions.write_paragraphs]
             )
             ;
 
@@ -211,30 +211,36 @@
 
         blurb =
             "blurb" >> hard_space
- >> inside_paragraph [actions.blurb]
- >> cl::eps_p
+ >> scoped_block(actions)[inside_paragraph]
+ [actions.blurb]
             ;
 
         blockquote =
             ':' >> blank >>
- inside_paragraph [actions.blockquote]
+ scoped_block(actions)[inside_paragraph]
+ [actions.blockquote]
             ;
 
         admonition =
             "warning" >> blank >>
- inside_paragraph [actions.warning]
+ scoped_block(actions)[inside_paragraph]
+ [actions.warning]
             |
             "caution" >> blank >>
- inside_paragraph [actions.caution]
+ scoped_block(actions)[inside_paragraph]
+ [actions.caution]
             |
             "important" >> blank >>
- inside_paragraph [actions.important]
+ scoped_block(actions)[inside_paragraph]
+ [actions.important]
             |
             "note" >> blank >>
- inside_paragraph [actions.note]
+ scoped_block(actions)[inside_paragraph]
+ [actions.note]
             |
             "tip" >> blank >>
- inside_paragraph [actions.tip]
+ scoped_block(actions)[inside_paragraph]
+ [actions.tip]
             ;
 
         preformatted =
@@ -302,10 +308,11 @@
>>
             (
                 (
- varlistterm [actions.start_varlistitem]
- >> ( +varlistitem
+ varlistterm
+ >> ( scoped_block(actions) [+varlistitem]
+ [actions.varlistitem]
                         | cl::eps_p [actions.error]
- ) [actions.end_varlistitem]
+ )
>> cl::ch_p(']') [actions.end_varlistentry]
>> space
                 )
@@ -367,17 +374,15 @@
             ;
 
         table_cell =
- space
- >> cl::ch_p('[') [actions.start_cell]
- >>
- (
- (
- inside_paragraph
- >> cl::ch_p(']') [actions.end_cell]
+ space
+ >> cl::ch_p('[')
+ >> ( scoped_block(actions) [
+ inside_paragraph
+ >> cl::ch_p(']')
>> space
- )
+ ] [actions.cell]
                 | cl::eps_p [actions.error]
- )
+ )
             ;
 
         xinclude =
@@ -458,7 +463,7 @@
 
         paragraph =
            +( common
- | (cl::anychar_p - // Make sure we don't go past
+ | (cl::anychar_p - // Make sure we don't go past
                     paragraph_end // a single block.
                 ) [actions.plain_char]
             )

Modified: trunk/tools/quickbook/src/code_snippet.cpp
==============================================================================
--- trunk/tools/quickbook/src/code_snippet.cpp (original)
+++ trunk/tools/quickbook/src/code_snippet.cpp 2010-09-14 16:44:22 EDT (Tue, 14 Sep 2010)
@@ -249,7 +249,7 @@
             }
 
             cl::rule<Scanner>
- start_, identifier, code_elements, start_snippet, end_snippet,
+ start_, identifier, code_elements, start_snippet, end_snippet,
                 escaped_comment, inline_callout, line_callout, ignore;
 
             cl::rule<Scanner> const&

Modified: trunk/tools/quickbook/src/doc_info_grammar.cpp
==============================================================================
--- trunk/tools/quickbook/src/doc_info_grammar.cpp (original)
+++ trunk/tools/quickbook/src/doc_info_grammar.cpp 2010-09-14 16:44:22 EDT (Tue, 14 Sep 2010)
@@ -179,15 +179,15 @@
                 ) [cl::assign_a(actions.source_mode)]
             ;
 
- doc_biblioid =
- "biblioid"
- >> hard_space
- >> (+cl::alnum_p) [cl::assign_a(actions.doc_biblioid.first)]
- >> hard_space
- >> (+(~cl::eps_p(']') >> char_))
- [actions.extract_doc_biblioid]
- [cl::push_back_a(actions.doc_biblioid_items, actions.doc_biblioid)]
- ;
+ doc_biblioid =
+ "biblioid"
+ >> hard_space
+ >> (+cl::alnum_p) [cl::assign_a(actions.doc_biblioid.first)]
+ >> hard_space
+ >> (+(~cl::eps_p(']') >> char_))
+ [actions.extract_doc_biblioid]
+ [cl::push_back_a(actions.doc_biblioid_items, actions.doc_biblioid)]
+ ;
 
         comment =
             "[/" >> *(cl::anychar_p - ']') >> ']'

Added: trunk/tools/quickbook/src/scoped_block.hpp
==============================================================================
--- (empty file)
+++ trunk/tools/quickbook/src/scoped_block.hpp 2010-09-14 16:44:22 EDT (Tue, 14 Sep 2010)
@@ -0,0 +1,134 @@
+/*=============================================================================
+ Copyright (c) 2010 Daniel James
+ Copyright (c) 2003 Martin Wille
+ http://spirit.sourceforge.net/
+
+ Distributed under 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)
+ =============================================================================*/
+
+// Used to parse an inner block, saves the streams and restores them if the
+// parse fails. On success the action is passed the resulting block.
+//
+// Might be a good idea to do something more generic in the future.
+//
+// This is based on `boost::spirit::classic::scoped_lock` by Martin Wille
+
+#ifndef BOOST_QUICKBOOK_SCOPED_BLOCK_HPP
+#define BOOST_QUICKBOOK_SCOPED_BLOCK_HPP
+
+#include <boost/spirit/home/classic/namespace.hpp>
+#include <boost/spirit/home/classic/core/composite/composite.hpp>
+#include "actions_class.hpp"
+
+namespace quickbook {
+
+ namespace cl = boost::spirit::classic;
+
+ struct scoped_block_push
+ {
+ typedef std::string attribute;
+
+ scoped_block_push(quickbook::actions& actions)
+ : actions(actions)
+ {
+ actions.out.push();
+ actions.phrase.push();
+ }
+
+ ~scoped_block_push()
+ {
+ actions.phrase.pop();
+ actions.out.pop();
+ }
+
+ std::string const& finish()
+ {
+ actions.inside_paragraph();
+ return actions.out.str();
+ }
+
+ quickbook::actions& actions;
+ };
+
+ template <typename ParserT>
+ struct scoped_block_parser
+ : public cl::unary< ParserT, cl::parser< scoped_block_parser<ParserT> > >
+ {
+ typedef scoped_block_parser<ParserT> self_t;
+ typedef cl::unary< ParserT, cl::parser< scoped_block_parser<ParserT> > > base_t;
+
+ template <typename ScannerT>
+ struct result
+ {
+ typedef cl::match<std::string> type;
+ };
+
+ scoped_block_parser(quickbook::actions& a, ParserT const &p)
+ : base_t(p)
+ , actions(a)
+ {}
+
+ template <typename ScannerT>
+ cl::match<std::string> parse(ScannerT const &scan) const
+ {
+ typedef typename ScannerT::iterator_t iterator_t;
+ iterator_t save = scan.first;
+
+ scoped_block_push push(actions);
+ typename cl::parser_result<ParserT, ScannerT>::type result
+ = this->subject().parse(scan);
+
+ if (result)
+ return scan.create_match(result.length(), push.finish(), save, scan.first);
+ else
+ return scan.no_match();
+ }
+
+ quickbook::actions& actions;
+ };
+
+ ///////////////////////////////////////////////////////////////////////////
+ //
+ // scoped_block_parser_gen
+ //
+ // generator for scoped_block_parser objects
+ // operator[] returns scoped_block_parser according to its argument
+ //
+ ///////////////////////////////////////////////////////////////////////////
+ struct scoped_block_parser_gen
+ {
+ explicit scoped_block_parser_gen(quickbook::actions& actions)
+ : actions(actions) {}
+
+ template<typename ParserT>
+ scoped_block_parser
+ <
+ typename cl::as_parser<ParserT>::type
+ >
+ operator[](ParserT const &p) const
+ {
+ typedef cl::as_parser<ParserT> as_parser_t;
+ typedef typename as_parser_t::type parser_t;
+
+ return scoped_block_parser<parser_t>
+ (actions, as_parser_t::convert(p));
+ }
+
+ quickbook::actions& actions;
+ };
+
+
+ ///////////////////////////////////////////////////////////////////////////
+ //
+ // scoped_block_d parser directive
+ //
+ // constructs a scoped_block_parser generator from its argument
+ //
+ ///////////////////////////////////////////////////////////////////////////
+ inline scoped_block_parser_gen scoped_block(quickbook::actions& actions) {
+ return scoped_block_parser_gen(actions);
+ }
+
+}
+#endif // BOOST_QUICKBOOK_SCOPED_BLOCK_HPP


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk