Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r59301 - in branches/quickbook-1.5-spirit2: . detail
From: daniel_james_at_[hidden]
Date: 2010-01-27 17:02:10


Author: danieljames
Date: 2010-01-27 17:02:08 EST (Wed, 27 Jan 2010)
New Revision: 59301
URL: http://svn.boost.org/trac/boost/changeset/59301

Log:
Use attributes from macro, and convert the syntax highlighter to a
simpler phrase based version.
Text files modified:
   branches/quickbook-1.5-spirit2/detail/actions.cpp | 42 ++------------
   branches/quickbook-1.5-spirit2/detail/actions.hpp | 57 +++---------------
   branches/quickbook-1.5-spirit2/detail/actions_class.cpp | 9 +-
   branches/quickbook-1.5-spirit2/detail/actions_class.hpp | 5 -
   branches/quickbook-1.5-spirit2/detail/syntax_highlight.cpp | 25 ++------
   branches/quickbook-1.5-spirit2/phrase.cpp | 8 +-
   branches/quickbook-1.5-spirit2/phrase.hpp | 2
   branches/quickbook-1.5-spirit2/phrase_actions.cpp | 19 ++++++
   branches/quickbook-1.5-spirit2/syntax_highlight.hpp | 119 +++++++++++----------------------------
   9 files changed, 89 insertions(+), 197 deletions(-)

Modified: branches/quickbook-1.5-spirit2/detail/actions.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/detail/actions.cpp (original)
+++ branches/quickbook-1.5-spirit2/detail/actions.cpp 2010-01-27 17:02:08 EST (Wed, 27 Jan 2010)
@@ -116,9 +116,9 @@
     void simple_phrase_action::operator()(iterator_range const& x, unused_type, unused_type) const
     {
         out << pre;
- if (std::string const* ptr = macro.find(x))
+ if (quickbook::macro const* ptr = macro.find(x))
         {
- out << *ptr;
+ out << ptr->raw_markup;
         }
         else
         {
@@ -247,26 +247,6 @@
         out << "</phrase>";
     }
 
- void do_macro_action::operator()(std::string const& str, unused_type, unused_type) const
- {
- if (str == quickbook_get_date)
- {
- char strdate[64];
- strftime(strdate, sizeof(strdate), "%Y-%b-%d", current_time);
- phrase << strdate;
- }
- else if (str == quickbook_get_time)
- {
- char strdate[64];
- strftime(strdate, sizeof(strdate), "%I:%M:%S %p", current_time);
- phrase << strdate;
- }
- else
- {
- phrase << str;
- }
- }
-
     void space::operator()(char ch, unused_type, unused_type) const
     {
         detail::print_space(ch, out.get());
@@ -279,17 +259,6 @@
             detail::print_space(*first++, out.get());
     }
 
- void pre_escape_back::operator()(unused_type, unused_type, unused_type) const
- {
- escape_actions.phrase.push(); // save the stream
- }
-
- void post_escape_back::operator()(unused_type, unused_type, unused_type) const
- {
- out << escape_actions.phrase.str();
- escape_actions.phrase.pop(); // restore the stream
- }
-
     void code_action::operator()(iterator_range x, unused_type, unused_type) const
     {
         // preprocess the code section to remove the initial indentation
@@ -350,7 +319,7 @@
         actions.macro.add(
             actions.macro_id.begin()
           , actions.macro_id.end()
- , actions.phrase.str());
+ , quickbook::macro(actions.phrase.str()));
         actions.phrase.pop(); // restore the phrase
     }
 
@@ -821,7 +790,7 @@
         actions.doc_last_revision.swap(doc_last_revision);
 
         // scope the macros
- string_symbols macro = actions.macro;
+ macro_symbols macro = actions.macro;
         // scope the templates
         //~ template_symbols templates = actions.templates; $$$ fixme $$$
 
@@ -834,7 +803,8 @@
         }
 
         // update the __FILENAME__ macro
- *actions.macro.find("__FILENAME__") = actions.filename.native_file_string();
+ *actions.macro.find("__FILENAME__") =
+ quickbook::macro(actions.filename.native_file_string());
 
         // parse the file
         quickbook::parse(actions.filename.native_file_string().c_str(), actions, true);

Modified: branches/quickbook-1.5-spirit2/detail/actions.hpp
==============================================================================
--- branches/quickbook-1.5-spirit2/detail/actions.hpp (original)
+++ branches/quickbook-1.5-spirit2/detail/actions.hpp 2010-01-27 17:02:08 EST (Wed, 27 Jan 2010)
@@ -36,14 +36,20 @@
     namespace fs = boost::filesystem;
     using boost::spirit::unused_type;
 
- // TODO: This is defined in two places.
- typedef qi::symbols<char, std::string> string_symbols;
+ struct macro {
+ macro() {}
+ explicit macro(char const* x) : raw_markup(x) {};
+ explicit macro(std::string const& x) : raw_markup(x) {};
+
+ std::string raw_markup;
+ };
+
+ typedef qi::symbols<char, macro> macro_symbols;
 
     typedef boost::spirit::classic::position_iterator<
         std::string::const_iterator> iterator;
     typedef boost::spirit::classic::file_position file_position;
     typedef boost::iterator_range<iterator> iterator_range;
- typedef qi::symbols<char, std::string> string_symbols;
     typedef std::map<std::string, std::string> attribute_map;
 
     struct actions;
@@ -159,7 +165,7 @@
             collector& out
           , std::string const& pre
           , std::string const& post
- , string_symbols const& macro)
+ , macro_symbols const& macro)
         : out(out)
         , pre(pre)
         , post(post)
@@ -170,7 +176,7 @@
         collector& out;
         std::string pre;
         std::string post;
- string_symbols const& macro;
+ macro_symbols const& macro;
     };
 
     struct list_action
@@ -247,17 +253,6 @@
     extern char const* quickbook_get_date;
     extern char const* quickbook_get_time;
 
- struct do_macro_action
- {
- // Handles macro substitutions
-
- do_macro_action(collector& phrase)
- : phrase(phrase) {}
-
- void operator()(std::string const& str, unused_type, unused_type) const;
- collector& phrase;
- };
-
     struct space
     {
         // Prints a space
@@ -271,33 +266,6 @@
         collector& out;
     };
 
- struct pre_escape_back
- {
- // Escapes back from code to quickbook (Pre)
-
- pre_escape_back(actions& escape_actions, std::string& save)
- : escape_actions(escape_actions), save(save) {}
-
- void operator()(unused_type, unused_type, unused_type) const;
-
- actions& escape_actions;
- std::string& save;
- };
-
- struct post_escape_back
- {
- // Escapes back from code to quickbook (Post)
-
- post_escape_back(collector& out, actions& escape_actions, std::string& save)
- : out(out), escape_actions(escape_actions), save(save) {}
-
- void operator()(unused_type, unused_type, unused_type) const;
-
- collector& out;
- actions& escape_actions;
- std::string& save;
- };
-
     struct raw_char_action
     {
         // Prints a single raw (unprocessed) char.
@@ -358,10 +326,8 @@
     {
         syntax_highlight(
             std::string const& source_mode
- , string_symbols const& macro
           , actions& escape_actions)
         : source_mode(source_mode)
- , macro(macro)
         , escape_actions(escape_actions)
         {
         }
@@ -369,7 +335,6 @@
         std::string operator()(iterator begin, iterator end) const;
 
         std::string const& source_mode;
- string_symbols const& macro;
         actions& escape_actions;
     };
 

Modified: branches/quickbook-1.5-spirit2/detail/actions_class.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/detail/actions_class.cpp (original)
+++ branches/quickbook-1.5-spirit2/detail/actions_class.cpp 2010-01-27 17:02:08 EST (Wed, 27 Jan 2010)
@@ -69,7 +69,7 @@
         , extract_doc_license(doc_license, phrase)
         , extract_doc_purpose(doc_purpose, phrase)
 
- , syntax_p(source_mode, macro, *this)
+ , syntax_p(source_mode, *this)
         , code(out, phrase, syntax_p)
         , paragraph(out, phrase, paragraph_pre, paragraph_post)
         , inside_paragraph(temp_para, phrase, paragraph_pre, paragraph_post)
@@ -106,7 +106,6 @@
 
         , macro_identifier(*this)
         , macro_definition(*this)
- , do_macro(phrase)
         , template_body(*this)
         , url_pre(url_pre_)
         , url_post(url_post_)
@@ -131,9 +130,9 @@
 
         // add the predefined macros
         macro.add
- ("__DATE__", std::string(quickbook_get_date))
- ("__TIME__", std::string(quickbook_get_time))
- ("__FILENAME__", filename_str)
+ ("__DATE__", quickbook::macro(quickbook_get_date))
+ ("__TIME__", quickbook::macro(quickbook_get_time))
+ ("__FILENAME__", quickbook::macro(filename_str))
         ;
     }
 

Modified: branches/quickbook-1.5-spirit2/detail/actions_class.hpp
==============================================================================
--- branches/quickbook-1.5-spirit2/detail/actions_class.hpp (original)
+++ branches/quickbook-1.5-spirit2/detail/actions_class.hpp 2010-01-27 17:02:08 EST (Wed, 27 Jan 2010)
@@ -59,7 +59,7 @@
     // state
         fs::path filename;
         fs::path outdir;
- string_symbols macro;
+ macro_symbols macro;
         int section_level;
         std::string section_id;
         std::string qualified_section_id;
@@ -68,7 +68,7 @@
         typedef boost::tuple<
             fs::path
           , fs::path
- , string_symbols
+ , macro_symbols
           , int
           , std::string
           , std::string
@@ -128,7 +128,6 @@
 
         macro_identifier_action macro_identifier;
         macro_definition_action macro_definition;
- do_macro_action do_macro;
         template_body_action template_body;
         char const* url_pre;
         char const* url_post;

Modified: branches/quickbook-1.5-spirit2/detail/syntax_highlight.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/detail/syntax_highlight.cpp (original)
+++ branches/quickbook-1.5-spirit2/detail/syntax_highlight.cpp 2010-01-27 17:02:08 EST (Wed, 27 Jan 2010)
@@ -6,10 +6,6 @@
     typedef cpp_highlight<
         span
       , space
- , string_symbols
- , do_macro_action
- , pre_escape_back
- , post_escape_back
       , unexpected_char
       , collector>
     cpp_p_type;
@@ -17,41 +13,33 @@
     typedef python_highlight<
         span
       , space
- , string_symbols
- , do_macro_action
- , pre_escape_back
- , post_escape_back
       , unexpected_char
       , collector>
     python_p_type;
     
     typedef teletype_highlight<
         plain_char_action
- , string_symbols
- , do_macro_action
- , pre_escape_back
- , post_escape_back
       , collector>
     teletype_p_type;
     
     std::string syntax_highlight::operator()(iterator first, iterator last) const
     {
- collector temp;
+ escape_actions.phrase.push();
 
         // print the code with syntax coloring
         if (source_mode == "c++")
         {
- cpp_p_type cpp_p(temp, macro, do_macro_action(temp), escape_actions);
+ cpp_p_type cpp_p(escape_actions.phrase, escape_actions);
             parse(first, last, cpp_p);
         }
         else if (source_mode == "python")
         {
- python_p_type python_p(temp, macro, do_macro_action(temp), escape_actions);
+ python_p_type python_p(escape_actions.phrase, escape_actions);
             parse(first, last, python_p);
         }
         else if (source_mode == "teletype")
         {
- teletype_p_type teletype_p(temp, macro, do_macro_action(temp), escape_actions);
+ teletype_p_type teletype_p(escape_actions.phrase, escape_actions);
             parse(first, last, teletype_p);
         }
         else
@@ -60,8 +48,9 @@
         }
 
         std::string str;
- temp.swap(str);
-
+ escape_actions.phrase.swap(str);
+ escape_actions.phrase.pop();
+
         return str;
     }
 }
\ No newline at end of file

Modified: branches/quickbook-1.5-spirit2/phrase.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/phrase.cpp (original)
+++ branches/quickbook-1.5-spirit2/phrase.cpp 2010-01-27 17:02:08 EST (Wed, 27 Jan 2010)
@@ -206,10 +206,10 @@
             | comment
             ;
 
- macro =
- &(actions.macro // must not be followed by
- >> (qi::eps - (qi::alpha | '_'))) // alpha or underscore
- >> actions.macro [actions.do_macro]
+ macro =
+ ( actions.macro // must not be followed by
+ >> &(qi::eps - (qi::alpha | '_')) // alpha or underscore
+ ) [actions.process]
             ;
 
         // Template call

Modified: branches/quickbook-1.5-spirit2/phrase.hpp
==============================================================================
--- branches/quickbook-1.5-spirit2/phrase.hpp (original)
+++ branches/quickbook-1.5-spirit2/phrase.hpp 2010-01-27 17:02:08 EST (Wed, 27 Jan 2010)
@@ -19,6 +19,7 @@
 {
     // TODO: Add to a forward header somewhere.
     class actions;
+ struct macro;
     typedef boost::spirit::classic::file_position file_position;
 
     struct source_mode {
@@ -91,6 +92,7 @@
     };
 
     void process(quickbook::actions&, source_mode const&);
+ void process(quickbook::actions&, macro const&);
     void process(quickbook::actions&, template_ const&);
     void process(quickbook::actions&, anchor const&);
     void process(quickbook::actions&, link const&);

Modified: branches/quickbook-1.5-spirit2/phrase_actions.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/phrase_actions.cpp (original)
+++ branches/quickbook-1.5-spirit2/phrase_actions.cpp 2010-01-27 17:02:08 EST (Wed, 27 Jan 2010)
@@ -19,6 +19,25 @@
         actions.source_mode = s.mode;
     }
 
+ void process(quickbook::actions& actions, macro const& x) {
+ if (x.raw_markup == quickbook_get_date)
+ {
+ char strdate[64];
+ strftime(strdate, sizeof(strdate), "%Y-%b-%d", current_time);
+ actions.phrase << strdate;
+ }
+ else if (x.raw_markup == quickbook_get_time)
+ {
+ char strdate[64];
+ strftime(strdate, sizeof(strdate), "%I:%M:%S %p", current_time);
+ actions.phrase << strdate;
+ }
+ else
+ {
+ actions.phrase << x.raw_markup;
+ }
+ }
+
     void process(quickbook::actions& actions, anchor const& x) {
         actions.phrase << "<anchor id=\"";
         detail::print_string(x.id, actions.phrase.get());

Modified: branches/quickbook-1.5-spirit2/syntax_highlight.hpp
==============================================================================
--- branches/quickbook-1.5-spirit2/syntax_highlight.hpp (original)
+++ branches/quickbook-1.5-spirit2/syntax_highlight.hpp 2010-01-27 17:02:08 EST (Wed, 27 Jan 2010)
@@ -15,6 +15,7 @@
 #include <boost/spirit/include/qi_string.hpp>
 #include <boost/spirit/include/qi_directive.hpp>
 #include "./grammars.hpp"
+#include "./phrase.hpp"
 
 namespace quickbook
 {
@@ -45,17 +46,13 @@
     template <
         typename Process
       , typename Space
- , typename Macro
- , typename DoMacro
- , typename PreEscape
- , typename PostEscape
       , typename Unexpected
       , typename Out>
     struct cpp_highlight
     : public qi::grammar<iterator>
     {
- cpp_highlight(Out& out, Macro const& macro_symbols, DoMacro do_macro, quickbook::actions& escape_actions)
- : cpp_highlight::base_type(program), out(out), macro_symbols(macro_symbols), do_macro(do_macro), escape_actions(escape_actions)
+ cpp_highlight(Out& out, quickbook::actions& escape_actions)
+ : cpp_highlight::base_type(program), out(out), escape_actions(escape_actions)
         , parse_escaped(escape_actions)
         {
             program
@@ -75,30 +72,18 @@
                 )
                 ;
 
- macro =
- &(macro_symbols // must not be followed by
- >> (qi::eps - (qi::alpha | '_'))) // alpha or underscore
- >> macro_symbols [do_macro]
+ macro =
+ ( escape_actions.macro // must not be followed by
+ >> &(qi::eps - (qi::alpha | '_')) // alpha or underscore
+ ) [escape_actions.process]
                 ;
 
             escape =
- qi::string("``") [PreEscape(escape_actions, save)]
- >>
- (
- (
- (
- // TODO: Is this right?
- qi::raw[+(qi::char_ - "``") >> &qi::lit("``")]
- [parse_escaped]
- )
- >> qi::string("``")
- )
- |
- (
- qi::raw[qi::eps] [escape_actions.error]
- >> *qi::char_
- )
- ) [PostEscape(out, escape_actions, save)]
+ "``" >> (
+ (qi::raw[+(qi::char_ - "``")] >> "``")
+ [parse_escaped]
+ | qi::raw[*qi::char_] [escape_actions.error]
+ )
                 ;
 
             preprocessor
@@ -165,8 +150,6 @@
                         string_char;
 
         Out& out;
- Macro const& macro_symbols;
- DoMacro do_macro;
         quickbook::actions& escape_actions;
 
         qi::symbols<> keyword_;
@@ -180,17 +163,13 @@
     template <
         typename Process
       , typename Space
- , typename Macro
- , typename DoMacro
- , typename PreEscape
- , typename PostEscape
       , typename Unexpected
       , typename Out>
     struct python_highlight
     : public qi::grammar<iterator>
     {
- python_highlight(Out& out, Macro const& macro_symbols, DoMacro do_macro, quickbook::actions& escape_actions)
- : python_highlight::base_type(program), out(out), macro_symbols(macro_symbols), do_macro(do_macro), escape_actions(escape_actions)
+ python_highlight(Out& out, quickbook::actions& escape_actions)
+ : python_highlight::base_type(program), out(out), escape_actions(escape_actions)
         , parse_escaped(escape_actions)
         {
             program
@@ -208,29 +187,18 @@
                 )
                 ;
 
- macro =
- &(macro_symbols // must not be followed by
- >> (qi::eps - (qi::alpha | '_'))) // alpha or underscore
- >> macro_symbols [do_macro]
+ macro =
+ ( escape_actions.macro // must not be followed by
+ >> &(qi::eps - (qi::alpha | '_')) // alpha or underscore
+ ) [escape_actions.process]
                 ;
 
             escape =
- qi::string("``") [PreEscape(escape_actions, save)]
- >>
- (
- (
- (
- qi::raw[+(qi::char_ - "``") >> &qi::lit("``")]
- [parse_escaped]
- )
- >> qi::string("``")
- )
- |
- (
- qi::raw[qi::eps] [escape_actions.error]
- >> *qi::char_
- )
- ) [PostEscape(out, escape_actions, save)]
+ "``" >> (
+ (qi::raw[+(qi::char_ - "``")] >> "``")
+ [parse_escaped]
+ | qi::raw[*qi::char_] [escape_actions.error]
+ )
                 ;
 
             comment
@@ -301,8 +269,6 @@
                         escape, string_char;
 
         Out& out;
- Macro const& macro_symbols;
- DoMacro do_macro;
         quickbook::actions& escape_actions;
 
         qi::symbols<> keyword_;
@@ -313,16 +279,12 @@
     // Grammar for plain text (no actual highlighting)
     template <
         typename CharProcess
- , typename Macro
- , typename DoMacro
- , typename PreEscape
- , typename PostEscape
       , typename Out>
     struct teletype_highlight
     : public qi::grammar<iterator>
     {
- teletype_highlight(Out& out, Macro const& macro_symbols, DoMacro do_macro, quickbook::actions& escape_actions)
- : teletype_highlight::base_type(program), out(out), macro_symbols(macro_symbols), do_macro(do_macro), escape_actions(escape_actions)
+ teletype_highlight(Out& out, quickbook::actions& escape_actions)
+ : teletype_highlight::base_type(program), out(out), escape_actions(escape_actions)
         , parse_escaped(escape_actions)
         {
             program
@@ -333,37 +295,24 @@
                 )
                 ;
 
- macro =
- &(macro_symbols // must not be followed by
- >> (qi::eps - (qi::alpha | '_'))) // alpha or underscore
- >> macro_symbols [do_macro]
+ macro =
+ ( escape_actions.macro // must not be followed by
+ >> &(qi::eps - (qi::alpha | '_')) // alpha or underscore
+ ) [escape_actions.process]
                 ;
 
             escape =
- qi::lit("``") [PreEscape(escape_actions, save)]
- >>
- (
- (
- (
- qi::raw[+(qi::char_ - "``") >> &qi::lit("``")]
- [parse_escaped]
- )
- >> qi::string("``")
- )
- |
- (
- qi::raw[qi::eps] [escape_actions.error]
- >> *qi::char_
- )
- ) [PostEscape(out, escape_actions, save)]
+ "``" >> (
+ (qi::raw[+(qi::char_ - "``")] >> "``")
+ [parse_escaped]
+ | qi::raw[*qi::char_] [escape_actions.error]
+ )
                 ;
         }
 
         qi::rule<iterator> program, macro, escape;
 
         Out& out;
- Macro const& macro_symbols;
- DoMacro do_macro;
         quickbook::actions& escape_actions;
 
         parse_escaped_impl parse_escaped;


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