Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r60137 - branches/quickbook-1.5-spirit2
From: daniel_james_at_[hidden]
Date: 2010-03-03 18:30:48


Author: danieljames
Date: 2010-03-03 18:30:47 EST (Wed, 03 Mar 2010)
New Revision: 60137
URL: http://svn.boost.org/trac/boost/changeset/60137

Log:
Add a class to store rules withot declaring them in the class' declarartion.
Added:
   branches/quickbook-1.5-spirit2/rule_store.hpp (contents, props changed)
Text files modified:
   branches/quickbook-1.5-spirit2/block_grammar.cpp | 101 +++++++++++++++++++++++----------------
   branches/quickbook-1.5-spirit2/fwd.hpp | 2
   branches/quickbook-1.5-spirit2/phrase_grammar.cpp | 85 +++++++++++++++++++--------------
   3 files changed, 109 insertions(+), 79 deletions(-)

Modified: branches/quickbook-1.5-spirit2/block_grammar.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/block_grammar.cpp (original)
+++ branches/quickbook-1.5-spirit2/block_grammar.cpp 2010-03-03 18:30:47 EST (Wed, 03 Mar 2010)
@@ -25,6 +25,7 @@
 #include "parse_utils.hpp"
 #include "code.hpp"
 #include "misc_rules.hpp"
+#include "rule_store.hpp"
 
 BOOST_FUSION_ADAPT_STRUCT(
     quickbook::paragraph,
@@ -123,48 +124,8 @@
         bool no_eols;
         phrase_grammar common;
         
- qi::rule<iterator> start_, blocks, block_markup;
- qi::rule<iterator, quickbook::begin_section()> begin_section;
- qi::rule<iterator, quickbook::end_section()> end_section;
- qi::rule<iterator, quickbook::heading()> heading;
- qi::symbols<char, int> heading_symbol;
- qi::rule<iterator, quickbook::formatted()> paragraph_block, blockquote, preformatted;
- qi::symbols<char, quickbook::formatted_type> paragraph_blocks;
- qi::rule<iterator, quickbook::def_macro()> def_macro;
- qi::rule<iterator, quickbook::table()> table;
- qi::rule<iterator, quickbook::table_row()> table_row;
- qi::rule<iterator, quickbook::table_cell()> table_cell;
- qi::rule<iterator, quickbook::formatted()> table_cell_body;
- qi::rule<iterator, quickbook::variablelist()> variablelist;
- qi::rule<iterator, quickbook::varlistentry()> varlistentry;
- qi::rule<iterator, quickbook::formatted()>
- varlistterm, varlistterm_body,
- varlistitem, varlistitem_body;
- qi::rule<iterator, quickbook::xinclude()> xinclude;
- qi::rule<iterator, quickbook::include()> include;
- qi::rule<iterator, quickbook::import()> import;
- qi::rule<iterator, quickbook::define_template()> define_template;
- qi::rule<iterator, quickbook::template_value()> template_body;
- qi::rule<iterator> template_body_recurse;
- qi::rule<iterator, quickbook::code()> code;
- qi::rule<iterator> code_line;
- qi::rule<iterator, quickbook::list()> list;
- qi::rule<iterator, quickbook::list_item()> list_item;
- qi::rule<iterator, std::string()> list_item_content;
- qi::rule<iterator, quickbook::hr()> hr;
- qi::rule<iterator, quickbook::paragraph()> paragraph;
- qi::rule<iterator, std::string()> paragraph_content;
- qi::rule<iterator> paragraph_end;
- qi::symbols<> paragraph_end_markups;
- qi::rule<iterator, quickbook::title()> title_phrase;
- qi::rule<iterator, std::string()> inside_paragraph;
- qi::rule<iterator, quickbook::formatted()> inside_paragraph2;
- qi::rule<iterator, std::string()> phrase_attr;
- qi::rule<iterator> phrase_end;
- qi::rule<iterator, boost::optional<std::string>()> element_id;
- qi::rule<iterator, std::string()> element_id_part;
- qi::rule<iterator, std::string()> template_id;
- qi::rule<iterator> error;
+ rule_store store_;
+ qi::rule<iterator> start_;
     };
 
     block_grammar::block_grammar(quickbook::actions& actions_)
@@ -177,6 +138,32 @@
     block_grammar::rules::rules(quickbook::actions& actions_)
         : actions(actions_), no_eols(true), common(actions, no_eols)
     {
+ qi::rule<iterator>& blocks = store_.create();
+ qi::rule<iterator>& block_markup = store_.create();
+ qi::rule<iterator, quickbook::begin_section()>& begin_section = store_.create();
+ qi::rule<iterator, quickbook::end_section()>& end_section = store_.create();
+ qi::rule<iterator, quickbook::heading()>& heading = store_.create();
+ qi::rule<iterator, quickbook::formatted()>& paragraph_block = store_.create();
+ qi::rule<iterator, quickbook::formatted()>& blockquote = store_.create();
+ qi::rule<iterator, quickbook::formatted()>& preformatted = store_.create();
+ qi::rule<iterator, quickbook::def_macro()>& def_macro = store_.create();
+ qi::rule<iterator, quickbook::table()>& table = store_.create();
+ qi::rule<iterator, quickbook::variablelist()>& variablelist = store_.create();
+ qi::rule<iterator, quickbook::xinclude()>& xinclude = store_.create();
+ qi::rule<iterator, quickbook::include()>& include = store_.create();
+ qi::rule<iterator, quickbook::import()>& import = store_.create();
+ qi::rule<iterator, quickbook::define_template()>& define_template = store_.create();
+ qi::rule<iterator, quickbook::code()>& code = store_.create();
+ qi::rule<iterator, quickbook::list()>& list = store_.create();
+ qi::rule<iterator, quickbook::hr()>& hr = store_.create();
+ qi::rule<iterator, quickbook::paragraph()>& paragraph = store_.create();
+ qi::rule<iterator, quickbook::title()>& title_phrase = store_.create();
+ qi::rule<iterator, std::string()>& inside_paragraph = store_.create();
+ qi::rule<iterator, std::string()>& phrase_attr = store_.create();
+ qi::rule<iterator>& phrase_end = store_.create();
+ qi::rule<iterator, boost::optional<std::string>()>& element_id = store_.create();
+ qi::rule<iterator>& error = store_.create();
+
         start_ =
             blocks >> blank
             ;
@@ -226,6 +213,7 @@
>> qi::attr(nothing())
             ;
 
+ qi::symbols<char, int>& heading_symbol = store_.create();
         heading = heading_symbol >> hard_space >> title_phrase;
 
         heading_symbol.add
@@ -237,6 +225,8 @@
             ("h6", 6)
             ("heading", -1);
 
+ qi::symbols<char, quickbook::formatted_type>& paragraph_blocks = store_.create();
+
         paragraph_block =
             paragraph_blocks >> hard_space >> inside_paragraph
             ;
@@ -274,6 +264,10 @@
>> phrase_attr
             ;
 
+ qi::rule<iterator, quickbook::table_row()>& table_row = store_.create();
+ qi::rule<iterator, quickbook::table_cell()>& table_cell = store_.create();
+ qi::rule<iterator, quickbook::formatted()>& table_cell_body = store_.create();
+
         table =
                 "table"
>> (&(*qi::blank >> qi::eol) | hard_space)
@@ -305,6 +299,12 @@
>> inside_paragraph
             ;
 
+ qi::rule<iterator, quickbook::varlistentry()>& varlistentry = store_.create();
+ qi::rule<iterator, quickbook::formatted()>& varlistterm = store_.create();
+ qi::rule<iterator, quickbook::formatted()>& varlistterm_body = store_.create();
+ qi::rule<iterator, quickbook::formatted()>& varlistitem = store_.create();
+ qi::rule<iterator, quickbook::formatted()>& varlistitem_body = store_.create();
+
         variablelist =
                 "variablelist"
>> (&(*qi::blank >> qi::eol) | hard_space)
@@ -376,6 +376,10 @@
>> qi::attr(nothing())
             ;
 
+ qi::rule<iterator, std::string()>& template_id = store_.create();
+ qi::rule<iterator, quickbook::template_value()>& template_body = store_.create();
+ qi::rule<iterator>& template_body_recurse = store_.create();
+
         define_template =
                 "template"
>> hard_space
@@ -405,6 +409,8 @@
 
         // Blocks indicated by text layout (indentation, leading characters etc.)
 
+ qi::rule<iterator>& code_line = store_.create();
+
         code =
                 position
>> qi::raw[
@@ -421,6 +427,9 @@
>> eol
             ;
 
+ qi::rule<iterator, quickbook::list_item()>& list_item = store_.create();
+ qi::rule<iterator, std::string()>& list_item_content = store_.create();
+
         list =
                 &qi::char_("*#")
>> +list_item
@@ -455,6 +464,10 @@
             ] >> qi::attr(quickbook::hr())
             ;
 
+ qi::rule<iterator, std::string()>& paragraph_content = store_.create();
+ qi::rule<iterator>& paragraph_end = store_.create();
+ qi::symbols<>& paragraph_end_markups = store_.create();
+
         paragraph = paragraph_content >> qi::attr(nothing());
 
         paragraph_content =
@@ -489,6 +502,8 @@
             ] [ph::at_c<0>(qi::_val) = qi::_1]
             ;
 
+ qi::rule<iterator, quickbook::formatted()>& inside_paragraph2 = store_.create();
+
         inside_paragraph =
                 qi::eps [actions.phrase_push]
>> inside_paragraph2 [actions.process]
@@ -520,6 +535,8 @@
 
         // Identifiers
 
+ qi::rule<iterator, std::string()>& element_id_part = store_.create();
+
         element_id =
             ( ':'
>> -(qi::eps(qbk_since(105u)) >> space)

Modified: branches/quickbook-1.5-spirit2/fwd.hpp
==============================================================================
--- branches/quickbook-1.5-spirit2/fwd.hpp (original)
+++ branches/quickbook-1.5-spirit2/fwd.hpp 2010-03-03 18:30:47 EST (Wed, 03 Mar 2010)
@@ -28,6 +28,8 @@
 
     struct collector;
     struct string_stream;
+
+ struct rule_store;
  
     typedef boost::spirit::classic::position_iterator<
         std::string::const_iterator> iterator;

Modified: branches/quickbook-1.5-spirit2/phrase_grammar.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/phrase_grammar.cpp (original)
+++ branches/quickbook-1.5-spirit2/phrase_grammar.cpp 2010-03-03 18:30:47 EST (Wed, 03 Mar 2010)
@@ -27,6 +27,7 @@
 #include "template.hpp"
 #include "parse_utils.hpp"
 #include "misc_rules.hpp"
+#include "rule_store.hpp"
 
 BOOST_FUSION_ADAPT_STRUCT(
     quickbook::anchor,
@@ -101,43 +102,8 @@
         quickbook::actions& actions;
         bool& no_eols;
 
- qi::rule<iterator, std::string()> phrase;
- qi::rule<iterator> common;
- qi::rule<iterator> macro;
- qi::rule<iterator> phrase_markup;
- qi::rule<iterator, quickbook::code()> code_block;
- qi::rule<iterator, quickbook::code()> inline_code;
- qi::rule<iterator, quickbook::simple_markup(), qi::locals<char> > simple_format;
- qi::rule<iterator> simple_phrase_end;
- qi::rule<iterator> escape;
- qi::rule<iterator, quickbook::break_()> escape_break;
- qi::rule<iterator, quickbook::formatted()> escape_punct;
- qi::rule<iterator, quickbook::formatted()> escape_markup;
- qi::rule<iterator, quickbook::unicode_char()> escape_unicode;
- qi::rule<iterator, quickbook::callout_link()> callout_link;
- qi::rule<iterator, quickbook::cond_phrase()> cond_phrase;
- qi::rule<iterator, quickbook::image()> image, image_1_4, image_1_5;
- qi::rule<iterator, std::string()> image_filename;
- qi::rule<iterator, quickbook::image::attribute_map()> image_attributes;
- qi::rule<iterator, std::pair<std::string, std::string>()> image_attribute;
- qi::rule<iterator, std::string()> image_attribute_key, image_attribute_value;
- qi::rule<iterator, quickbook::link()> url;
- qi::rule<iterator, quickbook::link()> link;
- qi::symbols<char, formatted_type> link_symbol;
- qi::rule<iterator, quickbook::anchor()> anchor;
- qi::symbols<char, quickbook::source_mode> source_mode;
- qi::rule<iterator, quickbook::formatted()> formatted;
- qi::symbols<char, formatted_type> format_symbol;
- qi::rule<iterator, quickbook::formatted()> footnote;
- qi::rule<iterator, quickbook::call_template()> call_template;
- qi::rule<iterator, std::vector<quickbook::template_value>()> template_args;
- qi::rule<iterator, quickbook::template_value()> template_arg_1_4;
- qi::rule<iterator> brackets_1_4;
- qi::rule<iterator, quickbook::template_value()> template_arg_1_5;
- qi::rule<iterator> brackets_1_5;
- qi::rule<iterator, quickbook::break_()> break_;
- qi::rule<iterator> phrase_end;
-
+ rule_store store_;
+ qi::rule<iterator> common;
     };
 
     phrase_grammar::phrase_grammar(quickbook::actions& actions, bool& no_eols)
@@ -152,6 +118,26 @@
     phrase_grammar::rules::rules(quickbook::actions& actions, bool& no_eols)
         : actions(actions), no_eols(no_eols)
     {
+ qi::rule<iterator, std::string()>& phrase = store_.create();
+ qi::rule<iterator>& macro = store_.create();
+ qi::rule<iterator>& phrase_markup = store_.create();
+ qi::rule<iterator, quickbook::code()>& code_block = store_.create();
+ qi::rule<iterator, quickbook::code()>& inline_code = store_.create();
+ qi::rule<iterator, quickbook::simple_markup(), qi::locals<char> >& simple_format = store_.create();
+ qi::rule<iterator>& escape = store_.create();
+ qi::rule<iterator, quickbook::callout_link()>& callout_link = store_.create();
+ qi::rule<iterator, quickbook::cond_phrase()>& cond_phrase = store_.create();
+ qi::rule<iterator, quickbook::image()>& image = store_.create();
+ qi::rule<iterator, quickbook::link()>& url = store_.create();
+ qi::rule<iterator, quickbook::link()>& link = store_.create();
+ qi::rule<iterator, quickbook::anchor()>& anchor = store_.create();
+ qi::symbols<char, quickbook::source_mode>& source_mode = store_.create();
+ qi::rule<iterator, quickbook::formatted()>& formatted = store_.create();
+ qi::rule<iterator, quickbook::formatted()>& footnote = store_.create();
+ qi::rule<iterator, quickbook::call_template()>& call_template = store_.create();
+ qi::rule<iterator, quickbook::break_()>& break_ = store_.create();
+ qi::rule<iterator>& phrase_end = store_.create();
+
         phrase =
                 qi::eps [actions.phrase_push]
>> *( common
@@ -227,6 +213,8 @@
>> qi::attr(false)
             ;
 
+ qi::rule<iterator>& simple_phrase_end = store_.create();
+
         simple_format %=
                 qi::char_("*/_=") [qi::_a = qi::_1]
>> qi::raw
@@ -254,6 +242,11 @@
 
         simple_phrase_end = '[' | phrase_end;
 
+ qi::rule<iterator, quickbook::break_()>& escape_break = store_.create();
+ qi::rule<iterator, quickbook::formatted()>& escape_punct = store_.create();
+ qi::rule<iterator, quickbook::formatted()>& escape_markup = store_.create();
+ qi::rule<iterator, quickbook::unicode_char()>& escape_unicode = store_.create();
+
         escape =
             ( escape_break
             | "\\ " // ignore an escaped char
@@ -304,6 +297,14 @@
>> -phrase
             ;
 
+ qi::rule<iterator, quickbook::image()>& image_1_4 = store_.create();
+ qi::rule<iterator, quickbook::image()>& image_1_5 = store_.create();
+ qi::rule<iterator, std::string()>& image_filename = store_.create();
+ qi::rule<iterator, quickbook::image::attribute_map()>& image_attributes = store_.create();
+ qi::rule<iterator, std::pair<std::string, std::string>()>& image_attribute = store_.create();
+ qi::rule<iterator, std::string()>& image_attribute_key = store_.create();
+ qi::rule<iterator, std::string()>& image_attribute_value = store_.create();
+
         image =
             (qi::eps(qbk_since(105u)) >> image_1_5) |
             (qi::eps(qbk_before(105u)) >> image_1_4);
@@ -355,6 +356,8 @@
                 )
             ;
 
+ qi::symbols<char, formatted_type>& link_symbol = store_.create();
+
         link =
                 link_symbol
>> hard_space
@@ -389,6 +392,8 @@
             ("teletype", quickbook::source_mode("teletype"))
             ;
 
+ qi::symbols<char, formatted_type>& format_symbol = store_.create();
+
         formatted = format_symbol >> blank >> phrase;
 
         format_symbol.add
@@ -410,6 +415,12 @@
 
         // Template call
 
+ qi::rule<iterator, std::vector<quickbook::template_value>()>& template_args = store_.create();
+ qi::rule<iterator, quickbook::template_value()>& template_arg_1_4 = store_.create();
+ qi::rule<iterator>& brackets_1_4 = store_.create();
+ qi::rule<iterator, quickbook::template_value()>& template_arg_1_5 = store_.create();
+ qi::rule<iterator>& brackets_1_5 = store_.create();
+
         call_template =
                 position
>> qi::matches['`']

Added: branches/quickbook-1.5-spirit2/rule_store.hpp
==============================================================================
--- (empty file)
+++ branches/quickbook-1.5-spirit2/rule_store.hpp 2010-03-03 18:30:47 EST (Wed, 03 Mar 2010)
@@ -0,0 +1,82 @@
+/*=============================================================================
+ Copyright (c) 2010 Daniel James
+
+ 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)
+=============================================================================*/
+
+// This header defines a class which can will manage quickbook rules for a
+// grammar class so that it doesn't have to declare every rule it'll use.
+//
+// TODO: Noncopyable, but some sort of reference counting scheme would work.
+
+#if !defined(BOOST_SPIRIT_QUICKBOOK_RULE_STORE_HPP)
+#define BOOST_SPIRIT_QUICKBOOK_RULE_STORE_HPP
+
+#include <deque>
+#include <boost/assert.hpp>
+
+namespace quickbook
+{
+ namespace detail
+ {
+ template <typename T>
+ void delete_impl(void* ptr) {
+ delete static_cast<T*>(ptr);
+ }
+
+ struct scoped_void
+ {
+ void* ptr_;
+ void (*del_)(void* x);
+
+ scoped_void() : ptr_(0), del_(0) {}
+ scoped_void(scoped_void const& src) : ptr_(0), del_(0) {
+ BOOST_ASSERT(!src.ptr_);
+ }
+ ~scoped_void() {
+ if(ptr_) del_(ptr_);
+ }
+
+ template <typename T>
+ T* instantiate() {
+ T* obj;
+
+ del_ = &delete_impl<T>;
+ ptr_ = obj = new T();
+ return obj;
+ }
+ private:
+ scoped_void& operator=(scoped_void const&);
+ };
+ }
+
+ struct rule_store
+ {
+ struct instantiate
+ {
+ rule_store& s;
+ instantiate(rule_store& s) : s(s) {}
+
+ template <typename T>
+ operator T&() {
+ s.store_.push_back(detail::scoped_void());
+ return *s.store_.back().instantiate<T>();
+ }
+ };
+
+ rule_store() {}
+
+ instantiate create() {
+ return instantiate(*this);
+ }
+
+ std::deque<detail::scoped_void> store_;
+ private:
+ rule_store& operator=(rule_store const&);
+ rule_store(rule_store const&);
+ };
+}
+
+#endif


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