-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add tooling to debug pattern matching. #135
base: main
Are you sure you want to change the base?
Conversation
b22af62
to
76a240c
Compare
This comment was marked as off-topic.
This comment was marked as off-topic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
Dbg
function 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
start
token, 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
Dbg
function as a wrapper:The first
If
indicates that the debugging should start at the firstIf
token 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_ptr
where 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