在Treetop语法中匹配标签对
我不想重复Cthulhu的答案,但我希望使用Treetop匹配开启和关闭HTML标签的对。使用这个语法,我可以匹配开始标记和结束标记,但现在我想要一个规则将它们绑在一起。我已尝试过以下内容,但使用此方法会使我的解析器永远继续(无限循环):
rule html_tag_pair
html_open_tag (!html_close_tag (html_tag_pair / '' / text / newline /
whitespace))+ html_close_tag <HTMLTagPair>
end
我试图将此基于递归括号示例和Treetop Github页面上的否定前瞻示例。我引用的其他规则如下:
rule newline
[nr] {
def content
:newline
end
}
end
rule tab
"t" {
def content
:tab
end
}
end
rule whitespace
(newline / tab / [s]) {
def content
:whitespace
end
}
end
rule text
[^<]+ {
def content
[:text, text_value]
end
}
end
rule html_open_tag
"<" html_tag_name attribute_list ">" <HTMLOpenTag>
end
rule html_empty_tag
"<" html_tag_name attribute_list whitespace* "/>" <HTMLEmptyTag>
end
rule html_close_tag
"</" html_tag_name ">" <HTMLCloseTag>
end
rule html_tag_name
[A-Za-z0-9]+ {
def content
text_value
end
}
end
rule attribute_list
attribute* {
def content
elements.inject({}){ |hash, e| hash.merge(e.content) }
end
}
end
rule attribute
whitespace+ html_tag_name "=" quoted_value {
def content
{elements[1].content => elements[3].content}
end
}
end
rule quoted_value
('"' [^"]* '"' / "'" [^']* "'") {
def content
elements[1].text_value
end
}
end
我知道我需要允许匹配单个开始或结束标签,但如果存在一对HTML标签,我想将它们作为一对组合在一起。通过将它们与我的语法相匹配来做到这一点似乎最干净,但也许有更好的方法呢?
没有找到相关结果
已邀请:
2 个回复
烷刨画颠离
博沮富教全