2010-03-17 25 views
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 

回答

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跳過]時的規則相同。

+0

很多非常感謝克里斯爲這些詳細的答案我現在真的解析進展:) – 2010-03-18 07:58:11

+0

但很明顯,不硬編碼像這樣的字符串長度!由於一個單詞的解析默認行爲是評估它,你可以使* replacement:{Visible =「false」} *,然後* replacement-length:length?替代*。在規則中使用替換和替換長度。當然,這一切都假設使用非DOM面向XML/HTML的解析器是一個好主意,我將重申它不是... :) – HostileFork 2010-03-18 19:41:21

+0

+1爲人們做Rebol Q&A之外的想法AltME和R3聊天。如果社區中的其他人可以談論加入文明的其他部分,它可能不會像這樣一種死去的語言...讓我們得到更多的東西。 – HostileFork 2010-03-18 19:42:54