$include_dir="/home/hyper-archives/boost-commit/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-commit] svn:boost r53021 - in trunk/tools/quickbook: . detail
From: daniel_james_at_[hidden]
Date: 2009-05-15 02:13:18
Author: danieljames
Date: 2009-05-15 02:13:17 EDT (Fri, 15 May 2009)
New Revision: 53021
URL: http://svn.boost.org/trac/boost/changeset/53021
Log:
Separate code snippet grammar and actions.
Text files modified: 
   trunk/tools/quickbook/code_snippet.hpp   |   115 +++++++++++++++++++++++++-------------- 
   trunk/tools/quickbook/detail/actions.cpp |    21 ++++--                                  
   2 files changed, 85 insertions(+), 51 deletions(-)
Modified: trunk/tools/quickbook/code_snippet.hpp
==============================================================================
--- trunk/tools/quickbook/code_snippet.hpp	(original)
+++ trunk/tools/quickbook/code_snippet.hpp	2009-05-15 02:13:17 EDT (Fri, 15 May 2009)
@@ -15,10 +15,9 @@
 
 namespace quickbook
 {
-  struct code_snippet_grammar
-      : grammar<code_snippet_grammar>
-  {
-        code_snippet_grammar(std::vector<template_symbol>& storage,
+    struct code_snippet_actions
+    {
+        code_snippet_actions(std::vector<template_symbol>& storage,
                                  std::string const& doc_id,
                                  bool is_python)
             : storage(storage)
@@ -26,24 +25,43 @@
             , is_python(is_python)
         {}
 
+        void pass_thru(iterator first, iterator last);
+        void escaped_comment(iterator first, iterator last);
+        void compile(iterator first, iterator last);
+        void callout(iterator first, iterator last, char const* role);
+        void inline_callout(iterator first, iterator last);
+        void line_callout(iterator first, iterator last);
+
+        std::string code;
+        std::string snippet;
+        std::string id;
+        std::vector<std::string> callouts;
+        std::vector<template_symbol>& storage;
+        std::string const doc_id;
+        bool const is_python;
+    };
+
+    struct python_code_snippet_grammar
+        : grammar<python_code_snippet_grammar>
+    {
+        typedef code_snippet_actions actions_type;
+  
+        python_code_snippet_grammar(actions_type & actions)
+            : actions(actions)
+        {}
+
         template <typename Scanner>
         struct definition
         {
-            typedef code_snippet_grammar self_type;
+            typedef code_snippet_actions actions_type;
             
-            definition(self_type const& self)
+            definition(python_code_snippet_grammar const& self)
             {
-                self.is_python
-                    ? definition_python(self)
-                    : definition_cpp(self)
-                    ;
-            }
+                actions_type& actions = self.actions;
             
-            void definition_python(self_type const& self)
-            {
                 start_ =
                     +(
-                            snippet                 [boost::bind(&self_type::compile, &self, _1, _2)]
+                            snippet                 [boost::bind(&actions_type::compile, &actions, _1, _2)]
                         |   anychar_p
                     )
                     ;
@@ -54,7 +72,7 @@
 
                 snippet =
                     "#[" >> *space_p
-                    >> identifier                   [assign_a(self.id)]
+                    >> identifier                   [assign_a(actions.id)]
                     >> (*(code_elements - "#]"))
                     >> "#]"
                     ;
@@ -62,7 +80,7 @@
                 code_elements =
                         escaped_comment
                     |   ignore
-                    |   (anychar_p - "#]")         [boost::bind(&self_type::pass_thru, &self, _1, _2)]
+                    |   (anychar_p - "#]")         [boost::bind(&actions_type::pass_thru, &actions, _1, _2)]
                     ;
 
                 ignore =
@@ -80,18 +98,43 @@
                 escaped_comment =
                         *space_p >> "#`"
                         >> ((*(anychar_p - eol_p))
-                            >> eol_p)               [boost::bind(&self_type::escaped_comment, &self, _1, _2)]
+                            >> eol_p)               [boost::bind(&actions_type::escaped_comment, &actions, _1, _2)]
                     |   *space_p >> "\"\"\"`"
-                        >> (*(anychar_p - "\"\"\""))    [boost::bind(&self_type::escaped_comment, &self, _1, _2)]
+                        >> (*(anychar_p - "\"\"\""))    [boost::bind(&actions_type::escaped_comment, &actions, _1, _2)]
                         >> "\"\"\""
                     ;
             }
-            
-            void definition_cpp(self_type const& self)
+
+            rule<Scanner>
+                start_, snippet, identifier, code_elements, escaped_comment,
+                inline_callout, line_callout, ignore;
+
+            rule<Scanner> const&
+            start() const { return start_; }
+        };
+
+        actions_type& actions;
+    };  
+
+    struct cpp_code_snippet_grammar
+        : grammar<cpp_code_snippet_grammar>
+    {
+        typedef code_snippet_actions actions_type;
+  
+        cpp_code_snippet_grammar(actions_type & actions)
+            : actions(actions)
+        {}
+
+        template <typename Scanner>
+        struct definition
+        {
+            definition(cpp_code_snippet_grammar const& self)
             {
+                actions_type& actions = self.actions;
+            
                 start_ =
                     +(
-                            snippet                 [boost::bind(&self_type::compile, &self, _1, _2)]
+                            snippet                 [boost::bind(&actions_type::compile, &actions, _1, _2)]
                         |   anychar_p
                     )
                     ;
@@ -102,12 +145,12 @@
 
                 snippet =
                         "//[" >> *space_p
-                        >> identifier                   [assign_a(self.id)]
+                        >> identifier                   [assign_a(actions.id)]
                         >> (*(code_elements - "//]"))
                         >> "//]"
                     |
                         "/*[" >> *space_p
-                        >> identifier                   [assign_a(self.id)]
+                        >> identifier                   [assign_a(actions.id)]
                         >> *space_p >> "*/"
                         >> (*(code_elements - "/*]*"))
                         >> "/*]*/"
@@ -118,18 +161,18 @@
                     |   ignore
                     |   line_callout
                     |   inline_callout
-                    |   (anychar_p - "//]" - "/*]*/")    [boost::bind(&self_type::pass_thru, &self, _1, _2)]
+                    |   (anychar_p - "//]" - "/*]*/")    [boost::bind(&actions_type::pass_thru, &actions, _1, _2)]
                     ;
 
                 inline_callout =
                     "/*<"
-                    >> (*(anychar_p - ">*/"))       [boost::bind(&self_type::inline_callout, &self, _1, _2)]
+                    >> (*(anychar_p - ">*/"))       [boost::bind(&actions_type::inline_callout, &actions, _1, _2)]
                     >> ">*/"
                     ;
 
                 line_callout =
                     "/*<<"
-                    >> (*(anychar_p - ">>*/"))      [boost::bind(&self_type::line_callout, &self, _1, _2)]
+                    >> (*(anychar_p - ">>*/"))      [boost::bind(&actions_type::line_callout, &actions, _1, _2)]
                     >> ">>*/"
                     >> *space_p
                     ;
@@ -149,9 +192,9 @@
                 escaped_comment =
                         *space_p >> "//`"
                         >> ((*(anychar_p - eol_p))
-                            >> eol_p)               [boost::bind(&self_type::escaped_comment, &self, _1, _2)]
+                            >> eol_p)               [boost::bind(&actions_type::escaped_comment, &actions, _1, _2)]
                     |   *space_p >> "/*`"
-                        >> (*(anychar_p - "*/"))    [boost::bind(&self_type::escaped_comment, &self, _1, _2)]
+                        >> (*(anychar_p - "*/"))    [boost::bind(&actions_type::escaped_comment, &actions, _1, _2)]
                         >> "*/"
                     ;
             }
@@ -164,22 +207,8 @@
             start() const { return start_; }
         };
 
-        void pass_thru(iterator first, iterator last) const;
-        void escaped_comment(iterator first, iterator last) const;
-        void compile(iterator first, iterator last) const;
-        void callout(iterator first, iterator last, char const* role) const;
-        void inline_callout(iterator first, iterator last) const;
-        void line_callout(iterator first, iterator last) const;
-
-        mutable std::string code;
-        mutable std::string snippet;
-        mutable std::string id;
-        mutable std::vector<std::string> callouts;
-        std::vector<template_symbol>& storage;
-        std::string doc_id;
-        bool is_python;
+        actions_type& actions;
     };
-  
 }
 
 #endif // BOOST_SPIRIT_QUICKBOOK_CODE_SNIPPET_HPP
Modified: trunk/tools/quickbook/detail/actions.cpp
==============================================================================
--- trunk/tools/quickbook/detail/actions.cpp	(original)
+++ trunk/tools/quickbook/detail/actions.cpp	2009-05-15 02:13:17 EDT (Fri, 15 May 2009)
@@ -968,7 +968,7 @@
         out << "\" />\n";
     }
 
-    void code_snippet_grammar::pass_thru(iterator first, iterator last) const
+    void code_snippet_actions::pass_thru(iterator first, iterator last)
     {
         code += *first;
     }
@@ -978,7 +978,7 @@
         int callout_id = 0;
     }
 
-    void code_snippet_grammar::callout(iterator first, iterator last, char const* role) const
+    void code_snippet_actions::callout(iterator first, iterator last, char const* role)
     {
         using detail::callout_id;
         code += "``'''";
@@ -993,17 +993,17 @@
         callouts.push_back(std::string(first, last));
     }
 
-    void code_snippet_grammar::inline_callout(iterator first, iterator last) const
+    void code_snippet_actions::inline_callout(iterator first, iterator last)
     {
         callout(first, last, "callout_bug");
     }
 
-    void code_snippet_grammar::line_callout(iterator first, iterator last) const
+    void code_snippet_actions::line_callout(iterator first, iterator last)
     {
         callout(first, last, "line_callout_bug");
     }
 
-    void code_snippet_grammar::escaped_comment(iterator first, iterator last) const
+    void code_snippet_actions::escaped_comment(iterator first, iterator last)
     {
         if (!code.empty())
         {
@@ -1024,7 +1024,7 @@
         }
     }
 
-    void code_snippet_grammar::compile(iterator first, iterator last) const
+    void code_snippet_actions::compile(iterator first, iterator last)
     {
         using detail::callout_id;
         if (!code.empty())
@@ -1088,9 +1088,14 @@
         size_t fname_len = file.size();
         bool is_python = fname_len >= 3
             && file[--fname_len]=='y' && file[--fname_len]=='p' && file[--fname_len]=='.';
-        code_snippet_grammar g(storage, doc_id, is_python);
+        code_snippet_actions a(storage, doc_id, is_python);
         // TODO: Should I check that parse succeeded?
-        boost::spirit::classic::parse(first, last, g);
+        if(is_python) {
+            boost::spirit::classic::parse(first, last, python_code_snippet_grammar(a));
+        }
+        else {
+            boost::spirit::classic::parse(first, last, cpp_code_snippet_grammar(a));
+        }
 
         return 0;
     }