Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r59316 - branches/quickbook-1.5-spirit2
From: daniel_james_at_[hidden]
Date: 2010-01-27 17:05:08


Author: danieljames
Date: 2010-01-27 17:05:07 EST (Wed, 27 Jan 2010)
New Revision: 59316
URL: http://svn.boost.org/trac/boost/changeset/59316

Log:
Convert code_snippets to use adapted structs.
Text files modified:
   branches/quickbook-1.5-spirit2/code_snippet.cpp | 113 +++++++++++++++------------------
   branches/quickbook-1.5-spirit2/code_snippet.hpp | 133 ++++++++++++++++++++++++++-------------
   branches/quickbook-1.5-spirit2/code_snippet_types.hpp | 77 ++++++++++++++++++++--
   3 files changed, 207 insertions(+), 116 deletions(-)

Modified: branches/quickbook-1.5-spirit2/code_snippet.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/code_snippet.cpp (original)
+++ branches/quickbook-1.5-spirit2/code_snippet.cpp 2010-01-27 17:05:07 EST (Wed, 27 Jan 2010)
@@ -23,9 +23,9 @@
 
 namespace quickbook
 {
- void code_snippet_actions::pass_thru(char x)
+ void code_snippet_actions::process_action::operator()(char x, unused_type, unused_type) const
     {
- code += x;
+ actions.code += x;
     }
 
     namespace detail
@@ -33,94 +33,83 @@
         int callout_id = 0;
     }
 
- void code_snippet_actions::callout(std::string const& x, char const* role)
+ void code_snippet_actions::process_action::operator()(callout const& x, unused_type, unused_type) const
     {
         using detail::callout_id;
- code += "``'''";
- code += std::string("<phrase role=\"") + role + "\">";
- code += "<co id=\"";
- code += doc_id + boost::lexical_cast<std::string>(callout_id + callouts.size()) + "co\" ";
- code += "linkends=\"";
- code += doc_id + boost::lexical_cast<std::string>(callout_id + callouts.size()) + "\" />";
- code += "</phrase>";
- code += "'''``";
+ actions.code += "``'''";
+ actions.code += std::string("<phrase role=\"") + x.role + "\">";
+ actions.code += "<co id=\"";
+ actions.code += actions.doc_id + boost::lexical_cast<std::string>(callout_id + actions.callouts.size()) + "co\" ";
+ actions.code += "linkends=\"";
+ actions.code += actions.doc_id + boost::lexical_cast<std::string>(callout_id + actions.callouts.size()) + "\" />";
+ actions.code += "</phrase>";
+ actions.code += "'''``";
 
- callouts.push_back(x);
+ actions.callouts.push_back(x.content);
     }
 
- void code_snippet_actions::inline_callout(std::string const& x)
+ void code_snippet_actions::process_action::operator()(escaped_comment const& x, unused_type, unused_type) const
     {
- callout(x, "callout_bug");
- }
-
- void code_snippet_actions::line_callout(std::string const& x)
- {
- callout(x, "line_callout_bug");
- }
-
- void code_snippet_actions::escaped_comment(std::string const& x)
- {
- if (!code.empty())
+ if (!actions.code.empty())
         {
- detail::unindent(code); // remove all indents
- if (code.size() != 0)
+ detail::unindent(actions.code); // remove all indents
+ if (actions.code.size() != 0)
             {
- snippet += "\n\n";
- snippet += source_type;
- snippet += "``\n" + code + "``\n\n";
- code.clear();
+ actions.snippet += "\n\n";
+ actions.snippet += actions.source_type;
+ actions.snippet += "``\n" + actions.code + "``\n\n";
+ actions.code.clear();
             }
         }
- std::string temp(x);
+ std::string temp(x.content);
         detail::unindent(temp); // remove all indents
         if (temp.size() != 0)
         {
- snippet += "\n" + temp; // add a linebreak to allow block marskups
+ actions.snippet += "\n" + temp; // add a linebreak to allow block marskups
         }
     }
 
- void code_snippet_actions::compile(boost::iterator_range<iterator> x)
+ void code_snippet_actions::output_action::operator()(code_snippet const& x, unused_type, unused_type) const
     {
         using detail::callout_id;
- if (!code.empty())
+ if (!actions.code.empty())
         {
- detail::unindent(code); // remove all indents
- if (code.size() != 0)
+ detail::unindent(actions.code); // remove all indents
+ if (actions.code.size() != 0)
             {
- snippet += "\n\n";
- snippet += source_type;
- snippet += "```\n" + code + "```\n\n";
+ actions.snippet += "\n\n";
+ actions.snippet += actions.source_type;
+ actions.snippet += "```\n" + actions.code + "```\n\n";
             }
 
- if(callouts.size() > 0)
+ if(actions.callouts.size() > 0)
             {
- snippet += "'''<calloutlist>'''";
- for (size_t i = 0; i < callouts.size(); ++i)
+ actions.snippet += "'''<calloutlist>'''";
+ for (size_t i = 0; i < actions.callouts.size(); ++i)
               {
- snippet += "'''<callout arearefs=\"";
- snippet += doc_id + boost::lexical_cast<std::string>(callout_id + i) + "co\" ";
- snippet += "id=\"";
- snippet += doc_id + boost::lexical_cast<std::string>(callout_id + i) + "\">";
- snippet += "'''";
-
- snippet += "'''<para>'''";
- snippet += callouts[i];
- snippet += "'''</para>'''";
- snippet += "'''</callout>'''";
+ actions.snippet += "'''<callout arearefs=\"";
+ actions.snippet += actions.doc_id + boost::lexical_cast<std::string>(callout_id + i) + "co\" ";
+ actions.snippet += "id=\"";
+ actions.snippet += actions.doc_id + boost::lexical_cast<std::string>(callout_id + i) + "\">";
+ actions.snippet += "'''";
+
+ actions.snippet += "'''<para>'''";
+ actions.snippet += actions.callouts[i];
+ actions.snippet += "'''</para>'''";
+ actions.snippet += "'''</callout>'''";
               }
- snippet += "'''</calloutlist>'''";
+ actions.snippet += "'''</calloutlist>'''";
             }
         }
 
         std::vector<std::string> tinfo;
- tinfo.push_back(id);
- tinfo.push_back(snippet);
- storage.push_back(template_symbol(tinfo, x.begin().get_position()));
-
- callout_id += callouts.size();
- callouts.clear();
- code.clear();
- snippet.clear();
- id.clear();
+ tinfo.push_back(x.identifier);
+ tinfo.push_back(actions.snippet);
+ actions.storage.push_back(template_symbol(tinfo, x.position));
+
+ callout_id += actions.callouts.size();
+ actions.callouts.clear();
+ actions.code.clear();
+ actions.snippet.clear();
     }
 }

Modified: branches/quickbook-1.5-spirit2/code_snippet.hpp
==============================================================================
--- branches/quickbook-1.5-spirit2/code_snippet.hpp (original)
+++ branches/quickbook-1.5-spirit2/code_snippet.hpp 2010-01-27 17:05:07 EST (Wed, 27 Jan 2010)
@@ -11,6 +11,8 @@
 
 #include <boost/spirit/include/qi_core.hpp>
 #include <boost/spirit/include/qi_eol.hpp>
+#include <boost/spirit/include/qi_eps.hpp>
+#include <boost/spirit/include/qi_attr.hpp>
 #include <boost/spirit/include/phoenix_core.hpp>
 #include <boost/spirit/include/phoenix_bind.hpp>
 #include <boost/spirit/include/phoenix_operator.hpp>
@@ -24,6 +26,14 @@
 {
     namespace ph = boost::phoenix;
 
+ namespace
+ {
+ // Shared rules
+
+ qi::rule<iterator, file_position()>
+ position = qi::raw[qi::eps] [get_position];
+ }
+
     struct python_code_snippet_grammar::rules
     {
         typedef code_snippet_actions actions_type;
@@ -33,10 +43,15 @@
         actions_type& actions;
 
         qi::rule<iterator>
- start_, snippet, code_elements, escaped_comment,
- inline_callout, line_callout, ignore;
+ start_, ignore;
+ qi::rule<iterator, quickbook::code_snippet()>
+ snippet;
         qi::rule<iterator, std::string()>
- identifier;
+ code_elements, identifier;
+ qi::rule<iterator, quickbook::callout()>
+ inline_callout, line_callout;
+ qi::rule<iterator, quickbook::escaped_comment()>
+ escaped_comment;
     };
 
     python_code_snippet_grammar::python_code_snippet_grammar(actions_type & actions)
@@ -51,7 +66,7 @@
     {
         start_ =
             +(
- qi::raw[snippet] [ph::bind(&actions_type::compile, &actions, qi::_1)]
+ snippet [actions.output]
                 | qi::char_
             )
             ;
@@ -61,16 +76,18 @@
             ;
 
         snippet =
- "#[" >> *qi::space
- >> identifier [ph::ref(actions.id) = qi::_1]
- >> (*(code_elements - "#]"))
- >> "#]"
+ position
+ >> "#["
+ >> qi::omit[*qi::space]
+ >> identifier
+ >> qi::omit[*(code_elements - "#]")]
+ >> "#]"
             ;
 
         code_elements =
- escaped_comment
+ escaped_comment [actions.process]
             | ignore
- | (qi::char_ - "#]") [ph::bind(&actions_type::pass_thru, &actions, qi::_1)]
+ | (qi::char_ - "#]") [actions.process]
             ;
 
         ignore =
@@ -86,13 +103,18 @@
             ;
 
         escaped_comment =
- *qi::space >> "#`"
- >> ((*(qi::char_ - qi::eol))
- >> qi::eol) [ph::bind(&actions_type::escaped_comment, &actions, as_string(qi::_1))]
- | *qi::space >> "\"\"\"`"
- >> (*(qi::char_ - "\"\"\""))
- [ph::bind(&actions_type::escaped_comment, &actions, as_string(qi::_1))]
- >> "\"\"\""
+ qi::omit[*qi::space >> "#`"]
+ >> (
+ *(qi::char_ - qi::eol)
+ >> qi::eol
+ )
+ >> qi::attr("dummy")
+ | qi::omit[*qi::space >> "\"\"\"`"]
+ >> (
+ *(qi::char_ - "\"\"\"")
+ )
+ >> "\"\"\""
+ >> qi::attr("dummy")
             ;
     }
 
@@ -105,10 +127,15 @@
         actions_type& actions;
 
         qi::rule<iterator>
- start_, snippet, code_elements, escaped_comment,
- inline_callout, line_callout, ignore;
+ start_, code_elements, ignore;
+ qi::rule<iterator, quickbook::code_snippet()>
+ snippet;
         qi::rule<iterator, std::string()>
             identifier;
+ qi::rule<iterator, quickbook::callout()>
+ inline_callout, line_callout;
+ qi::rule<iterator, quickbook::escaped_comment()>
+ escaped_comment;
     };
 
     cpp_code_snippet_grammar::cpp_code_snippet_grammar(actions_type & actions)
@@ -123,7 +150,7 @@
     {
         start_ =
             +(
- qi::raw[snippet] [ph::bind(&actions_type::compile, &actions, qi::_1)]
+ snippet [actions.output]
                 | qi::char_
             )
             ;
@@ -133,37 +160,43 @@
             ;
 
         snippet =
- "//[" >> *qi::space
- >> identifier [ph::ref(actions.id) = qi::_1]
- >> (*(code_elements - "//]"))
- >> "//]"
+ position
+ >> "//["
+ >> qi::omit[*qi::space]
+ >> identifier
+ >> qi::omit[*(code_elements - "//]")]
+ >> "//]"
             |
- "/*[" >> *qi::space
- >> identifier [ph::ref(actions.id) = qi::_1]
- >> *qi::space >> "*/"
- >> (*(code_elements - "/*]*"))
- >> "/*]*/"
+ position
+ >> "/*["
+ >> qi::omit[*qi::space]
+ >> identifier
+ >> qi::omit[*qi::space >> "*/"]
+ >> qi::omit[*(code_elements - "/*]*")]
+ >> "/*]*/"
             ;
 
         code_elements =
- escaped_comment
+ escaped_comment [actions.process]
             | ignore
- | line_callout
- | inline_callout
- | (qi::char_ - "//]" - "/*]*/") [ph::bind(&actions_type::pass_thru, &actions, qi::_1)]
+ | line_callout [actions.process]
+ | inline_callout [actions.process]
+ | (qi::char_ - "//]" - "/*]*/") [actions.process]
             ;
 
         inline_callout =
- "/*<"
- >> (*(qi::char_ - ">*/")) [ph::bind(&actions_type::inline_callout, &actions, as_string(qi::_1))]
- >> ">*/"
+ "/*<"
+ >> *(qi::char_ - ">*/")
+ >> ">*/"
+ >> qi::attr("callout_bug")
             ;
 
         line_callout =
- "/*<<"
- >> (*(qi::char_ - ">>*/")) [ph::bind(&actions_type::line_callout, &actions, as_string(qi::_1))]
- >> ">>*/"
- >> *qi::space
+ "/*<<"
+ >> *(qi::char_ - ">>*/")
+ >> ">>*/"
+ >> qi::omit[*qi::space]
+ >> qi::attr("line_callout_bug")
             ;
 
         ignore =
@@ -179,12 +212,20 @@
             ;
 
         escaped_comment =
- *qi::space >> "//`"
- >> ((*(qi::char_ - qi::eol))
- >> qi::eol) [ph::bind(&actions_type::escaped_comment, &actions, as_string(qi::_1))]
- | *qi::space >> "/*`"
- >> (*(qi::char_ - "*/")) [ph::bind(&actions_type::escaped_comment, &actions, as_string(qi::_1))]
- >> "*/"
+ qi::omit[*qi::space]
+ >> "//`"
+ >> (
+ (*(qi::char_ - qi::eol))
+ >> qi::eol
+ )
+ >> qi::attr("dummy")
+ | qi::omit[*qi::space]
+ >> "/*`"
+ >> (
+ *(qi::char_ - "*/")
+ )
+ >> "*/"
+ >> qi::attr("dummy")
             ;
     }
 }

Modified: branches/quickbook-1.5-spirit2/code_snippet_types.hpp
==============================================================================
--- branches/quickbook-1.5-spirit2/code_snippet_types.hpp (original)
+++ branches/quickbook-1.5-spirit2/code_snippet_types.hpp 2010-01-27 17:05:07 EST (Wed, 27 Jan 2010)
@@ -14,30 +14,91 @@
 
 #include <vector>
 #include <string>
+#include <boost/fusion/include/adapt_struct.hpp>
+#include "./parse_types.hpp"
 #include "./detail/actions.hpp"
 
 namespace quickbook
 {
+ struct code_snippet
+ {
+ file_position position;
+ std::string identifier;
+ };
+
+ struct callout
+ {
+ std::string content;
+ char const* role;
+ };
+
+ struct escaped_comment
+ {
+ std::string content;
+ char const* dummy;
+ };
+}
+
+BOOST_FUSION_ADAPT_STRUCT(
+ quickbook::code_snippet,
+ (quickbook::file_position, position)
+ (std::string, identifier)
+)
+
+BOOST_FUSION_ADAPT_STRUCT(
+ quickbook::callout,
+ (std::string, content)
+ (char const*, role)
+)
+
+BOOST_FUSION_ADAPT_STRUCT(
+ quickbook::escaped_comment,
+ (std::string, content)
+ (char const*, dummy)
+)
+
+namespace quickbook
+{
+ using boost::spirit::unused_type;
+
     struct code_snippet_actions
     {
         code_snippet_actions(std::vector<template_symbol>& storage,
                                  std::string const& doc_id,
                                  char const* source_type)
- : storage(storage)
+ : process(*this)
+ , output(*this)
+ , storage(storage)
             , doc_id(doc_id)
             , source_type(source_type)
         {}
 
- void pass_thru(char);
- void escaped_comment(std::string const&);
- void compile(boost::iterator_range<iterator>);
- void callout(std::string const&, char const* role);
- void inline_callout(std::string const&);
- void line_callout(std::string const&);
+ struct process_action
+ {
+ explicit process_action(code_snippet_actions& a)
+ : actions(a) {}
+
+ void operator()(char x, unused_type, unused_type) const;
+ void operator()(callout const& x, unused_type, unused_type) const;
+ void operator()(escaped_comment const& x, unused_type, unused_type) const;
+
+ code_snippet_actions& actions;
+ };
+
+ struct output_action
+ {
+ explicit output_action(code_snippet_actions& a)
+ : actions(a) {}
+
+ void operator()(code_snippet const& x, unused_type, unused_type) const;
+
+ code_snippet_actions& actions;
+ };
 
+ process_action process;
+ output_action output;
         std::string code;
         std::string snippet;
- std::string id;
         std::vector<std::string> callouts;
         std::vector<template_symbol>& storage;
         std::string const doc_id;


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