Created
March 10, 2011 21:48
-
-
Save brehaut/865003 to your computer and use it in GitHub Desktop.
fix-parens; a function to correct bracket types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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