comby-tools/comby

Add Support for dependent matches

talios opened this issue · 2 comments

I have a series of rewrite rules I've been working with lately with some reafactorings on our Java code base:

[remove-import-notnull]
match='''
import org.jetbrains.annotations.NotNull;
'''
rewrite=""

[remove-method-notnull]
match='''
:[leading~^\s+]@NotNull:[line]
'''
rewrite=":[leading]:[line]"

[remove-arguments-notnull]
match="@NotNull :[var]"
rewrite=":[var]"

Sometimes, I'm hitting code that imports a different implementation of the @NotNull annotation - so that fhe first rule fails, and then the next two trigger and produce broken code.

It would be awesome if one could add a rule clause with an expression that triggers only if (in the above) the matcher remove-import-notnull had triggered for the file.

I've been working on a small scripting language that should help with this. It's tricky to import the result state ("rewrite succeeded") from a previous rule and then import that state in a subsequent rule. Instead, in the scripting language, you'll have a sequence operation with a ; syntax that essentially says "if this thing succeeded, then do this next thing", something like:

old-step-1 -> new-step-1;
old-step2 -> new-step-2 

The entire expression will only succeed if all steps succeed. If you have a lot of steps, and want partial steps to succeed, this might need repeating some rules, but in general it shouldn't be too bad, and if that doesn't generalize well, then perhaps I can introduce an additional constructs that succeeds for partial steps, but I don't want to get ahead too far here. Summary is: it's easier, and hopefully more clear, to have expressions in a script be self-contained, as far as scope, so that you can express "do X only if previous step Y succeeds".

want partial steps to succeed, this might need repeating some rules,

The think the only partial success I'd want would be (based on the above rules), to remove the import if the others didn't match - I guess a no-op pass-thru the step chain.

Self-contained definitely sounds preferable to me as well.