Thierry, I have a last question. My last requirement will basically be to find ANY node that would match the regex *oldSelector* and rename it to *newSelector* being that.. a literal string, a literal symbol, a message send, a tempVar name, a comment, etc... I guess there is a simpler way that defining rewrite for each type of node right? I can always do a "(methodSource substrings detect: [:each | each matches: '*oldSelector'] ifNone: [#()] ) each2: [:each2 | copReplace. .... ]
but RB is probably better. Any clues in this last requirement? On Mon, Nov 9, 2015 at 11:01 AM, Thierry Goubier <[email protected]> wrote: > > > 2015-11-09 14:45 GMT+01:00 Mariano Martinez Peck <[email protected]>: > >> >>> >>> Pharo 4.0 as well. I made a mistake in my example :(, so I'm adding more >>> test cases to check ;). >>> >>> | tree | >>> RBParseTreeRewriter new >>> replace: '``@object `oldSelector `{:node | node selector asString >>> matchesRegex: ''.*oldSelector.*''}' >>> with: '`{RBMessageNode receiver: ``@object selector: (`oldSelector >>> copyReplaceAll: ''oldSelector'' with: ''newSelector'') asSymbol arguments: >>> #()}'; >>> executeTree: >>> (tree := RBParser >>> parseMethod: >>> 'DoIt ^ [:proxy | >>> proxy at: #oldSelector. >>> proxy preoldSelector. >>> self preoldSelector oldSelectorPost. >>> proxy preoldSelector: proxy. >>> proxy oldSelectorAnotherMethod. >>> proxy do: ''oldSelectorAndSomething''. >>> "a comment with #oldSelector " >>> ]'). >>> tree newSource. >>> >>> >> >> Thanks, that worked much better! >> >> Here you have a much larger example/test: >> >> | rewriter tree oldSeletor newSelector | >> "closure method sourceCode" >> tree := RBParser parseMethod: 'DoIt ^ [:proxy | >> | oldSelector oldSelectorsuffix prefixoldSelector prefixoldSelectorSuffix >> | >> oldSelector := 1. >> oldSelectorsuffix := 1. >> prefixoldSelector := 1. >> prefixoldSelectorSuffix := 1. >> proxy at: #oldSelector. >> proxy at: #oldSelectorSuffix. >> proxy at: #prefixoldSelector. >> proxy at: #prefixoldSelectorSuffix. >> proxy oldSelector. >> proxy oldSelectorSuffix. >> proxy prefixoldSelector. >> proxy prefixoldSelectorSuffix. >> proxy do: ''oldSelector''. >> proxy do: ''oldSelectorSuffix''. >> proxy do: ''prefixoldSelector''. >> proxy do: ''prefixoldSelectorSuffix''. >> "a comment with #oldSelector " >> "a comment with #oldSelectorSuffix " >> "a comment with #prefixoldSelector " >> "a comment with #prefixoldSelectorSuffix " >> ]'. >> oldSeletor := #oldSelector. >> newSelector := #newSelector. >> >> rewriter := RBParseTreeRewriter new >> replace: '``@object `oldSelector `{:node | node selector asString >> matchesRegex: ''.*oldSelector.*''}' >> with: '`{RBMessageNode receiver: ``@object selector: (`oldSelector >> copyReplaceAll: ''oldSelector'' with: ''newSelector'') asSymbol arguments: >> #()}'. >> >> rewriter executeTree: tree. >> tree newSource >> >> >> I think I only miss the literal ones (for example proxy at: #oldSelector >> and the rest) which previously were catched by " RBParseTreeRewriter >> replaceLiteral: oldSeletor with: newSelector. " >> > > Yes, I didn't focus on this one. Didn't understood why it was there :) > > Something like, for the match: > '`#aLiteral {:node | node value isSymbol and: [node value > matchesRegex: '.*oldSelector.*'] }' > > I think it could be simplified; the #copyReplaceAll: probably works just > all well on all message sends. > > RBParseTreeRewriter new > replace: '``@object `oldSelector' > with: '`{RBMessageNode receiver: ``@object selector: (`oldSelector > copyReplaceAll: ''oldSelector'' with: ''newSelector'') asSymbol arguments: > #()}'; > > > >> >> I guess I can apply a similar logic for with the regex. I will see if I >> can came up with that. If you happen to know how ;););) >> >> Yes, I will go to Smalltalks and I will give a talk. So I will ask him if >> there are remaining questions! >> BTW, later on today I will send you an screenshot to show you where you >> helped me :) >> > > Cool! Lucky you. Have fun :) > > Thierry > > >> >> Thanks!!! >> >> -- >> Mariano >> http://marianopeck.wordpress.com >> > > -- Mariano http://marianopeck.wordpress.com
