Rewriting Rust code

SmaCC provides mechanisms for parsing and transformation of code written in arbitrary languages. This can be used in several ways, one of which is rewriting code from a dedicated snippet.

Let's look at a Rust example. We first clone the sources we are interested to search and replace in:

rootDirectory := 'gtoolkit-boxer' asFileReference.
rootDirectory ensureDeleteAll.
repository := IceRepositoryCreator
		fromUrl: ''
		to: rootDirectory.

And we get all the Rust files from that directory:

allFiles := (rootDirectory / 'boxer') allFiles select: [:each | each extension = 'rs' ]

Now, let's assume we want to change:

assert_eq!(something.is_null(), false, "message")

to something like:

assert_not_null(something, "message")

This requires a rewrite. A rewrite is made out of two parts: a search, and a replacement specification.

Our case, like many such cases, entails a case for which we can use a dedicated SmaCC rewrite snippet like the one below. In the snippet we have to specify the scope to which to apply the search and replacement. We can do that by picking allFiles from the first dropdown.

Then we can just search using the first inspect button, or we can trigger the whole rewrite using the second inspect button:

Triggering a search spawns an inspector with the search result allowing you to explore it. Triggering the rewrite spawns another inspector on an object that allows you to preview the changes and apply them.

That is it.