Skip to content

Instantly share code, notes, and snippets.

@stathissideris
Last active August 29, 2015 13:56
Show Gist options
  • Save stathissideris/8969123 to your computer and use it in GitHub Desktop.
Save stathissideris/8969123 to your computer and use it in GitHub Desktop.
parse quoted strings with instaparse
;;see http://stackoverflow.com/questions/16215169/clojure-csv-with-escaped-comma
(require '[instaparse.core :as insta])
((insta/parser
"quoted-field = <'\\\"'> (#'[^\"\\\\]+' | escaped-char)* <'\\\"'>
<escaped-char> = #'\\\\.'") "\"hello\\\" world\"")
;;and to parse comments like /** this is a test */
;;http://stackoverflow.com/questions/11125459/java-regex-negative-lookahead
;;positive lookahead
(def comment-parser
(insta/parser "comment = <'/**'> #'.+(?=\\*\\*/)' <'**/'>"))
;;with negative lookahead it's more complicated
(def comment-parser
(insta/parser "comment = <'/**'> #'.+?(?!.+\\*\\*/)' <'**/'>"))
;;user> (comment-parser "/** this is a test **/")
;;[:comment " this is a test "]
;;or https://github.com/Engelberg/instaparse/blob/master/docs/ExperimentalFeatures.md#auto-whitespace
(def comment-parser2
(insta/parser
"comment = <'/**'> inside-comment* <'**/'>
<inside-comment> = !('**/'|'/**') #'.'"))
;;user>(comment-parser2 "/**this is a test**/")
;;[:comment "t" "h" "i" "s" " " "i" "s" " " "a" " " "t" "e" "s" "t"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment