From 4b1e037fb0f763cd3e3401d71b66269440d75dbf Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sun, 27 Sep 2009 18:20:49 +0200 Subject: Generate parsing constructors and parsing code Also generate some runtime support code such exceptions, value parsers, etc. --- cli/runtime-source.cxx | 177 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 cli/runtime-source.cxx (limited to 'cli/runtime-source.cxx') diff --git a/cli/runtime-source.cxx b/cli/runtime-source.cxx new file mode 100644 index 0000000..3cb5f2e --- /dev/null +++ b/cli/runtime-source.cxx @@ -0,0 +1,177 @@ +// file : cli/runtime-source.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#include "runtime-source.hxx" + +using namespace std; + +void +generate_runtime_source (context& ctx) +{ + ostream& os (ctx.os); + + os << "#include " << endl + << "#include " << endl + << "#include " << endl + << "#include " << endl + << endl; + + os << "namespace cli" + << "{"; + + // unknown_option + // + os << "// unknown_option" << endl + << "//" << endl + << "unknown_option::" << endl + << "~unknown_option () throw ()" + << "{" + << "}" + << "void unknown_option::" << endl + << "print (std::ostream& os) const" + << "{" + << "os << \"unknown option '\" << option () << \"'\";" + << "}" + << "const char* unknown_option::" << endl + << "what () const throw ()" + << "{" + << "return \"unknown option\";" + << "}"; + + // unknown_argument + // + os << "// unknown_argument" << endl + << "//" << endl + << "unknown_argument::" << endl + << "~unknown_argument () throw ()" + << "{" + << "}" + << "void unknown_argument::" << endl + << "print (std::ostream& os) const" + << "{" + << "os << \"unknown argument '\" << argument () << \"'\";" + << "}" + << "const char* unknown_argument::" << endl + << "what () const throw ()" + << "{" + << "return \"unknown argument\";" + << "}"; + + // missing_value + // + os << "// missing_value" << endl + << "//" << endl + << "missing_value::" << endl + << "~missing_value () throw ()" + << "{" + << "}" + << "void missing_value::" << endl + << "print (std::ostream& os) const" + << "{" + << "os << \"missing value for option '\" << option () << \"'\";" + << "}" + << "const char* missing_value::" << endl + << "what () const throw ()" + << "{" + << "return \"missing option value\";" + << "}"; + + // invalid_value + // + os << "// invalid_value" << endl + << "//" << endl + << "invalid_value::" << endl + << "~invalid_value () throw ()" + << "{" + << "}" + << "void invalid_value::" << endl + << "print (std::ostream& os) const" + << "{" + << "os << \"invalid value '\" << value () << \"' for option '\"" << endl + << " << option () << \"'\";" + << "}" + << "const char* invalid_value::" << endl + << "what () const throw ()" + << "{" + << "return \"invalid option value\";" + << "}"; + + // parser class template & its specializations + // + os << "template " << endl + << "struct parser" + << "{" + << "static int" << endl + << "parse (X& x, char** argv, int n)" + << "{" + << "if (n > 1)" + << "{" + << "std::istringstream is (argv[1]);" + << "if (!(is >> x && is.eof ()))" << endl + << "throw invalid_value (argv[0], argv[1]);" + << "return 2;" + << "}" + << "else" << endl + << "throw missing_value (argv[0]);" + << "}" + << "};"; + + // parser + // + os << "template <>" << endl + << "struct parser" + << "{" + << "static int" << endl + << "parse (bool& x, char**, int)" + << "{" + << "x = true;" + << "return 1;" + << "}" + << "};"; + + // parser + // + os << "template <>" << endl + << "struct parser" + << "{" + << "static int" << endl + << "parse (std::string& x, char** argv, int n)" + << "{" + << "if (n > 1)" + << "{" + << "x = argv[1];" + << "return 2;" + << "}" + << "else" << endl + << "throw missing_value (argv[0]);" + << "}" + << "};"; + + // parser> + // + os << "template " << endl + << "struct parser >" + << "{" + << "static int" << endl + << "parse (std::vector& v, char** argv, int n)" + << "{" + << "X x;" + << "int i (parser::parse (x, argv, n));" + << "v.push_back (x);" + << "return i;" + << "}" + << "};"; + + // Parser thunk. + // + os << "template " << endl + << "int" << endl + << "thunk (X& x, char** argv, int n)" + << "{" + << "return parser::parse (x.*P, argv, n);" + << "}"; + + os << "}"; // namespace cli +} -- cgit v1.1