Search code examples
regexsmalltalksqueak

squeak(Smalltalk) search regex in string


I'm trying to write a method 'compile' that gets a string and a collection and see if the string matches the conditions. the method signature is:

compile: stringCode where: argTypeCollection

example of use (assuming C is the class):

C compile: 
'first: i second: any third: n
| local |
   local := i + n.
  ^(local*local)'
where: #(Integer nil Number).

the first thing the method should do is analyze the string by check if the number of arguments is correct, I thought of doing so with regex. I tried to look for regex use explanation here and here but the only example is for files and I didn't succeed to scan the string and count matches for [a-zA-z][a-zA-Z0-9]*: the same way.

any example of using regex on string in squeak will help.


Solution

  • When analyzing Smalltalk source code, the best option is to use the very same objects the Smalltalk compiler employs for parsing, compiling and evaluating methods and code snnipets. In other words, having the full range of compiling tools at your disposal it makes little sense to use regex for these kinds of tasks.

    For instance, you can analyze the header of your method (i.e., the part of the source code defining the selector and formal arguments) using the Parser like this

    Parser new parse: aString class: aClass
    

    where aString is the method's source code and aClass is the target class, i.e., the class for which the method would make sense.

    In your example the class is C. Note however that when the source code contains no reference to ivars (or for that matter class or shared variables) the argument aClass becomes irrelevant and can be replaced by Object.

    The result of the parse:class: message, if the parsing succeeds, will be an Abstract Parse Tree (a.k.a. AST) whose nodes will bring more information useful for further analysis. If the parsing fails, you will get access to the parsing error object that will let you determine why the code is non-conformant with the Smalltalk syntax. As you can see, you will have everything you need to reflect on the source code under analysis.