not used
[ghsmtp.git] / jnk / rh.cpp
blobf75634c39240fdf2840ad0068777f13a0991d1aa
1 #include <iostream>
2 #include <istream>
3 #include <streambuf>
4 #include <string_view>
6 #include <tao/pegtl.hpp>
7 #include <tao/pegtl/contrib/abnf.hpp>
9 using namespace tao::pegtl;
10 using namespace tao::pegtl::abnf;
12 std::string esc(std::string_view str)
14 std::string ret;
15 for (auto c : str) {
16 switch (c) {
17 case '\n': ret += "\\n"; break;
18 case '\r': ret += "\\r"; break;
19 default: ret += c;
22 return ret;
25 struct membuf : std::streambuf {
26 membuf(char const* base, size_t size)
28 auto const p = const_cast<char*>(base);
29 this->setg(p, p, p + size);
33 struct mystream : virtual membuf, std::istream {
34 mystream(std::string_view s)
35 : membuf(s.data(), s.length())
36 , std::istream(static_cast<std::streambuf*>(this))
40 virtual std::streamsize xsgetn(char* s, std::streamsize count)
42 auto const read = membuf::xsgetn(s, count);
43 std::cout << "xsgetn(" << count << ") «" << esc(std::string_view(s, read))
44 << \n";
45 return read;
49 namespace rules {
50 struct chunk_size : plus<DIGIT> {
53 struct bdat : seq<TAO_PEGTL_ISTRING("BDAT"), SP, chunk_size, CRLF> {
56 struct end_marker : TAO_PEGTL_ISTRING("LAST") {
59 struct bdat_last
60 : seq<TAO_PEGTL_ISTRING("BDAT"), SP, chunk_size, SP, end_marker, CRLF> {
63 struct quit : seq<TAO_PEGTL_ISTRING("QUIT"), CRLF> {
66 struct anything_else : seq<star<not_one<'\n'>>, one<'\n'>> {
69 struct any_cmd : seq<sor<bdat, bdat_last, quit, anything_else>, discard> {
72 struct grammar : plus<any_cmd> {
74 } // namespace rules
76 namespace actions {
77 template <typename Rule>
78 struct action : nothing<Rule> {
81 template <>
82 struct action<rules::bdat> {
83 template <typename Input>
84 static void apply(Input const& in)
86 std::cout << "action<bdat> «" << esc(in.string()) << \n";
90 template <>
91 struct action<rules::bdat_last> {
92 template <typename Input>
93 static void apply(Input const& in)
95 std::cout << "action<bdat_last> «" << esc(in.string()) << \n";
99 template <>
100 struct action<rules::anything_else> {
101 template <typename Input>
102 static void apply(Input const& in)
104 std::cout << "action<anything_else> «" << esc(in.string()) << \n";
108 template <>
109 struct action<rules::quit> {
110 template <typename Input>
111 static void apply(Input const& in)
113 std::cout << "QUIT\n";
116 } // namespace actions
118 int main()
120 auto constexpr data = "BDAT 28\r\n"
121 "ABCDEFGHIJKLNMOPQRSTUVWXYZ\r\n"
122 "BDAT 0 LAST\r\n"
123 "QUIT\r\n";
125 mystream istream(data);
127 istream_input<eol::crlf, 1> in{istream, 128, "input-stream"};
129 return parse<rules::grammar, actions::action>(in) ? 0 : 1;