Is there a portable way to check if a string is a block?

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

Is there a portable way to check if a string is a block?

Mariano Martinez Peck
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?

Thanks in advance

--
Reply | Threaded
Open this post in threaded view
|

Re: Is there a portable way to check if a string is a block?

Nicolas Cellier
Presumably, you must parse the string (not evaluate), obtain an Abstract Syntax Tree on return (or an Exception), and check that this AST conforms to some pattern (at worse you code naïvly the pattern matching, at best you can reuse the refactor/rewrite engine).

I've not followed latest Compiler developments, but from what I understand, more and more tools are based on AST...
so it sounds pretty straight-forward.

2017-07-24 18:21 GMT+02:00 Mariano Martinez Peck <[hidden email]>:
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?

Thanks in advance

--

Reply | Threaded
Open this post in threaded view
|

Re: Is there a portable way to check if a string is a block?

Eliot Miranda-2
In reply to this post by Mariano Martinez Peck
Hi Mariano,

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

On Jul 24, 2017, at 9:21 AM, Mariano Martinez Peck <[hidden email]> 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

--
Reply | Threaded
Open this post in threaded view
|

Re: Is there a portable way to check if a string is a block?

Guillermo Polito
RBParser parseMethod: '...'

parses a method

RBParser parseExpression: '...'

does it for an expression

But I think both of them return a method node? I cannot recall.

However, you can always do at the end

   node methodNode statements last isBlockNode

or something like that. Can't you?

On Sun, Aug 13, 2017 at 4:32 AM, Eliot Miranda <[hidden email]> wrote:
Hi Mariano,

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

On Jul 24, 2017, at 9:21 AM, Mariano Martinez Peck <[hidden email]> 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

--



--

   

Guille Polito


Research Engineer

French National Center for Scientific Research - http://www.cnrs.fr



Web: http://guillep.github.io

Phone: +33 06 52 70 66 13