Skip to content

Instantly share code, notes, and snippets.

@brehaut
Created March 10, 2011 21:48
Show Gist options
  • Save brehaut/865003 to your computer and use it in GitHub Desktop.
Save brehaut/865003 to your computer and use it in GitHub Desktop.
fix-parens; a function to correct bracket types
(use 'name.choi.joshua.fnparse)
(def tokenize (partial re-seq #"\\[\[\](){}]|\"(?:\\.|[^\"])*\"|[\[\](){}]|[^\[\](){}]+"))
(def initial-state (comp (partial array-map :remainder) tokenize))
(def brackets {"[" "]" "(" ")" "{" "}"})
(def opening (term (set (keys brackets))))
(def closing (failpoint (term (set (vals brackets)))
vector))
(def content (term (complement (set (flatten (seq brackets))))))
(def expression (alt (complex [open opening
body (rep* expression)
_ closing]
(str open (apply str body) (brackets open)))
content))
(def fix-parens (comp (partial rule-match expression prn prn)
initial-state))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment