Skip to content

Instantly share code, notes, and snippets.

@alexkehayias
Last active March 15, 2022 16:30
Show Gist options
  • Save alexkehayias/634b7ea74b034ec22dc0ac127d6d01d1 to your computer and use it in GitHub Desktop.
Save alexkehayias/634b7ea74b034ec22dc0ac127d6d01d1 to your computer and use it in GitHub Desktop.
The start of an EBNF grammer to parse org-mode docs
(require '[instaparse.core :as insta])
(def org-parse
(insta/parser
"<DOC> = <EOL>* heading*
heading = level <SPC> [status <SPC>] text tag* [<EOL> content]
sub-heading = sub-level <SPC> [status <SPC>] text tag* [<EOL> content]
EOL = '\n' | '\r\n' | #'$'
SPC = ' '
level = #'\\*+'
sub-level = #'\\*{2,}'
text = !status #'^.+'
content = (<EOL> | sub-heading | text | list-item)+
status = 'TODO' | 'DONE'
tag = <':'> text <':'>
indent = #'\\s+'
list-item = <indent> list-bullet text [<EOL> list-item]
list-bullet = ('-' | '+') <SPC>"))
;; Example
(comment
(org-parse "
* TODO h1
** h2
- Bullet 1
- Bullet 2
- Nested bullet 1
- Nested bullet 2
* Another h1
Hello!
")
([:heading
[:level "*"]
[:status "TODO"]
[:text "h1"]
[:content
[:sub-heading [:sub-level "**"] [:text "h2"]]
[:list-item [:list-bullet "-"] [:text "Bullet 1"]]
[:list-item [:list-bullet "-"] [:text "Bullet 2"]]
[:list-item
[:list-bullet "-"]
[:text "Nested bullet 1"]
[:list-item [:list-bullet "-"] [:text "Nested bullet 2"]]]]]
[:heading [:level "*"] [:text "Another h1"] [:content [:text "Hello!"]]]))
@qazwsxpawel
Copy link

Hi Alex!
Did you get a chance to complete the grammar or maybe found an authoritative source for this?
Just attempting to do this and was wandering if it was done before?
Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment