Skip to content

Commit

Permalink
Add cases to tracing.cpp to cover the printing of token parsers; fix …
Browse files Browse the repository at this point in the history
…errors.

See #202.
  • Loading branch information
tzlaine committed Nov 29, 2024
1 parent 1a5b746 commit 00510ed
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 18 deletions.
47 changes: 29 additions & 18 deletions include/boost/parser/detail/printing_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -944,34 +944,45 @@ namespace boost { namespace parser { namespace detail {

#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);
constexpr bool do_print_value = requires { parser.expected_.value_; };

auto print_value = [&] {
if constexpr (do_print_value) {
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 << '"';
}
};

if constexpr (requires {
os << TokenSpec::id;
} && std::is_enum_v<typename TokenSpec::id_type>) {
if constexpr (do_print_value) {
print_value();
} else {
detail::print(os, parser.expected_.value_);
os << TokenSpec::id;
}
} else {
os << "tok<" << (int)TokenSpec::id << '>';
if constexpr (do_print_value) {
os << '(';
print_value();
os << ')';
}
os << ')';
}
}

Expand Down
64 changes: 64 additions & 0 deletions test/tracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
* http://www.boost.org/LICENSE_1_0.txt)
*/

#include <boost/parser/config.hpp>
#if BOOST_PARSER_USE_CONCEPTS
#include <boost/parser/lexer.hpp>
#endif
#include <boost/parser/parser.hpp>


Expand Down Expand Up @@ -495,4 +499,64 @@ int main()

PARSE_CHAR32(float_);
PARSE_CHAR32(double_);

#if BOOST_PARSER_USE_CONCEPTS
{
std::cout << "\n\n"
<< "----------------------------------------\n"
<< "| unprintable_foo (token_spec) |\n"
<< "----------------------------------------\n";

constexpr auto unprintable_foo =
token_spec<"\\w\\w\\w", unprintable_tokens::foo>;
constexpr auto unprintable_lexer =
lexer<char, unprintable_tokens> | unprintable_foo;

std::cout << "token_spec<\"\\w\\w\\w\", unprintable_tokens::foo>:\n";
parse(str | to_tokens(unprintable_lexer), unprintable_foo, trace::on);

std::cout
<< "token_spec<\"\\w\\w\\w\", unprintable_tokens::foo>(\"foo\"):\n";
parse(
str | to_tokens(unprintable_lexer),
unprintable_foo("foo"),
trace::on);
}

{
std::cout << "\n\n"
<< "----------------------------------------\n"
<< "| printable_foo (token_spec) |\n"
<< "----------------------------------------\n";

constexpr auto printable_foo =
token_spec<"\\w\\w\\w", printable_tokens::foo>;
constexpr auto printable_lexer =
lexer<char, printable_tokens> | printable_foo;

std::cout << "token_spec<\"\\w\\w\\w\", printable_tokens::foo>:\n";
parse(str | to_tokens(printable_lexer), printable_foo, trace::on);

std::cout
<< "token_spec<\"\\w\\w\\w\", printable_tokens::foo>(\"bar\"):\n";
parse(
str | to_tokens(printable_lexer), printable_foo("bar"), trace::on);
}

{
std::cout << "\n\n"
<< "----------------------------------------\n"
<< "| int_foo (token_spec) |\n"
<< "----------------------------------------\n";

constexpr auto int_foo = token_spec<"\\w\\w\\w", 42, int>;
constexpr auto int_lexer = lexer<char, int> | int_foo;

std::cout << "token_spec<\"\\w\\w\\w\", 42, int>:\n";
parse(str | to_tokens(int_lexer), int_foo, trace::on);

std::cout << "token_spec<\"\\w\\w\\w\", 42, int>(13):\n";
parse(str | to_tokens(int_lexer), int_foo(13), trace::on);
}
#endif
}

0 comments on commit 00510ed

Please sign in to comment.