我在 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
- 我可以理解为什么存在歧义,我只是不确定解决它的最佳方法。例如
(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."]]])
最后一个是我正在寻找的结果。如何最好地消除歧义并将结果缩小到该列表中的最后一个?