Comments on: PARSE: any, some, loop with break and reject
Here's the implementation planned for A90:
any
attempts rule until it fails or the input did not change. Always succeeds.
some
the same as any except requires at least one match to succeed.
while
the same as any except does not care if the input changed. The while keyword is used for special cases where iterations may not modify the input position, but we want the loop to continue.
break
breaks out of any of the above loops, always indicating success.
reject
breaks out of any of the above loops, always indicating failure.
Note that both break and reject stop the loop iteration immediately. Any pending copy or set actions are not done.
Updated
The while keyword is used instead of loop. The rationale is that a while loop continues while the condition is true. So, while is closer in meaning than loop.
hmm, so out of all votes you chosed 'loop name noone voted for :-)
Carl Sassenrath 14-Oct-2009 17:01:25
I considered each name carefully:
every - a great word, but sounds like it does every rule, which is not true. (But, maybe we can use this instead of OF rule in 3.1).
always - I liked it, but BrianH thought it was ambiguous. True.
cycle - Pekr (you) thought it too much like recycle. Not really a problem, but it does seem a bit out there (e.g. CPU cycles.)
forever - not really true - the loop continues until no rule matches. So, it is not forever.
I realized that this loop is actually more like while, but in the form of REBOL's until (with a single block). That is, it loops while the block is true (meaning something matched) and exits when the block is false (nothing matched.)
-pekr- 14-Oct-2009 17:24:36
OK, the explanation sounds good. I liked 'every later, as it kind of complements sounding of words like 'any, 'some, I wonder if we would find usage for 'all.
As for 'every instead of 'of? 'of is "one of following rules", so I don't understand, how 'every name is better than 'of for such purpose?
Brian Hawley 14-Oct-2009 19:29:30
I like while, good choice.
Does break break out of the nearest enclosing any, some or while loops, or out of any iteration including the implicit 1 before every rule?
Breaking out of every iteration as break does in R2 has been problematic, especially since it forces you to physically put the break at the same nesting level as the outermost rule block that it is breaking from. This limits your algorithmic options a bit.
If you are changing break to break out of any, some or while (as I would prefer), then it would be a good idea to rename it to accept to signify that R2 code that uses break might need some going over to make sure it still works. And you are right that it matches reject better, as long as its breaking behavior is the same. If you are keeping the old behavior, you can keep the old name.
Kaj 15-Oct-2009 7:17:07
A90 seems to break the SECURE dialect.
Ben 15-Oct-2009 11:42:36
+1 for accept
Hostile Fork 15-Oct-2009 17:06:16
+1 for accept (to pair with reject)
As an alternative, return true and return false would be okay. But break just doesn't suggest what will be handed back to the caller (I would have assumed false over true actually).
Brian Hawley 16-Oct-2009 1:14:39
Fork, the proposed break (or better, accept) doesn't return anything to the caller, it just breaks out of an iterated rule (all rules are implicitly iterated, apparently) and tells PARSE that the rule was successful.
Returning something to the caller is what return does.