1 ;;; Copyright 2005 Manuel Odendahl
2 ;;; Copyright 2005-2006 Edward Marco Baringer
3 ;;; Copyright 2007 Attila Lendvai
4 ;;; Copyright 2007 Red Daly
5 ;;; Copyright 2007-2012 Vladimir Sedach
6 ;;; Copyright 2008 Travis Cross
8 ;;; SPDX-License-Identifier: BSD-3-Clause
10 ;;; Redistribution and use in source and binary forms, with or
11 ;;; without modification, are permitted provided that the following
12 ;;; conditions are met:
14 ;;; 1. Redistributions of source code must retain the above copyright
15 ;;; notice, this list of conditions and the following disclaimer.
17 ;;; 2. Redistributions in binary form must reproduce the above
18 ;;; copyright notice, this list of conditions and the following
19 ;;; disclaimer in the documentation and/or other materials provided
20 ;;; with the distribution.
22 ;;; 3. Neither the name of the copyright holder nor the names of its
23 ;;; contributors may be used to endorse or promote products derived
24 ;;; from this software without specific prior written permission.
26 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
27 ;;; CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
28 ;;; INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
29 ;;; MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 ;;; DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
31 ;;; BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 ;;; EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
33 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 ;;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
35 ;;; ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 ;;; OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 ;;; OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 ;;; POSSIBILITY OF SUCH DAMAGE.
40 (in-package #:parenscript
)
42 (let ((cache (make-hash-table :test
'equal
)))
43 (defun encode-js-identifier (identifier)
44 "Given a string, produces to a valid JavaScript identifier by
45 following transformation heuristics case conversion. For example,
46 paren-script becomes parenScript, *some-global* becomes SOMEGLOBAL."
47 (when (and (not (string= identifier
"[]"))
48 (find #\
[ identifier
))
49 (warn "Parenscript symbol ~A contains a literal array accessor.
50 This compound naming convention is deprecated and will be removed!
51 Use AREF, ELT, GETPROP, @, or CHAIN instead."
53 (when (find #\. identifier
)
54 (warn "Parenscript symbol ~A contains one or more dot operators.
55 This compound naming convention is deprecated and will be removed!
56 Use GETPROP, @, or CHAIN instead."
59 (gethash identifier cache
)
61 (gethash identifier cache
)
63 ((some (lambda (c) (find c
"-*+!?#@%/=:<>^")) identifier
)
68 (cl-ppcre:scan-to-strings
69 "[\\*|\\+](.+)[\\*|\\+](.*)"
70 identifier
:sharedp t
))
72 identifier
(concatenate
73 'string
(aref it
0) (aref it
1))))
74 ((and (> (length identifier
) 1)
75 (or (eql (char identifier
0) #\
+)
76 (eql (char identifier
0) #\
*)))
78 identifier
(subseq identifier
1))))
79 (with-output-to-string (acc)
81 for c across identifier
84 (setf lowercase
(not lowercase
)))
85 ((position c
"!?#@%+*/=:<>^")
87 (aref #("bang" "what" "hash" "at" "percent"
88 "plus" "star" "slash" "equals" "colon"
89 "lessthan" "greaterthan" "caret")
94 (if (and lowercase
(not all-uppercase
))
98 (setf lowercase t
)))))))
99 (#.
(eql :invert
(readtable-case
100 (named-readtables:find-readtable
:parenscript
)))
102 ((every #'upper-case-p
103 (remove-if-not #'alpha-char-p identifier
))
104 (string-downcase identifier
))
105 ((every #'lower-case-p
106 (remove-if-not #'alpha-char-p identifier
))
107 (string-upcase identifier
))
111 (defun ordered-set-difference (list1 list2
&key
(test #'eql
))
112 "CL set-difference may not preserve order."
113 (reduce (lambda (list el
) (remove el list
:test test
))
116 (defun flatten (x &optional acc
)
118 ((atom x
) (cons x acc
))
119 (t (flatten (car x
) (flatten (cdr x
) acc
)))))
121 (defun flatten-blocks (body)
123 (if (and (listp (car body
)) (eq 'ps-js
:block
(caar body
)))
124 (append (flatten-blocks (cdr (car body
)))
125 (flatten-blocks (cdr body
)))
126 (cons (car body
) (flatten-blocks (cdr body
))))))
128 (defun tree-find (A tree
)
131 (loop for x on tree thereis
132 (or (tree-find A
(car x
))
133 (unless (listp (cdr x
))
134 (equal A
(cdr x
))))))))
136 (defun parse-semver (semver-string)
137 (let ((semver-list (cl-ppcre:split
"\\." semver-string
))
141 (* (expt 1000 (- 2 i
))
142 (parse-integer (or (nth i semver-list
) "0")))))
145 (defun js-target-at-least (version)
146 (>= (parse-semver *js-target-version
*) (parse-semver version
)))