我以为这个解析会很简单

发布于 2024-08-25 03:11:02 字数 854 浏览 1 评论 0原文

...而且我碰壁了,我不明白为什么这不起作用(我需要能够解析单个标签版本(以 /> 结尾)或 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

... and I'm hitting the wall, I don't understand why this doesn't work (I need to be able to parse either the single tag version (terminated with />) or the 2 tag versions (terminated with ) ):

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

涙—继续流 2024-09-01 03:11:02

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

举例说明:

>> 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 跳过]。

In this case, the problem isn't your rule - it's that your 'insert after each tag changes alters the position at the point you do the insert.

To illustrate:

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

The insert is correct, but after the insert, the parse rule is still at position 2, and before where there was just "d", there is now "cd" and the rule fails. Three strategies:

1) Incorporate the new content:

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

2) Calculate the length of the new content and skip:

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

3) Change the position after the manipulation:

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

Number 2) would be the quickest in your case as you know your string length is 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
]

Note: this is the same rule as yours with just [16 skip] added.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文