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
)
17 case '\n': ret
+= "\\n"; break;
18 case '\r': ret
+= "\\r"; break;
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
))
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") {
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
> {
77 template <typename Rule
>
78 struct action
: nothing
<Rule
> {
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";
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";
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";
109 struct action
<rules::quit
> {
110 template <typename Input
>
111 static void apply(Input
const& in
)
113 std::cout
<< "QUIT\n";
116 } // namespace actions
120 auto constexpr data
= "BDAT 28\r\n"
121 "ABCDEFGHIJKLNMOPQRSTUVWXYZ\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;