Skip to content

Commit

Permalink
Sketch in the printing implemetnation for token_parser.
Browse files Browse the repository at this point in the history
See #202.
  • Loading branch information
tzlaine committed Nov 10, 2024
1 parent 519d276 commit dd5ffe7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
7 changes: 7 additions & 0 deletions include/boost/parser/detail/printing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,13 @@ namespace boost { namespace parser { namespace detail {
std::ostream & os,
int components = 0);

template<typename Context, typename TokenSpec, typename Expected>
void print_parser(
Context const & context,
token_parser<TokenSpec, Expected> const & parser,
std::ostream & os,
int components = 0);

enum { trace_indent_factor = 2 };

inline void trace_indent(std::ostream & os, int indent)
Expand Down
35 changes: 35 additions & 0 deletions include/boost/parser/detail/printing_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename Context, typename TokenSpec, typename Expected>
void print_parser(
Context const & context,
token_parser<TokenSpec, Expected> 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
1 change: 0 additions & 1 deletion include/boost/parser/token_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename TokenSpec, typename Expected>
Expand Down
11 changes: 11 additions & 0 deletions test/tracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; };
Expand Down

0 comments on commit dd5ffe7

Please sign in to comment.