From 287fac00da42d9393589fdf8ea0672ab20da0504 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Wed, 31 Mar 2010 21:40:45 +0000 Subject: [PATCH] Command line macros. Needs error checking. [SVN r60981] --- detail/actions.hpp | 1 + detail/quickbook.cpp | 83 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/detail/actions.hpp b/detail/actions.hpp index acfea62..89e9959 100644 --- a/detail/actions.hpp +++ b/detail/actions.hpp @@ -43,6 +43,7 @@ namespace quickbook extern tm* current_gm_time; // the current UTC time extern bool debug_mode; extern std::vector include_path; + extern std::vector preset_defines; // forward declarations struct actions; diff --git a/detail/quickbook.cpp b/detail/quickbook.cpp index d5be0fa..938397b 100644 --- a/detail/quickbook.cpp +++ b/detail/quickbook.cpp @@ -43,6 +43,79 @@ namespace quickbook unsigned qbk_version_n = 0; // qbk_major_version * 100 + qbk_minor_version bool ms_errors = false; // output errors/warnings as if for VS std::vector include_path; + std::vector preset_defines; + + /////////////////////////////////////////////////////////////////////////// + // + // Parse the macros passed as command line parameters + // + /////////////////////////////////////////////////////////////////////////// + template + struct command_line_grammar + : public grammar > + { + command_line_grammar(Actions& actions) + : actions(actions) {} + + template + struct definition + { + definition(command_line_grammar const& self) + : unused(false), common(self.actions, unused) + { + 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; } + }; + + Actions& actions; + }; + + static void set_macros(actions& actor) + { + quickbook::command_line_grammar grammar(actor); + + for(std::vector::const_iterator + it = preset_defines.begin(), + end = preset_defines.end(); + it != end; ++it) + { + typedef position_iterator iterator_type; + iterator_type first(it->begin(), it->end(), "command line parameter"); + iterator_type last(it->end(), it->end()); + + parse(first, last, grammar); + // TODO: Check result? + } + } /////////////////////////////////////////////////////////////////////////// // @@ -103,6 +176,7 @@ namespace quickbook parse(char const* filein_, fs::path const& outdir, string_stream& out, bool ignore_docinfo = false) { actions actor(filein_, outdir, out); + set_macros(actor); bool r = parse(filein_, actor); if (actor.section_level != 0) detail::outwarn(filein_) @@ -177,6 +251,7 @@ main(int argc, char* argv[]) ("debug", "debug mode (for developers)") ("ms-errors", "use Microsoft Visual Studio style error & warn message format") ("include-path,I", value< std::vector >(), "include path") + ("define,D", value< std::vector >(), "define macro") ; positional_options_description p; @@ -247,6 +322,12 @@ main(int argc, char* argv[]) = std::vector(paths.begin(), paths.end()); } + if (vm.count("define")) + { + quickbook::preset_defines + = vm["define"].as >(); + } + if (vm.count("input-file")) { std::string filein @@ -274,7 +355,7 @@ main(int argc, char* argv[]) quickbook::detail::outerr("") << "Error: No filename given\n\n" << desc << std::endl; return 1; - } + } } catch(std::exception& e)