【发布时间】:2016-02-22 08:46:29
【问题描述】:
我在 insta 中遇到了一个模棱两可的解析问题。语法如下:
(def yip-shape
(insta/parser
(str/join "\n"
["S = ( list-item | heading | text-block )*"
;; lists and that
"list-item = list-level <ws> anything"
"list-level = #' {0,3}\\*'"
;; headings
"heading = heading-level <ws> ( heading-keyword <ws> )? ( heading-date <ws> )? anything <eol?>"
"heading-level = #'#{1,6}'"
"heading-date = <'<'> #'[\\d-:]+' <'>'>"
"heading-keyword = 'TODO' | 'DONE'"
"text-block = anything*"
"anything = #'.+'"
"<eol> = '\\r'? '\\n'"
"<ws> = #'\\s+'"])))
问题在于像## TODO Done 这样的标题 - 我可以理解为什么存在歧义,但我不确定解决它的最佳方法。 E.G
(insta/parses yip-shape "## TODO Done.")
生产:
([:S [:text-block [:anything "## TODO Done."]]]
[:S [:heading [:heading-level "##"] [:anything "TODO Done."]]]
[:S [:heading [:heading-level "##"] [:heading-keyword "TODO"] [:anything "Done."]]])
最后一个是我正在寻找的结果。如何最好地消除歧义并将结果缩小到该列表中的最后一个?
【问题讨论】:
标签: parsing clojure ebnf instaparse