From 21da9d06bd27196f7bda3dadb81627ffeca91c25 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Thu, 26 Aug 2010 20:57:45 +0000 Subject: [PATCH] Separate grammar compilation. [SVN r65031] --- src/Jamfile.v2 | 3 + src/actions.cpp | 7 +- src/{block.hpp => block_grammar.cpp} | 77 +++++++-------- src/{doc_info.hpp => doc_info_grammar.cpp} | 58 +++++------ src/grammar.hpp | 83 ++++++++++++++++ src/{phrase.hpp => phrase_grammar.cpp} | 108 ++++++++++++--------- src/phrase_grammar.hpp | 45 +++++++++ src/quickbook.cpp | 56 +---------- src/syntax_highlight.cpp | 6 +- src/syntax_highlight.hpp | 2 +- 10 files changed, 269 insertions(+), 176 deletions(-) rename src/{block.hpp => block_grammar.cpp} (96%) rename src/{doc_info.hpp => doc_info_grammar.cpp} (92%) create mode 100644 src/grammar.hpp rename src/{phrase.hpp => phrase_grammar.cpp} (90%) create mode 100644 src/phrase_grammar.hpp diff --git a/src/Jamfile.v2 b/src/Jamfile.v2 index c497fb0..a7b0d2e 100644 --- a/src/Jamfile.v2 +++ b/src/Jamfile.v2 @@ -28,6 +28,9 @@ exe quickbook template_stack.cpp markups.cpp syntax_highlight.cpp + block_grammar.cpp + phrase_grammar.cpp + doc_info_grammar.cpp /boost//program_options /boost//filesystem : #QUICKBOOK_NO_DATES diff --git a/src/actions.cpp b/src/actions.cpp index 7eecb8b..37f85d2 100644 --- a/src/actions.cpp +++ b/src/actions.cpp @@ -22,8 +22,7 @@ #include "./utils.hpp" #include "./markups.hpp" #include "./actions_class.hpp" -#include "./block.hpp" -#include "./phrase.hpp" +#include "./grammar.hpp" #include "./code_snippet.hpp" namespace quickbook @@ -764,7 +763,7 @@ namespace quickbook // do a phrase level parse iterator first(body.content.begin(), body.content.end(), body.position); iterator last(body.content.end(), body.content.end()); - bool r = boost::spirit::classic::parse(first, last, phrase_p).full; + bool r = quickbook::parse(first, last, phrase_p).full; actions.phrase.swap(result); return r; } @@ -779,7 +778,7 @@ namespace quickbook std::string content = body.content + "\n\n"; iterator first(content.begin(), content.end(), body.position); iterator last(content.end(), content.end()); - bool r = boost::spirit::classic::parse(first, last, block_p).full; + bool r = quickbook::parse(first, last, block_p).full; actions.inside_paragraph(); actions.out.swap(result); return r; diff --git a/src/block.hpp b/src/block_grammar.cpp similarity index 96% rename from src/block.hpp rename to src/block_grammar.cpp index e272c21..ebd5752 100644 --- a/src/block.hpp +++ b/src/block_grammar.cpp @@ -7,32 +7,52 @@ 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_BLOCK_HPP) -#define BOOST_SPIRIT_QUICKBOOK_BLOCK_HPP +#include "./phrase_grammar.hpp" #include "./quickbook.hpp" #include "./utils.hpp" -#include "./phrase.hpp" -#include +#include "./actions_class.hpp" #include #include #include #include #include +#include namespace quickbook { using namespace boost::spirit::classic; - struct block_grammar : grammar - { - block_grammar(quickbook::actions& actions_, bool skip_initial_spaces = false) - : actions(actions_), skip_initial_spaces(skip_initial_spaces) { } - template - struct definition + struct block_grammar::definition { - definition(block_grammar const& self) + definition(block_grammar const&); + + bool no_eols; + + rule start_, blocks, block_markup, code, code_line, blank_line, + paragraph, space, blank, comment, headings, h, h1, h2, + h3, h4, h5, h6, hr, blurb, blockquote, admonition, + phrase, list, phrase_end, ordered_list, def_macro, + macro_identifier, table, table_row, variablelist, + varlistentry, varlistterm, varlistitem, table_cell, + preformatted, list_item, begin_section, end_section, + xinclude, include, hard_space, eol, paragraph_end, + template_, template_id, template_formal_arg, + template_body, identifier, dummy_block, import, + inside_paragraph, + element_id, element_id_1_5, element_id_1_6; + + symbols<> paragraph_end_markups; + + phrase_grammar common; + + rule const& + start() const { return start_; } + }; + + template + block_grammar::definition::definition(block_grammar const& self) : no_eols(true) , common(self.actions, no_eols) { @@ -450,32 +470,13 @@ namespace quickbook ; } - bool no_eols; + template + parse_info parse(Iterator& first, Iterator last, Grammar& g) + { + return boost::spirit::classic::parse(first, last, g); + } - rule start_, blocks, block_markup, code, code_line, blank_line, - paragraph, space, blank, comment, headings, h, h1, h2, - h3, h4, h5, h6, hr, blurb, blockquote, admonition, - phrase, list, phrase_end, ordered_list, def_macro, - macro_identifier, table, table_row, variablelist, - varlistentry, varlistterm, varlistitem, table_cell, - preformatted, list_item, begin_section, end_section, - xinclude, include, hard_space, eol, paragraph_end, - template_, template_id, template_formal_arg, - template_body, identifier, dummy_block, import, - inside_paragraph, - element_id, element_id_1_5, element_id_1_6; - - symbols<> paragraph_end_markups; - - phrase_grammar common; - - rule const& - start() const { return start_; } - }; - - quickbook::actions& actions; - bool const skip_initial_spaces; - }; + void instantiate_block_grammar(quickbook::iterator i, block_grammar& g) { + parse(i, i, g); + } } - -#endif // BOOST_SPIRIT_QUICKBOOK_BLOCK_HPP diff --git a/src/doc_info.hpp b/src/doc_info_grammar.cpp similarity index 92% rename from src/doc_info.hpp rename to src/doc_info_grammar.cpp index 79f6c9d..a073e96 100644 --- a/src/doc_info.hpp +++ b/src/doc_info_grammar.cpp @@ -7,32 +7,43 @@ 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_DOC_INFO_HPP) -#define BOOST_SPIRIT_QUICKBOOK_DOC_INFO_HPP -#include "./phrase.hpp" +#include "./phrase_grammar.hpp" #include "./quickbook.hpp" +#include "./actions_class.hpp" #include #include #include #include +#include namespace quickbook { using namespace boost::spirit::classic; - struct doc_info_grammar - : public grammar - { - doc_info_grammar(quickbook::actions& actions) - : actions(actions) {} - template - struct definition + struct doc_info_grammar::definition { + definition(doc_info_grammar const&); + typedef uint_parser uint2_t; - definition(doc_info_grammar const& self) + bool unused; + std::string category; + rule doc_info, doc_title, doc_version, doc_id, doc_dirname, + doc_copyright, doc_purpose, doc_category, doc_authors, + doc_author, comment, space, hard_space, doc_license, + doc_last_revision, doc_source_mode, phrase, quickbook_version, + char_; + phrase_grammar common; + symbols<> doc_types; + + rule const& + start() const { return doc_info; } + }; + + template + doc_info_grammar::definition::definition(doc_info_grammar const& self) : unused(false), common(self.actions, unused) { quickbook::actions& actions = self.actions; @@ -194,23 +205,14 @@ namespace quickbook ; } - bool unused; - std::string category; - rule doc_info, doc_title, doc_version, doc_id, doc_dirname, - doc_copyright, doc_purpose, doc_category, doc_authors, - doc_author, comment, space, hard_space, doc_license, - doc_last_revision, doc_source_mode, phrase, quickbook_version, - char_; - phrase_grammar common; - symbols<> doc_types; - rule const& - start() const { return doc_info; } - }; + template + parse_info parse(Iterator& first, Iterator last, Grammar& g) + { + return boost::spirit::classic::parse(first, last, g); + } - quickbook::actions& actions; - }; + void instantiate_doc_info_grammar(quickbook::iterator i, doc_info_grammar& g) { + parse(i, i, g); + } } - -#endif // BOOST_SPIRIT_QUICKBOOK_DOC_INFO_HPP - diff --git a/src/grammar.hpp b/src/grammar.hpp new file mode 100644 index 0000000..669f4a9 --- /dev/null +++ b/src/grammar.hpp @@ -0,0 +1,83 @@ +/*============================================================================= + Copyright (c) 2002 2004 2006Joel de Guzman + Copyright (c) 2004 Eric Niebler + 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_GRAMMARS_HPP) +#define BOOST_SPIRIT_QUICKBOOK_GRAMMARS_HPP + +#include +#include "./actions.hpp" + +namespace quickbook +{ + using namespace boost::spirit::classic; + + struct doc_info_grammar + : public grammar + { + doc_info_grammar(quickbook::actions& actions) + : actions(actions) {} + + template + struct definition; + + quickbook::actions& actions; + }; + + struct block_grammar : grammar + { + block_grammar(quickbook::actions& actions_, bool skip_initial_spaces = false) + : actions(actions_), skip_initial_spaces(skip_initial_spaces) { } + + template + struct definition; + + quickbook::actions& actions; + bool const skip_initial_spaces; + }; + + struct phrase_grammar : grammar + { + phrase_grammar(quickbook::actions& actions, bool& no_eols) + : no_eols(no_eols), actions(actions) {} + + template + struct definition; + + bool& no_eols; + quickbook::actions& actions; + }; + + struct simple_phrase_grammar : public grammar + { + simple_phrase_grammar(quickbook::actions& actions) + : actions(actions) {} + + template + struct definition; + + quickbook::actions& actions; + }; + + struct command_line_grammar + : public grammar + { + command_line_grammar(quickbook::actions& actions) + : actions(actions) {} + + template + struct definition; + + quickbook::actions& actions; + }; + + template + parse_info parse(Iterator&, Iterator, Grammar&); +} + +#endif \ No newline at end of file diff --git a/src/phrase.hpp b/src/phrase_grammar.cpp similarity index 90% rename from src/phrase.hpp rename to src/phrase_grammar.cpp index 0e0f211..4cd1cb4 100644 --- a/src/phrase.hpp +++ b/src/phrase_grammar.cpp @@ -7,12 +7,11 @@ 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_PHRASE_HPP) -#define BOOST_SPIRIT_QUICKBOOK_PHRASE_HPP +#include "./phrase_grammar.hpp" #include "./quickbook.hpp" #include "./actions_class.hpp" -#include "utils.hpp" +#include "./utils.hpp" #include #include #include @@ -59,15 +58,8 @@ namespace quickbook ; } - struct phrase_grammar : grammar - { - phrase_grammar(quickbook::actions& actions, bool& no_eols) - : no_eols(no_eols), actions(actions) {} - - template - struct definition - { - definition(phrase_grammar const& self) + template + phrase_grammar::definition::definition(phrase_grammar const& self) { using detail::var; quickbook::actions& actions = self.actions; @@ -470,37 +462,8 @@ namespace quickbook ; } - rule space, blank, comment, phrase, phrase_markup, image, - simple_phrase_end, phrase_end, bold, italic, underline, teletype, - strikethrough, escape, url, common, funcref, classref, - memberref, enumref, macroref, headerref, conceptref, globalref, - anchor, link, hard_space, eol, inline_code, simple_format, - simple_bold, simple_italic, simple_underline, - simple_teletype, source_mode, template_, - quote, code_block, footnote, replaceable, macro, - dummy_block, cond_phrase, macro_identifier, template_args, - template_args_1_4, template_arg_1_4, - template_inner_arg_1_4, brackets_1_4, - template_args_1_5, template_arg_1_5, - template_inner_arg_1_5, brackets_1_5 - ; - - rule const& - start() const { return common; } - }; - - bool& no_eols; - quickbook::actions& actions; - }; - - struct simple_phrase_grammar - : public grammar - { - simple_phrase_grammar(quickbook::actions& actions) - : actions(actions) {} - template - struct definition + struct simple_phrase_grammar::definition { definition(simple_phrase_grammar const& self) : unused(false), common(self.actions, unused) @@ -531,9 +494,60 @@ namespace quickbook start() const { return phrase; } }; - quickbook::actions& actions; - }; + template + struct command_line_grammar::definition + { + definition(command_line_grammar const& self) + : unused(false), common(self.actions, unused) + { + quickbook::actions& actions = self.actions; + + macro = + *space_p + >> macro_identifier [actions.macro_identifier] + >> *space_p + >> ( '=' + >> *space_p + >> phrase [actions.macro_definition] + >> *space_p + ) + | eps_p [actions.macro_definition] + ; + + macro_identifier = + +(anychar_p - (space_p | ']' | '=')) + ; + + phrase = + *( common + | (anychar_p - ']') [actions.plain_char] + ) + ; + } + + bool unused; + rule macro, macro_identifier, phrase; + phrase_grammar common; + + rule const& + start() const { return macro; } + }; + + template + parse_info parse(Iterator& first, Iterator last, Grammar& g) + { + return boost::spirit::classic::parse(first, last, g); + } + + void instantiate_phrase_grammar(quickbook::iterator i, phrase_grammar& g) { + parse(i, i, g); + } + + void instantiate_simple_phrase_grammar(quickbook::iterator i, simple_phrase_grammar& g) { + parse(i, i, g); + } + + void instantiate_command_line_grammar(quickbook::iterator i, command_line_grammar& g) { + parse(i, i, g); + } } - -#endif // BOOST_SPIRIT_QUICKBOOK_PHRASE_HPP - diff --git a/src/phrase_grammar.hpp b/src/phrase_grammar.hpp new file mode 100644 index 0000000..3425e21 --- /dev/null +++ b/src/phrase_grammar.hpp @@ -0,0 +1,45 @@ +/*============================================================================= + Copyright (c) 2002 2004 2006 Joel de Guzman + Copyright (c) 2004 Eric Niebler + 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_PHRASE_HPP) +#define BOOST_SPIRIT_QUICKBOOK_PHRASE_HPP + +#include "./grammar.hpp" + +namespace quickbook +{ + using namespace boost::spirit::classic; + + template + struct phrase_grammar::definition + { + definition(phrase_grammar const& self); + + rule space, blank, comment, phrase, phrase_markup, image, + simple_phrase_end, phrase_end, bold, italic, underline, teletype, + strikethrough, escape, url, common, funcref, classref, + memberref, enumref, macroref, headerref, conceptref, globalref, + anchor, link, hard_space, eol, inline_code, simple_format, + simple_bold, simple_italic, simple_underline, + simple_teletype, source_mode, template_, + quote, code_block, footnote, replaceable, macro, + dummy_block, cond_phrase, macro_identifier, template_args, + template_args_1_4, template_arg_1_4, + template_inner_arg_1_4, brackets_1_4, + template_args_1_5, template_arg_1_5, + template_inner_arg_1_5, brackets_1_5 + ; + + rule const& + start() const { return common; } + }; +} + +#endif // BOOST_SPIRIT_QUICKBOOK_PHRASE_HPP + diff --git a/src/quickbook.cpp b/src/quickbook.cpp index f5e064b..ee0598f 100644 --- a/src/quickbook.cpp +++ b/src/quickbook.cpp @@ -7,10 +7,9 @@ License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) =============================================================================*/ +#include "./grammar.hpp" #include "./quickbook.hpp" #include "./actions_class.hpp" -#include "./block.hpp" -#include "./doc_info.hpp" #include "./post_process.hpp" #include "./utils.hpp" #include "./input_path.hpp" @@ -45,59 +44,6 @@ namespace quickbook std::vector include_path; std::vector preset_defines; - /////////////////////////////////////////////////////////////////////////// - // - // Parse the macros passed as command line parameters - // - /////////////////////////////////////////////////////////////////////////// - struct command_line_grammar - : public grammar - { - command_line_grammar(quickbook::actions& actions) - : actions(actions) {} - - template - struct definition - { - definition(command_line_grammar const& self) - : unused(false), common(self.actions, unused) - { - quickbook::actions& actions = self.actions; - - macro = - *space_p - >> macro_identifier [actions.macro_identifier] - >> *space_p - >> ( '=' - >> *space_p - >> phrase [actions.macro_definition] - >> *space_p - ) - | eps_p [actions.macro_definition] - ; - - macro_identifier = - +(anychar_p - (space_p | ']' | '=')) - ; - - phrase = - *( common - | (anychar_p - ']') [actions.plain_char] - ) - ; - } - - bool unused; - rule macro, macro_identifier, phrase; - phrase_grammar common; - - rule const& - start() const { return macro; } - }; - - quickbook::actions& actions; - }; - static void set_macros(actions& actor) { quickbook::command_line_grammar grammar(actor); diff --git a/src/syntax_highlight.cpp b/src/syntax_highlight.cpp index f8771c8..f4c1183 100644 --- a/src/syntax_highlight.cpp +++ b/src/syntax_highlight.cpp @@ -49,17 +49,17 @@ namespace quickbook if (source_mode == "c++") { cpp_p_type cpp_p(temp, macro, do_macro_action(temp), escape_actions); - parse(first, last, cpp_p); + boost::spirit::classic::parse(first, last, cpp_p); } else if (source_mode == "python") { python_p_type python_p(temp, macro, do_macro_action(temp), escape_actions); - parse(first, last, python_p); + boost::spirit::classic::parse(first, last, python_p); } else if (source_mode == "teletype") { teletype_p_type teletype_p(temp, macro, do_macro_action(temp), escape_actions); - parse(first, last, teletype_p); + boost::spirit::classic::parse(first, last, teletype_p); } else { diff --git a/src/syntax_highlight.hpp b/src/syntax_highlight.hpp index 48c91eb..dd79cb2 100644 --- a/src/syntax_highlight.hpp +++ b/src/syntax_highlight.hpp @@ -15,7 +15,7 @@ #include #include #include -#include "./phrase.hpp" +#include "./phrase_grammar.hpp" namespace quickbook {