Skip to content

Instantly share code, notes, and snippets.

@valer-cara
Created January 22, 2023 21:14
Show Gist options
  • Save valer-cara/9f7af6b95309fceecde551f51a3fd0c0 to your computer and use it in GitHub Desktop.
Save valer-cara/9f7af6b95309fceecde551f51a3fd0c0 to your computer and use it in GitHub Desktop.
Instaparse grammar for dot/graphviz. Incomplete.
;; original spec https://graphviz.org/doc/info/lang.html
;; this is only a partial implementation
(def grammar "
G = 'digraph' <WS> graph-name <WS*> block <WS*>
<ws> = <#'[\\s\\t]+'> (* whitespace *)
<WS> = <#'[\\s\\t\\n\\r]+'> (* multiline whitespace, graphviz is very newline tolerant *)
<string-q> = <'\"'> #'[^\"]*' <'\"'>
<string-uq> = #'[a-zA-Z_][a-zA-Z_0-9]+'
<string> = string-q | string-uq
block = <'{'> WS* stmt-list WS* <'}'>
graph-name = string
node = string
node-from = node
node-to = node
arrow = '->'
propName = string
propValue = string
prop = propName <'='> propValue
propList = propList? prop <ws*>
props = <'['> propList? <']'>
nodeSpec = <WS*> node <WS*> props?
edgeSpec = <WS*> node-from <WS*> <arrow> <WS*> node-to <WS*> props?
stmt-list = stmt-list? stmt
<stmt> = nodeSpec | edgeSpec
")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment