Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r53018 - in trunk/tools/quickbook: . detail
From: daniel_james_at_[hidden]
Date: 2009-05-15 02:12:14


Author: danieljames
Date: 2009-05-15 02:12:13 EDT (Fri, 15 May 2009)
New Revision: 53018
URL: http://svn.boost.org/trac/boost/changeset/53018

Log:
Import Python and C code snippets in quickbook, by Jaroslav Gresula. Refs #3029.
Text files modified:
   trunk/tools/quickbook/code_snippet.hpp | 88 +++++++++++++++++++++++++++++++++++----
   trunk/tools/quickbook/detail/actions.cpp | 16 ++++---
   2 files changed, 87 insertions(+), 17 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:12:13 EDT (Fri, 15 May 2009)
@@ -15,20 +15,32 @@
 
 namespace quickbook
 {
- struct cpp_code_snippet_grammar
- : grammar<cpp_code_snippet_grammar>
- {
- cpp_code_snippet_grammar(std::vector<template_symbol>& storage, std::string const& doc_id)
+ struct code_snippet_grammar
+ : grammar<code_snippet_grammar>
+ {
+ code_snippet_grammar(std::vector<template_symbol>& storage,
+ std::string const& doc_id,
+ bool is_python)
             : storage(storage)
             , doc_id(doc_id)
+ , is_python(is_python)
         {}
 
         template <typename Scanner>
         struct definition
         {
- definition(cpp_code_snippet_grammar const& self)
+ typedef code_snippet_grammar self_type;
+
+ definition(self_type const& self)
+ {
+ self.is_python
+ ? definition_python(self)
+ : definition_cpp(self)
+ ;
+ }
+
+ void definition_python(self_type const& self)
             {
- typedef cpp_code_snippet_grammar self_type;
                 start_ =
                     +(
                             snippet [boost::bind(&self_type::compile, &self, _1, _2)]
@@ -41,10 +53,64 @@
                     ;
 
                 snippet =
- "//[" >> *space_p
+ "#[" >> *space_p
>> identifier [assign_a(self.id)]
- >> (*(code_elements - "//]"))
- >> "//]"
+ >> (*(code_elements - "#]"))
+ >> "#]"
+ ;
+
+ code_elements =
+ escaped_comment
+ | ignore
+ | (anychar_p - "#]") [boost::bind(&self_type::pass_thru, &self, _1, _2)]
+ ;
+
+ ignore =
+ *blank_p >> "#<-"
+ >> (*(anychar_p - "#->"))
+ >> "#->" >> *blank_p >> eol_p
+ | "\"\"\"<-\"\"\""
+ >> (*(anychar_p - "\"\"\"->\"\"\""))
+ >> "\"\"\"->\"\"\""
+ | "\"\"\"<-"
+ >> (*(anychar_p - "->\"\"\""))
+ >> "->\"\"\""
+ ;
+
+ escaped_comment =
+ *space_p >> "#`"
+ >> ((*(anychar_p - eol_p))
+ >> eol_p) [boost::bind(&self_type::escaped_comment, &self, _1, _2)]
+ | *space_p >> "\"\"\"`"
+ >> (*(anychar_p - "\"\"\"")) [boost::bind(&self_type::escaped_comment, &self, _1, _2)]
+ >> "\"\"\""
+ ;
+ }
+
+ void definition_cpp(self_type const& self)
+ {
+ start_ =
+ +(
+ snippet [boost::bind(&self_type::compile, &self, _1, _2)]
+ | anychar_p
+ )
+ ;
+
+ identifier =
+ (alpha_p | '_') >> *(alnum_p | '_')
+ ;
+
+ snippet =
+ "//[" >> *space_p
+ >> identifier [assign_a(self.id)]
+ >> (*(code_elements - "//]"))
+ >> "//]"
+ |
+ "/*[" >> *space_p
+ >> identifier [assign_a(self.id)]
+ >> *space_p >> "*/"
+ >> (*(code_elements - "/*]*"))
+ >> "/*]*/"
                     ;
 
                 code_elements =
@@ -52,7 +118,7 @@
                     | ignore
                     | line_callout
                     | inline_callout
- | (anychar_p - "//]") [boost::bind(&self_type::pass_thru, &self, _1, _2)]
+ | (anychar_p - "//]" - "/*]*/") [boost::bind(&self_type::pass_thru, &self, _1, _2)]
                     ;
 
                 inline_callout =
@@ -111,7 +177,9 @@
         mutable std::vector<std::string> callouts;
         std::vector<template_symbol>& storage;
         std::string doc_id;
+ bool is_python;
     };
+
 }
 
 #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:12:13 EDT (Fri, 15 May 2009)
@@ -968,7 +968,7 @@
         out << "\" />\n";
     }
 
- void cpp_code_snippet_grammar::pass_thru(iterator first, iterator last) const
+ void code_snippet_grammar::pass_thru(iterator first, iterator last) const
     {
         code += *first;
     }
@@ -978,7 +978,7 @@
         int callout_id = 0;
     }
 
- void cpp_code_snippet_grammar::callout(iterator first, iterator last, char const* role) const
+ void code_snippet_grammar::callout(iterator first, iterator last, char const* role) const
     {
         using detail::callout_id;
         code += "``'''";
@@ -993,17 +993,17 @@
         callouts.push_back(std::string(first, last));
     }
 
- void cpp_code_snippet_grammar::inline_callout(iterator first, iterator last) const
+ void code_snippet_grammar::inline_callout(iterator first, iterator last) const
     {
         callout(first, last, "callout_bug");
     }
 
- void cpp_code_snippet_grammar::line_callout(iterator first, iterator last) const
+ void code_snippet_grammar::line_callout(iterator first, iterator last) const
     {
         callout(first, last, "line_callout_bug");
     }
 
- void cpp_code_snippet_grammar::escaped_comment(iterator first, iterator last) const
+ void code_snippet_grammar::escaped_comment(iterator first, iterator last) const
     {
         if (!code.empty())
         {
@@ -1022,7 +1022,7 @@
         }
     }
 
- void cpp_code_snippet_grammar::compile(iterator first, iterator last) const
+ void code_snippet_grammar::compile(iterator first, iterator last) const
     {
         using detail::callout_id;
         if (!code.empty())
@@ -1081,7 +1081,9 @@
         iterator_type first(code.begin(), code.end(), file);
         iterator_type last(code.end(), code.end());
 
- cpp_code_snippet_grammar g(storage, doc_id);
+ size_t fname_len = file.size();
+ bool is_python = file[--fname_len]=='y' && file[--fname_len]=='p';
+ code_snippet_grammar g(storage, doc_id, is_python);
         // TODO: Should I check that parse succeeded?
         boost::spirit::classic::parse(first, last, g);
 


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