Debian Codegraph @

contrib/unicode/ucd.lisp

raw
1 ;;; Common
2
3 (defparameter *destination*
4 (merge-pathnames "../../src/c/unicode/"
5 (or *load-truename* *compile-pathname*)))
6
7 (defvar *extension-directory*
8 (make-pathname :directory (pathname-directory *load-truename*)))
9
10 (defparameter *unicode-char-limit* #x110000)
11 (defparameter *page-size-exponent* 8)
12 (defparameter *page-size* (ash 1 *page-size-exponent*))
13
14 (defun total-ucd-pages ()
15 (floor *unicode-char-limit* *page-size*))
16
17 (defun cp-high (cp)
18 (ash cp (- *page-size-exponent*)))
19
20 (defun cp-low (cp)
21 (ldb (byte *page-size-exponent* 0) cp))
22
23 ;;; Generator
24
25 (defstruct ucd misc transform)
26
27 (defparameter *unicode-character-database*
28 (make-pathname :directory (pathname-directory *load-truename*)))
29
30 (defparameter *ucd-base* nil)
31 (defparameter *unicode-names* (make-hash-table))
32
33 (defparameter *last-uppercase* nil)
34 (defparameter *uppercase-transition-count* 0)
35 (defparameter *different-titlecases* nil)
36 (defparameter *different-numerics* nil)
37 (defparameter *name-size* 0)
38 (defparameter *misc-classless* 0)
39 (defparameter *misc-hash* (make-hash-table :test #'equal))
40 (defparameter *misc-index* -1)
41 (defparameter *misc-table* nil)
42 (defparameter *misc-mapping* nil)
43 (defparameter *both-cases* nil)
44 (defparameter *decompositions* nil)
45 (defparameter *decomposition-length-max* nil)
46 (defparameter *decomposition-types* nil)
47 (defparameter *decomposition-base* nil)
48
49 (defun hash-misc (gc-index bidi-index ccc-index decimal-digit digit
50 bidi-mirrored cl-both-case-p)
51 (let* ((list (list gc-index bidi-index ccc-index decimal-digit digit
52 bidi-mirrored cl-both-case-p))
53 (index (gethash list *misc-hash*)))
54 (or index
55 (progn
56 (vector-push list *misc-table*)
57 (setf (gethash list *misc-hash*)
58 (incf *misc-index*))))))
59
60 (defun compare-misc-entry (left right)
61 (destructuring-bind (left-gc-index left-bidi-index left-ccc-index
62 left-decimal-digit left-digit left-bidi-mirrored
63 left-cl-both-case-p)
64 left
65 (destructuring-bind (right-gc-index right-bidi-index right-ccc-index
66 right-decimal-digit right-digit right-bidi-mirrored
67 right-cl-both-case-p)
68 right
69 (or (and left-cl-both-case-p (not right-cl-both-case-p))
70 (and (or left-cl-both-case-p (not right-cl-both-case-p))
71 (or (< left-gc-index right-gc-index)
72 (and (= left-gc-index right-gc-index)
73 (or (< left-bidi-index right-bidi-index)
74 (and (= left-bidi-index right-bidi-index)
75 (or (< left-ccc-index right-ccc-index)
76 (and (= left-ccc-index right-ccc-index)
77 (or (string< left-decimal-digit
78 right-decimal-digit)
79 (and (string= left-decimal-digit
80 right-decimal-digit)
81 (or (string< left-digit right-digit)
82 (and (string= left-digit
83 right-digit)
84 (string< left-bidi-mirrored
85 right-bidi-mirrored))))))))))))))))
86
87 (defun build-misc-table ()
88 (sort *misc-table* #'compare-misc-entry)
89 (setq *misc-mapping* (make-array (1+ *misc-index*)))
90 (loop for i from 0 to *misc-index*
91 do (setf (aref *misc-mapping*
92 (gethash (aref *misc-table* i) *misc-hash*))
93 i)))
94
95 (defun slurp-ucd ()
96 (setq *last-uppercase* nil)
97 (setq *uppercase-transition-count* 0)
98 (setq *different-titlecases* nil)
99 (setq *different-numerics* nil)
100 (setq *name-size* 0)
101 (setq *misc-hash* (make-hash-table :test #'equal))
102 (setq *misc-index* -1)
103 (setq *misc-table* (make-array *page-size* :fill-pointer 0))
104 (setq *both-cases* nil)
105 (setq *decompositions* 0)
106 (setq *decomposition-types* (make-hash-table :test #'equal))
107 (setq *decomposition-length-max* 0)
108 (setq *decomposition-base* (make-array (total-ucd-pages) :initial-element nil))
109 (setq *ucd-base* (make-array (total-ucd-pages) :initial-element nil))
110 (with-open-file (*standard-input*
111 (make-pathname :name "UnicodeData" :type "txt"
112 :defaults *extension-directory*)
113 :direction :input :external-format :default)
114 (loop for line = (read-line nil nil)
115 while line
116 do (slurp-ucd-line line)))
117 (setf *misc-classless* (hash-misc 0 0 0 "" "" "N" nil))
118 (second-pass)
119 (build-misc-table)
120 *decompositions*)
121
122 (defun split-string (line character)
123 (loop for prev-position = 0 then (1+ position)
124 for position = (position character line :start prev-position)
125 collect (subseq line prev-position position)
126 do (unless position
127 (loop-finish))))
128
129 (defun init-indices (strings)
130 (let ((hash (make-hash-table :test #'equal)))
131 (loop for string in strings
132 for index from 0
133 do (setf (gethash string hash) index))
134 hash))
135
136 (defparameter *general-categories*
137 ;; Do not change the order of this list!
138
139 ;; As an optimization, the categories for ordinary lower and
140 ;; uppercase characters are put first in ecl_ucd_misc_table. This
141 ;; allows us to check whether a character is lower or uppercase
142 ;; simply by checking the index without having to consult the table
143 ;; itself.
144
145 ;; Moreover, order of the categories is used in src/c/char_ctype.d
146 (init-indices '("Lu" "Ll" "Lt" "Lm" "Lo" "Cc" "Cf" "Co" "Cs" "Mc"
147 "Me" "Mn" "Nd" "Nl" "No" "Pc" "Pd" "Pe" "Pf" "Pi"
148 "Po" "Ps" "Sc" "Sk" "Sm" "So" "Zl" "Zp" "Zs")))
149 (defparameter *bidi-classes*
150 (init-indices '("AL" "AN" "B" "BN" "CS" "EN" "ES" "ET" "FSI" "L"
151 "LRE" "LRI" "LRO" "NSM" "ON" "PDF" "PDI" "R" "RLE"
152 "RLI" "RLO" "S" "WS")))
153
154
155 (defparameter *block-first* nil)
156
157 (defun normalize-character-name (name)
158 (when (find #\_ name)
159 (error "Bad name for a character: ~A" name))
160 (unless (or (zerop (length name)) (find #\< name) (find #\> name))
161 (substitute #\_ #\Space name)))
162
163 ;;; 3400 -- 4DB5 : cjk ideograph extension a ;Lo;0;L;;;;;N;;;;;
164 ;;; AC00 -- D7A3 : hangul syllables ;Lo;0;L;;;;;N;;;;;
165 ;;; D800 -- F8FF : surrogates and private use
166 ;;; 20000 -- 2A6D6 : cjk ideograph extension b ;Lo;0;L;;;;;N;;;;;
167 ;;; F0000 -- FFFFD : private use
168 ;;; 100000 -- 10FFFD: private use
169 (defun encode-ucd-line (line code-point)
170 (destructuring-bind (name general-category canonical-combining-class
171 bidi-class decomposition-type-and-mapping
172 decimal-digit digit numeric bidi-mirrored
173 unicode-1-name iso-10646-comment simple-uppercase
174 simple-lowercase simple-titlecase)
175 line
176 (declare (ignore unicode-1-name iso-10646-comment))
177 (if (and (> (length name) 8)
178 (string= ", First>" name :start2 (- (length name) 8)))
179 (progn
180 (setq *block-first* code-point)
181 nil)
182 (let* ((gc-index (or (gethash general-category *general-categories*)
183 (error "unknown general category ~A"
184 general-category)))
185 (bidi-index (or (gethash bidi-class *bidi-classes*)
186 (error "unknown bidirectional class ~A"
187 bidi-class)))
188 (ccc-index (parse-integer canonical-combining-class))
189 (digit-index (unless (string= "" decimal-digit)
190 (parse-integer decimal-digit)))
191 (upper-index (unless (string= "" simple-uppercase)
192 (parse-integer simple-uppercase :radix 16)))
193 (lower-index (unless (string= "" simple-lowercase)
194 (parse-integer simple-lowercase :radix 16)))
195 (title-index (unless (string= "" simple-titlecase)
196 (parse-integer simple-titlecase :radix 16)))
197 (cl-both-case-p
198 (not (null (or (and (= gc-index 0) lower-index)
199 (and (= gc-index 1) upper-index)))))
200 (misc-index (hash-misc gc-index bidi-index ccc-index
201 decimal-digit digit bidi-mirrored
202 cl-both-case-p)))
203 (declare (ignore digit-index))
204 (incf *name-size* (length name))
205 (when (string/= "" decomposition-type-and-mapping)
206 (let ((split (split-string decomposition-type-and-mapping
207 #\Space)))
208 (when (char= #\< (aref (first split) 0))
209 (setf (gethash (pop split) *decomposition-types*) t))
210 (unless (aref *decomposition-base* (cp-high code-point))
211 (setf (aref *decomposition-base* (cp-high code-point))
212 (make-array (ash 1 *page-size-exponent*)
213 :initial-element nil)))
214 (setf (aref (aref *decomposition-base* (cp-high code-point))
215 (cp-low code-point))
216 (mapcar #'(lambda (string)
217 (parse-integer string :radix 16))
218 split))
219 (setq *decomposition-length-max*
220 (max *decomposition-length-max* (length split)))
221 (incf *decompositions* (length split))))
222 (when (and (string/= "" simple-uppercase)
223 (string/= "" simple-lowercase))
224 (push (list code-point upper-index lower-index) *both-cases*))
225 (when (string/= simple-uppercase simple-titlecase)
226 (push (cons code-point title-index) *different-titlecases*))
227 (when (string/= digit numeric)
228 (push (cons code-point numeric) *different-numerics*))
229 (cond
230 ((= gc-index 8)
231 (unless *last-uppercase*
232 (incf *uppercase-transition-count*))
233 (setq *last-uppercase* t))
234 (t
235 (when *last-uppercase*
236 (incf *uppercase-transition-count*))
237 (setq *last-uppercase* nil)))
238 (when (> ccc-index 255)
239 (error "canonical combining class too large ~A" ccc-index))
240 (let ((result (make-ucd :misc misc-index
241 :transform (or upper-index lower-index 0))))
242 (when (and (> (length name) 7)
243 (string= ", Last>" name :start2 (- (length name) 7)))
244 (let ((page-start (ash (+ *block-first*
245 (ash 1 *page-size-exponent*)
246 -1)
247 (- *page-size-exponent*)))
248 (page-end (ash code-point (- *page-size-exponent*))))
249 (loop for point from *block-first*
250 below (ash page-start *page-size-exponent*)
251 do (setf (aref (aref *ucd-base* (cp-high point))
252 (cp-low point))
253 result))
254 (loop for page from page-start below page-end
255 do (setf (aref *ucd-base* page)
256 (make-array (ash 1 *page-size-exponent*)
257 :initial-element result)))
258 (loop for point from (ash page-end *page-size-exponent*)
259 below code-point
260 do (setf (aref (aref *ucd-base* (cp-high point))
261 (cp-low point))
262 result))))
263 (values result (normalize-character-name name)))))))
264
265 (defun slurp-ucd-line (line)
266 (let* ((split-line (split-string line #\;))
267 (code-point (parse-integer (first split-line) :radix 16))
268 (code-high (ash code-point (- *page-size-exponent*)))
269 (code-low (ldb (byte *page-size-exponent* 0) code-point)))
270 (unless (aref *ucd-base* code-high)
271 (setf (aref *ucd-base* code-high)
272 (make-array (ash 1 *page-size-exponent*)
273 :initial-element nil)))
274 (multiple-value-bind (encoding name)
275 (encode-ucd-line (cdr split-line) code-point)
276 (setf (aref (aref *ucd-base* code-high) code-low) encoding
277 (gethash code-point *unicode-names*) name))))
278
279 (defun second-pass ()
280 (loop for i from 0 below (length *ucd-base*)
281 when (aref *ucd-base* i)
282 do (loop for j from 0 below (length (aref *ucd-base* i))
283 for result = (aref (aref *ucd-base* i) j)
284 when result
285 when (let* ((transform-point (ucd-transform result))
286 (transform-high (ash transform-point
287 (- *page-size-exponent*)))
288 (transform-low (ldb (byte *page-size-exponent* 0)
289 transform-point)))
290 (and (plusp transform-point)
291 (/= (ucd-transform
292 (aref (aref *ucd-base* transform-high)
293 transform-low))
294 (+ (ash i *page-size-exponent*) j))))
295 do (destructuring-bind (gc-index bidi-index ccc-index
296 decimal-digit digit bidi-mirrored
297 cl-both-case-p)
298 (aref *misc-table* (ucd-misc result))
299 (declare (ignore cl-both-case-p))
300 (format t "~A~%" (+ (ash i *page-size-exponent*) j))
301 (setf (ucd-misc result)
302 (hash-misc gc-index bidi-index ccc-index
303 decimal-digit digit bidi-mirrored
304 nil))))))
305
306 (defun write-3-byte (triplet stream)
307 (write-2-byte triplet stream)
308 (write-byte (ldb (byte 8 16) triplet) stream))
309
310 (defun write-2-byte (doublet stream)
311 (write-byte (ldb (byte 8 0) doublet) stream)
312 (write-byte (ldb (byte 8 8) doublet) stream))
313
314 (defun digit-to-byte (digit)
315 (if (string= "" digit)
316 255
317 (parse-integer digit)))
318
319 (defun output (&optional small-unicode)
320 (let* ((num-pages (/ (if small-unicode #x10000 *unicode-char-limit*)
321 *page-size*))
322 (ucd-file-name (concatenate 'base-string "ucd" (if small-unicode "16" "")))
323 (hash (make-hash-table :test #'equalp))
324 (index 0))
325 (loop for page across *ucd-base*
326 for i from 0 below num-pages
327 do (when page
328 (unless (gethash page hash)
329 (setf (gethash page hash)
330 (incf index)))))
331 (let ((array (make-array (1+ index))))
332 (maphash #'(lambda (key value)
333 (setf (aref array value) key))
334 hash)
335 (setf (aref array 0)
336 (make-array (ash 1 *page-size-exponent*) :initial-element nil))
337 (with-open-file (stream (make-pathname :name ucd-file-name
338 :type "dat"
339 :defaults *extension-directory*)
340 :direction :output
341 :element-type '(unsigned-byte 8)
342 :if-exists :supersede
343 :if-does-not-exist :create)
344 (let ((offset (* (length *misc-table*) 8)))
345 (write-byte (mod offset *page-size*) stream)
346 (write-byte (floor offset *page-size*) stream))
347 (loop for (gc-index bidi-index ccc-index decimal-digit digit
348 bidi-mirrored)
349 across *misc-table*
350 do (write-byte gc-index stream)
351 do (write-byte bidi-index stream)
352 do (write-byte ccc-index stream)
353 do (write-byte (digit-to-byte decimal-digit) stream)
354 do (write-byte (digit-to-byte digit) stream)
355 do (write-byte (if (string= "N" bidi-mirrored) 0 1) stream)
356 do (write-byte 0 stream)
357 do (write-byte 0 stream))
358 (print (length *misc-table*))
359 (loop for page across *ucd-base*
360 for i from 0 below num-pages
361 do (write-byte (if page (gethash page hash) 0) stream))
362 (loop for page across array
363 for i from 0 below num-pages
364 do (loop for entry across page
365 do (write-byte (if entry
366 (aref *misc-mapping* (ucd-misc entry))
367 *misc-classless*)
368 stream)
369 do (funcall (if small-unicode 'write-2-byte 'write-3-byte)
370 (if entry (ucd-transform entry) 0)
371 stream))))))
372 #+(or)
373 (with-open-file (f (make-pathname :name "ucd-names" :type "lisp-expr"
374 :defaults *extension-directory*)
375 :direction :output
376 :if-exists :supersede
377 :if-does-not-exist :create)
378 (with-standard-io-syntax
379 (write-string ";;; Do not edit by hand: generated by ucd.lisp" f)
380 (maphash (lambda (code name)
381 (when name
382 (print code f)
383 (prin1 name f)))
384 *unicode-names*))
385 (setf *unicode-names* nil))
386 (with-open-file (*standard-output*
387 (make-pathname :name "numerics"
388 :type "lisp-expr"
389 :defaults *extension-directory*)
390 :direction :output
391 :if-exists :supersede
392 :if-does-not-exist :create)
393 (let ((*print-pretty* t))
394 (prin1 (mapcar #'(lambda (x) (cons (car x) (read-from-string (cdr x))))
395 *different-numerics*))))
396 (with-open-file (*standard-output*
397 (make-pathname :name "titlecases"
398 :type "lisp-expr"
399 :defaults *extension-directory*)
400 :direction :output
401 :if-exists :supersede
402 :if-does-not-exist :create)
403 (let ((*print-pretty* t))
404 (prin1 *different-titlecases*)))
405 (with-open-file (*standard-output*
406 (make-pathname :name "misc"
407 :type "lisp-expr"
408 :defaults *extension-directory*)
409 :direction :output
410 :if-exists :supersede
411 :if-does-not-exist :create)
412 (let ((*print-pretty* t))
413 (prin1 `(:length ,(length *misc-table*)
414 :uppercase ,(loop for (gc-index) across *misc-table*
415 for i from 0
416 when (= gc-index 0)
417 collect i)
418 :lowercase ,(loop for (gc-index) across *misc-table*
419 for i from 0
420 when (= gc-index 1)
421 collect i)
422 :titlecase ,(loop for (gc-index) across *misc-table*
423 for i from 0
424 when (= gc-index 2)
425 collect i)))))
426 (values))
427
428 (defmacro with-c-file ((stream-var name) &body body)
429 `(with-open-file (,stream-var ,name
430 :direction :output
431 :external-format #-unicode :default #+unicode :us-ascii
432 :if-exists :supersede
433 :if-does-not-exist :create)
434 ,@body))
435
436 (defun write-enums (hash-table name-creator stream)
437 (let (enums)
438 (format stream "~%enum {~%")
439 (maphash #'(lambda (k v)
440 (push (cons v (funcall name-creator k)) enums))
441 hash-table)
442 (setf enums (sort enums #'< :key #'car))
443 (mapc #'(lambda (e) (format stream "~A,~%" (cdr e))) enums)
444 (format stream "};~%")))
445
446 (defun output-c (&optional small-unicode)
447 (let* ((num-pages (/ (if small-unicode #x10000 *unicode-char-limit*)
448 *page-size*))
449 (ucd-file-name (concatenate 'base-string "ucd" (if small-unicode "16" "")))
450 (hash (make-hash-table :test #'equalp))
451 (index 0)
452 array)
453 (with-c-file (stream (make-pathname :name ucd-file-name
454 :type "h"
455 :defaults *destination*))
456 (format stream "/*
457 * Unicode character database.
458 *
459 * auto-generated, do not edit! (see contrib/unicode/)
460 */")
461 (write-enums *general-categories*
462 #'(lambda (name) (format nil "ECL_UCD_GENERAL_CATEGORY_~A" name))
463 stream)
464 (write-enums *bidi-classes*
465 #'(lambda (name) (format nil "ECL_UCD_BIDIRECTIONAL_CLASS_~A" name))
466 stream)
467 (assert (and (= (gethash "Lu" *general-categories*) 0)
468 (= (gethash "Ll" *general-categories*) 1)))
469 (let* ((uppercase-limit
470 (position-if-not #'(lambda (entry)
471 ;; gc-index = 0, both-case-p = t
472 (and (= (first entry) 0) (first (last entry))))
473 *misc-table*))
474 (lowercase-limit
475 (position-if-not #'(lambda (entry)
476 ;; gc-index = 1, both-case-p = t
477 (and (= (first entry) 1) (first (last entry))))
478 *misc-table*
479 :start uppercase-limit)))
480 (format stream "#define ECL_UCD_UPPERCASE_LIMIT ~D~%"
481 uppercase-limit)
482 (format stream "#define ECL_UCD_LOWERCASE_LIMIT ~D~%"
483 lowercase-limit)))
484 (with-c-file (stream (make-pathname :name ucd-file-name
485 :type "c"
486 :defaults *destination*))
487 (format stream "/*
488 * Unicode character database.
489 *
490 * auto-generated, do not edit! (see contrib/unicode/)
491 */")
492 (format stream "~%extern const unsigned char ecl_ucd_page_table_0[];")
493 (loop for page across *ucd-base*
494 for i from 0 below num-pages
495 do (when page
496 (unless (gethash page hash)
497 (setf (gethash page hash) (incf index))
498 (print index)
499 (format stream "~%extern const unsigned char ecl_ucd_page_table_~D[];" index))))
500 (setf array (make-array (incf index)))
501 (maphash #'(lambda (key value)
502 (setf (aref array value) key))
503 hash)
504 (setf (aref array 0)
505 (make-array (ash 1 *page-size-exponent*) :initial-element nil))
506 (format stream "~%~%const unsigned char ecl_ucd_misc_table[~D] = {"
507 (* 8 (length *misc-table*)))
508 (loop with comma = ""
509 for (gc-index bidi-index ccc-index decimal-digit digit
510 bidi-mirrored)
511 across *misc-table*
512 do (format stream "~%~A~D, ~D, ~D, ~D, ~D, ~D, ~D, ~D"
513 comma
514 gc-index bidi-index ccc-index
515 (digit-to-byte decimal-digit)
516 (digit-to-byte digit)
517 (if (string= "N" bidi-mirrored) 0 1)
518 0
519 0)
520 do (setf comma ","))
521 (print *misc-table*)
522 (print (length *misc-table*))
523 (format stream "~%};")
524 (format stream "~%~%const unsigned char *const ecl_ucd_page_table[~D] = {"
525 num-pages)
526 (loop with comma = ""
527 for page across *ucd-base*
528 for i from 0 below num-pages
529 for name = (if page
530 (format nil "ecl_ucd_page_table_~D" (gethash page hash))
531 ;; fixme, this was so previously
532 "ecl_ucd_page_table_0")
533 do (format stream "~%~A~A"
534 comma
535 name)
536 do (setf comma ","))
537 (format stream "~%};"))
538 (print index)
539 (loop for i from 0 below index by 16
540 for next = (min index (+ i 16))
541 for c-file = (format nil "~A-~4,'0D" ucd-file-name i)
542 do (with-c-file (stream (make-pathname :name c-file
543 :type "c"
544 :defaults *destination*))
545 (loop for j from i below next
546 for page = (aref array j)
547 do (format stream "~%const unsigned char ecl_ucd_page_table_~D[] = {" j)
548 do (loop with comma = ""
549 for entry across page
550 for other-case = (if entry (ucd-transform entry) 0)
551 do (format stream
552 (if small-unicode
553 "~%~A~D,~D,~D"
554 "~%~A~D,~D,~D,~D")
555 comma
556 (if entry
557 (aref *misc-mapping* (ucd-misc entry))
558 *misc-classless*)
559 (ldb (byte 8 0) other-case)
560 (ldb (byte 8 8) other-case)
561 (ldb (byte 8 16) other-case))
562 do (setf comma ","))
563 do (format stream "~%};")))))
564 (values))
565
566 (defun read-compiled-ucd ()
567 (with-open-file (stream (make-pathname :name *ucd-file-name*
568 :type "dat"
569 :defaults *extension-directory*)
570 :direction :input
571 :element-type '(unsigned-byte 8))
572 (let ((length (file-length stream)))
573 (setq *compiled-ucd*
574 (make-array length :element-type '(unsigned-byte 8)))
575 (read-sequence *compiled-ucd* stream)))
576 (values))
577
578 #|
579 (slurp-ucd)
580 (output)
581 (output t)
582 |#