-
We can print the result of source file parsing through visualize. But it will discard all generated values. To get the result of the parsing, we need parse. (I tried to store the results in the state, so that the return of This causes that if you want to get both the result and parse_tree, you must parse them twice. auto file = lexy::read_file<lexy::utf8_encoding>(filename.data());
if (!file)
{
throw std::runtime_error{"Cannot read file!"};
}
ParseState state{string::string{filename}, std::move(file).buffer()};
if (const auto result =
lexy::parse<grammar::module_declaration>(state.buffer, state, lexy_ext::report_error.opts({.flags = lexy::visualize_fancy}).path(state.filename.c_str()));
!result.is_success())
{
throw std::runtime_error{"Cannot parse file!"};
}
#ifdef PARSE_AS_TREE
lexy::parse_tree_for<decltype(state.buffer)> tree{};
lexy::parse_as_tree<grammar::module_declaration>(tree, state.buffer, state, lexy_ext::report_error.opts({.flags = lexy::visualize_fancy}).path(state.filename.c_str()));
lexy::visualize(stdout, tree, {lexy::visualize_fancy});
#endif |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
That's not supported by the library at the moment. I could add a Why do you need both the tree and the value? If you have the tree, you have all the information you need to compute the values yourself. |
Beta Was this translation helpful? Give feedback.
-
I've started implementing something like that, but it's not trivial. I don't think it's necessary: if you just want to visualize the parse tree for debugging purposes, just parse it twice - performance doesn't matter there anyway. |
Beta Was this translation helpful? Give feedback.
I've started implementing something like that, but it's not trivial. I don't think it's necessary: if you just want to visualize the parse tree for debugging purposes, just parse it twice - performance doesn't matter there anyway.