regex patch
authorHenrik Hjelte <henrik.hjelte@evahjelte.com>
Tue, 11 Jul 2006 13:47:05 +0000 (13:47 +0000)
committerHenrik Hjelte <henrik.hjelte@evahjelte.com>
Tue, 11 Jul 2006 13:47:05 +0000 (13:47 +0000)
Suggested by: Lou Vanek <vanek@acd.net>

docs/manual.pdf
docs/reference.lisp
src/js.lisp
t/ref2test.lisp
t/reference-tests.lisp

index 6c53cfb..739344e 100644 (file)
Binary files a/docs/manual.pdf and b/docs/manual.pdf differ
index c7ae72f..70a718f 100644 (file)
@@ -233,9 +233,14 @@ an-object.foo => anObject.foo
 ;
 ; 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
 
index 329e8b2..e543092 100644 (file)
@@ -1313,9 +1313,14 @@ vice-versa.")
 (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
 
index a1cee4e..84c3dde 100644 (file)
@@ -67,8 +67,8 @@
                  ((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
index 4d3e821..b010ad4 100644 (file)
   "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")