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-header.cxx | 177 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 cli/runtime-header.cxx (limited to 'cli/runtime-header.cxx') diff --git a/cli/runtime-header.cxx b/cli/runtime-header.cxx new file mode 100644 index 0000000..87cdf11 --- /dev/null +++ b/cli/runtime-header.cxx @@ -0,0 +1,177 @@ +// file : cli/runtime-header.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#include "runtime-header.hxx" + +using namespace std; + +void +generate_runtime_header_decl (context& ctx) +{ + ostream& os (ctx.os); + + os << "#include " << endl + << "#include " << endl + << "#include " << endl + << endl; + + os << "namespace cli" + << "{"; + + // unknown_mode + // + os << "class unknown_mode" + << "{" + << "public:" + << "enum value" + << "{" + << "skip," << endl + << "stop," << endl + << "fail" << endl + << "};" + << "unknown_mode (value v)" << endl + << ": v_ (v) {}" + << "operator value () const {return v_;}" + << "private:" << endl + << "value v_;" + << "};"; + + // Exceptions. + // + + os << "// Exceptions." << endl + << "//" << endl + << endl; + + os << "class exception: public std::exception" + << "{" + << "public:" << endl + << "virtual void" << endl + << "print (std::ostream&) const = 0;" + << "};"; + + os << "inline std::ostream&" << endl + << "operator<< (std::ostream& os, const exception& e)" + << "{" + << "e.print (os);" + << "return os;" + << "}"; + + os << "class unknown_option: public exception" + << "{" + << "public:" << endl + << "virtual" << endl + << "~unknown_option () throw ();" + << endl + << "unknown_option (const std::string& option)" << endl + << ": option_ (option)" + << "{" + << "}" + << "const std::string&" << endl + << "option () const" + << "{" + << "return option_;" + << "}" + << "virtual void" << endl + << "print (std::ostream&) const;" + << endl + << "virtual const char*" << endl + << "what () const throw ();" + << endl + << "private:" << endl + << "std::string option_;" + << "};"; + + os << "class unknown_argument: public exception" + << "{" + << "public:" << endl + << "virtual" << endl + << "~unknown_argument () throw ();" + << endl + << "unknown_argument (const std::string& argument)" << endl + << ": argument_ (argument)" + << "{" + << "}" + << "const std::string&" << endl + << "argument () const" + << "{" + << "return argument_;" + << "}" + << "virtual void" << endl + << "print (std::ostream&) const;" + << endl + << "virtual const char*" << endl + << "what () const throw ();" + << endl + << "private:" << endl + << "std::string argument_;" + << "};"; + + os << "class missing_value: public exception" + << "{" + << "public:" << endl + << "virtual" << endl + << "~missing_value () throw ();" + << endl + << "missing_value (const std::string& option)" << endl + << ": option_ (option)" + << "{" + << "}" + << "const std::string&" << endl + << "option () const" + << "{" + << "return option_;" + << "}" + << "virtual void" << endl + << "print (std::ostream&) const;" + << endl + << "virtual const char*" << endl + << "what () const throw ();" + << endl + << "private:" << endl + << "std::string option_;" + << "};"; + + os << "class invalid_value: public exception" + << "{" + << "public:" << endl + << "virtual" << endl + << "~invalid_value () throw ();" + << endl + << "invalid_value (const std::string& option," << endl + << "const std::string& value)" << endl + << ": option_ (option)," + << " value_ (value)" + << "{" + << "}" + << "const std::string&" << endl + << "option () const" + << "{" + << "return option_;" + << "}" + << "const std::string&" << endl + << "value () const" + << "{" + << "return value_;" + << "}" + << "virtual void" << endl + << "print (std::ostream&) const;" + << endl + << "virtual const char*" << endl + << "what () const throw ();" + << endl + << "private:" << endl + << "std::string option_;" + << "std::string value_;" + << "};"; + + os << "}"; // namespace cli +} + +void +generate_runtime_header_impl (context& ctx) +{ + ostream& os (ctx.os); +} -- cgit v1.1