;
; regex ::= a Lisp string
-;;; Regular expressions can be created by using the `REGEX' form. The
-;;; regex form actually does nothing at all to its argument, and
-;;; prints it as is.
+;;; Regular expressions can be created by using the `REGEX' form. If
+;;; the argument does not start with a slash, it is surrounded by
+;;; slashes to make it a proper JavaScript regex. If the argument
+;;; starts with a slash it is left as it is. This makes it possible
+;;; to use modifiers such as slash-i (case-insensitive) or
+;;; slash-g (match-globally (all)).
+
+(regex "foobar") => /foobar/
(regex "/foobar/i") => /foobar/i
(define-js-compiler-macro regex (regex)
(make-instance 'regex :value (string regex)))
+(defun first-slash-p (string)
+ (and (> (length string) 0)
+ (eq (char string 0) '#\/)))
+
(defmethod js-to-strings ((regex regex) start-pos)
- (declare (ignore start-pos))
- (list (format nil "/~A/" (value regex))))
+ (declare (ignore start-pos))
+ (let ((slash (if (first-slash-p (value regex)) nil "/")))
+ (list (format nil (concatenate 'string slash "~A" slash) (value regex)))))
;;; conditional compilation
((search "=>" (subseq built (+ 1 sep-pos)))
(format t "Error , two separators found~%"))
((and (string= heading "regular-expression-literals")
- (= 2 heading-count)) ;requires cl-interpol reader
- (format t "Skipping regex-test two~&"))
+ (= 3 heading-count)) ;requires cl-interpol reader
+ (format t "Skipping regex-test with cl-interpol&"))
((and lisp-part javascript-part)
(format out-stream "(test-ps-js ~a-~a~% ~a~% \"~a\")~%~%"
heading heading-count
"this.a + this.b + this.c;")
(test-ps-js regular-expression-literals-1
+ (regex "foobar")
+ "/foobar/")
+
+(test-ps-js regular-expression-literals-2
(regex "/foobar/i")
"/foobar/i")