summaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2009-10-04 10:49:52 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2009-10-04 10:49:52 +0200
commit68803e8bc8888abdc65cba1a1a6d8b4e9eeea27e (patch)
tree3191a743f371d648c6f4c4dd9dc074b941de938e /cli
parentb0cbea713f15e511630d2626c4694f119206dc94 (diff)
Add a parser for std::map
Diffstat (limited to 'cli')
-rw-r--r--cli/runtime-source.cxx59
1 files changed, 58 insertions, 1 deletions
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 <string>" << endl
+ os << "#include <map>" << endl
+ << "#include <string>" << endl
<< "#include <vector>" << endl
<< "#include <ostream>" << endl
<< "#include <sstream>" << endl
@@ -164,6 +165,62 @@ generate_runtime_source (context& ctx)
<< "}"
<< "};";
+ // parser<std::map<K,V>>
+ //
+ os << "template <typename K, typename V>" << endl
+ << "struct parser<std::map<K, V> >"
+ << "{"
+ << "static int" << endl
+ << "parse (std::map<K, V>& 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 <typename X, typename T, T X::*P>" << endl