Using the ParseTreeSearcher?

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

Using the ParseTreeSearcher?

Stew MacLean

Hi,

 

I’m internationalising my app, and need to derive a list of the first argument of a certain set of messages.

 

After sprinkling my code with sends of asLiteral (that does a lookup for translated text), for example:

 

addModify: ‘Modify’ asLiteral status: Any position: 10

 

I’ve figured a more elegant way would be to find all the methods that send the selectors, then get the ParseTree of that method, and then find each matching node and pluck the first argument from it.

 

I’ve found ParseTreeSearcher which looks like it will do the job. However, as this is new territory for me, I’d appreciate some pointers as to how to use it (or any other better way of doing this).

 

Thanks,

 

Stewart

Reply | Threaded
Open this post in threaded view
|

RE: Using the ParseTreeSearcher?

Stew MacLean

It’s ok, I’ve figured it out…

 

            selectors := … some selectors…

            mc := MethodCollector new.

            methods :=

                        mc select:

                                    (mc methodsSelect:

                                                [:m |

                                                m messages includesAny: selectors]) & (mc searchClassHierarchy: ObjectAbstract).

            parseTrees := methods collect: [: m | m implementingClass parseTreeFor: m selector].

 

            literals := OrderedCollection new.

           

            selectors do:

                        [: selector |

                        parseTrees do:

                                    [: parseTree |

                                    searcher := Refactory.Browser.ParseTreeSearcher new.

                                    searcher

                                                matches: '`@object ' , (Refactory.Browser.ParseTreeSearcher buildSelectorString: selector) do:

                                                            [: node : answer |

                                                            (node arguments size > 0 and: [(node arguments at: 1) isLiteralNode])

                                                                        ifTrue: [literals add: (node arguments at: 1) token value]].

                                    searcher executeTree: parseTree initialAnswer: false]].

 

 

And to extract the labels within windowSpecs….

 

            methods := …MyClass…. class withAllSubclasses inject: OrderedCollection new into:

                        [: result : class |

                        result

                                    addAll: ((class methodDictionary values select: [: each | each resourceType = #canvas])

                                                                        collect: [: each | class soleInstance -> each selector]);

                                    yourself].

            labels := Set new.

            enum := FilteredSpecEnumerator new.

                        enum block:

                                    [:each |

                                    (each respondsTo: #label)

                                                ifTrue:

                                                            [labels add: each label].

                                    true].

            methods do:

                        [: method |

                        enum doSpec: (method key interfaceSpecFor: method value)].

 

            ^labels asOrderedCollection select: [: each | each isSymbol not & ((each allSatisfy: [: char | char isSeparator]) not)]

 

It’s then a small step to writing out the catalog file… Smalltalk is Cool!

 

Cheers,

 

Stewart

 

 

-----Original Message-----
From: Stewart MacLean [mailto:[hidden email]]
Sent
:
11 February 2007 9:10 a.m.
To: [hidden email]
Subject: Using the ParseTreeSearcher?

 

Hi,

 

I’m internationalising my app, and need to derive a list of the first argument of a certain set of messages.

 

After sprinkling my code with sends of asLiteral (that does a lookup for translated text), for example:

 

addModify: ‘Modify’ asLiteral status: Any position: 10

 

I’ve figured a more elegant way would be to find all the methods that send the selectors, then get the ParseTree of that method, and then find each matching node and pluck the first argument from it.

 

I’ve found ParseTreeSearcher which looks like it will do the job. However, as this is new territory for me, I’d appreciate some pointers as to how to use it (or any other better way of doing this).

 

Thanks,

 

Stewart