Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r69310 - in branches/quickbook-filenames/tools/quickbook: src test
From: dnljms_at_[hidden]
Date: 2011-02-26 13:32:32


Author: danieljames
Date: 2011-02-26 13:32:31 EST (Sat, 26 Feb 2011)
New Revision: 69310
URL: http://svn.boost.org/trac/boost/changeset/69310

Log:
Use a phoenix closure to inline the simple markup parser.
Text files modified:
   branches/quickbook-filenames/tools/quickbook/src/actions.cpp | 24 ++++++-
   branches/quickbook-filenames/tools/quickbook/src/actions.hpp | 6 --
   branches/quickbook-filenames/tools/quickbook/src/actions_class.cpp | 6 -
   branches/quickbook-filenames/tools/quickbook/src/actions_class.hpp | 6 -
   branches/quickbook-filenames/tools/quickbook/src/main_grammar.cpp | 110 +++++++++++++++++----------------------
   branches/quickbook-filenames/tools/quickbook/test/simple_markup.gold | 2
   branches/quickbook-filenames/tools/quickbook/test/simple_markup.quickbook | 2
   7 files changed, 70 insertions(+), 86 deletions(-)

Modified: branches/quickbook-filenames/tools/quickbook/src/actions.cpp
==============================================================================
--- branches/quickbook-filenames/tools/quickbook/src/actions.cpp (original)
+++ branches/quickbook-filenames/tools/quickbook/src/actions.cpp 2011-02-26 13:32:31 EST (Sat, 26 Feb 2011)
@@ -16,6 +16,7 @@
 #include <boost/lexical_cast.hpp>
 #include <boost/range/distance.hpp>
 #include <boost/algorithm/string/replace.hpp>
+#include <boost/next_prior.hpp>
 #include "quickbook.hpp"
 #include "actions.hpp"
 #include "utils.hpp"
@@ -310,18 +311,31 @@
     {
         if(!actions.output_pre(out)) return;
 
- out << pre;
- std::string str(first, last);
+ char mark = *first;
+ int tag =
+ mark == '*' ? phrase_tags::bold :
+ mark == '/' ? phrase_tags::italic :
+ mark == '_' ? phrase_tags::underline :
+ mark == '=' ? phrase_tags::teletype :
+ 0;
+
+ assert(tag != 0);
+ detail::markup markup = detail::markups[tag];
+
+ std::string str(
+ boost::next(first.base()),
+ boost::prior(last.base()));
+
+ out << markup.pre;
         if (std::string const* ptr = find(macro, str.c_str()))
         {
             out << *ptr;
         }
         else
         {
- while (first != last)
- detail::print_char(*first++, out.get());
+ detail::print_string(str, out.get());
         }
- out << post;
+ out << markup.post;
     }
 
     bool cond_phrase_push::start()

Modified: branches/quickbook-filenames/tools/quickbook/src/actions.hpp
==============================================================================
--- branches/quickbook-filenames/tools/quickbook/src/actions.hpp (original)
+++ branches/quickbook-filenames/tools/quickbook/src/actions.hpp 2011-02-26 13:32:31 EST (Sat, 26 Feb 2011)
@@ -136,21 +136,15 @@
 
         simple_phrase_action(
             collector& out
- , std::string const& pre
- , std::string const& post
           , string_symbols const& macro
           , quickbook::actions& actions)
         : out(out)
- , pre(pre)
- , post(post)
         , macro(macro)
         , actions(actions) {}
 
         void operator()(iterator first, iterator last) const;
 
         collector& out;
- std::string pre;
- std::string post;
         string_symbols const& macro;
         quickbook::actions& actions;
     };

Modified: branches/quickbook-filenames/tools/quickbook/src/actions_class.cpp
==============================================================================
--- branches/quickbook-filenames/tools/quickbook/src/actions_class.cpp (original)
+++ branches/quickbook-filenames/tools/quickbook/src/actions_class.cpp 2011-02-26 13:32:31 EST (Sat, 26 Feb 2011)
@@ -77,11 +77,7 @@
         , raw_char(phrase, *this)
         , escape_unicode(phrase, *this)
 
- , simple_bold(phrase, bold_pre_, bold_post_, macro, *this)
- , simple_italic(phrase, italic_pre_, italic_post_, macro, *this)
- , simple_underline(phrase, underline_pre_, underline_post_, macro, *this)
- , simple_teletype(phrase, teletype_pre_, teletype_post_, macro, *this)
- , simple_strikethrough(phrase, strikethrough_pre_, strikethrough_post_, macro, *this)
+ , simple_markup(phrase, macro, *this)
 
         , break_(phrase, *this)
         , do_macro(phrase, *this)

Modified: branches/quickbook-filenames/tools/quickbook/src/actions_class.hpp
==============================================================================
--- branches/quickbook-filenames/tools/quickbook/src/actions_class.hpp (original)
+++ branches/quickbook-filenames/tools/quickbook/src/actions_class.hpp 2011-02-26 13:32:31 EST (Sat, 26 Feb 2011)
@@ -122,11 +122,7 @@
         raw_char_action raw_char;
         escape_unicode_action escape_unicode;
 
- simple_phrase_action simple_bold;
- simple_phrase_action simple_italic;
- simple_phrase_action simple_underline;
- simple_phrase_action simple_teletype;
- simple_phrase_action simple_strikethrough;
+ simple_phrase_action simple_markup;
 
         break_action break_;
         do_macro_action do_macro;

Modified: branches/quickbook-filenames/tools/quickbook/src/main_grammar.cpp
==============================================================================
--- branches/quickbook-filenames/tools/quickbook/src/main_grammar.cpp (original)
+++ branches/quickbook-filenames/tools/quickbook/src/main_grammar.cpp 2011-02-26 13:32:31 EST (Sat, 26 Feb 2011)
@@ -19,6 +19,7 @@
 #include <boost/spirit/include/classic_chset.hpp>
 #include <boost/spirit/include/classic_if.hpp>
 #include <boost/spirit/include/classic_loops.hpp>
+#include <boost/spirit/include/classic_attribute.hpp>
 #include <boost/spirit/include/phoenix1_primitives.hpp>
 #include <boost/spirit/include/phoenix1_casts.hpp>
 
@@ -26,47 +27,6 @@
 {
     namespace cl = boost::spirit::classic;
 
- template <typename Rule, typename Action>
- inline void
- simple_markup(
- Rule& simple
- , char mark
- , Action const& action
- , Rule const& close
- )
- {
- simple =
- mark
- >> lookback
- [ cl::anychar_p
- >> ~cl::eps_p(mark) // first mark not be preceeded by
- // the same character.
- >> (cl::space_p | cl::punct_p | cl::end_p)
- // first mark must be preceeded
- // by space or punctuation or the
- // mark character or a the start.
- ]
- >> ( cl::graph_p // graph_p must follow first mark
- >> *( cl::anychar_p -
- ( lookback[cl::graph_p]
- // final mark must be preceeded by
- // graph_p
- >> mark
- >> ~cl::eps_p(mark) // final mark not be followed by
- // the same character.
- >> (cl::space_p | cl::punct_p | cl::end_p)
- // final mark must be followed by
- // space or punctuation
- | close // Make sure that we don't go
- // past a single block
- )
- )
- >> cl::eps_p(mark)
- ) [action]
- >> mark
- ;
- }
-
     struct main_grammar_local
     {
         struct assign_element_type {
@@ -141,11 +101,9 @@
                         top_level, blocks, paragraph_separator,
                         code, code_line, blank_line, hr,
                         list, list_item, element,
- simple_phrase_end,
                         escape,
- inline_code, simple_format,
- simple_bold, simple_italic, simple_underline,
- simple_teletype, template_,
+ inline_code,
+ template_,
                         code_block, macro,
                         template_args,
                         template_args_1_4, template_arg_1_4,
@@ -157,6 +115,15 @@
                         dummy_block
                         ;
 
+ struct simple_markup_closure
+ : cl::closure<simple_markup_closure, char>
+ {
+ member1 mark;
+ };
+
+ cl::rule<scanner, simple_markup_closure::context_t>
+ simple_markup, simple_markup_end;
+
         element_info::type_enum element_type;
         cl::rule<scanner> element_rule;
         value::tag_type element_tag;
@@ -293,7 +260,7 @@
             | local.break_
             | local.code_block
             | local.inline_code
- | local.simple_format
+ | local.simple_markup
             | local.escape
             | comment
             ;
@@ -413,23 +380,40 @@
                 )
             ;
 
- local.simple_format =
- local.simple_bold
- | local.simple_italic
- | local.simple_underline
- | local.simple_teletype
- ;
-
- local.simple_phrase_end = cl::ch_p('[') | "'''" | '`' | phrase_end;
-
- simple_markup(local.simple_bold,
- '*', actions.simple_bold, local.simple_phrase_end);
- simple_markup(local.simple_italic,
- '/', actions.simple_italic, local.simple_phrase_end);
- simple_markup(local.simple_underline,
- '_', actions.simple_underline, local.simple_phrase_end);
- simple_markup(local.simple_teletype,
- '=', actions.simple_teletype, local.simple_phrase_end);
+ local.simple_markup =
+ ( cl::chset<>("*/_=") [local.simple_markup.mark = ph::arg1]
+ >> lookback
+ [ cl::anychar_p
+ >> ~cl::eps_p(cl::f_ch_p(local.simple_markup.mark))
+ // first mark not be preceeded by
+ // the same character.
+ >> (cl::space_p | cl::punct_p | cl::end_p)
+ // first mark must be preceeded
+ // by space or punctuation or the
+ // mark character or a the start.
+ ]
+ >> cl::graph_p // graph_p must follow first mark
+ >> *(cl::anychar_p - local.simple_markup_end(local.simple_markup.mark))
+ >> cl::f_ch_p(local.simple_markup.mark)
+ ) [actions.simple_markup]
+ ;
+
+ local.simple_markup_end
+ = ( lookback[cl::graph_p] // final mark must be preceeded by
+ // graph_p
+ >> cl::f_ch_p(local.simple_markup_end.mark)
+ >> ~cl::eps_p(cl::f_ch_p(local.simple_markup_end.mark))
+ // final mark not be followed by
+ // the same character.
+ >> (cl::space_p | cl::punct_p | cl::end_p)
+ // final mark must be followed by
+ // space or punctuation
+ )
+ | '['
+ | "'''"
+ | '`'
+ | phrase_end
+ ;
 
         phrase =
             actions.scoped_context(element_info::in_phrase)

Modified: branches/quickbook-filenames/tools/quickbook/test/simple_markup.gold
==============================================================================
--- branches/quickbook-filenames/tools/quickbook/test/simple_markup.gold (original)
+++ branches/quickbook-filenames/tools/quickbook/test/simple_markup.gold 2011-02-26 13:32:31 EST (Sat, 26 Feb 2011)
@@ -17,7 +17,7 @@
       not__underlined__hopefully
     </para>
     <para>
- (<emphasis role="bold">bold</emphasis>)
+ (<emphasis role="bold">bold</emphasis>) <emphasis role="underline">und/er/lined</emphasis>
     </para>
     <para>
       <emphasis>all/italic</emphasis> * not bold*

Modified: branches/quickbook-filenames/tools/quickbook/test/simple_markup.quickbook
==============================================================================
--- branches/quickbook-filenames/tools/quickbook/test/simple_markup.quickbook (original)
+++ branches/quickbook-filenames/tools/quickbook/test/simple_markup.quickbook 2011-02-26 13:32:31 EST (Sat, 26 Feb 2011)
@@ -10,7 +10,7 @@
 
 not__underlined__hopefully
 
-(*bold*)
+(*bold*) _und/er/lined_
 
 /all/italic/ * not bold*
 


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