More refactorings to the printer, exported symbols that control printer behavior.
authorVladimir Sedach <vsedach@gmail.com>
Mon, 29 Oct 2007 19:45:21 +0000 (19:45 +0000)
committerVladimir Sedach <vsedach@gmail.com>
Mon, 29 Oct 2007 19:45:21 +0000 (19:45 +0000)
parenscript.asd
src/compilation-interface.lisp
src/package.lisp
src/printer.lisp [moved from src/js-translation.lisp with 94% similarity]

index b6867db..51b86a0 100644 (file)
@@ -23,8 +23,8 @@
                             (:file "compiler" :depends-on ("namespace"))
                             (:file "js-macrology" :depends-on ("compiler"))
                             (:file "ps-macrology" :depends-on ("utils" "js-macrology" "parse-lambda-list"))
-                            (:file "js-translation" :depends-on ("ps-macrology"))
-                            (:file "compilation-interface" :depends-on ("package" "js-translation"))
+                            (:file "printer" :depends-on ("ps-macrology"))
+                            (:file "compilation-interface" :depends-on ("package" "printer"))
                              (:file "deprecated-interface" :depends-on ("compilation-interface"))
                             ;; standard library
                              (:module :lib
index ec35faa..fed343d 100644 (file)
@@ -8,7 +8,8 @@ is output to the OUTPUT-STREAM stream."
   (parenscript-print (compile-parenscript-form script-form :expecting :statement) output-stream))
 
 (defmacro ps (&body body)
-  "A macro that returns a Javascript string of the supplied Parenscript forms."
+  "Given Parenscript forms (an implicit progn), expands to code which
+compiles those forms to a JavaScript string."
   `(ps* '(progn ,@body)))
 
 (defun ps* (&rest body)
@@ -16,13 +17,13 @@ is output to the OUTPUT-STREAM stream."
 Body is evaluated."
   (compile-script `(progn ,@body)))
 
-(defun ps-inline* (form &optional (quote-char #\"))
-  (let ((*js-quote-char* quote-char))
-    (concatenate 'string
-                 "javascript:"
-                 (remove #\Newline
-                         (parenscript-print (compile-parenscript-form form :expecting :statement))
-                         :from-end t))))
+(defvar *js-inline-string-delimiter* #\"
+  "Controls the string delimiter char used when compiling Parenscript in ps-inline.")
 
-(defmacro ps-inline (form &optional (quote-char #\"))
-  `(ps-inline* ',form ,quote-char))
+(defun ps-inline* (form &optional (*js-string-delimiter* *js-inline-string-delimiter*))
+  (concatenate 'string
+               "javascript:"
+               (parenscript-print (compile-parenscript-form form :expecting :statement))))
+
+(defmacro ps-inline (form &optional (string-delimiter '*js-inline-string-delimiter*))
+  `(ps-inline* ',form ,string-delimiter))
index 8928193..639eefd 100644 (file)
    #:obfuscate-package
    #:unobfuscate-package
 
+   ;; printer
+   #:*js-string-delimiter*
+   #:*js-inline-string-delimiter*
+   #:*ps-print-pretty*
+   #:*indent-num-spaces*
+
    ;; deprecated interface
    #:gen-js-name
    #:gen-js-name-string
similarity index 94%
rename from src/js-translation.lisp
rename to src/printer.lisp
index 5570ded..ac5ac52 100644 (file)
@@ -16,9 +16,7 @@
           (print-ps ps-form)))))
 
 (defun psw (obj) ;; parenscript-write
-  (cond ((stringp obj) (write-string obj *ps-output-stream*))
-        ((characterp obj) (write-char obj *ps-output-stream*))
-        (t (princ obj *ps-output-stream*))))    
+  (princ obj *ps-output-stream*))    
 
 (defgeneric ps-print% (special-form-name special-form-args))
 
@@ -35,7 +33,7 @@ arguments, defines a printer for that form using the given body."
 
 (defgeneric ps-print (compiled-form))
 
-(defmethod ps-print ((form null)) ;; don't print nils (ex: result of defining macros, etc.)
+(defmethod ps-print ((form null)) ;; don't print top-level nils (ex: result of defining macros, etc.)
   )
 
 (defmethod ps-print ((compiled-form cons))
@@ -43,20 +41,20 @@ arguments, defines a printer for that form using the given body."
 indent position."
   (ps-print% (car compiled-form) (cdr compiled-form)))
 
-;;; indenter
-
-(defparameter *indent-num-space* 4)
+;;; indentation
+(defvar *ps-print-pretty* t)
+(defvar *indent-num-spaces* 4)
 
 (defun newline-and-indent ()
-  (when (fresh-line *ps-output-stream*)
-    (loop repeat (* *indent-level* *indent-num-space*)
+  (when (and (fresh-line *ps-output-stream*) *ps-print-pretty*)
+    (loop repeat (* *indent-level* *indent-num-spaces*)
           do (psw #\Space))))
 
 ;;; string literals
-(defvar *js-quote-char* #\'
-  "Specifies which character JS should use for delimiting strings.
+(defvar *js-string-delimiter* #\'
+  "Specifies which character should be used for delimiting strings.
 
-This variable is useful when have to embed some javascript code
+This variable is used when you want to embed the resulting JavaScript
 in an html attribute delimited by #\\\" as opposed to #\\', or
 vice-versa.")
 
@@ -72,16 +70,15 @@ vice-versa.")
 (defmethod ps-print ((string string))
   (flet ((lisp-special-char-to-js (lisp-char)
            (car (rassoc lisp-char *js-lisp-escaped-chars*))))
-    (psw *js-quote-char*)
+    (psw *js-string-delimiter*)
     (loop for char across string
           for code = (char-code char)
           for special = (lisp-special-char-to-js char)
-          do (cond (special (psw #\\)
-                            (psw special))
+          do (cond (special (psw #\\) (psw special))
                    ((or (<= code #x1f) (>= code #x80))
                     (format *ps-output-stream* "\\u~4,'0x" code))
-                   (t (psw char)))
-          finally (psw *js-quote-char*))))
+                   (t (psw char))))
+    (psw *js-string-delimiter*)))
 
 (defmethod ps-print ((number number))
   (format *ps-output-stream* (if (integerp number) "~S" "~F") number))