From dca38b27afc25d329fd7a7241095b40e2a1ecae2 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sat, 27 Jul 2019 11:38:53 +0200 Subject: Add support for option merging (--generate-merge) --- cli/runtime-source.cxx | 263 ++++++++++++++++++++++++++++--------------------- 1 file changed, 150 insertions(+), 113 deletions(-) (limited to 'cli/runtime-source.cxx') diff --git a/cli/runtime-source.cxx b/cli/runtime-source.cxx index 778bed4..54a085f 100644 --- a/cli/runtime-source.cxx +++ b/cli/runtime-source.cxx @@ -802,166 +802,203 @@ generate_runtime_source (context& ctx, bool complete) // generated header file, we always generate the following templates // in the source file. // - bool sp (ctx.specifier); + bool sp (ctx.gen_specifier); + bool gen_merge (ctx.gen_merge); // parser class template & its specializations // os << "template " << endl << "struct parser" - << "{" - << "static void" << endl - << "parse (X& x, " << (sp ? "bool& xs, " : "") << "scanner& s)" - << "{" - << "using namespace std;" - << endl - << "const char* o (s.next ());" - << endl - << "if (s.more ())" - << "{" - << "string v (s.next ());" - << "istringstream is (v);" - << "if (!(is >> x && " << - "is.peek () == istringstream::traits_type::eof ()))" << endl - << "throw invalid_value (o, v);" - << "}" - << "else" << endl - << "throw missing_value (o);"; - + << "{"; + + os << "static void" << endl + << "parse (X& x, " << (sp ? "bool& xs, " : "") << "scanner& s)" + << "{" + << "using namespace std;" + << endl + << "const char* o (s.next ());" + << "if (s.more ())" + << "{" + << "string v (s.next ());" + << "istringstream is (v);" + << "if (!(is >> x && is.peek () == istringstream::traits_type::eof ()))" << endl + << "throw invalid_value (o, v);" + << "}" + << "else" << endl + << "throw missing_value (o);"; if (sp) os << endl - << "xs = true;"; + << "xs = true;"; + os << "}"; - os << "}" - << "};"; + if (gen_merge) + os << "static void" << endl + << "merge (X& b, const X& a)" + << "{" + << "b = a;" + << "}"; + + os << "};"; // parser // os << "template <>" << endl << "struct parser" - << "{" - << "static void" << endl - << "parse (bool& x, scanner& s)" - << "{" - << "s.next ();" - << "x = true;" - << "}" - << "};"; + << "{"; + + os << "static void" << endl + << "parse (bool& x, scanner& s)" + << "{" + << "s.next ();" + << "x = true;" + << "}"; + + if (gen_merge) + os << "static void" << endl + << "merge (bool& b, const bool&)" + << "{" + << "b = true;" // We wouldn't be here if a is false. + << "}"; + + os << "};"; // parser // os << "template <>" << endl << "struct parser" - << "{" - << "static void" << endl - << "parse (std::string& x, " << (sp ? "bool& xs, " : "") << "scanner& s)" - << "{" - << "const char* o (s.next ());" - << endl - << "if (s.more ())" << endl - << "x = s.next ();" - << "else" << endl - << "throw missing_value (o);"; - + << "{"; + + os << "static void" << endl + << "parse (std::string& x, " << (sp ? "bool& xs, " : "") << "scanner& s)" + << "{" + << "const char* o (s.next ());" + << endl + << "if (s.more ())" << endl + << "x = s.next ();" + << "else" << endl + << "throw missing_value (o);"; if (sp) - os << endl + os << endl << "xs = true;"; + os << "}"; - os << "}" - << "};"; + if (gen_merge) + os << "static void" << endl + << "merge (std::string& b, const std::string& a)" + << "{" + << "b = a;" + << "}"; + + os << "};"; // parser> // os << "template " << endl << "struct parser >" - << "{" - << "static void" << endl - << "parse (std::vector& c, " << (sp ? "bool& xs, " : "") << - "scanner& s)" - << "{" - << "X x;"; + << "{"; + os << "static void" << endl + << "parse (std::vector& c, " << (sp ? "bool& xs, " : "") << "scanner& s)" + << "{" + << "X x;"; if (sp) - os << "bool dummy;"; - - os << "parser::parse (x, " << (sp ? "dummy, " : "") << "s);" - << "c.push_back (x);"; - + os << "bool dummy;"; + os << "parser::parse (x, " << (sp ? "dummy, " : "") << "s);" + << "c.push_back (x);"; if (sp) - os << "xs = true;"; + os << "xs = true;"; + os << "}"; + + if (gen_merge) + os << "static void" << endl + << "merge (std::vector& b, const std::vector& a)" + << "{" + << "b.insert (b.end (), a.begin (), a.end ());" + << "}"; - os << "}" - << "};"; + os << "};"; // parser> // os << "template " << endl << "struct parser >" - << "{" - << "static void" << endl - << "parse (std::set& c, " << (sp ? "bool& xs, " : "") << "scanner& s)" - << "{" - << "X x;"; + << "{"; + os << "static void" << endl + << "parse (std::set& c, " << (sp ? "bool& xs, " : "") << "scanner& s)" + << "{" + << "X x;"; if (sp) - os << "bool dummy;"; - - os << "parser::parse (x, " << (sp ? "dummy, " : "") << "s);" - << "c.insert (x);"; - + os << "bool dummy;"; + os << "parser::parse (x, " << (sp ? "dummy, " : "") << "s);" + << "c.insert (x);"; if (sp) - os << "xs = true;"; + os << "xs = true;"; + os << "}"; + + if (gen_merge) + os << "static void" << endl + << "merge (std::set& b, const std::set& a)" + << "{" + << "b.insert (a.begin (), a.end ());" + << "}"; - os << "}" - << "};"; + os << "};"; // parser> // os << "template " << endl << "struct parser >" - << "{" - << "static void" << endl - << "parse (std::map& m, " << (sp ? "bool& xs, " : "") << - "scanner& s)" - << "{" - << "const char* o (s.next ());" - << endl - << "if (s.more ())" - << "{" - << "std::string ov (s.next ());" - << "std::string::size_type p = ov.find ('=');" - << endl - << "K k = K ();" - << "V v = V ();" - << "std::string kstr (ov, 0, p);" - << "std::string vstr (ov, (p != std::string::npos ? p + 1 : ov.size ()));" - << endl - << "int ac (2);" - << "char* av[] = {const_cast (o), 0};"; + << "{"; + + os << "static void" << endl + << "parse (std::map& m, " << (sp ? "bool& xs, " : "") << "scanner& s)" + << "{" + << "const char* o (s.next ());" + << endl + << "if (s.more ())" + << "{" + << "std::string ov (s.next ());" + << "std::string::size_type p = ov.find ('=');" + << endl + << "K k = K ();" + << "V v = V ();" + << "std::string kstr (ov, 0, p);" + << "std::string vstr (ov, (p != std::string::npos ? p + 1 : ov.size ()));" + << endl + << "int ac (2);" + << "char* av[] = {const_cast (o), 0};"; if (sp) - os << "bool dummy;"; - os << "if (!kstr.empty ())" - << "{" - << "av[1] = const_cast (kstr.c_str ());" - << "argv_scanner s (0, ac, av);" - << "parser::parse (k, " << (sp ? "dummy, " : "") << "s);" - << "}" - << "if (!vstr.empty ())" - << "{" - << "av[1] = const_cast (vstr.c_str ());" - << "argv_scanner s (0, ac, av);" - << "parser::parse (v, " << (sp ? "dummy, " : "") << "s);" - << "}" - << "m[k] = v;" - << "}" - << "else" << endl - << "throw missing_value (o);"; - + os << "bool dummy;"; + os << "if (!kstr.empty ())" + << "{" + << "av[1] = const_cast (kstr.c_str ());" + << "argv_scanner s (0, ac, av);" + << "parser::parse (k, " << (sp ? "dummy, " : "") << "s);" + << "}" + << "if (!vstr.empty ())" + << "{" + << "av[1] = const_cast (vstr.c_str ());" + << "argv_scanner s (0, ac, av);" + << "parser::parse (v, " << (sp ? "dummy, " : "") << "s);" + << "}" + << "m[k] = v;" + << "}" + << "else" << endl + << "throw missing_value (o);"; if (sp) os << endl - << "xs = true;"; + << "xs = true;"; + os << "}"; + + if (gen_merge) + os << "static void" << endl + << "merge (std::map& b, const std::map& a)" + << "{" + << "b.insert (a.begin (), a.end ());" + << "}"; - os << "}" - << "};"; + os << "};"; // Parser thunk. // @@ -972,7 +1009,7 @@ generate_runtime_source (context& ctx, bool complete) << "parser::parse (x.*M, s);" << "}"; - if (ctx.specifier) + if (ctx.gen_specifier) os << "template " << endl << "void" << endl << "thunk (X& x, scanner& s)" -- cgit v1.1