2bc0014964ed8b80e87763ee17f71542bf76278d
[gnulib.git] / lib / uniname / gen-uninames.lisp
1 #!/usr/local/bin/clisp -C
2
3 ;;; Creation of gnulib's uninames.h from the UnicodeData.txt table.
4 ;;; Bruno Haible 2000-12-28
5
6 (defparameter add-comments nil)
7
8 (defstruct unicode-char
9   (code nil :type integer)
10   (name nil :type string)
11   word-indices
12   word-indices-index
13 )
14
15 (defstruct word-list
16   (hashed nil :type hash-table)
17   (sorted nil :type list)
18   size                          ; number of characters total
19   length                        ; number of words
20 )
21
22 (defun main (inputfile outputfile)
23   (declare (type string inputfile outputfile))
24   #+UNICODE (setq *default-file-encoding* charset:utf-8)
25   (let ((all-chars '()))
26     ;; Read all characters and names from the input file.
27     (with-open-file (istream inputfile :direction :input)
28       (loop
29         (let ((line (read-line istream nil nil)))
30           (unless line (return))
31           (let* ((i1 (position #\; line))
32                  (i2 (position #\; line :start (1+ i1)))
33                  (code-string (subseq line 0 i1))
34                  (code (parse-integer code-string :radix 16))
35                  (name-string (subseq line (1+ i1) i2)))
36             ; Ignore characters whose name starts with "<".
37             (unless (eql (char name-string 0) #\<)
38               ; Also ignore Hangul syllables; they are treated specially.
39               (unless (<= #xAC00 code #xD7A3)
40                 ; Also ignore CJK compatibility ideographs; they are treated
41                 ; specially as well.
42                 (unless (or (<= #xF900 code #xFA2D) (<= #xFA30 code #xFA6A)
43                             (<= #xFA70 code #xFAD9) (<= #x2F800 code #x2FA1D))
44                   ; Transform the code so that it fits in 16 bits. In
45                   ; Unicode 3.1 the following ranges are used.
46                   ;   0x00000..0x04DFF  >>12=  0x00..0x04  -> 0x0..0x4
47                   ;   0x0A000..0x0A4FF  >>12=  0x0A        -> 0x5
48                   ;   0x0F900..0x0FFFF  >>12=  0x0F        -> 0x6
49                   ;   0x10300..0x104FF  >>12=  0x10        -> 0x7
50                   ;   0x12000..0x12473  >>12=  0x12        -> 0x8
51                   ;   0x1D000..0x1D7DD  >>12=  0x1D        -> 0x9
52                   ;   0x2F800..0x2FAFF  >>12=  0x2F        -> 0xA
53                   ;   0xE0000..0xE00FF  >>12=  0xE0        -> 0xB
54                   (flet ((transform (x)
55                            (dpb
56                              (case (ash x -12)
57                                ((#x00 #x01 #x02 #x03 #x04) (ash x -12))
58                                (#x0A 5)
59                                (#x0F 6)
60                                (#x10 7)
61                                (#x12 8)
62                                (#x1D 9)
63                                (#x2F #xA)
64                                (#xE0 #xB)
65                                (t (error "Update the transform function for 0x~5,'0X" x))
66                              )
67                              (byte 8 12)
68                              x
69                         )) )
70                     (push (make-unicode-char :code (transform code)
71                                              :name name-string)
72                           all-chars
73             ) ) ) ) )
74     ) ) ) )
75     (setq all-chars (nreverse all-chars))
76     ;; Split into words.
77     (let ((words-by-length (make-array 0 :adjustable t)))
78       (dolist (name (list* "HANGUL SYLLABLE" "CJK COMPATIBILITY" (mapcar #'unicode-char-name all-chars)))
79         (let ((i1 0))
80           (loop
81             (when (>= i1 (length name)) (return))
82             (let ((i2 (or (position #\Space name :start i1) (length name))))
83               (let* ((word (subseq name i1 i2))
84                      (len (length word)))
85                 (when (>= len (length words-by-length))
86                   (adjust-array words-by-length (1+ len))
87                 )
88                 (unless (aref words-by-length len)
89                   (setf (aref words-by-length len)
90                         (make-word-list
91                           :hashed (make-hash-table :test #'equal)
92                           :sorted '()
93                 ) )     )
94                 (let ((word-list (aref words-by-length len)))
95                   (unless (gethash word (word-list-hashed word-list))
96                     (setf (gethash word (word-list-hashed word-list)) t)
97                     (push word (word-list-sorted word-list))
98                 ) )
99               )
100               (setq i1 (1+ i2))
101       ) ) ) )
102       ;; Sort the word lists.
103       (dotimes (len (length words-by-length))
104         (unless (aref words-by-length len)
105           (setf (aref words-by-length len)
106                 (make-word-list
107                   :hashed (make-hash-table :test #'equal)
108                   :sorted '()
109         ) )     )
110         (let ((word-list (aref words-by-length len)))
111           (setf (word-list-sorted word-list)
112                 (sort (word-list-sorted word-list) #'string<)
113           )
114           (setf (word-list-size word-list)
115                 (reduce #'+ (mapcar #'length (word-list-sorted word-list)))
116           )
117           (setf (word-list-length word-list)
118                 (length (word-list-sorted word-list))
119       ) ) )
120       ;; Output the tables.
121       (with-open-file (ostream outputfile :direction :output
122                        #+UNICODE :external-format #+UNICODE charset:ascii)
123         (format ostream "/*~%")
124         (format ostream " * ~A~%" (file-namestring outputfile))
125         (format ostream " *~%")
126         (format ostream " * Unicode character name table.~%")
127         (format ostream " * Generated automatically by the gen-uninames utility.~%")
128         (format ostream " */~%")
129         (format ostream "~%")
130         (format ostream "static const char unicode_name_words[~D] = {~%"
131                         (let ((sum 0))
132                           (dotimes (len (length words-by-length))
133                             (let ((word-list (aref words-by-length len)))
134                               (incf sum (word-list-size word-list))
135                           ) )
136                           sum
137         )               )
138         (dotimes (len (length words-by-length))
139           (let ((word-list (aref words-by-length len)))
140             (dolist (word (word-list-sorted word-list))
141               (format ostream " ~{ '~C',~}~%" (coerce word 'list))
142         ) ) )
143         (format ostream "};~%")
144         (format ostream "#define UNICODE_CHARNAME_NUM_WORDS ~D~%"
145                         (let ((sum 0))
146                           (dotimes (len (length words-by-length))
147                             (let ((word-list (aref words-by-length len)))
148                               (incf sum (word-list-length word-list))
149                           ) )
150                           sum
151         )               )
152         #| ; Redundant data
153         (format ostream "static const uint16_t unicode_name_word_offsets[~D] = {~%"
154                         (let ((sum 0))
155                           (dotimes (len (length words-by-length))
156                             (let ((word-list (aref words-by-length len)))
157                               (incf sum (word-list-length word-list))
158                           ) )
159                           sum
160         )               )
161         (dotimes (len (length words-by-length))
162           (let ((word-list (aref words-by-length len)))
163             (when (word-list-sorted word-list)
164               (format ostream " ")
165               (do ((l (word-list-sorted word-list) (cdr l))
166                    (offset 0 (+ offset (length (car l)))))
167                   ((endp l))
168                 (format ostream "~<~% ~0,79:; ~D,~>" offset)
169               )
170               (format ostream "~%")
171         ) ) )
172         (format ostream "};~%")
173         |#
174         (format ostream "static const struct { uint16_t extra_offset; uint16_t ind_offset; } unicode_name_by_length[~D] = {~%"
175                         (1+ (length words-by-length))
176         )
177         (let ((extra-offset 0)
178               (ind-offset 0))
179           (dotimes (len (length words-by-length))
180             (let ((word-list (aref words-by-length len)))
181               (format ostream "  { ~D, ~D },~%" extra-offset ind-offset)
182               (incf extra-offset (word-list-size word-list))
183               (incf ind-offset (word-list-length word-list))
184           ) )
185           (format ostream "  { ~D, ~D }~%" extra-offset ind-offset)
186         )
187         (format ostream "};~%")
188         (let ((ind-offset 0))
189           (dotimes (len (length words-by-length))
190             (let ((word-list (aref words-by-length len)))
191               (dolist (word (word-list-sorted word-list))
192                 (setf (gethash word (word-list-hashed word-list)) ind-offset)
193                 (incf ind-offset)
194         ) ) ) )
195         (dolist (word '("HANGUL" "SYLLABLE" "CJK" "COMPATIBILITY"))
196           (format ostream "#define UNICODE_CHARNAME_WORD_~A ~D~%" word
197                           (gethash word (word-list-hashed (aref words-by-length (length word))))
198         ) )
199         ;; Compute the word-indices for every unicode-char.
200         (dolist (uc all-chars)
201           (let ((name (unicode-char-name uc))
202                 (indices '()))
203             (let ((i1 0))
204               (loop
205                 (when (>= i1 (length name)) (return))
206                 (let ((i2 (or (position #\Space name :start i1) (length name))))
207                   (let* ((word (subseq name i1 i2))
208                          (len (length word)))
209                     (push (gethash word (word-list-hashed (aref words-by-length len)))
210                           indices
211                     )
212                   )
213                   (setq i1 (1+ i2))
214             ) ) )
215             (setf (unicode-char-word-indices uc)
216                   (coerce (nreverse indices) 'vector)
217             )
218         ) )
219         ;; Sort the list of unicode-chars by word-indices.
220         (setq all-chars
221               (sort all-chars
222                     (lambda (vec1 vec2)
223                       (let ((len1 (length vec1))
224                             (len2 (length vec2)))
225                         (do ((i 0 (1+ i)))
226                             (nil)
227                           (if (< i len2)
228                             (if (< i len1)
229                               (cond ((< (aref vec1 i) (aref vec2 i)) (return t))
230                                     ((> (aref vec1 i) (aref vec2 i)) (return nil))
231                               )
232                               (return t)
233                             )
234                             (return nil)
235                     ) ) ) )
236                     :key #'unicode-char-word-indices
237         )     )
238         ;; Output the word-indices.
239         (format ostream "static const uint16_t unicode_names[~D] = {~%"
240                         (reduce #'+ (mapcar (lambda (uc) (length (unicode-char-word-indices uc))) all-chars))
241         )
242         (let ((i 0))
243           (dolist (uc all-chars)
244             (format ostream " ~{ ~D,~}"
245                             (maplist (lambda (r) (+ (* 2 (car r)) (if (cdr r) 1 0)))
246                                      (coerce (unicode-char-word-indices uc) 'list)
247                             )
248             )
249             (when add-comments
250               (format ostream "~40T/* ~A */" (unicode-char-name uc))
251             )
252             (format ostream "~%")
253             (setf (unicode-char-word-indices-index uc) i)
254             (incf i (length (unicode-char-word-indices uc)))
255         ) )
256         (format ostream "};~%")
257         (format ostream "static const struct { uint16_t code; uint32_t name:24; }~%")
258         (format ostream "#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)~%__attribute__((__packed__))~%#endif~%")
259         (format ostream "unicode_name_to_code[~D] = {~%"
260                         (length all-chars)
261         )
262         (dolist (uc all-chars)
263           (format ostream "  { 0x~4,'0X, ~D },"
264                           (unicode-char-code uc)
265                           (unicode-char-word-indices-index uc)
266           )
267           (when add-comments
268             (format ostream "~21T/* ~A */" (unicode-char-name uc))
269           )
270           (format ostream "~%")
271         )
272         (format ostream "};~%")
273         (format ostream "static const struct { uint16_t code; uint32_t name:24; }~%")
274         (format ostream "#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)~%__attribute__((__packed__))~%#endif~%")
275         (format ostream "unicode_code_to_name[~D] = {~%"
276                         (length all-chars)
277         )
278         (dolist (uc (sort (copy-list all-chars) #'< :key #'unicode-char-code))
279           (format ostream "  { 0x~4,'0X, ~D },"
280                           (unicode-char-code uc)
281                           (unicode-char-word-indices-index uc)
282           )
283           (when add-comments
284             (format ostream "~21T/* ~A */" (unicode-char-name uc))
285           )
286           (format ostream "~%")
287         )
288         (format ostream "};~%")
289         (format ostream "#define UNICODE_CHARNAME_MAX_LENGTH ~D~%"
290                         (reduce #'max (mapcar (lambda (uc) (length (unicode-char-name uc))) all-chars))
291         )
292         (format ostream "#define UNICODE_CHARNAME_MAX_WORDS ~D~%"
293                         (reduce #'max (mapcar (lambda (uc) (length (unicode-char-word-indices uc))) all-chars))
294         )
295       )
296 ) ) )
297
298 (main (first *args*) (second *args*))