Deceptive Incomplete Parsing: A Common Problem

Some code I'd written checked for file extensions, wanting only %.r and %.reb files to be processed. It looked like this:

 for-each file ... [
     parse file [thru ".reb" | thru ".r"] else [continue]

That doesn't work anymore, because PARSE raises an error instead of returning null. The ELSE needs to be an EXCEPT, or you have to use IF NOT OK?, or some other solution.

My first thought on fixing it was why not just put the CONTINUE inside the parse...

 for-each file ... [
     parse file [thru ".reb" | thru ".r" | (continue)]

That's a neat Rebolism that shows the kind of malleability other languages just don't have.

But it has a problem. Can you spot it?

Old Redbol Conventions Wouldn't Catch The Mistake

Imagine if the file is named or %foo.rar. One of the THRUs succeeds, so it won't run the continue alternate. But it won't reach the end of the filename.

Historical Redbol would have had the PARSE return false, but would have just blindly continued running, passing those unintended filenames!!!

Now we're a step ahead, because PARSE will error if it doesn't reach the end! :clap:

But How To Stop the Error?

We don't want an error if it doesn't terminate in %.r or %.reb, we want to continue the loop.

This will work:

 parse file [thru ".reb" <end> | thru ".r" <end> | (continue)]

And it's not catastrophically bad. But it feels weird.

You can remove the <end> duplication:

 parse file [thru [".reb" | ".r"] <end> | (continue)]

You can also do that with ANY:

 parse file [thru any [".reb" ".r"] <end> | (continue)]

You can of course go for the conventional forms:

parse file [thru ".reb" | thru ".r"] except [continue]

if not ok? parse file [thru ".reb" | thru ".r"] [continue]

But I feel like there's something missing when you write something like this without making the <end> explicit...because it leads to someone getting the bright idea (as I did) to reformulate it without taking the <end> into account.

In Any Case, This Being Overlooked Is Now Caught!

It's food for thought on what style you want. But no matter what style you like, I think it shows a clear win for raising the error when the parse doesn't reach the end.

That %foo.rar or -- when they occur -- will not be accepted quietly in the reformulation!

1 Like