Add tooling to debug pattern matching.#135
Conversation
b22af62 to
76a240c
Compare
This comment was marked as off-topic.
This comment was marked as off-topic.
mjp41
left a comment
There was a problem hiding this comment.
@xFrednet thanks for looking into this. I am unclear on the precise aims of this. As you are only generating pretty print on the final state of a match can we get the same information in the debugger without code changes?
Adding logging to the pattern matching code be very helpful for investigating why something failed internally.
Does this provide the part of the pattern that matched, and the part that didn't?
I have been wondering if we should have an AST for rewrite patterns, and I think that could help with this kind of debugging experience as you would be able to return the continuation that has not matched. Currently there is no printable representation for that.
| bool match(NodeIt& it, const Node& parent, Match& match) const& override | ||
| { |
There was a problem hiding this comment.
I'm unsure on what this will produce in the failure case for a disjunction? Will it only report the failure of the final disjunct?
There was a problem hiding this comment.
Yes, this is sadly a limitation of this non-intrusive approach.
You could say that this is "only generating pretty print". It should be possible to get the same information with a debugger, but then the question becomes where you set the break points? Patterns are checked for every node. So I don't believe that you can just say This is meant to get this information fast.
I haven't seen any logging in the existing infrastructure and therefore didn't consider this possibility. If we have general logging, it would likely be more flexible. I'm uncertain how we would add it, but it's certainly a valid alternative. Assuming that it doesn't ruin performance.
It shows which nodes have been matched and what groups have been captured. The current implementation can't say was part didn't match as that would require a more intrusive approach.
Interesting idea! I was considering to manually walk the rewrite pattern to include information, like which It would also feel like a more elementary change that I'm not yet comfortable making in Trieste 😅 |
It can be hard to determine why a certain pattern isn't matching with the given tree. This PR adds a
Dbgfunction which can be wrapped around the pattern and effect to debug the matching step.The function has a custom pattern that wraps around the original pattern. It then counts how many nodes have been matched until the match failed. The debug macro has a defined
starttoken, that engages the debugging. The results are remitted as error nodes to the AST.Example
Let's consider the following AST as an example:
I wrote the following rule, to match the expressions:
However, the match sadly fails. Now I can add the
Dbgfunction as a wrapper:The first
Ifindicates that the debugging should start at the firstIftoken it encounters. The function generates the following output:It's indicated that the first five nodes
(if)(int)(leq)(int)(then)could be matched.Planned Improvements:
My C++ is a bit rusty, so there are probably several unidiomatic expressions. I'll be happy to fix them.
I also encountered a problem with
intrusive_ptrwhere I couldn't passintrusive_ptr<DebugPattern>into a function expectingPatternPtr. Is there a simple way to do this conversion?Also, I wasn't sure how to properly test this, since the usage of this function should be an exception and not the rule. 😅
cc: @EliasC