Skip to content

Instantly share code, notes, and snippets.

@mkamotsu
Created June 7, 2013 07:12
Show Gist options
  • Save mkamotsu/5727548 to your computer and use it in GitHub Desktop.
Save mkamotsu/5727548 to your computer and use it in GitHub Desktop.
(expand t "${FOO} ${BAR}")を (format t "~A ~A" FOO BAR)に変換するマクロ
(ql:quickload :cl-ppcre)
(defpackage :expand
(:use :cl :ppcre))
(in-package :expand)
(defconstant +regex+ "\\$\\{(.*?)\\}")
(defun expand-string (string)
(regex-replace-all +regex+ string "~A"))
(defun expand-args (string)
(let ((args nil))
(do-register-groups (arg)
(+regex+ string (nreverse args))
(push (read-from-string arg) args))))
(defmacro expand (stream string)
`(format ,stream ,(expand-string string) ,@(expand-args string)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment