still must match obs first
[ghsmtp.git] / jnk / p-test.cpp
blob8a29ef394c99a95951860a0baad398a15c4d3cb4
1 #include <iostream>
3 #include <tao/pegtl.hpp>
4 #include <tao/pegtl/contrib/abnf.hpp>
5 #include <tao/pegtl/contrib/alphabet.hpp>
7 using namespace tao::pegtl;
8 using namespace tao::pegtl::abnf;
9 using namespace tao::pegtl::alphabet;
11 #include <glog/logging.h>
13 namespace smtp {
15 struct Ctx {
18 struct String : plus<sor<ALPHA, DIGIT>> {
21 struct foo : seq<TAOCPP_PEGTL_ISTRING("FOO"), opt<SP, String>, CRLF> {
24 struct bar : seq<TAOCPP_PEGTL_ISTRING("BAR "), String, CRLF> {
27 struct baz : seq<TAOCPP_PEGTL_ISTRING("BAZ"), CRLF> {
30 struct quit : seq<TAOCPP_PEGTL_ISTRING("QUIT"), CRLF> {
33 struct lng : seq<TAOCPP_PEGTL_ISTRING("SUPERLONGTHING"), CRLF> {
36 struct any_cmd : seq<sor<baz, foo, bar, quit, lng>, discard> {
39 struct cmds : plus<any_cmd> {
42 struct grammar : seq<cmds, eof> {
45 template <typename Rule>
46 struct action : nothing<Rule> {
49 template <>
50 struct action<foo> {
51 template <typename Input>
52 static void apply(const Input& in, Ctx& ctx)
54 std::cout << "foo";
55 if (in.string().length())
56 std::cout << " " << in.string();
57 std::cout << "\n";
61 template <>
62 struct action<bar> {
63 template <typename Input>
64 static void apply(const Input& in, Ctx& ctx)
66 std::cout << "bar " << in.string() << '\n';
70 template <>
71 struct action<baz> {
72 static void apply0(Ctx& ctx) { std::cout << "baz\n"; }
75 template <>
76 struct action<quit> {
77 static void apply0(Ctx& ctx) { std::cout << "quit\n"; }
79 } // namespace smtp
81 int main(int argc, char const* argv[])
83 std::ios::sync_with_stdio(false);
84 google::InitGoogleLogging(argv[0]);
86 smtp::Ctx ctx;
87 istream_input<eol::crlf> in(std::cin, 20, "cin");
89 std::cout << "250 start\n";
91 try {
92 LOG(INFO) << "calling parse";
93 parse<smtp::grammar, smtp::action>(in, ctx);
94 LOG(INFO) << "parse return";
96 catch (parse_error const& e) {
97 std::cout << e.what() << '\n';
98 return 1;