开发者

Parse and Break: why break cannot be used for getting out of "any" or "some" rule?

开发者 https://www.devze.com 2022-12-22 22:50 出处:网络
Let say I have to parse a hierarchical set of tags <tag> <subtag1 attr1=value1 attr2=value2>

Let say I have to parse a hierarchical set of tags

<tag>
<subtag1 attr1=value1 attr2=value2>
<subtag1 attr1=value1 attr2=value2>
<subtag1 attr1=value1 attr2=value2>
</tag>

Why can't I use break inside some or any to get out of a level hierarchy ? This would allow to do that kind of parsing instead of having a headache to do so ?

I'm asking this b开发者_开发技巧ecause I read here http://www.codeconscious.com/rebol/parse-tutorial.html it would create an infinite loop

This case produces an infinite loop. Because the BREAK is within a sub-rule of the rule that SOME is processing. The BREAK does not affect success/failure status or the input pointer - it just exits a rule early:

rule-to-break: [(print "Break") break] == [(print "Break") break] parse "X" [some [rule-to-break] "X"] Break Break ... Break Break(escape)


That gives an infinite loop in Rebol 2, you are correct. But remember that parse has undergone major upgrades and revisions in the most recent version, based on feedback of users.

So in Rebol 3, you get:

>> rule-to-break: [(print "Break") break] 
== [(print "Break") break]

>> parse "X" [some [rule-to-break] "X"]
Break
== true

Carl wrote a bit on the nuance of Rebol 3's break behavior on the R3 blog:

http://www.rebol.net/r3blogs/0277.html

  • fail: explicitly fail a single rule, skip to the next alternative (if it has one).
  • break: explicitly exit the entire rule block, skip all alternatives.
  • return: explicitly exit all rules, return from the parse function.

Rebol 2 is kind of set in stone at this point; there's limits to how much work is going to be done to fix it. You should test all your examples in Rebol 3.

0

精彩评论

暂无评论...
验证码 换一张
取 消