From: myun2 Date: Mon, 11 Jun 2012 04:26:32 +0000 (+0900) Subject: lexical2/to_string.hpp Add. X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=b8ef4af8bfe1042bbffbf5e94a524d53e87e018a;p=roast%2Froast.git lexical2/to_string.hpp Add. --- diff --git a/roast/include/roast/lexical2/to_string.hpp b/roast/include/roast/lexical2/to_string.hpp new file mode 100644 index 00000000..55f45817 --- /dev/null +++ b/roast/include/roast/lexical2/to_string.hpp @@ -0,0 +1,157 @@ +// Roast+ License +/* +*/ +#ifndef __SFJP_ROAST__lexical2__to_string_HPP__ +#define __SFJP_ROAST__lexical2__to_string_HPP__ + +//#include // str_generate() +#include "roast/lexical/iterator_util.hpp" +#include "roast/stream/string_stream.hpp" +#include "roast/stream/console_stream.hpp" +//#include + +namespace roast +{ + namespace lexical + { + ////////////////////////////////////////////////////////////////////////////////// + + // Null Document Class (for Not Used Document Only!!) + + template + class null_document_ + { + public: + template + bool operator << (T){ return true; } + }; + /*static*/ null_document_ null_document; + + ////////////////////////////////////////////////////////////////////////////////// + + // Analyze Utility Functions. + + template + bool analyze(const _It& it, _Param& param) + { + ::roast::lexical::rule::work_iterator<_Rule> r; + return r.analyze(it, param); + } + + template + bool parse(const _It& it, _Param& param){ return analyze<_Rule>(it, param); } + + ///// + + template + bool str_analyze(const _It& it, _Param& param) + { + using namespace ::roast::lexical::rule; + typedef work_iterator > _StrRule; + return analyze<_StrRule>(it, param); + } + + template + bool str_parse(const _It& it, _Param& param){ return str_analyze<_Rule>(it, param); } + + ////////////////////////////////////////////////////////////////////////////////// + + // Generate Utility Functions. + + template + bool generate(_Strm& strm, _Document& doc = null_document) + { + _Rule r; + return r.generate(strm,doc); + } + ///// + + template + ::std::string str_generate() + { + ::std::string s; + ::roast::string_output_stream ss(s); + rule::false_ret_to_exception<_Rule> r; + r.generate(ss, null_document); + return s; + } + template + ::std::string to_string(){ + return str_generate<_Rule, _Document>(); + } + + /// + template + ::std::string str_generate(_Document& doc) + { + ::std::string s; + ::roast::string_output_stream ss(s); + rule::false_ret_to_exception<_Rule> r; + r.generate(ss,doc); + return s; + } + template + ::std::string to_string(_Document& doc){ + return str_generate<_Rule>(doc); + } + + ////////// + template + ::std::string to_string(_Rule& r, _Document& doc){ + ::std::string s; + ::roast::string_output_stream ss(s); + //if ( r.generate(ss,doc) == false ) + r.generate(ss,doc); + return s; + } + + template + ::std::string to_string(_Rule& r){ + return to_string(r, null_document); + } + + template + ::std::string to_string2(_Rule& r) + { + + ::std::string s; + ::roast::string_output_stream ss(s); + //generate(ss,r); + + //if ( r.generate(ss,doc) == false ) + r.generate(ss,r); + return s; + } + + ////////////////////////////////////////////////////////////////////////////////// + + template + void print(_Rule& r, _Document& doc){ + //r.generate(::std::cout, doc); + r.generate(::roast::stream::cout, doc); + } + + template + void print(_Rule& r){ + //r.generate(::std::cout, doc); + r.generate(::roast::stream::cout, null_document); + } + + template + void println(_Rule& r){ + //r.generate(::std::cout, doc); + r.generate(::roast::stream::cout, null_document); + ::roast::stream::cout << '\n'; + } + + ////////////////////////////////////////////////////////////////////////////////// + } + + template + ::std::string lex_to_string(_Rule& r) + { + return lexical::to_string2(r); + } +} + +#endif//__SFJP_ROAST__lexical2__to_string_HPP__