红色解析与休息 [英] Red parse with break

查看:114
本文介绍了红色解析与休息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的带有break的解析代码不起作用,我不应该在文本中获取最后一个div块:

my parse code with break doesn't work, I shouldn't get last div block in text:

src: {
<div class="main">
    <div>
        test
    </div>

    <div>
        test2
    </div>
    <div>
        test3
    </div>
</div>

<div class="test">
</div>
}

rules: [
    (div-count: 0)
    some [
        to "<div"
        (div-count: div-count + 1) [if (div-count = 1) mark1:] 
        |
        thru "</div>"
        (div-count: div-count - 1) [if (div-count = 0) mark2: break]
    ]

    text: copy/part mark1 mark2

]

parse src rules
print text

我想要的预期结果是:

    {
    <div class="main">
        <div>
            test
        </div>

        <div>
            test2
        </div>
        <div>
            test3
        </div>
    </div>
    }

推荐答案

Red和Rebol的答案可能是这样

An answer for Red and Rebol could look like this

rules: [
    (div-count: 0   clear rules/3/8 )
    some [
        mark:  "<div"  
        (if  equal? 1  div-count: div-count + 1  [
            mark1:  mark  
        ] )   | 
         "</div>"  mark2:
        ( 
        if equal? 0  div-count: div-count - 1  [
            text: copy/part mark1 mark2    
            insert rules/3/8 [to end]  
        ]  )  
        [] | skip
    ]
]

规则的问题之一是您使用 to |(含义或) thru ,这样大多数</div> s将被跳过.在不比较以下子规则的情况下,满足第一个匹配<div并继续到下一个打开<div.但是光标没有前进,下一个<div仍然相同.红色大概发现了无尽的循环(没有前进)并中断了它.

One of the problems with your rules are that you use to and | (meaning or) thru, so that most closing </div> s would be skipped. The first match <div is satisfied and on to the next opening<div without comparing the following sub rules. But the cursor is not advancing, the next <div is still the same. Probably Red discovers the endless loop (no advancing) and interrupts it.

我使用动态修改的规则而不是break,因为break突破了Rebol中的(子)规则,但是并没有停止整个解析过程,如您在此处看到的那样.

I use dynamically modified rules instead of break, as break breaks out of (sub) rules in Rebol, but does not stop the whole parsing process as you can see here.

 >> parse "aaa" [(n: 0)some ["a" [break] (ask form n: n + 1) ]]
 1
 2
 3
 == true

这与Red中断解析的Red不同.

That's different to Red, where it interupts parse.

>> parse "aaa" [(n: 0)some ["a" [break] (ask form n: n + 1) ]]
1
== false

因此,适用于Red(不适用于Rebol)的简单解决方案看起来像这样

So a simple solution suitable for Red, not for Rebol can look like that

rules: [
    (div-count: 0)
    some [
        mark: "<div"
        (if  equal? 1  div-count: div-count + 1  [mark1:  mark]) 
        |
        "</div>" mark2:
        if (equal? 0  div-count: div-count - 1 )  
          [(print text: copy/part mark1 mark2 )  break]
        |
        skip
    ]
]

这篇关于红色解析与休息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆