Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r65285 - branches/quickbook-1.5-spirit2/src
From: dnljms_at_[hidden]
Date: 2010-09-05 05:33:07


Author: danieljames
Date: 2010-09-05 05:33:05 EDT (Sun, 05 Sep 2010)
New Revision: 65285
URL: http://svn.boost.org/trac/boost/changeset/65285

Log:
More self contained code snippet grammar.
Removed:
   branches/quickbook-1.5-spirit2/src/code_snippet_grammar.hpp
Text files modified:
   branches/quickbook-1.5-spirit2/src/code_snippet_actions.cpp | 131 ++++++++++++++++++++++++++++++---------
   branches/quickbook-1.5-spirit2/src/code_snippet_grammar.cpp | 102 ++++++++++++++++--------------
   branches/quickbook-1.5-spirit2/src/code_snippet_types.hpp | 79 +++++------------------
   3 files changed, 172 insertions(+), 140 deletions(-)

Modified: branches/quickbook-1.5-spirit2/src/code_snippet_actions.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/src/code_snippet_actions.cpp (original)
+++ branches/quickbook-1.5-spirit2/src/code_snippet_actions.cpp 2010-09-05 05:33:05 EDT (Sun, 05 Sep 2010)
@@ -14,19 +14,85 @@
 #include <functional>
 #include <algorithm>
 #include <iterator>
+#include <stack>
 #include <boost/lexical_cast.hpp>
 #include "utils.hpp"
 #include "grammar.hpp"
 #include "code_snippet_types.hpp"
 #include "template.hpp"
 #include "quickbook.hpp"
+#include "utils.hpp"
 
 namespace quickbook
 {
- void code_snippet_actions::append_code()
+ struct code_snippet_state
+ {
+ code_snippet_state(std::vector<define_template>& storage,
+ std::string const& doc_id,
+ char const* source_type)
+ : storage(storage)
+ , doc_id(doc_id)
+ , source_type(source_type)
+ {}
+
+ void append_code();
+ void close_code();
+
+ struct snippet_data
+ {
+ snippet_data(std::string const& id, int callout_base_id, file_position position)
+ : id(id)
+ , callout_base_id(callout_base_id)
+ , position(position)
+ , content()
+ , start_code(false)
+ , end_code(false)
+ {}
+
+ std::string id;
+ int callout_base_id;
+ file_position position;
+ std::string content;
+ bool start_code;
+ bool end_code;
+ quickbook::callouts callouts;
+ };
+
+ int callout_id;
+ std::stack<snippet_data> snippet_stack;
+ std::string code;
+ std::vector<define_template>& storage;
+ std::string const doc_id;
+ char const* const source_type;
+ };
+
+ int load_snippets(
+ std::string const& file
+ , std::vector<define_template>& storage // for storing snippets are stored in a
+ // vector of define_templates
+ , std::string const& extension
+ , std::string const& doc_id)
+ {
+ std::string code;
+ int err = detail::load(file, code);
+ if (err != 0)
+ return err; // return early on error
+
+ iterator first(code.begin(), code.end(), file.c_str());
+ iterator last(code.end(), code.end());
+
+ bool is_python = extension == ".py";
+ code_snippet_state state(storage, doc_id, is_python ? "[python]" : "[c++]");
+ snippet_actions actions(state);
+ load_code_snippets(actions, storage, is_python, first, last);
+
+ return 0;
+ }
+
+ void code_snippet_state::append_code()
     {
         if(snippet_stack.empty()) return;
- snippet_data& snippet = snippet_stack.top();
+ code_snippet_state::snippet_data& snippet = snippet_stack.top();
 
         if (!code.empty())
         {
@@ -50,10 +116,10 @@
         }
     }
 
- void code_snippet_actions::close_code()
+ void code_snippet_state::close_code()
     {
         if(snippet_stack.empty()) return;
- snippet_data& snippet = snippet_stack.top();
+ code_snippet_state::snippet_data& snippet = snippet_stack.top();
 
         if(snippet.end_code)
         {
@@ -62,30 +128,33 @@
         }
     }
 
- void code_snippet_actions::process_action::operator()(char x, unused_type, unused_type) const
+ snippet_actions::snippet_actions(code_snippet_state& a)
+ : state(a) {}
+
+ void snippet_actions::operator()(char x, unused_type, unused_type) const
     {
- if(actions.snippet_stack.empty()) return;
- actions.code += x;
+ if(state.snippet_stack.empty()) return;
+ state.code += x;
     }
 
- void code_snippet_actions::process_action::operator()(callout const& x, unused_type, unused_type) const
+ void snippet_actions::operator()(callout const& x, unused_type, unused_type) const
     {
- if(actions.snippet_stack.empty()) return;
- actions.code += "``[[callout" + boost::lexical_cast<std::string>(actions.callout_id) + "]]``";
+ if(state.snippet_stack.empty()) return;
+ state.code += "``[[callout" + boost::lexical_cast<std::string>(state.callout_id) + "]]``";
      
         callout_source item;
         item.body = template_body(x.content, x.position, true);
         item.role = x.role;
- actions.snippet_stack.top().callouts.push_back(item);
- ++actions.callout_id;
+ state.snippet_stack.top().callouts.push_back(item);
+ ++state.callout_id;
     }
 
- void code_snippet_actions::process_action::operator()(escaped_comment const& x, unused_type, unused_type) const
+ void snippet_actions::operator()(escaped_comment const& x, unused_type, unused_type) const
     {
- if(actions.snippet_stack.empty()) return;
- snippet_data& snippet = actions.snippet_stack.top();
- actions.append_code();
- actions.close_code();
+ if(state.snippet_stack.empty()) return;
+ code_snippet_state::snippet_data& snippet = state.snippet_stack.top();
+ state.append_code();
+ state.close_code();
         
         std::string temp(x.content);
         detail::unindent(temp); // remove all indents
@@ -95,26 +164,26 @@
         }
     }
 
- void code_snippet_actions::process_action::operator()(start_snippet const& x, unused_type, unused_type) const
+ void snippet_actions::operator()(start_snippet const& x, unused_type, unused_type) const
     {
- actions.append_code();
- actions.snippet_stack.push(snippet_data(x.identifier, actions.callout_id, x.position));
+ state.append_code();
+ state.snippet_stack.push(code_snippet_state::snippet_data(x.identifier, state.callout_id, x.position));
     }
 
- void code_snippet_actions::process_action::operator()(end_snippet const& x, unused_type, unused_type) const
+ void snippet_actions::operator()(end_snippet const& x, unused_type, unused_type) const
     {
         // TODO: Error?
- if(actions.snippet_stack.empty()) return;
+ if(state.snippet_stack.empty()) return;
 
- actions.append_code();
+ state.append_code();
 
- snippet_data snippet = actions.snippet_stack.top();
- actions.snippet_stack.pop();
+ code_snippet_state::snippet_data snippet = state.snippet_stack.top();
+ state.snippet_stack.pop();
 
         std::string body;
         if(snippet.start_code) {
             body += "\n\n";
- body += actions.source_type;
+ body += state.source_type;
             body += "```\n";
         }
         body += snippet.content;
@@ -130,20 +199,20 @@
 
         define_template d(snippet.id, params, template_body(body, snippet.position, true));
         d.callouts = snippet.callouts;
- actions.storage.push_back(d);
+ state.storage.push_back(d);
 
         // Merge the snippet into its parent
 
- if(!actions.snippet_stack.empty())
+ if(!state.snippet_stack.empty())
         {
- snippet_data& next = actions.snippet_stack.top();
+ code_snippet_state::snippet_data& next = state.snippet_stack.top();
             if(!snippet.content.empty()) {
                 if(!snippet.start_code) {
- actions.close_code();
+ state.close_code();
                 }
                 else if(!next.end_code) {
                     next.content += "\n\n";
- next.content += actions.source_type;
+ next.content += state.source_type;
                     next.content += "```\n";
                 }
                 

Modified: branches/quickbook-1.5-spirit2/src/code_snippet_grammar.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/src/code_snippet_grammar.cpp (original)
+++ branches/quickbook-1.5-spirit2/src/code_snippet_grammar.cpp 2010-09-05 05:33:05 EDT (Sun, 05 Sep 2010)
@@ -1,5 +1,6 @@
 /*=============================================================================
     Copyright (c) 2006 Joel de Guzman
+ Copyright (c) 2010 Daniel James
     http://spirit.sourceforge.net/
 
     Use, modification and distribution is subject to the Boost Software
@@ -12,12 +13,11 @@
 #include <boost/spirit/include/qi_eps.hpp>
 #include <boost/spirit/include/qi_attr.hpp>
 #include <boost/spirit/repository/include/qi_confix.hpp>
+#include <boost/scoped_ptr.hpp>
 #include "fwd.hpp"
 #include "code_snippet_types.hpp"
-#include "code_snippet_grammar.hpp"
 #include "misc_rules.hpp"
 #include "parse_utils.hpp"
-#include "utils.hpp" // For 'detail::load', rewrite so it isn't used here?
 
 namespace quickbook
 {
@@ -26,9 +26,41 @@
     namespace qi = boost::spirit::qi;
     namespace repo = boost::spirit::repository;
 
+ struct python_code_snippet_grammar
+ : qi::grammar<iterator>
+ {
+ typedef snippet_actions actions_type;
+
+ python_code_snippet_grammar(actions_type& actions);
+ ~python_code_snippet_grammar();
+
+ struct rules;
+ boost::scoped_ptr<rules> rules_pimpl;
+ qi::rule<iterator> start;
+ private:
+ python_code_snippet_grammar(python_code_snippet_grammar const&);
+ python_code_snippet_grammar& operator=(python_code_snippet_grammar const&);
+ };
+
+ struct cpp_code_snippet_grammar
+ : qi::grammar<iterator>
+ {
+ typedef snippet_actions actions_type;
+
+ cpp_code_snippet_grammar(actions_type& actions);
+ ~cpp_code_snippet_grammar();
+
+ struct rules;
+ boost::scoped_ptr<rules> rules_pimpl;
+ qi::rule<iterator> start;
+ private:
+ cpp_code_snippet_grammar(cpp_code_snippet_grammar const&);
+ cpp_code_snippet_grammar& operator=(cpp_code_snippet_grammar const&);
+ };
+
     struct python_code_snippet_grammar::rules
     {
- typedef code_snippet_actions actions_type;
+ typedef snippet_actions actions_type;
   
         rules(actions_type & actions);
 
@@ -64,24 +96,13 @@
             (qi::alpha | '_') >> *(qi::alnum | '_')
             ;
 
- /*
- snippet =
- position [member_assign(&quickbook::code_snippet::position)]
- >> repo::confix("#[", "#]")
- [ *qi::space
- >> identifier [member_assign(&quickbook::code_snippet::identifier)]
- >> *(!qi::lit("#]") >> code_elements)
- ]
- ;
- */
-
         code_elements =
- start_snippet [actions.process]
- | end_snippet [actions.process]
- | escaped_comment [actions.process]
+ start_snippet [actions]
+ | end_snippet [actions]
+ | escaped_comment [actions]
             | ignore
- | +qi::blank [actions.process]
- | qi::char_ [actions.process]
+ | +qi::blank [actions]
+ | qi::char_ [actions]
             ;
 
         start_snippet =
@@ -116,7 +137,7 @@
 
     struct cpp_code_snippet_grammar::rules
     {
- typedef code_snippet_actions actions_type;
+ typedef snippet_actions actions_type;
   
         rules(actions_type & actions);
 
@@ -153,14 +174,14 @@
             ;
 
         code_elements =
- start_snippet [actions.process]
- | end_snippet [actions.process]
- | escaped_comment [actions.process]
+ start_snippet [actions]
+ | end_snippet [actions]
+ | escaped_comment [actions]
             | ignore
- | line_callout [actions.process]
- | inline_callout [actions.process]
- | +qi::blank [actions.process]
- | qi::char_ [actions.process]
+ | line_callout [actions]
+ | inline_callout [actions]
+ | +qi::blank [actions]
+ | qi::char_ [actions]
             ;
 
         start_snippet
@@ -216,35 +237,22 @@
             ;
     }
 
- int load_snippets(
- std::string const& file
+ void load_code_snippets(
+ snippet_actions& actions
       , std::vector<define_template>& storage // for storing snippets are stored in a
                                                 // vector of define_templates
- , std::string const& extension
- , std::string const& doc_id)
+ , bool is_python
+ , iterator& first
+ , iterator last)
     {
- std::string code;
- int err = detail::load(file, code);
- if (err != 0)
- return err; // return early on error
-
- iterator first(code.begin(), code.end(), file.c_str());
- iterator last(code.end(), code.end());
-
- 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_actions a(storage, doc_id, is_python ? "[python]" : "[c++]");
         // TODO: Should I check that parse succeeded?
         if(is_python) {
- python_code_snippet_grammar g(a);
+ python_code_snippet_grammar g(actions);
             boost::spirit::qi::parse(first, last, g);
         }
         else {
- cpp_code_snippet_grammar g(a);
+ cpp_code_snippet_grammar g(actions);
             boost::spirit::qi::parse(first, last, g);
         }
-
- return 0;
     }
 }

Deleted: branches/quickbook-1.5-spirit2/src/code_snippet_grammar.hpp
==============================================================================
--- branches/quickbook-1.5-spirit2/src/code_snippet_grammar.hpp 2010-09-05 05:33:05 EDT (Sun, 05 Sep 2010)
+++ (empty file)
@@ -1,55 +0,0 @@
-/*=============================================================================
- Copyright (c) 2002 2004 2006 Joel de Guzman
- Copyright (c) 2004 Eric Niebler
- Copyright (c) 2010 Daniel James
- http://spirit.sourceforge.net/
-
- Use, modification and distribution is subject to 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)
-=============================================================================*/
-#if !defined(BOOST_SPIRIT_QUICKBOOK_CODE_SNIPPET_GRAMMARS_HPP)
-#define BOOST_SPIRIT_QUICKBOOK_CODE_SNIPPET_GRAMMARS_HPP
-
-#include <boost/spirit/include/qi_core.hpp>
-#include <boost/scoped_ptr.hpp>
-#include "fwd.hpp"
-
-namespace quickbook
-{
- namespace qi = boost::spirit::qi;
-
- struct python_code_snippet_grammar
- : qi::grammar<iterator>
- {
- typedef code_snippet_actions actions_type;
-
- python_code_snippet_grammar(actions_type& actions);
- ~python_code_snippet_grammar();
-
- struct rules;
- boost::scoped_ptr<rules> rules_pimpl;
- qi::rule<iterator> start;
- private:
- python_code_snippet_grammar(python_code_snippet_grammar const&);
- python_code_snippet_grammar& operator=(python_code_snippet_grammar const&);
- };
-
- struct cpp_code_snippet_grammar
- : qi::grammar<iterator>
- {
- typedef code_snippet_actions actions_type;
-
- cpp_code_snippet_grammar(actions_type& actions);
- ~cpp_code_snippet_grammar();
-
- struct rules;
- boost::scoped_ptr<rules> rules_pimpl;
- qi::rule<iterator> start;
- private:
- cpp_code_snippet_grammar(cpp_code_snippet_grammar const&);
- cpp_code_snippet_grammar& operator=(cpp_code_snippet_grammar const&);
- };
-}
-
-#endif // BOOST_SPIRIT_QUICKBOOK_CODE_SNIPPET_GRAMMARS_HPP

Modified: branches/quickbook-1.5-spirit2/src/code_snippet_types.hpp
==============================================================================
--- branches/quickbook-1.5-spirit2/src/code_snippet_types.hpp (original)
+++ branches/quickbook-1.5-spirit2/src/code_snippet_types.hpp 2010-09-05 05:33:05 EDT (Sun, 05 Sep 2010)
@@ -14,14 +14,16 @@
 
 #include <vector>
 #include <string>
-#include <stack>
 #include <boost/spirit/include/support_unused.hpp>
 #include "fwd.hpp"
 #include "template.hpp"
-#include "strings.hpp"
 
 namespace quickbook
 {
+ using boost::spirit::unused_type;
+
+ struct code_snippet_state;
+
     struct start_snippet
     {
         file_position position;
@@ -43,69 +45,22 @@
     {
         std::string content;
     };
-}
 
-namespace quickbook
-{
- using boost::spirit::unused_type;
-
- struct code_snippet_actions
+ struct snippet_actions
     {
- code_snippet_actions(std::vector<define_template>& storage,
- std::string const& doc_id,
- char const* source_type)
- : process(*this)
- , storage(storage)
- , doc_id(doc_id)
- , source_type(source_type)
- {}
-
- 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;
- void operator()(start_snippet const& x, unused_type, unused_type) const;
- void operator()(end_snippet const& x, unused_type, unused_type) const;
-
- code_snippet_actions& actions;
- };
-
- void append_code();
- void close_code();
-
- struct snippet_data
- {
- snippet_data(std::string const& id, int callout_base_id, file_position position)
- : id(id)
- , callout_base_id(callout_base_id)
- , position(position)
- , content()
- , start_code(false)
- , end_code(false)
- {}
-
- std::string id;
- int callout_base_id;
- file_position position;
- std::string content;
- bool start_code;
- bool end_code;
- quickbook::callouts callouts;
- };
-
- process_action process;
-
- int callout_id;
- std::stack<snippet_data> snippet_stack;
- std::string code;
- std::vector<define_template>& storage;
- std::string const doc_id;
- char const* const source_type;
+ explicit snippet_actions(code_snippet_state&);
+
+ 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;
+ void operator()(start_snippet const& x, unused_type, unused_type) const;
+ void operator()(end_snippet const& x, unused_type, unused_type) const;
+
+ code_snippet_state& state;
     };
+
+ void load_code_snippets(snippet_actions&, std::vector<define_template>&,
+ bool is_python, iterator&, iterator last);
 }
 
 #endif
\ No newline at end of file


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