[Pharo-dev] Is there a portable way to check if a string is a block?

Eliot Miranda eliot.miranda at gmail.com
Sat Aug 12 22:32:51 EDT 2017

Hi Mariano,

_,,,^..^,,,_ (phone)

> On Jul 24, 2017, at 9:21 AM, Mariano Martinez Peck <marianopeck at gmail.com> wrote:
> Hi guys,
> Is there a portable (that would work in GemStone too) and safe way to know if a String would be a block? 
> I must support this kind of strings:
> 'singleString'   -> false
> 'multi string' -> false
> '      [:a | 42]' -> true
> '   '' ''  [:a | 42]' -> true
> '[:a | 42]' -> true
> ' "a commet" [:a | 42]' -> true
> ' WhateverClass someVeryBadHackishMethod. [:a | 42]' -> true 
> I need to identify whether the string is a closure or not. And ideally, I don't want to do an #evaluate: because I don't want any left hand code to be executed. 
> I tried with #parse:  why I am having problems. 
> Any ideas?

Not sure if you're still stuck but if you dive into evaluate: you'll be able to find the point at which it parses, and find out how to invoke the parser for an expression (a doit).   evaluate: parses an expression so you want to check that the resulting parse tree is that of a return node whose expression is a literal block.  You can also check the block's argument count.

Once you've extracted the parser invocation you can wrap this in a suitable exception handler.  Then you have your block parser.  You still have to validate the parse tree and/or generated code before evaluating since someone could write malicious code within the block right?

> Thanks in advance
> -- 
> Mariano
> http://marianopeck.wordpress.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.pharo.org/pipermail/pharo-dev_lists.pharo.org/attachments/20170812/4fd29ced/attachment.html>

More information about the Pharo-dev mailing list