For example, you can search for assignments of the value 1234 to a variable named myVar
with the following pattern:
This will find the assignments even if the code is formatted with whiteSpace or comments in between or both.
myVar := 1234
Likewise, you can search for all creations of three-element Arrays, using a constant array size,
with the pattern:
Try using the codeSearcher on some of your own code
(and place comments or whitespace into your code).
Array new:3
To allow for those to be specified in the search-code pattern, the search-syntax supports pattern variables (also called meta variables).
Each metaPattern-variable must begin with a ` (backquote) character.
Immediately following the ` character, other characters can be entered to specify
what type of node this metaPattern-variable can match.
After all the special character have been entered, you must then enter a valid variable name.
(the matched code is actually bound to this name inside the searcher and later
used by the code-rewriter.)
The special characters currently supported are listed in the following table:
@ - list
When applied to a keyword in a message, it will match a list of keyword messages (i.e., any message send)
When applied with a statement character, it will match a list of statements
For example:
matches list of temps
| `@Temps |
matches list of statements
`@.Statements
matches any message node, literal node or block node
`@object
matches any message sent to
foo `@message: `@args
foo.
. - statement
For example:
matches a single statement
`.Statement
# - literal
For example:
matches any literal (
`#literal
#(), #foo, 1, etc.)
` (backquote) - recurse into
`@object foo
matches a foo message sent to any expression on the outer level.
However, the code "self foo foo" will be matched only once.
In contrast,
also matches
``@object foo
foo sent to any object,
plus for each match found, it will look for more matches in the ``@object part.
Thus, this will match twice for the "self foo foo" example.
`sel
^ `sel
AND we must specify that the search should be a "method-search", by setting the "Method"-CheckBox field
in the dialog.
Array new:3
Search for Array instance creations with any constant size:
Array new: `#n
Search for Array instance creations where a variable specifies the size:
Array new: `v
Search for all Array instance creations (any expression as size):
Array new: `@e
at:put: being sent to the Smalltalk-global,
with a variables value as argument,
use
Smalltalk at:`key put:`val
the above does not match for literal values or expression values as argument(s).
Smalltalk at:`@key put:`@val
to even look into the argument and look for sends there too,
use:
Smalltalk at:``@key put:``@val
``@rec `@msg: ``@args
``@rec on: ``@arg1 do: ``@arg2
and:
``@rec handle: ``@arg1 do: ``@arg2
or, for a particular class:
StreamError handle: ``@arg1 do: ``@arg2
Error handle: [ :``@args | ] do: ``@blk
and:
``@blk on: Error do: [ :``@args | ]
``@object not ifTrue: ``@block
and:
``@object not ifFalse: ``@block
are obviously easier written by negating the if-test message.
As a final example, of a more complex pattern,
the following searches for collect:-messages which collect the results of
a simple message send (i.e. are effectively map: operations):
``@expr collect:[:`v | `v `@sel: ``@args]
Copyright © eXept Software AG, all rights reserved