2012-12-07 44 views
1

我是Treetop的新手,並且有一個非常簡單的語法,我只是無法工作。我有一些測試:Treetop語法不識別「/」

it "parses a open tag as some text surrouded by brackets" do 
    document = "[b]" 
    Parser.parse(document).should_not be_nil 
end 
it "parses a close tag as a tag with a/preceeding the tag name" do 
    document = '[/b]' 
    Parser.parse(document).should_not be_nil 
end 

這是我的語法:

grammar BBCode 
    rule open_tag 
    "[" tag_name "]" 
    end 

    rule tag_name 
    [a-zA-Z\*]+ 
    end 

    rule close_tag 
    "[/" tag_name "]" 
    end 
end 

第一個測試通過,第二次測試失敗。我也嘗試了這些備用規則:

"[" [\/] tag_name "]" 
"[" "/" tag_name "]" 
"[\/" tag_name "]" 

所有這些都失敗。

我似乎無法讓它識別結束標記,無論我嘗試什麼。

+0

嗯......看在規則「DIV」 [mathematica語法](https://github.com/farleyknight/mathematica_parser/blob/master/lib/mathematica.treetop),它有一個''/'',它似乎工作。 –

+0

我見過很多的例子。因此,我的困惑:/ – DVG

回答

1

我發現此問題:https://github.com/nathansobo/treetop/issues/25,它似乎回答了我的問題。

我的語法不包含頂級規則,將允許打開或關閉標籤,因此第二種可能性甚至沒有考慮:

grammar BBCode 
    rule document 
    (open_tag/close_tag) 
    end 

    rule open_tag 
    ("[" tag_name "]") 
    end 

    rule tag_name 
    [a-zA-Z\*]+ 
    end 

    rule close_tag 
    ("[/" tag_name "]") 
    end 
end 
+0

另一方面,你不需要規則外的括號,除非你將它們標記爲稍後用於類。 –