1

...而且我碰壁了,我不明白为什么这不起作用(我需要能够解析单个标签版本(以 /> 终止)或 2 个标签版本(以 终止) ):

Rebol[]

content: {<pre:myTag      attr1="helloworld" attr2="hello"/>
<pre:myTag      attr1="helloworld" attr2="hello">
</pre:myTag>
<pre:myTag      attr3="helloworld" attr4="hello"/>
}

spacer: charset reduce [#" " newline]
letter: charset reduce ["ABCDEFGHIJKLMNOPQRSTUabcdefghijklmnopqrstuvwxyz1234567890="]

rule: [

any [
{<pre:myTag} 
any [any letter {"} any letter {"}] mark: 
(print {clipboard... after any letter {"} any letter {"}} write clipboard:// mark input)
any spacer mark: (print "clipboard..." write clipboard:// mark input) ["/>" | ">" 
any spacer </pre:myTag>
]
any spacer
(insert mark { Visible="false"}) 
]
to end

]

parse content rule
write clipboard:// content
print "The end"
input
4

1 回答 1

5

在这种情况下,问题不在于您的规则——而是您在每个标签更改后的“插入”会改变您插入时的位置。

为了显示:

>> probe parse str: "abd" ["ab" mark: (insert mark "c") "d"] probe str
false
"abcd"
== "abcd"

插入是正确的,但是在插入之后,解析规则仍然在位置 2,之前只有“d”,现在有“cd”,规则失败。三种策略:

1) 加入新内容:

>> probe parse str: "abd" ["ab" mark: (insert mark "c") "cd"] probe str
true
"abcd"
== "abcd"

2)计算新内容的长度并跳过:

>> probe parse str: "abd" ["ab" mark: (insert mark "c") 1 skip "d"] probe str
true
"abcd"
== "abcd"

3) 操纵后改变位置:

>> probe parse str: "abd" ["ab" mark: (mark: insert mark "c") :mark "d"] probe str 
true
"abcd"
== "abcd"

数字 2) 在你的情况下是最快的,因为你知道你的字符串长度是 16:

rule: [
    any [
        {<pre:myTag} ; opens tag

        any [ ; eats through all attributes
            any letter {"} any letter {"}
        ]

        mark: ( ; mark after the last attribute, pause (input)
            print {clipboard... after any letter {"} any letter {"}}
            write clipboard:// mark
            input
        )

        any spacer mark: ; space, mark, print, pause
        (print "clipboard..." write clipboard:// mark input)

        [ ; close tag
            "/>"
            |
            ">" any spacer </pre:myTag>
        ]

        any spacer ; redundant without /all

        (insert mark { Visible="false"})
        16 skip ; adjust position based on the new content
    ]

    to end
]

注意:这与您的规则相同,只是添加了 [16 skip]。

于 2010-03-18T02:54:48.653 回答