From 298bae0058fba5b1a26240a92ce632b73516553c Mon Sep 17 00:00:00 2001 From: Zach Laine Date: Sat, 9 Nov 2024 22:29:40 -0600 Subject: [PATCH] Sketch in the printing implemetnation for token_parser. See #202. --- include/boost/parser/detail/printing.hpp | 7 ++++ include/boost/parser/detail/printing_impl.hpp | 35 +++++++++++++++++++ include/boost/parser/token_parser.hpp | 1 - test/tracing.cpp | 11 ++++++ 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/include/boost/parser/detail/printing.hpp b/include/boost/parser/detail/printing.hpp index 1143832a..7f6854c7 100644 --- a/include/boost/parser/detail/printing.hpp +++ b/include/boost/parser/detail/printing.hpp @@ -355,6 +355,13 @@ namespace boost { namespace parser { namespace detail { std::ostream & os, int components = 0); + template + void print_parser( + Context const & context, + token_parser const & parser, + std::ostream & os, + int components = 0); + enum { trace_indent_factor = 2 }; inline void trace_indent(std::ostream & os, int indent) diff --git a/include/boost/parser/detail/printing_impl.hpp b/include/boost/parser/detail/printing_impl.hpp index 37f780c5..2493bb2a 100644 --- a/include/boost/parser/detail/printing_impl.hpp +++ b/include/boost/parser/detail/printing_impl.hpp @@ -942,6 +942,41 @@ namespace boost { namespace parser { namespace detail { context, parser.or_parser_, os, components); } +#if defined(BOOST_PARSER_TOKEN_PARSER_HPP) + + // TODO: Needs testing. + template + void print_parser( + Context const & context, + token_parser const & parser, + std::ostream & os, + int components) + { + os << "tok<"; + if constexpr (requires { os << TokenSpec::id; }) { + os << TokenSpec::id; + } else { + os << (int)TokenSpec::id; + } + os << '>'; + if constexpr (requires { parser.expected_.value_; }) { + os << '('; + if constexpr (std::ranges::range< + decltype(parser.expected_.value_)>) { + os << '"'; + for (auto c : parser.expected_.value_ | text::as_utf8) { + detail::print_char(os, c); + } + os << '"'; + } else { + detail::print(os, parser.expected_.value_); + } + os << ')'; + } + } + +#endif + }}} #endif diff --git a/include/boost/parser/token_parser.hpp b/include/boost/parser/token_parser.hpp index 3d729813..06bc5cc9 100644 --- a/include/boost/parser/token_parser.hpp +++ b/include/boost/parser/token_parser.hpp @@ -69,7 +69,6 @@ namespace boost { namespace parser { #ifndef BOOST_PARSER_DOXYGEN - // TODO: Needs a printer. // TODO: Constrain the AttributeType to something that detail::token_as() // can handle. template diff --git a/test/tracing.cpp b/test/tracing.cpp index 1d94fb53..eb94b00a 100644 --- a/test/tracing.cpp +++ b/test/tracing.cpp @@ -26,6 +26,17 @@ struct globals_t globals_t const globals; +enum class unprintable_tokens { foo, bar }; +enum class printable_tokens { foo, bar }; +std::ostream & operator<<(std::ostream & os, printable_tokens tok) +{ + switch (tok) { + case printable_tokens::foo: os << "foo"; break; + case printable_tokens::bar: os << "bar"; break; + } + return os; +} + auto i = [](auto & ctx) { return _globals(ctx).i; }; auto i2 = [](auto & ctx) { return _globals(ctx).i2; }; auto u = [](auto & ctx) { return _globals(ctx).u; };