So, I wrote my grammar and would like to provide some debugging information, such as line numbers, in order to be able to go through the generated executable code using my own debugger.
After some searches, I found that you can completely define the tag classes used in such rules:
x3::rule<class CodeLine, ast::InstructionOrDirectiveAndArgs> const code_line = "code_line"; auto const code_line_def = ... class CodeLine { public: template <typename T, typename Iterator, typename Context> inline void on_success(Iterator const& first, Iterator const& last, T& ast, Context const& context) { static std::uint64_t line = 0; auto& error_handler = x3::get<error_handler_tag>(context).get(); error_handler.tag(ast, first, last); ast.line_no = line; if (*last == '\0') { line = 0; } else { line += 1; } } };
In these fully defined tag classes, you can implement the on_success method, which is called when the rule can be successfully matched. So I applied a tag class to a rule that matches a line of code. But since I could not find a way to get the current line number from the spirit, I turned to a static variable that keeps track of the current line. The problem is to find out when the string counter is reset, as you can see with my rather dumb attempt.
This seems to be a very confusing way to track line numbers, so there should be a better way.
The question is, what is the best or correct way to get the current line number?
Thank you for reading!
Usyer source share