[Pharo-project] Classes referenced in code string
How would I programmatically find out what classes are referenced in a string. For example: 'Object new. String new' "would return { Object. String }" -- View this message in context: http://forum.world.st/Classes-referenced-in-code-string-tp4244214p4244214.ht... Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
Can be easily done with RB: You parse it (RBParser parseExpression: aString) and do a semantic analysis (see the tests in AST-Tests-Semantics for examples). Lukas On Thursday, December 29, 2011, Sean P. DeNigris <sean@clipperadams.com> wrote:
How would I programmatically find out what classes are referenced in a string. For example: 'Object new. String new' "would return { Object. String }"
-- View this message in context: http://forum.world.st/Classes-referenced-in-code-string-tp4244214p4244214.ht... Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
-- Lukas Renggli (mobile) http://www.lukas-renggli.ch
Lukas Renggli wrote
Can be easily done with RB: You parse it (RBParser parseExpression: aString) and do a semantic analysis (see the tests in AST-Tests-Semantics for examples).
Thanks, Lukas! How did I know that you would answer ;-) I got that far, but couldn't figure out what to pass to annotateInClass:. Not calling it, or passing nil both lead to errors. -- View this message in context: http://forum.world.st/Classes-referenced-in-code-string-tp4244214p4244389.ht... Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
On 30 December 2011 01:23, Sean P. DeNigris <sean@clipperadams.com> wrote:
Lukas Renggli wrote
Can be easily done with RB: You parse it (RBParser parseExpression: aString) and do a semantic analysis (see the tests in AST-Tests-Semantics for examples).
Thanks, Lukas! How did I know that you would answer ;-)
I got that far, but couldn't figure out what to pass to annotateInClass:. Not calling it, or passing nil both lead to errors.
Yes, you need to call it. It tells the AST in what context (to what class "self" should refer to) it is used. For a workspace expression where "self" refers to "nil" you have to pass in "UndefinedObject". Lukas -- Lukas Renggli www.lukas-renggli.ch
This is the rest of the code: ast := RBParser parseExpression: 'Object new. String new'. ast annotateInClass: UndefinedObject. (ast allChildren select: [ :each | each isVariable and: [ each variableBinding isLiteralBinding ] ]) collect: [ :each | each variableBinding binding value ] --> an OrderedCollection(Object String) #variableBinding returns the object knowing all details about the variables. It is only available after you annotated the AST with #annotateInClass:. Lukas On 30 December 2011 08:56, Lukas Renggli <renggli@gmail.com> wrote:
On 30 December 2011 01:23, Sean P. DeNigris <sean@clipperadams.com> wrote:
Lukas Renggli wrote
Can be easily done with RB: You parse it (RBParser parseExpression: aString) and do a semantic analysis (see the tests in AST-Tests-Semantics for examples).
Thanks, Lukas! How did I know that you would answer ;-)
I got that far, but couldn't figure out what to pass to annotateInClass:. Not calling it, or passing nil both lead to errors.
Yes, you need to call it. It tells the AST in what context (to what class "self" should refer to) it is used. For a workspace expression where "self" refers to "nil" you have to pass in "UndefinedObject".
Lukas
-- Lukas Renggli www.lukas-renggli.ch
-- Lukas Renggli www.lukas-renggli.ch
Lukas Renggli wrote
This is the rest of the code:
Thanks Lukas! "#annotateInClass: nil" doesn't work... what's the class of nil? Seems so obvious now! -- View this message in context: http://forum.world.st/Classes-referenced-in-code-string-tp4244214p4246073.ht... Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
participants (2)
-
Lukas Renggli -
Sean P. DeNigris