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

自作多情 提交于 2020-01-17 02:39:48

问题


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 because 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)


回答1:


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.



来源:https://stackoverflow.com/questions/2457618/parse-and-break-why-break-cannot-be-used-for-getting-out-of-any-or-some-rul

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!