RBPattern syntax

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

RBPattern syntax

Manuel Leuenberger
Hi,

I am looking into the RB pattern language for refactoring and I am having trouble matching and replacing non-trivial pattern. Given the following excerpt, I want to match "b shape <rest>." and replace it with "b shape: [ :x | x <rest> ]"

        b shape circle
                size: 15;
                color: (Color veryLightGray alpha: 0.4);
                if: [ :value | toBeRed includes: value ] fillColor: Color red.

How can I do this? I tried using "b shape ``@messages", but this only matches "b shape circle". Using "b shape `message `;middle; `;last" for some reason then also matches the following, which I think it should not:

        b edges
                moveBehind;
                connectToAll: [ :v |
                        v \\ 20 ~~ 0
                                ifTrue: [ Array with: v + 1 with: v + 20 ]
                                ifFalse: [ Array with: v + 20 ] ].

There seems not be too much documentation about the pattern language, only found tests, some short help description from RB and Yuriy's MatchTool.
How can I match the node correctly?

Cheers,
Manuel


Reply | Threaded
Open this post in threaded view
|

Re: RBPattern syntax

Henrik Sperre Johansen
Manuel Leuenberger wrote

> Hi,
>
> I am looking into the RB pattern language for refactoring and I am having
> trouble matching and replacing non-trivial pattern. Given the following
> excerpt, I want to match "b shape
> <rest>
> ." and replace it with "b shape: [ :x | x
> <rest>
>  ]"
>
> b shape circle
> size: 15;
> color: (Color veryLightGray alpha: 0.4);
> if: [ :value | toBeRed includes: value ] fillColor: Color red.
>
> How can I do this? I tried using "b shape ``@messages", but this only
> matches "b shape circle". Using "b shape `message `;middle; `;last" for
> some reason then also matches the following, which I think it should not:
>
> b edges
> moveBehind;
> connectToAll: [ :v |
> v \\ 20 ~~ 0
> ifTrue: [ Array with: v + 1 with: v + 20 ]
> ifFalse: [ Array with: v + 20 ] ].
>
> There seems not be too much documentation about the pattern language, only
> found tests, some short help description from RB and Yuriy's MatchTool.
> How can I match the node correctly?
>
> Cheers,
> Manuel

The best documentation is at https://refactory.com/rewrite-tool/
What isn't covered, is cascade nodes (`@;messages1), but my first naive
attempt did not work as I expected on your example;
'(b shape `@method: `@keywords)
   @;messages1: `@args'
->
'b shape: [ :x |  
   (x `@method: `@keywords)
      @;messages1: `@args ]'

b shape circle
   size: 15;
   color: (Color veryLightGray alpha: 0.4);
   if: [ :value | toBeRed includes: value ] fillColor: Color red
->
b
   shape: [ :x | x circle size: 15 ];
   shape: [ :x | x circle color: (Color veryLightGray alpha: 0.4) ];
   shape: [ :x | x circle if: [ :value | toBeRed includes: value ]
fillColor: Color red ]



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: RBPattern syntax

Manuel Leuenberger
Hi Henrik,

Thanks for the the link. Interestingly, I was presented the same page on an old version, https://web.archive.org/web/20090227160738/http://st-www.cs.uiuc.edu/users/brant/Refactory/Rewrite.html. Good to see, that there is still a maintained source.

As for the rule you presented, that is pretty cool, you got way closer to the goal than I did. John Brant mentioned that these kind of matches/transformations are not easily supported by RB by design, especially cascades seem to be problematic, as well as sequenced/nested message sends.

I am now looking into the RewriteTool (http://smalltalkhub.com/#!/~MarkRizun/RewriteTool/commits) and Phorms (http://smalltalkhub.com/#!/~CamilleTeruel/Patterns/commits), which seems to have a more powerful pattern matching language.

Cheers,
Manuel

On 19 Feb 2019, at 14:04, Henrik Sperre Johansen <[hidden email]> wrote:

Manuel Leuenberger wrote
Hi,

I am looking into the RB pattern language for refactoring and I am having
trouble matching and replacing non-trivial pattern. Given the following
excerpt, I want to match "b shape
<rest>
." and replace it with "b shape: [ :x | x
<rest>
]"

b shape circle
size: 15;
color: (Color veryLightGray alpha: 0.4);
if: [ :value | toBeRed includes: value ] fillColor: Color red.

How can I do this? I tried using "b shape ``@messages", but this only
matches "b shape circle". Using "b shape `message `;middle; `;last" for
some reason then also matches the following, which I think it should not:

b edges
moveBehind;
connectToAll: [ :v |
v \\ 20 ~~ 0
ifTrue: [ Array with: v + 1 with: v + 20 ]
ifFalse: [ Array with: v + 20 ] ].

There seems not be too much documentation about the pattern language, only
found tests, some short help description from RB and Yuriy's MatchTool.
How can I match the node correctly?

Cheers,
Manuel

The best documentation is at https://refactory.com/rewrite-tool/
What isn't covered, is cascade nodes (`@;messages1), but my first naive
attempt did not work as I expected on your example;
'(b shape `@method: `@keywords)
  @;messages1: `@args'
->
'b shape: [ :x |  
  (x `@method: `@keywords)
     @;messages1: `@args ]'

b shape circle
  size: 15;
  color: (Color veryLightGray alpha: 0.4);
  if: [ :value | toBeRed includes: value ] fillColor: Color red
->
b
  shape: [ :x | x circle size: 15 ];
  shape: [ :x | x circle color: (Color veryLightGray alpha: 0.4) ];
  shape: [ :x | x circle if: [ :value | toBeRed includes: value ]
fillColor: Color red ]



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html