From 68803e8bc8888abdc65cba1a1a6d8b4e9eeea27e Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sun, 4 Oct 2009 10:49:52 +0200 Subject: Add a parser for std::map --- cli/runtime-source.cxx | 59 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) (limited to 'cli') diff --git a/cli/runtime-source.cxx b/cli/runtime-source.cxx index 3cb5f2e..13718b7 100644 --- a/cli/runtime-source.cxx +++ b/cli/runtime-source.cxx @@ -12,7 +12,8 @@ generate_runtime_source (context& ctx) { ostream& os (ctx.os); - os << "#include " << endl + os << "#include " << endl + << "#include " << endl << "#include " << endl << "#include " << endl << "#include " << endl @@ -164,6 +165,62 @@ generate_runtime_source (context& ctx) << "}" << "};"; + // parser> + // + os << "template " << endl + << "struct parser >" + << "{" + << "static int" << endl + << "parse (std::map& m, char** argv, int n)" + << "{" + << "if (n > 1)" + << "{" + << "std::string s (argv[1]);" + << "std::string::size_type p (s.find ('='));" + << endl + << "if (p == std::string::npos)" + << "{" + << "K k = K ();" + << endl + << "if (!s.empty ())" + << "{" + << "std::istringstream ks (s);" + << endl + << "if (!(ks >> k && ks.eof ()))" << endl + << "throw invalid_value (argv[0], argv[1]);" + << "}" + << "m[k] = V ();" + << "}" + << "else" + << "{" + << "K k = K ();" + << "V v = V ();" + << "std::string kstr (s, 0, p);" + << "std::string vstr (s, p + 1);" + << endl + << "if (!kstr.empty ())" + << "{" + << "std::istringstream ks (kstr);" + << endl + << "if (!(ks >> k && ks.eof ()))" << endl + << "throw invalid_value (argv[0], argv[1]);" + << "}" + << "if (!vstr.empty ())" + << "{" + << "std::istringstream vs (vstr);" + << endl + << "if (!(vs >> v && vs.eof ()))" << endl + << "throw invalid_value (argv[0], argv[1]);" + << "}" + << "m[k] = v;" + << "}" + << "return 2;" + << "}" + << "else" << endl + << "throw missing_value (argv[0]);" + << "}" + << "};"; + // Parser thunk. // os << "template " << endl -- cgit v1.1