From a8dfaf18521d4abd41f5c19320bbc656de86c166 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Sun, 7 Feb 2010 09:12:02 +0000 Subject: [PATCH] Separate actions and state. [SVN r59563] --- Jamfile.v2 | 2 +- actions.cpp | 15 ++- actions.hpp | 28 ++++- block.cpp | 4 +- block_actions.cpp | 94 +++++++------- block_list.cpp | 5 +- boostbook.cpp | 218 ++++++++++++++++----------------- boostbook.hpp | 38 +++--- code_snippet.cpp | 2 +- doc_info.cpp | 5 +- doc_info_actions.cpp | 3 +- fwd.hpp | 2 + phrase.cpp | 3 +- phrase_actions.cpp | 9 +- phrase_image.cpp | 5 +- process.cpp | 6 +- quickbook.cpp | 18 +-- actions_class.cpp => state.cpp | 17 +-- actions_class.hpp => state.hpp | 20 +-- syntax_highlight.cpp | 10 +- template.cpp | 48 ++++---- 21 files changed, 288 insertions(+), 264 deletions(-) rename actions_class.cpp => state.cpp (87%) rename actions_class.hpp => state.hpp (79%) diff --git a/Jamfile.v2 b/Jamfile.v2 index d6ac6a4..6e0ca84 100644 --- a/Jamfile.v2 +++ b/Jamfile.v2 @@ -22,7 +22,7 @@ exe quickbook process.cpp quickbook.cpp actions.cpp - actions_class.cpp + state.cpp utils.cpp input_path.cpp post_process.cpp diff --git a/actions.cpp b/actions.cpp index ecbe180..40ef290 100644 --- a/actions.cpp +++ b/actions.cpp @@ -10,7 +10,7 @@ =============================================================================*/ #include "actions.hpp" -#include "actions_class.hpp" +#include "state.hpp" #include "doc_info.hpp" #include "utils.hpp" @@ -22,6 +22,17 @@ namespace quickbook unsigned qbk_minor_version = 0; unsigned qbk_version_n = 0; // qbk_major_version * 100 + qbk_minor_version + actions::actions(state& state) + : state_(state) + , templates(state.templates) + , macro(state.macro) + , process(*this) + , phrase_push(state.phrase) + , phrase_pop(state.phrase) + , error(state.error_count) + , syntax_p(state.source_mode, *this) + {} + namespace { std::string fully_qualified_id(std::string const& library_id, std::string const& qualified_section_id, @@ -50,7 +61,7 @@ namespace quickbook detail::outwarn(pos.file,pos.line) << "Empty id.\n"; } - void phrase_push_action::operator()(unused_type, unused_type, unused_type) const + void phrase_push_action::operator()() const { phrase.push(); } diff --git a/actions.hpp b/actions.hpp index d40eace..1e2fba4 100644 --- a/actions.hpp +++ b/actions.hpp @@ -55,6 +55,8 @@ namespace quickbook boost::phoenix::function qbk_before; } + // TODO: Define this elsewhere? + struct macro { macro() {} explicit macro(char const* x) : raw_markup(x) {}; @@ -109,7 +111,11 @@ namespace quickbook phrase_push_action(collector& phrase) : phrase(phrase) {} - void operator()(unused_type, unused_type, unused_type) const; + void operator()(unused_type, unused_type, unused_type) const { + return (*this)(); + } + + void operator()() const; collector& phrase; }; @@ -163,6 +169,26 @@ namespace quickbook quickbook::actions& actions; }; + + /////////////////////////////////////////////////////////////////////////// + // actions + /////////////////////////////////////////////////////////////////////////// + + struct actions + { + actions(state&); + + state& state_; + template_stack& templates; + macro_symbols& macro; + + process_action process; + phrase_push_action phrase_push; + phrase_pop_action phrase_pop; + error_action error; + element_id_warning_action element_id_warning; + syntax_highlight syntax_p; + }; } #ifdef BOOST_MSVC diff --git a/block.cpp b/block.cpp index b4cf0a2..545c368 100644 --- a/block.cpp +++ b/block.cpp @@ -20,8 +20,8 @@ #include #include "grammars.hpp" #include "block.hpp" -#include "utils.hpp" -#include "actions_class.hpp" +#include "template.hpp" +#include "actions.hpp" #include "parse_utils.hpp" #include "code.hpp" diff --git a/block_actions.cpp b/block_actions.cpp index da73d8a..4e8473d 100644 --- a/block_actions.cpp +++ b/block_actions.cpp @@ -12,7 +12,8 @@ #include #include #include "block_actions.hpp" -#include "actions_class.hpp" +#include "actions.hpp" +#include "state.hpp" #include "quickbook.hpp" #include "grammars.hpp" #include "code_snippet_types.hpp" @@ -45,30 +46,30 @@ namespace quickbook begin_section2 process(quickbook::actions& actions, begin_section const& x) { // TODO: This uses the generated title. - actions.section_id = x.id ? *x.id : + actions.state_.section_id = x.id ? *x.id : detail::make_identifier( x.content.raw_markup.begin(), x.content.raw_markup.end()); - if (actions.section_level != 0) { - actions.qualified_section_id += '.'; + if (actions.state_.section_level != 0) { + actions.state_.qualified_section_id += '.'; } else { - BOOST_ASSERT(actions.qualified_section_id.empty()); + BOOST_ASSERT(actions.state_.qualified_section_id.empty()); } - actions.qualified_section_id += actions.section_id; - ++actions.section_level; + actions.state_.qualified_section_id += actions.state_.section_id; + ++actions.state_.section_level; begin_section2 r; if (qbk_version_n < 103) // version 1.2 and below { - r.id = actions.doc_id + "." + actions.section_id; + r.id = actions.state_.doc_id + "." + actions.state_.section_id; } else // version 1.3 and above { - r.linkend = r.id = actions.doc_id + "." + actions.qualified_section_id; + r.linkend = r.id = actions.state_.doc_id + "." + actions.state_.qualified_section_id; } r.content = x.content.content; @@ -78,26 +79,26 @@ namespace quickbook end_section2 process(quickbook::actions& actions, end_section const& x) { - --actions.section_level; - if (actions.section_level < 0) + --actions.state_.section_level; + if (actions.state_.section_level < 0) { detail::outerr(x.position.file,x.position.line) << "Mismatched [endsect] near column " << x.position.column << ".\n"; - ++actions.error_count; + ++actions.state_.error_count; // $$$ TODO: somehow fail parse else BOOST_ASSERT(std::string::npos != n) // $$$ below will assert. } - if (actions.section_level == 0) + if (actions.state_.section_level == 0) { - actions.qualified_section_id.clear(); + actions.state_.qualified_section_id.clear(); } else { std::string::size_type const n = - actions.qualified_section_id.find_last_of('.'); + actions.state_.qualified_section_id.find_last_of('.'); BOOST_ASSERT(std::string::npos != n); - actions.qualified_section_id.erase(n, std::string::npos); + actions.state_.qualified_section_id.erase(n, std::string::npos); } return end_section2(); @@ -112,7 +113,8 @@ namespace quickbook r.level = x.level; if(r.level < 0) { - r.level = actions.section_level + 2;// section_level is zero-based. We need to use a + r.level = actions.state_.section_level + 2; + // section_level is zero-based. We need to use a // one-based heading which is one greater // than the current. Thus: section_level + 2. if (r.level > 6) // The max is h6, clip it if it goes @@ -121,7 +123,7 @@ namespace quickbook if (!new_style) // version 1.2 and below { - r.id = actions.section_id + "." + + r.id = actions.state_.section_id + "." + detail::make_identifier( x.content.raw_markup.begin(), x.content.raw_markup.end()); @@ -129,7 +131,7 @@ namespace quickbook else // version 1.3 and above { r.linkend = r.id = fully_qualified_id( - actions.doc_id, actions.qualified_section_id, + actions.state_.doc_id, actions.state_.qualified_section_id, detail::make_identifier( x.content.raw_markup.begin(), x.content.raw_markup.end())); @@ -143,7 +145,7 @@ namespace quickbook nothing process(quickbook::actions& actions, def_macro const& x) { - actions.macro.add( + actions.state_.macro.add( x.macro_identifier.begin() , x.macro_identifier.end() , quickbook::macro(x.content)); @@ -152,10 +154,10 @@ namespace quickbook nothing process(quickbook::actions& actions, define_template const& x) { - if(!actions.templates.add(x)) { + if(!actions.state_.templates.add(x)) { detail::outerr(x.position.file, x.position.line) << "Template Redefinition: " << x.id << std::endl; - ++actions.error_count; + ++actions.state_.error_count; } return nothing(); @@ -169,12 +171,12 @@ namespace quickbook if(qbk_version_n >= 105) { if(x.id) { - r.id = fully_qualified_id(actions.doc_id, - actions.qualified_section_id, *x.id); + r.id = fully_qualified_id(actions.state_.doc_id, + actions.state_.qualified_section_id, *x.id); } else if(r.title) { - r.id = fully_qualified_id(actions.doc_id, - actions.qualified_section_id, + r.id = fully_qualified_id(actions.state_.doc_id, + actions.state_.qualified_section_id, detail::make_identifier(x.title.begin(), x.title.end())); } } @@ -284,9 +286,9 @@ namespace quickbook fs::path path(x); if (!path.is_complete()) { - fs::path infile = fs::complete(actions.filename).normalize(); + fs::path infile = fs::complete(actions.state_.filename).normalize(); path = (infile.branch_path() / path).normalize(); - fs::path outdir = fs::complete(actions.outdir).normalize(); + fs::path outdir = fs::complete(actions.state_.outdir).normalize(); path = path_difference(outdir, path); } return path; @@ -302,59 +304,59 @@ namespace quickbook nothing process(quickbook::actions& actions, include const& x) { - fs::path filein = include_search(actions.filename.branch_path(), x.path); + fs::path filein = include_search(actions.state_.filename.branch_path(), x.path); std::string doc_id; // swap the filenames - std::swap(actions.filename, filein); + std::swap(actions.state_.filename, filein); // save the doc info strings - actions.doc_id.swap(doc_id); + actions.state_.doc_id.swap(doc_id); // scope the macros - macro_symbols macro = actions.macro; + macro_symbols macro = actions.state_.macro; // scope the templates - //~ template_symbols templates = actions.templates; $$$ fixme $$$ + //~ template_symbols templates = actions.state_.templates; $$$ fixme $$$ // if an id is specified in this include (as in [include:id foo.qbk]) // then use it as the doc_id. - if (x.id) actions.doc_id = *x.id; + if (x.id) actions.state_.doc_id = *x.id; // update the __FILENAME__ macro - *actions.macro.find("__FILENAME__") = - quickbook::macro(actions.filename.native_file_string()); + *actions.state_.macro.find("__FILENAME__") = + quickbook::macro(actions.state_.filename.native_file_string()); // parse the file - quickbook::parse(actions.filename.native_file_string().c_str(), actions, true); + quickbook::parse(actions.state_.filename.native_file_string().c_str(), actions, true); // restore the values - std::swap(actions.filename, filein); + std::swap(actions.state_.filename, filein); - actions.doc_id.swap(doc_id); + actions.state_.doc_id.swap(doc_id); // restore the macros - actions.macro = macro; + actions.state_.macro = macro; // restore the templates - //~ actions.templates = templates; $$$ fixme $$$ + //~ actions.state_.templates = templates; $$$ fixme $$$ return nothing(); } nothing process(quickbook::actions& actions, import const& x) { - fs::path path = include_search(actions.filename.branch_path(), x.path); + fs::path path = include_search(actions.state_.filename.branch_path(), x.path); std::string ext = fs::extension(path); std::vector storage; - actions.error_count += - load_snippets(path.string(), storage, ext, actions.doc_id); + actions.state_.error_count += + load_snippets(path.string(), storage, ext, actions.state_.doc_id); BOOST_FOREACH(define_template const& definition, storage) { - if (!actions.templates.add(definition)) + if (!actions.state_.templates.add(definition)) { detail::outerr(definition.position.file, definition.position.line) << "Template Redefinition: " << definition.id << std::endl; - ++actions.error_count; + ++actions.state_.error_count; } } diff --git a/block_list.cpp b/block_list.cpp index 489aa27..b1c5f38 100644 --- a/block_list.cpp +++ b/block_list.cpp @@ -10,7 +10,8 @@ #include #include -#include "actions_class.hpp" +#include "actions.hpp" +#include "state.hpp" #include "gen_types.hpp" #include "utils.hpp" @@ -82,7 +83,7 @@ namespace quickbook << "Illegal change of list style near column " << pos.column << ".\n"; detail::outwarn(pos.file,pos.line) << "Ignoring change of list style" << std::endl; - ++actions.error_count; + ++actions.state_.error_count; } } diff --git a/boostbook.cpp b/boostbook.cpp index 81c4a37..2f4e512 100644 --- a/boostbook.cpp +++ b/boostbook.cpp @@ -3,18 +3,18 @@ #include "fwd.hpp" #include "boostbook.hpp" #include "phrase.hpp" -#include "actions_class.hpp" +#include "state.hpp" namespace quickbook { struct output_action { - output_action(quickbook::actions& actions) : actions(actions) {} - quickbook::actions& actions; + output_action(quickbook::state& state) : state(state) {} + quickbook::state& state; template void operator()(T const& x) const { - output(actions, x); + output(state, x); } }; @@ -119,45 +119,45 @@ namespace quickbook } initialize_instance; } - void output(quickbook::actions& actions, std::string const& x) + void output(quickbook::state& state, std::string const& x) { - actions.phrase << x; + state.phrase << x; } - void output(quickbook::actions& actions, char x) + void output(quickbook::state& state, char x) { - actions.phrase << encode(x); + state.phrase << encode(x); } - void output(quickbook::actions& actions, anchor const& x) { - actions.phrase << "\n"; + void output(quickbook::state& state, anchor const& x) { + state.phrase << "\n"; } - void output(quickbook::actions& actions, link const& x) { + void output(quickbook::state& state, link const& x) { boostbook_markup m = markup_map.at(x.type); - actions.phrase << m.pre; - actions.phrase << encode(x.destination); - actions.phrase << "\">"; - actions.phrase << x.content; - actions.phrase << m.post; + state.phrase << m.pre; + state.phrase << encode(x.destination); + state.phrase << "\">"; + state.phrase << x.content; + state.phrase << m.post; } - void output(quickbook::actions& actions, formatted const& x) { + void output(quickbook::state& state, formatted const& x) { boostbook_markup m = markup_map.at(x.type); - actions.phrase << m.pre << x.content << m.post; + state.phrase << m.pre << x.content << m.post; } - void output(quickbook::actions& actions, break_ const& x) { + void output(quickbook::state& state, break_ const& x) { boostbook_markup m = markup_map.at("break"); - actions.phrase << m.pre; + state.phrase << m.pre; } - void output(quickbook::actions& actions, image2 const& x) { - actions.phrase << ""; + void output(quickbook::state& state, image2 const& x) { + state.phrase << ""; - actions.phrase << "first == "alt") continue; - actions.phrase + state.phrase << " " << attr_first->first << "=\"" @@ -173,35 +173,35 @@ namespace quickbook << "\""; } - actions.phrase << ">"; + state.phrase << ">"; image2::attribute_map::const_iterator it = x.attributes.find("alt"); if(it != x.attributes.end()) { // Also add a textobject -- use the basename of the image file. // This will mean we get "alt" attributes of the HTML img. - actions.phrase << ""; - actions.phrase << encode(it->second); - actions.phrase << ""; + state.phrase << ""; + state.phrase << encode(it->second); + state.phrase << ""; } - actions.phrase << ""; + state.phrase << ""; } - void output(quickbook::actions& actions, hr) { - actions.phrase << markup_map.at("hr").pre; + void output(quickbook::state& state, hr) { + state.phrase << markup_map.at("hr").pre; } - void output(quickbook::actions& actions, begin_section2 const& x) { - actions.phrase << "\n
\n"; + void output(quickbook::state& state, begin_section2 const& x) { + state.phrase << "\n
\n"; if(x.linkend.empty()) { - actions.phrase + state.phrase << "" << x.content << "\n" ; } else { - actions.phrase + state.phrase << "" << "<link linkend=\"" << x.linkend @@ -213,169 +213,169 @@ namespace quickbook } } - void output(quickbook::actions& actions, end_section2 const& x) { - actions.phrase << "</section>"; + void output(quickbook::state& state, end_section2 const& x) { + state.phrase << "</section>"; } - void output(quickbook::actions& actions, heading2 const& x) { - actions.phrase + void output(quickbook::state& state, heading2 const& x) { + state.phrase << "<anchor id=\"" << x.id << "\"/>" << "<bridgehead renderas=\"sect" << x.level << "\">"; if(x.linkend.empty()) { - actions.phrase << x.content; + state.phrase << x.content; } else { - actions.phrase + state.phrase << "<link linkend=\"" << x.linkend << "\">" << x.content << "</link>"; } - actions.phrase << "</bridgehead>"; + state.phrase << "</bridgehead>"; } - void output(quickbook::actions& actions, variablelist const& x) + void output(quickbook::state& state, variablelist const& x) { - actions.phrase << "<variablelist>\n"; + state.phrase << "<variablelist>\n"; - actions.phrase << "<title>"; - actions.phrase << encode(x.title); - actions.phrase << "\n"; + state.phrase << ""; + state.phrase << encode(x.title); + state.phrase << "\n"; boostbook_markup m = markup_map.at("varlistentry"); for(std::vector::const_iterator it = x.entries.begin(); it != x.entries.end(); ++it) { - actions.phrase << m.pre; - std::for_each(it->begin(), it->end(), output_action(actions)); - actions.phrase << m.post; + state.phrase << m.pre; + std::for_each(it->begin(), it->end(), output_action(state)); + state.phrase << m.post; } - actions.phrase << "\n"; + state.phrase << "\n"; } - void output(quickbook::actions& actions, table2 const& x) + void output(quickbook::state& state, table2 const& x) { if (x.title) { - actions.phrase << "\n"; - actions.phrase << ""; - actions.phrase << encode(*x.title); - actions.phrase << ""; + state.phrase << " id=\"" << *x.id << "\""; + state.phrase << ">\n"; + state.phrase << ""; + state.phrase << encode(*x.title); + state.phrase << ""; } else { - actions.phrase << "\n"; + state.phrase << " id=\"" << *x.id << "\""; + state.phrase << ">\n"; } // This is a bit odd for backwards compatability: the old version just // used the last count that was calculated. - actions.phrase << "\n"; + state.phrase << "\n"; boostbook_markup m = markup_map.at("row"); if (x.head) { - actions.phrase << ""; - actions.phrase << m.pre; - std::for_each(x.head->begin(), x.head->end(), actions.process); - actions.phrase << m.post; - actions.phrase << "\n"; + state.phrase << ""; + state.phrase << m.pre; + std::for_each(x.head->begin(), x.head->end(), output_action(state)); + state.phrase << m.post; + state.phrase << "\n"; } - actions.phrase << "\n"; + state.phrase << "\n"; for(std::vector::const_iterator it = x.rows.begin(); it != x.rows.end(); ++it) { - actions.phrase << m.pre; - std::for_each(it->begin(), it->end(), actions.process); - actions.phrase << m.post; + state.phrase << m.pre; + std::for_each(it->begin(), it->end(), output_action(state)); + state.phrase << m.post; } - actions.phrase << "\n" << "\n"; + state.phrase << "\n" << "\n"; if (x.title) { - actions.phrase << "
\n"; + state.phrase << "\n"; } else { - actions.phrase << "\n"; + state.phrase << "\n"; } } - void output(quickbook::actions& actions, xinclude2 const& x) + void output(quickbook::state& state, xinclude2 const& x) { - actions.phrase << "\n\n"; + state.phrase << "\n\n"; } - void output(quickbook::actions& actions, list2 const& x) + void output(quickbook::state& state, list2 const& x) { - actions.phrase << std::string(x.mark == '#' ? "\n" : "\n"); + state.phrase << std::string(x.mark == '#' ? "\n" : "\n"); for(std::vector::const_iterator it = x.items.begin(), end = x.items.end(); it != end; ++it) { - actions.phrase << "\n" << it->content; - if(!it->sublist.items.empty()) output(actions, it->sublist); - actions.phrase << std::string("\n"); + state.phrase << "\n" << it->content; + if(!it->sublist.items.empty()) output(state, it->sublist); + state.phrase << std::string("\n"); } - actions.phrase << std::string(x.mark == '#' ? "\n" : "\n"); + state.phrase << std::string(x.mark == '#' ? "\n" : "\n"); } - void output(quickbook::actions& actions, code_token const& x) + void output(quickbook::state& state, code_token const& x) { std::string type = x.type; if(type == "space") { - actions.phrase << x.text; + state.phrase << x.text; } else { - actions.phrase + state.phrase << "" << encode(x.text) << ""; } } - void output(quickbook::actions& actions, doc_info const& info) + void output(quickbook::state& state, doc_info const& info) { // if we're ignoring the document info, we're done. if (info.ignore) return; - actions.phrase + state.phrase << "\n" << ""; // Document tag - actions.phrase + state.phrase << '<' << info.doc_type << " id=\"" << info.doc_id << "\"\n"; if(info.doc_type == "library") { - actions.phrase << " name=\"" << info.doc_title << "\"\n"; + state.phrase << " name=\"" << info.doc_title << "\"\n"; } if(!info.doc_dirname.empty()) { - actions.phrase << " dirname=\"" << info.doc_dirname << "\"\n"; + state.phrase << " dirname=\"" << info.doc_dirname << "\"\n"; } - actions.phrase + state.phrase << "last-revision=\"" << info.doc_last_revision << "\"" << " xmlns:xi=\"http://www.w3.org/2001/XInclude\""; - actions.phrase << ">"; // end document tag. + state.phrase << ">"; // end document tag. // Title tag @@ -389,35 +389,35 @@ namespace quickbook } // For 'library', the title comes after the info block. - if(info.doc_type != "library") actions.phrase << title; + if(info.doc_type != "library") state.phrase << title; // Info tag - actions.phrase << "<" << info.doc_type << "info>\n"; + state.phrase << "<" << info.doc_type << "info>\n"; if(!info.doc_authors.empty()) { - actions.phrase << "\n"; + state.phrase << "\n"; BOOST_FOREACH(doc_info::author const& author, info.doc_authors) { - actions.phrase + state.phrase << "\n" << "" << author.first << "\n" << "" << author.second << "\n" << "\n"; } - actions.phrase << "\n"; + state.phrase << "\n"; } BOOST_FOREACH(doc_info::copyright_entry const& copyright, info.doc_copyrights) { - actions.phrase << "\n"; + state.phrase << "\n"; BOOST_FOREACH(std::string const& year, copyright.first) { - actions.phrase << "" << year << "\n"; + state.phrase << "" << year << "\n"; } - actions.phrase + state.phrase << "" << copyright.second << "\n" << "\n" ; @@ -425,7 +425,7 @@ namespace quickbook if (!info.doc_license.empty()) { - actions.phrase + state.phrase << "\n" << "\n" << info.doc_license @@ -438,7 +438,7 @@ namespace quickbook if (!info.doc_purpose.empty()) { - actions.phrase + state.phrase << "<" << info.doc_type << "purpose>\n" << info.doc_purpose << "\n" @@ -448,7 +448,7 @@ namespace quickbook if (!info.doc_category.empty()) { - actions.phrase + state.phrase << "<" << info.doc_type << "category name=\"category:" << info.doc_category << "\">\n" @@ -456,20 +456,20 @@ namespace quickbook ; } - actions.phrase + state.phrase << "\n" ; - if(info.doc_type == "library") actions.phrase << title; + if(info.doc_type == "library") state.phrase << title; } - void output(quickbook::actions& actions, doc_info_post const& x) + void output(quickbook::state& state, doc_info_post const& x) { // if we're ignoring the document info, do nothing. if (x.info.ignore) return; // We've finished generating our output. Here's what we'll do // *after* everything else. - actions.phrase << ""; + state.phrase << ""; } } diff --git a/boostbook.hpp b/boostbook.hpp index abb2289..720a805 100644 --- a/boostbook.hpp +++ b/boostbook.hpp @@ -25,33 +25,33 @@ namespace quickbook // Output function for boostbook, these should eventually become an // interface with implementations for boostbook and html. - // They probably shouldn't use quickbook::actions, instead they + // They probably shouldn't use quickbook::state, instead they // should either take a stream/collector to write to, or return their // output by value. - void output(quickbook::actions&, doc_info const&); - void output(quickbook::actions&, doc_info_post const&); + void output(quickbook::state&, doc_info const&); + void output(quickbook::state&, doc_info_post const&); // Note: char is a plain quickbook character, string is an encoded // boostbook string. Oops. - void output(quickbook::actions&, char); - void output(quickbook::actions&, std::string const&); - void output(quickbook::actions&, anchor const&); - void output(quickbook::actions&, link const&); - void output(quickbook::actions&, formatted const&); - void output(quickbook::actions&, break_ const&); - void output(quickbook::actions&, image2 const&); + void output(quickbook::state&, char); + void output(quickbook::state&, std::string const&); + void output(quickbook::state&, anchor const&); + void output(quickbook::state&, link const&); + void output(quickbook::state&, formatted const&); + void output(quickbook::state&, break_ const&); + void output(quickbook::state&, image2 const&); - void output(quickbook::actions&, hr); - void output(quickbook::actions&, begin_section2 const&); - void output(quickbook::actions&, end_section2 const&); - void output(quickbook::actions&, heading2 const&); - void output(quickbook::actions&, variablelist const&); - void output(quickbook::actions&, table2 const&); - void output(quickbook::actions&, xinclude2 const&); - void output(quickbook::actions&, list2 const&); + void output(quickbook::state&, hr); + void output(quickbook::state&, begin_section2 const&); + void output(quickbook::state&, end_section2 const&); + void output(quickbook::state&, heading2 const&); + void output(quickbook::state&, variablelist const&); + void output(quickbook::state&, table2 const&); + void output(quickbook::state&, xinclude2 const&); + void output(quickbook::state&, list2 const&); - void output(quickbook::actions&, code_token const&); + void output(quickbook::state&, code_token const&); std::string encode(std::string const&); std::string encode(char); diff --git a/code_snippet.cpp b/code_snippet.cpp index 1672f94..ffb5c3f 100644 --- a/code_snippet.cpp +++ b/code_snippet.cpp @@ -16,9 +16,9 @@ #include #include #include "utils.hpp" -#include "actions_class.hpp" #include "grammars.hpp" #include "code_snippet_types.hpp" +#include "template.hpp" namespace quickbook { diff --git a/doc_info.cpp b/doc_info.cpp index afdb24f..7049615 100644 --- a/doc_info.cpp +++ b/doc_info.cpp @@ -10,7 +10,8 @@ #include "doc_info.hpp" #include "grammars.hpp" -#include "actions_class.hpp" +#include "actions.hpp" +#include "state.hpp" #include "parse_utils.hpp" #include #include @@ -112,7 +113,7 @@ namespace quickbook | doc_last_revision [member_assign(&doc_info::doc_last_revision)] // This has to be set in actions so that source code in phrases use the // correct encoding. - | doc_source_mode [ph::ref(actions.source_mode) = qi::_1] + | doc_source_mode [ph::ref(actions.state_.source_mode) = qi::_1] ) >> space >> ']' >> +qi::eol ) diff --git a/doc_info_actions.cpp b/doc_info_actions.cpp index 68adc67..486da9c 100644 --- a/doc_info_actions.cpp +++ b/doc_info_actions.cpp @@ -16,7 +16,8 @@ #include "collector.hpp" #include "quickbook.hpp" #include "doc_info_actions.hpp" -#include "actions_class.hpp" +#include "actions.hpp" +#include "state.hpp" #include "utils.hpp" namespace quickbook diff --git a/fwd.hpp b/fwd.hpp index 34d1f8e..614b2d5 100644 --- a/fwd.hpp +++ b/fwd.hpp @@ -16,9 +16,11 @@ namespace quickbook { struct nothing {}; + struct template_stack; struct macro; struct doc_info; + struct state; struct actions; struct code_snippet_actions; diff --git a/phrase.cpp b/phrase.cpp index 6332486..7050f7b 100644 --- a/phrase.cpp +++ b/phrase.cpp @@ -21,7 +21,8 @@ #include "code.hpp" #include "phrase.hpp" #include "grammars.hpp" -#include "actions_class.hpp" +#include "actions.hpp" +#include "template.hpp" #include "parse_utils.hpp" BOOST_FUSION_ADAPT_STRUCT( diff --git a/phrase_actions.cpp b/phrase_actions.cpp index 1e71e34..a8ee57f 100644 --- a/phrase_actions.cpp +++ b/phrase_actions.cpp @@ -10,7 +10,8 @@ #include #include "phrase.hpp" -#include "actions_class.hpp" +#include "actions.hpp" +#include "state.hpp" #include "utils.hpp" #include "code.hpp" #include "boostbook.hpp" @@ -19,7 +20,7 @@ namespace quickbook { nothing process(quickbook::actions& actions, source_mode const& s) { - actions.source_mode = s.mode; + actions.state_.source_mode = s.mode; return nothing(); } @@ -97,12 +98,12 @@ namespace quickbook // TODO: I don't need to save this, do I? std::string save; - actions.phrase.swap(save); + actions.state_.phrase.swap(save); // print the code with syntax coloring std::string str = actions.syntax_p(first_, last_); - actions.phrase.swap(save); + actions.state_.phrase.swap(save); r.type = x.block ? "programlisting" : "code"; r.content = str; diff --git a/phrase_image.cpp b/phrase_image.cpp index 7e6f4fa..8236956 100644 --- a/phrase_image.cpp +++ b/phrase_image.cpp @@ -12,7 +12,8 @@ #include #include "phrase.hpp" #include "gen_types.hpp" -#include "actions_class.hpp" +#include "actions.hpp" +#include "state.hpp" #include "utils.hpp" namespace quickbook @@ -41,7 +42,7 @@ namespace quickbook << "Duplicate image attribute: " << begin->first << std::endl; - ++actions.error_count; + ++actions.state_.error_count; } } diff --git a/process.cpp b/process.cpp index 19ede7c..0224abb 100644 --- a/process.cpp +++ b/process.cpp @@ -25,7 +25,7 @@ namespace quickbook template void process_action::operator()(T const& x) const { - output(actions, process(actions, x)); + output(actions.state_, process(actions, x)); } template @@ -34,11 +34,9 @@ namespace quickbook return x; } - void output(quickbook::actions&, nothing) { + void output(quickbook::state&, nothing) { } - void output(quickbook::actions&, std::string const&); - template void process_action::operator()(formatted const&) const; template void process_action::operator()(source_mode const&) const; template void process_action::operator()(macro const&) const; diff --git a/quickbook.cpp b/quickbook.cpp index d11235e..e0af3c2 100644 --- a/quickbook.cpp +++ b/quickbook.cpp @@ -18,7 +18,8 @@ #include #include "fwd.hpp" #include "quickbook.hpp" -#include "actions_class.hpp" +#include "state.hpp" +#include "actions.hpp" #include "grammars.hpp" #include "post_process.hpp" #include "utils.hpp" @@ -56,7 +57,7 @@ namespace quickbook std::string storage; int err = detail::load(filein_, storage); if (err != 0) { - ++actor.error_count; + ++actor.state_.error_count; return err; } @@ -94,24 +95,25 @@ namespace quickbook file_position const pos = first.get_position(); detail::outerr(pos.file,pos.line) << "Syntax Error near column " << pos.column << ".\n"; - ++actor.error_count; + ++actor.state_.error_count; } - if(actor.error_count) + if(actor.state_.error_count) { detail::outerr(filein_) - << "Error count: " << actor.error_count << ".\n"; + << "Error count: " << actor.state_.error_count << ".\n"; } - return actor.error_count ? 1 : 0; + return actor.state_.error_count ? 1 : 0; } static int parse(char const* filein_, fs::path const& outdir, string_stream& out, bool ignore_docinfo = false) { - actions actor(filein_, outdir, out); + quickbook::state state(filein_, outdir, out); + actions actor(state); bool r = parse(filein_, actor); - if (actor.section_level != 0) + if (actor.state_.section_level != 0) detail::outwarn(filein_) << "Warning missing [endsect] detected at end of file." << std::endl; diff --git a/actions_class.cpp b/state.cpp similarity index 87% rename from actions_class.cpp rename to state.cpp index 2a22958..a0f9859 100644 --- a/actions_class.cpp +++ b/state.cpp @@ -10,7 +10,8 @@ =============================================================================*/ #include -#include "actions_class.hpp" +#include "actions.hpp" +#include "state.hpp" #include "quickbook.hpp" #if (defined(BOOST_MSVC) && (BOOST_MSVC <= 1310)) @@ -21,7 +22,7 @@ namespace quickbook { namespace fs = boost::filesystem; - actions::actions(char const* filein_, fs::path const& outdir_, string_stream& out_) + state::state(char const* filein_, fs::path const& outdir_, string_stream& out_) // header info : doc_id() , doc_title() @@ -42,14 +43,6 @@ namespace quickbook , template_depth(0) , templates() , error_count(0) - - // actions - , process(*this) - , phrase_push(phrase) - , phrase_pop(phrase) - , error(error_count) - - , syntax_p(source_mode, *this) { // turn off __FILENAME__ macro on debug mode = true std::string filename_str = debug_mode ? @@ -64,7 +57,7 @@ namespace quickbook ; } - void actions::push() + void state::push() { state_stack.push( boost::make_tuple( @@ -82,7 +75,7 @@ namespace quickbook templates.push(); } - void actions::pop() + void state::pop() { boost::tie( filename diff --git a/actions_class.hpp b/state.hpp similarity index 79% rename from actions_class.hpp rename to state.hpp index 61aed2d..5ecd337 100644 --- a/actions_class.hpp +++ b/state.hpp @@ -14,18 +14,18 @@ #include #include #include "fwd.hpp" -#include "actions.hpp" #include "collector.hpp" #include "template.hpp" +#include "actions.hpp" namespace quickbook { namespace qi = boost::spirit::qi; namespace fs = boost::filesystem; - struct actions + struct state { - actions(char const* filein_, fs::path const& outdir, string_stream& out_); + state(char const* filein_, fs::path const& outdir, string_stream& out_); /////////////////////////////////////////////////////////////////////////// // State @@ -70,19 +70,7 @@ namespace quickbook // push/pop the states and the streams void push(); void pop(); - - /////////////////////////////////////////////////////////////////////////// - // actions - /////////////////////////////////////////////////////////////////////////// - process_action process; - phrase_push_action phrase_push; - phrase_pop_action phrase_pop; - error_action error; - - syntax_highlight syntax_p; - - element_id_warning_action element_id_warning; - }; + }; } #endif // BOOST_SPIRIT_ACTIONS_CLASS_HPP diff --git a/syntax_highlight.cpp b/syntax_highlight.cpp index 1fbd3b2..ebd2487 100644 --- a/syntax_highlight.cpp +++ b/syntax_highlight.cpp @@ -13,7 +13,7 @@ #include #include #include -#include "actions_class.hpp" +#include "actions.hpp" #include "grammars.hpp" #include "phrase.hpp" #include "utils.hpp" @@ -370,7 +370,7 @@ namespace quickbook std::string syntax_highlight::operator()(iterator first, iterator last) const { - escape_actions.phrase.push(); + escape_actions.phrase_push(); // print the code with syntax coloring if (source_mode == "c++") @@ -393,10 +393,6 @@ namespace quickbook BOOST_ASSERT(0); } - std::string str; - escape_actions.phrase.swap(str); - escape_actions.phrase.pop(); - - return str; + return escape_actions.phrase_pop(); } } diff --git a/template.cpp b/template.cpp index 5fd8139..b268398 100644 --- a/template.cpp +++ b/template.cpp @@ -11,7 +11,7 @@ #include "template.hpp" #include "phrase_actions.hpp" #include "grammars.hpp" -#include "actions_class.hpp" +#include "state.hpp" #include "utils.hpp" #ifdef BOOST_MSVC @@ -268,13 +268,13 @@ namespace quickbook { std::vector empty_params; - if (!actions.templates.add( + if (!actions.state_.templates.add( define_template(*tpl, empty_params, *arg, pos), &scope)) { detail::outerr(pos.file,pos.line) << "Duplicate Symbol Found" << std::endl; - ++actions.error_count; + ++actions.state_.error_count; return std::make_pair(false, tpl); } ++arg; ++tpl; @@ -315,11 +315,11 @@ namespace quickbook simple_phrase_grammar phrase_p(actions); // do a phrase level parse - iterator first(body.begin(), body.end(), actions.filename.native_file_string().c_str()); + iterator first(body.begin(), body.end(), actions.state_.filename.native_file_string().c_str()); first.set_position(template_pos); iterator last(body.end(), body.end()); r = boost::spirit::qi::parse(first, last, phrase_p) && first == last; - actions.phrase.swap(result); + actions.state_.phrase.swap(result); } else { @@ -331,11 +331,11 @@ namespace quickbook body += "\n\n"; while (iter != body.end() && ((*iter == '\r') || (*iter == '\n'))) ++iter; // skip initial newlines - iterator first(iter, body.end(), actions.filename.native_file_string().c_str()); + iterator first(iter, body.end(), actions.state_.filename.native_file_string().c_str()); first.set_position(template_pos); iterator last(body.end(), body.end()); r = boost::spirit::qi::parse(first, last, block_p) && first == last; - actions.phrase.swap(result); + actions.state_.phrase.swap(result); } return r; } @@ -343,13 +343,13 @@ namespace quickbook std::string process(quickbook::actions& actions, call_template const& x) { - ++actions.template_depth; - if (actions.template_depth > actions.max_template_depth) + ++actions.state_.template_depth; + if (actions.state_.template_depth > actions.state_.max_template_depth) { detail::outerr(x.position.file, x.position.line) << "Infinite loop detected" << std::endl; - --actions.template_depth; - ++actions.error_count; + --actions.state_.template_depth; + ++actions.state_.error_count; return ""; } @@ -358,17 +358,17 @@ namespace quickbook // // Note that for quickbook 1.4- this value is just ignored when the // arguments are expanded. - template_scope const& call_scope = actions.templates.top_scope(); + template_scope const& call_scope = actions.state_.templates.top_scope(); std::string result; - actions.push(); // scope the actions' states + actions.state_.push(); // scope the actions' states { // Quickbook 1.4-: When expanding the tempalte continue to use the // current scope (the dynamic scope). // Quickbook 1.5+: Use the scope the template was defined in // (the static scope). if (qbk_version_n >= 105) - actions.templates.set_parent_scope(*x.symbol->parent); + actions.state_.templates.set_parent_scope(*x.symbol->parent); std::vector args = x.args; @@ -376,9 +376,9 @@ namespace quickbook // Break the arguments if (!break_arguments(args, x.symbol->params, x.position)) { - actions.pop(); // restore the actions' states - --actions.template_depth; - ++actions.error_count; + actions.state_.pop(); // restore the actions' states + --actions.state_.template_depth; + ++actions.state_.error_count; return ""; } @@ -392,8 +392,8 @@ namespace quickbook if (!get_arg_result) { - actions.pop(); // restore the actions' states - --actions.template_depth; + actions.state_.pop(); // restore the actions' states + --actions.state_.template_depth; return ""; } @@ -409,15 +409,15 @@ namespace quickbook << x.symbol->body << "------------------end--------------------" << std::endl << std::endl; - actions.pop(); // restore the actions' states - --actions.template_depth; - ++actions.error_count; + actions.state_.pop(); // restore the actions' states + --actions.state_.template_depth; + ++actions.state_.error_count; return ""; } } - actions.pop(); // restore the actions' states - --actions.template_depth; + actions.state_.pop(); // restore the actions' states + --actions.state_.template_depth; return result; }