| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
(define (string-split string at) |
| 11 |
(let ((count (string-length string))) |
| 12 |
(let loop ((index 0) |
| 13 |
(rev-result '())) |
| 14 |
(cond |
| 15 |
((>= index count) |
| 16 |
(reverse (cons "" rev-result))) |
| 17 |
((string-index string at index) |
| 18 |
=> (lambda (found) |
| 19 |
(loop (+ 1 found) |
| 20 |
(cons (substring string index found) |
| 21 |
rev-result)))) |
| 22 |
(else |
| 23 |
(reverse (cons (substring string index count) |
| 24 |
rev-result))))))) |
| 25 |
|
| 26 |
|
| 27 |
(define (split-unicode-data-record line) |
| 28 |
(string-split line #\;)) |
| 29 |
|
| 30 |
(define (maybe-code-point text default) |
| 31 |
(if (zero? (string-length text)) |
| 32 |
default |
| 33 |
(string->number text 16))) |
| 34 |
|
| 35 |
(define-record-type code-point-info :code-point-info |
| 36 |
(make-code-point-info code-point |
| 37 |
name |
| 38 |
general-category |
| 39 |
combining-class |
| 40 |
bidirectional-category-id |
| 41 |
canonical-decomposition |
| 42 |
compatibility-decomposition |
| 43 |
decimal-digit-value |
| 44 |
digit-value |
| 45 |
numeric-value |
| 46 |
mirrored? |
| 47 |
unicode-1.0-name |
| 48 |
iso-10646-comment |
| 49 |
uppercase-code-point |
| 50 |
lowercase-code-point |
| 51 |
titlecase-code-point) |
| 52 |
code-point-info? |
| 53 |
|
| 54 |
(code-point code-point-info-code-point) |
| 55 |
|
| 56 |
(name code-point-info-name) |
| 57 |
|
| 58 |
(general-category code-point-info-general-category) |
| 59 |
|
| 60 |
(combining-class code-point-info-combining-class) |
| 61 |
|
| 62 |
(bidirectional-category-id code-point-info-bidirectional-category-id) |
| 63 |
|
| 64 |
(canonical-decomposition code-point-info-canonical-decomposition) |
| 65 |
(compatibility-decomposition code-point-info-compatibility-decomposition) |
| 66 |
|
| 67 |
(decimal-digit-value code-point-info-decimal-digit-value) |
| 68 |
|
| 69 |
(digit-value code-point-info-digit-value) |
| 70 |
|
| 71 |
(numeric-value code-point-info-numeric-value) |
| 72 |
|
| 73 |
(mirrored? code-point-info-mirrored?) |
| 74 |
|
| 75 |
(unicode-1.0-name code-point-info-unicode-1.0-name) |
| 76 |
|
| 77 |
(iso-10646-comment code-point-info-iso-10646-comment) |
| 78 |
|
| 79 |
(uppercase-code-point code-point-info-uppercase-code-point) |
| 80 |
|
| 81 |
(lowercase-code-point code-point-info-lowercase-code-point) |
| 82 |
|
| 83 |
(titlecase-code-point code-point-info-titlecase-code-point)) |
| 84 |
|
| 85 |
(define-record-discloser :code-point-info |
| 86 |
(lambda (r) |
| 87 |
(list 'code-point-info |
| 88 |
(code-point-info-code-point r) |
| 89 |
(code-point-info-name r) |
| 90 |
(code-point-info-general-category r) |
| 91 |
(code-point-info-combining-class r) |
| 92 |
(code-point-info-bidirectional-category-id r) |
| 93 |
(code-point-info-canonical-decomposition r) |
| 94 |
(code-point-info-compatibility-decomposition r) |
| 95 |
(code-point-info-decimal-digit-value r) |
| 96 |
(code-point-info-digit-value r) |
| 97 |
(code-point-info-numeric-value r) |
| 98 |
(code-point-info-mirrored? r) |
| 99 |
(code-point-info-unicode-1.0-name r) |
| 100 |
(code-point-info-iso-10646-comment r) |
| 101 |
(code-point-info-uppercase-code-point r) |
| 102 |
(code-point-info-lowercase-code-point r) |
| 103 |
(code-point-info-titlecase-code-point r)))) |
| 104 |
|
| 105 |
(define (unicode-data-record->info line) |
| 106 |
(destructure (((code-point-hex |
| 107 |
name |
| 108 |
general-category-id |
| 109 |
combining-class-id |
| 110 |
bidirectional-category-text |
| 111 |
decomposition-text |
| 112 |
decimal-digit-value-text |
| 113 |
digit-value-text |
| 114 |
numeric-value-text |
| 115 |
mirrored-y/n |
| 116 |
unicode-1.0-name |
| 117 |
iso-10646-comment |
| 118 |
uppercase-code-point-hex |
| 119 |
lowercase-code-point-hex |
| 120 |
titlecase-code-point-hex) |
| 121 |
(split-unicode-data-record line))) |
| 122 |
(let ((code-point (maybe-code-point code-point-hex #f))) |
| 123 |
(let ((uppercase-code-point (maybe-code-point uppercase-code-point-hex code-point)) |
| 124 |
(lowercase-code-point (maybe-code-point lowercase-code-point-hex code-point)) |
| 125 |
(titlecase-code-point (maybe-code-point titlecase-code-point-hex code-point)) |
| 126 |
(decomposition (parse-decomposition decomposition-text))) |
| 127 |
|
| 128 |
(make-code-point-info code-point |
| 129 |
name |
| 130 |
(id->general-category general-category-id) |
| 131 |
(string->number combining-class-id) |
| 132 |
(string->symbol bidirectional-category-text) |
| 133 |
(and (and (pair? decomposition) (number? (car decomposition))) |
| 134 |
decomposition) |
| 135 |
(and (and (pair? decomposition) (symbol? (car decomposition))) |
| 136 |
(cdr decomposition)) |
| 137 |
(string->number decimal-digit-value-text) |
| 138 |
(string->number digit-value-text) |
| 139 |
(string->number numeric-value-text) |
| 140 |
(string=? mirrored-y/n "Y") |
| 141 |
unicode-1.0-name |
| 142 |
iso-10646-comment |
| 143 |
uppercase-code-point |
| 144 |
lowercase-code-point |
| 145 |
titlecase-code-point))))) |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
(define (parse-decomposition d) |
| 150 |
(cond |
| 151 |
((zero? (string-length d)) |
| 152 |
#f) |
| 153 |
((char=? #\< (string-ref d 0)) |
| 154 |
(let ((after (string-index d #\space))) |
| 155 |
(cons (string->symbol (substring d 0 after)) |
| 156 |
(call-with-values |
| 157 |
(lambda () |
| 158 |
(parse-scalar-values d after)) |
| 159 |
(lambda (l i) l))))) |
| 160 |
(else |
| 161 |
(call-with-values |
| 162 |
(lambda () |
| 163 |
(parse-scalar-values d 0)) |
| 164 |
(lambda (l i) l))))) |
| 165 |
|
| 166 |
|
| 167 |
(define (code-point-info-with-code-point+name info code-point name) |
| 168 |
(make-code-point-info code-point |
| 169 |
name |
| 170 |
(code-point-info-general-category info) |
| 171 |
(code-point-info-combining-class info) |
| 172 |
(code-point-info-bidirectional-category-id info) |
| 173 |
(code-point-info-canonical-decomposition info) |
| 174 |
(code-point-info-compatibility-decomposition info) |
| 175 |
(code-point-info-decimal-digit-value info) |
| 176 |
(code-point-info-digit-value info) |
| 177 |
(code-point-info-numeric-value info) |
| 178 |
(code-point-info-mirrored? info) |
| 179 |
(code-point-info-unicode-1.0-name info) |
| 180 |
(code-point-info-iso-10646-comment info) |
| 181 |
code-point code-point code-point)) |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
(define (expanded-code-point-info-source infos) |
| 191 |
(let ((first-info #f) |
| 192 |
(code-point #f) |
| 193 |
(last-code-point #f) |
| 194 |
(name-base #f)) |
| 195 |
(lambda () |
| 196 |
(let again () |
| 197 |
(cond |
| 198 |
(first-info |
| 199 |
(if (<= code-point last-code-point) |
| 200 |
(begin |
| 201 |
(set! code-point (+ 1 code-point)) |
| 202 |
(code-point-info-with-code-point+name |
| 203 |
first-info |
| 204 |
(- code-point 1) |
| 205 |
name-base)) |
| 206 |
|
| 207 |
(begin |
| 208 |
(set! first-info #f) |
| 209 |
(again)))) |
| 210 |
((null? infos) |
| 211 |
#f) |
| 212 |
(else |
| 213 |
(let* ((info (car infos)) |
| 214 |
(name (code-point-info-name info))) |
| 215 |
(cond |
| 216 |
((and (string-prefix? "<" name) |
| 217 |
(string-suffix? ", First>" name)) |
| 218 |
(set! first-info info) |
| 219 |
(set! code-point (code-point-info-code-point info)) |
| 220 |
(set! last-code-point (code-point-info-code-point (cadr infos))) |
| 221 |
(set! name-base (string-append |
| 222 |
(substring name |
| 223 |
1 |
| 224 |
(- (string-length name) |
| 225 |
8 |
| 226 |
)) |
| 227 |
"-<code point>")) |
| 228 |
(set! infos (cddr infos)) |
| 229 |
(again)) |
| 230 |
(else |
| 231 |
(set! infos (cdr infos)) |
| 232 |
info))))))))) |
| 233 |
|
| 234 |
(define (for-each-expanded-code-point-info proc infos) |
| 235 |
(let ((source (expanded-code-point-info-source infos))) |
| 236 |
(let loop () |
| 237 |
(let ((info (source))) |
| 238 |
(if info |
| 239 |
(begin |
| 240 |
(proc info) |
| 241 |
(loop))))))) |
| 242 |
|
| 243 |
(define (read-line port) |
| 244 |
(let loop ((l '())) |
| 245 |
(let ((c (read-char port))) |
| 246 |
(if (eof-object? c) |
| 247 |
c |
| 248 |
(if (char=? c #\newline) |
| 249 |
(list->string (reverse l)) |
| 250 |
(loop (cons c l))))))) |
| 251 |
|
| 252 |
(define (parse-unicode-data filename) |
| 253 |
(call-with-input-file filename |
| 254 |
(lambda (port) |
| 255 |
(let loop ((rev-infos '())) |
| 256 |
(let ((thing (read-line port))) |
| 257 |
(if (eof-object? thing) |
| 258 |
(reverse rev-infos) |
| 259 |
(loop (cons (unicode-data-record->info thing) rev-infos)))))))) |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
(define (mapping-offsets infos accessor) |
| 265 |
(let loop ((infos infos) |
| 266 |
(offsets '())) |
| 267 |
(if (null? infos) |
| 268 |
(list->vector offsets) |
| 269 |
(let* ((info (car infos)) |
| 270 |
(code-point (code-point-info-code-point info)) |
| 271 |
(other (accessor info)) |
| 272 |
(offset (- other code-point))) |
| 273 |
(if (member offset offsets) |
| 274 |
(loop (cdr infos) offsets) |
| 275 |
(loop (cdr infos) (cons offset offsets))))))) |
| 276 |
|
| 277 |
(define (vector-index vector value) |
| 278 |
(let ((count (vector-length vector))) |
| 279 |
(let loop ((i 0)) |
| 280 |
(cond |
| 281 |
((>= i count) #f) |
| 282 |
((equal? value (vector-ref vector i)) i) |
| 283 |
(else (loop (+ 1 i))))))) |
| 284 |
|
| 285 |
(define (code-point-info->case+general-category-encoding |
| 286 |
info |
| 287 |
specialcasing? |
| 288 |
special-lowercase-table special-uppercase-table |
| 289 |
uppercase-offsets lowercase-offsets titlecase-offsets |
| 290 |
uppercase-index-width lowercase-index-width titlecase-index-width) |
| 291 |
(let ((code-point (code-point-info-code-point info))) |
| 292 |
(let ((uppercase-index (vector-index uppercase-offsets |
| 293 |
(- (code-point-info-uppercase-code-point info) |
| 294 |
code-point))) |
| 295 |
(lowercase-index (vector-index lowercase-offsets |
| 296 |
(- (code-point-info-lowercase-code-point info) |
| 297 |
code-point))) |
| 298 |
(titlecase-index (vector-index titlecase-offsets |
| 299 |
(- (code-point-info-titlecase-code-point info) |
| 300 |
code-point))) |
| 301 |
(uppercase? (or (eq? (general-category uppercase-letter) |
| 302 |
(code-point-info-general-category info)) |
| 303 |
(table-ref special-uppercase-table code-point))) |
| 304 |
(lowercase? (or (eq? (general-category lowercase-letter) |
| 305 |
(code-point-info-general-category info)) |
| 306 |
(table-ref special-lowercase-table code-point)))) |
| 307 |
|
| 308 |
(bitwise-ior |
| 309 |
(arithmetic-shift |
| 310 |
(bitwise-ior |
| 311 |
(arithmetic-shift (bitwise-ior |
| 312 |
(arithmetic-shift |
| 313 |
(bitwise-ior |
| 314 |
(arithmetic-shift |
| 315 |
(bitwise-ior (if specialcasing? 4 0) |
| 316 |
(if uppercase? 2 0) |
| 317 |
(if lowercase? 1 0)) |
| 318 |
uppercase-index-width) |
| 319 |
uppercase-index) |
| 320 |
lowercase-index-width) |
| 321 |
lowercase-index) |
| 322 |
titlecase-index-width) |
| 323 |
titlecase-index) |
| 324 |
*general-category-bits*) |
| 325 |
(general-category-index (code-point-info-general-category info)))))) |
| 326 |
|
| 327 |
(define (code-point-encoding-uppercase? encoding |
| 328 |
uppercase-index-width lowercase-index-width titlecase-index-width) |
| 329 |
(not |
| 330 |
(zero? |
| 331 |
(bitwise-and 1 |
| 332 |
(arithmetic-shift encoding |
| 333 |
(- (+ 1 |
| 334 |
uppercase-index-width |
| 335 |
lowercase-index-width |
| 336 |
titlecase-index-width |
| 337 |
*general-category-bits*))))))) |
| 338 |
|
| 339 |
(define (code-point-encoding-lowercase? encoding |
| 340 |
uppercase-index-width lowercase-index-width titlecase-index-width) |
| 341 |
(not |
| 342 |
(zero? |
| 343 |
(bitwise-and 1 |
| 344 |
(arithmetic-shift encoding |
| 345 |
(- (+ uppercase-index-width |
| 346 |
lowercase-index-width |
| 347 |
titlecase-index-width |
| 348 |
*general-category-bits*))))))) |
| 349 |
|
| 350 |
(define (lookup-by-offset-index code-point offset-index offsets) |
| 351 |
(+ code-point (vector-ref offsets offset-index))) |
| 352 |
|
| 353 |
(define (code-point-encoding-uppercase-code-point code-point encoding |
| 354 |
uppercase-offsets |
| 355 |
uppercase-index-width lowercase-index-width titlecase-index-width) |
| 356 |
(lookup-by-offset-index |
| 357 |
code-point |
| 358 |
(bitwise-and (- (arithmetic-shift 1 uppercase-index-width) 1) |
| 359 |
(arithmetic-shift encoding |
| 360 |
(- (+ lowercase-index-width titlecase-index-width *general-category-bits*)))) |
| 361 |
uppercase-offsets)) |
| 362 |
|
| 363 |
(define (code-point-encoding-lowercase-code-point code-point encoding |
| 364 |
lowercase-offsets |
| 365 |
uppercase-index-width lowercase-index-width titlecase-index-width) |
| 366 |
(lookup-by-offset-index |
| 367 |
code-point |
| 368 |
(bitwise-and (- (arithmetic-shift 1 lowercase-index-width) 1) |
| 369 |
(arithmetic-shift encoding |
| 370 |
(- (+ titlecase-index-width *general-category-bits*)))) |
| 371 |
lowercase-offsets)) |
| 372 |
|
| 373 |
(define (code-point-encoding-titlecase-code-point code-point encoding |
| 374 |
titlecase-offsets |
| 375 |
uppercase-index-width lowercase-index-width titlecase-index-width) |
| 376 |
(lookup-by-offset-index |
| 377 |
code-point |
| 378 |
(bitwise-and (- (arithmetic-shift 1 titlecase-index-width) 1) |
| 379 |
(arithmetic-shift encoding (- *general-category-bits*))) |
| 380 |
titlecase-offsets)) |
| 381 |
|
| 382 |
(define *code-point-encoding-general-category-mask* |
| 383 |
(- (arithmetic-shift 1 *general-category-bits*) 1)) |
| 384 |
|
| 385 |
(define (code-point-encoding-general-category encoding) |
| 386 |
(vector-ref general-categories |
| 387 |
(bitwise-and encoding *code-point-encoding-general-category-mask*))) |
| 388 |
|
| 389 |
|
| 390 |
(define (max-code-point infos) |
| 391 |
(let loop ((max 0) (infos infos)) |
| 392 |
(cond |
| 393 |
((null? infos) max) |
| 394 |
((> (code-point-info-code-point (car infos)) |
| 395 |
max) |
| 396 |
(loop (code-point-info-code-point (car infos)) (cdr infos))) |
| 397 |
(else (loop max (cdr infos)))))) |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
(define (make-consecutive-info-source source make-default proc) |
| 405 |
(let ((next-info #f) |
| 406 |
(last-code-point -1)) |
| 407 |
(lambda () |
| 408 |
|
| 409 |
(define (upto info) |
| 410 |
(if (< last-code-point (code-point-info-code-point info)) |
| 411 |
(begin |
| 412 |
(set! next-info info) |
| 413 |
(proc (make-default last-code-point))) |
| 414 |
(begin |
| 415 |
(set! next-info #f) |
| 416 |
|
| 417 |
(if (eq? (code-point-info-general-category info) |
| 418 |
(general-category surrogate)) |
| 419 |
(proc (make-default last-code-point)) |
| 420 |
(proc info))))) |
| 421 |
|
| 422 |
(set! last-code-point (+ 1 last-code-point)) |
| 423 |
|
| 424 |
(cond |
| 425 |
((or next-info (source)) => upto) |
| 426 |
(else #f))))) |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
(define (parse-proplist-for-upper/lowercase filename) |
| 431 |
(call-with-input-file filename |
| 432 |
(lambda (port) |
| 433 |
(let ((uppercase (make-integer-table)) (lowercase (make-integer-table))) |
| 434 |
(let loop () |
| 435 |
(let ((thing (read-line port))) |
| 436 |
(if (eof-object? thing) |
| 437 |
(values uppercase lowercase) |
| 438 |
(call-with-values |
| 439 |
(lambda () |
| 440 |
(extract-upper/lowercase thing)) |
| 441 |
(lambda (uppers lowers) |
| 442 |
(for-each (lambda (u) |
| 443 |
(table-set! uppercase u #t)) |
| 444 |
uppers) |
| 445 |
(for-each (lambda (l) |
| 446 |
(table-set! lowercase l #t)) |
| 447 |
lowers) |
| 448 |
(loop)))))))))) |
| 449 |
|
| 450 |
(define (extract-upper/lowercase line) |
| 451 |
(cond |
| 452 |
((string-prefix? "#" line) |
| 453 |
(values '() '())) |
| 454 |
((string-contains line "Other_Uppercase") |
| 455 |
(values (proplist-line-range line) |
| 456 |
'())) |
| 457 |
((string-contains line "Other_Lowercase") |
| 458 |
(values '() |
| 459 |
(proplist-line-range line))) |
| 460 |
(else |
| 461 |
(values '() '())))) |
| 462 |
|
| 463 |
(define (proplist-line-range line) |
| 464 |
(let* ((i1 (string-skip line char-set:hex-digit)) |
| 465 |
(first (string->number (substring line 0 i1) 16))) |
| 466 |
(if (char=? #\. (string-ref line i1)) |
| 467 |
(let* ((i2 (string-skip line #\. i1)) |
| 468 |
(i3 (string-skip line char-set:hex-digit i2)) |
| 469 |
(last (string->number (substring line i2 i3) 16))) |
| 470 |
(let loop ((last last) (range '())) |
| 471 |
(if (= last first) |
| 472 |
(cons last range) |
| 473 |
(loop (- last 1) (cons last range))))) |
| 474 |
(list first)))) |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
(define (parse-scalar-values s start) |
| 480 |
(let ((size (string-length s))) |
| 481 |
(let loop ((start start) (rev-values '())) |
| 482 |
(let ((i1 (string-skip s char-set:whitespace start))) |
| 483 |
(cond |
| 484 |
((not i1) |
| 485 |
(values (reverse rev-values) (+ start 1))) |
| 486 |
((char=? #\; (string-ref s i1)) |
| 487 |
(values (reverse rev-values) (+ i1 1))) |
| 488 |
(else |
| 489 |
(let* ((i2 (or (string-skip s char-set:hex-digit i1) |
| 490 |
size)) |
| 491 |
(n (string->number (substring s i1 i2) 16))) |
| 492 |
(loop i2 (cons n rev-values))))))))) |
| 493 |
|
| 494 |
(define-record-type specialcasing :specialcasing |
| 495 |
(make-specialcasing scalar-value |
| 496 |
lowercase titlecase uppercase foldcase |
| 497 |
final-sigma?) |
| 498 |
specialcasing? |
| 499 |
(scalar-value specialcasing-scalar-value) |
| 500 |
(lowercase specialcasing-lowercase) |
| 501 |
(titlecase specialcasing-titlecase) |
| 502 |
(uppercase specialcasing-uppercase) |
| 503 |
|
| 504 |
(foldcase specialcasing-foldcase set-specialcasing-foldcase!) |
| 505 |
(final-sigma? specialcasing-final-sigma?)) |
| 506 |
|
| 507 |
(define (parse-specialcasing-line line) |
| 508 |
(let* ((i1 (string-skip line char-set:hex-digit 0)) |
| 509 |
(n (string->number (substring line 0 i1) 16))) |
| 510 |
(call-with-values |
| 511 |
(lambda () (parse-scalar-values line (+ 1 i1))) |
| 512 |
(lambda (lowercase i2) |
| 513 |
(call-with-values |
| 514 |
(lambda () (parse-scalar-values line i2)) |
| 515 |
(lambda (titlecase i3) |
| 516 |
(call-with-values |
| 517 |
(lambda () (parse-scalar-values line i3)) |
| 518 |
(lambda (uppercase i4) |
| 519 |
(let ((i5 (or (string-index line #\; (+ 1 i4)) |
| 520 |
(string-index line #\# (+ 1 i4)) |
| 521 |
(string-length line)))) |
| 522 |
(let ((conditions (string-trim-both (substring line i4 i5)))) |
| 523 |
(if (or (string=? "" conditions) |
| 524 |
(string=? "Final_Sigma" conditions)) |
| 525 |
(make-specialcasing n |
| 526 |
lowercase titlecase uppercase #f |
| 527 |
(string=? conditions "Final_Sigma")) |
| 528 |
#f))))))))))) |
| 529 |
|
| 530 |
(define (parse-specialcasing filename) |
| 531 |
(call-with-input-file filename |
| 532 |
(lambda (port) |
| 533 |
(let loop ((specialcasings '())) |
| 534 |
(let ((thing (read-line port))) |
| 535 |
(if (eof-object? thing) |
| 536 |
specialcasings |
| 537 |
(cond |
| 538 |
((and (not (string=? "" thing)) |
| 539 |
(not (char=? #\# (string-ref thing 0))) |
| 540 |
(parse-specialcasing-line thing)) |
| 541 |
=> (lambda (sc) |
| 542 |
(loop (cons sc specialcasings)))) |
| 543 |
(else (loop specialcasings))))))))) |
| 544 |
|
| 545 |
|
| 546 |
(define (parse-casefolding-line line) |
| 547 |
(let* ((i1 (string-skip line char-set:hex-digit 0)) |
| 548 |
(n (string->number (substring line 0 i1) 16)) |
| 549 |
(i2 (string-skip line char-set:whitespace (+ 1 i1))) |
| 550 |
(status (string-ref line i2))) |
| 551 |
(call-with-values |
| 552 |
(lambda () |
| 553 |
(parse-scalar-values line (+ 2 i2))) |
| 554 |
(lambda (scalar-values i) |
| 555 |
(cond |
| 556 |
((or (char=? status #\C) |
| 557 |
(char=? status #\F)) |
| 558 |
(cons n (cons status scalar-values))) |
| 559 |
((> (length scalar-values) 1) |
| 560 |
(error "multi-character common case-folding mapping")) |
| 561 |
(else #f)))))) |
| 562 |
|
| 563 |
(define (parse-casefolding filename) |
| 564 |
(call-with-input-file filename |
| 565 |
(lambda (port) |
| 566 |
(let loop ((casefoldings '())) |
| 567 |
(let ((thing (read-line port))) |
| 568 |
(cond |
| 569 |
((eof-object? thing) casefoldings) |
| 570 |
((and (not (string=? "" thing)) |
| 571 |
(not (char=? #\# (string-ref thing 0))) |
| 572 |
(parse-casefolding-line thing)) |
| 573 |
=> (lambda (folding) |
| 574 |
(loop (cons folding casefoldings)))) |
| 575 |
(else (loop casefoldings)))))))) |
| 576 |
|
| 577 |
(define (merge-specialcasings+casefoldings! specialcasings casefoldings) |
| 578 |
(for-each |
| 579 |
(lambda (casefolding) |
| 580 |
(let ((sv (car casefolding)) |
| 581 |
(status (cadr casefolding)) |
| 582 |
(folding (cddr casefolding))) |
| 583 |
(cond |
| 584 |
((find (lambda (specialcasing) |
| 585 |
(= (specialcasing-scalar-value specialcasing) sv)) |
| 586 |
specialcasings) |
| 587 |
=> (lambda (specialcasing) |
| 588 |
(set-specialcasing-foldcase! specialcasing folding))) |
| 589 |
((char=? status #\F) |
| 590 |
(let ((sv-list (list sv))) |
| 591 |
(set! specialcasings |
| 592 |
(cons |
| 593 |
(make-specialcasing sv |
| 594 |
sv-list sv-list sv-list |
| 595 |
folding |
| 596 |
#f) |
| 597 |
specialcasings))))))) |
| 598 |
casefoldings) |
| 599 |
specialcasings) |
| 600 |
|
| 601 |
(define (parse-specialcasing+casefolding specialcasing-filename casefolding-filename) |
| 602 |
(let ((specialcasings (parse-specialcasing specialcasing-filename)) |
| 603 |
(casefoldings (parse-casefolding casefolding-filename))) |
| 604 |
(merge-specialcasings+casefoldings! specialcasings casefoldings))) |
| 605 |
|
| 606 |
(define (list-prefix? l1 l2) |
| 607 |
(let loop ((l1 l1) (l2 l2)) |
| 608 |
(cond |
| 609 |
((null? l1) #t) |
| 610 |
((null? l2) #f) |
| 611 |
((equal? (car l1) (car l2)) |
| 612 |
(loop (cdr l1) (cdr l2))) |
| 613 |
(else #f)))) |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
(define (specialcasing-encoding specialcasings) |
| 620 |
(let ((casings '())) |
| 621 |
|
| 622 |
(define (add-casing! l) |
| 623 |
(let loop ((rest casings) |
| 624 |
(index 0)) |
| 625 |
(cond |
| 626 |
((null? rest) |
| 627 |
(set! casings (append casings l)) |
| 628 |
index) |
| 629 |
((list-prefix? l rest) |
| 630 |
index) |
| 631 |
(else |
| 632 |
(loop (cdr rest) (+ 1 index)))))) |
| 633 |
|
| 634 |
(define (transform-specialcasing s) |
| 635 |
(let ((lowercase (cons (add-casing! (specialcasing-lowercase s)) |
| 636 |
(length (specialcasing-lowercase s)))) |
| 637 |
(titlecase (cons (add-casing! (specialcasing-titlecase s)) |
| 638 |
(length (specialcasing-titlecase s)))) |
| 639 |
(uppercase (cons (add-casing! (specialcasing-uppercase s)) |
| 640 |
(length (specialcasing-uppercase s)))) |
| 641 |
(foldcase (cons (add-casing! (specialcasing-foldcase s)) |
| 642 |
(length (specialcasing-foldcase s))))) |
| 643 |
(make-specialcasing (specialcasing-scalar-value s) |
| 644 |
lowercase titlecase uppercase foldcase |
| 645 |
(specialcasing-final-sigma? s)))) |
| 646 |
|
| 647 |
(let ((transformed |
| 648 |
(map transform-specialcasing specialcasings))) |
| 649 |
(values transformed |
| 650 |
casings)))) |
| 651 |
|
| 652 |
(define (specialcasing-encoding-ref casings offset size) |
| 653 |
(let loop ((i 0) (r '())) |
| 654 |
(if (>= i size) |
| 655 |
(reverse r) |
| 656 |
(loop (+ 1 i) |
| 657 |
(cons (vector-ref casings (+ offset i)) |
| 658 |
r))))) |
| 659 |
|
| 660 |
|
| 661 |
(define (check-specialcasing-encodings specialcasings) |
| 662 |
(call-with-values |
| 663 |
(lambda () (specialcasing-encoding specialcasings)) |
| 664 |
(lambda (encodings casings) |
| 665 |
(let ((casings (list->vector casings))) |
| 666 |
(for-each |
| 667 |
(lambda (specialcasing encoding) |
| 668 |
|
| 669 |
(define (check select) |
| 670 |
(let ((pair (select encoding)) |
| 671 |
(reference (select specialcasing))) |
| 672 |
(if (not |
| 673 |
(equal? reference |
| 674 |
(specialcasing-encoding-ref casings |
| 675 |
(car pair) (cdr pair)))) |
| 676 |
(error "encoding failure" encoding |
| 677 |
reference (specialcasing-encoding-ref casings |
| 678 |
(car pair) (cdr pair)))))) |
| 679 |
|
| 680 |
(check specialcasing-lowercase) |
| 681 |
(check specialcasing-uppercase) |
| 682 |
(check specialcasing-titlecase) |
| 683 |
(check specialcasing-foldcase)) |
| 684 |
specialcasings encodings))))) |
| 685 |
|
| 686 |
(define (specialcasings->table specialcasings) |
| 687 |
(let ((table (make-integer-table))) |
| 688 |
(for-each (lambda (s) |
| 689 |
(table-set! table (specialcasing-scalar-value s) |
| 690 |
s)) |
| 691 |
specialcasings) |
| 692 |
table)) |
| 693 |
|
| 694 |
(define (make-scalar-value-case+general-category-encoding-tables |
| 695 |
infos |
| 696 |
special-lowercase-table special-uppercase-table |
| 697 |
specialcasings) |
| 698 |
|
| 699 |
(let ((uppercase-offsets (mapping-offsets infos code-point-info-uppercase-code-point)) |
| 700 |
(lowercase-offsets (mapping-offsets infos code-point-info-lowercase-code-point)) |
| 701 |
(titlecase-offsets (mapping-offsets infos code-point-info-titlecase-code-point))) |
| 702 |
|
| 703 |
(let ((uppercase-index-width (bits-necessary (vector-length uppercase-offsets))) |
| 704 |
(lowercase-index-width (bits-necessary (vector-length lowercase-offsets))) |
| 705 |
(titlecase-index-width (bits-necessary (vector-length titlecase-offsets))) |
| 706 |
|
| 707 |
(specialcasings-table (specialcasings->table specialcasings)) |
| 708 |
|
| 709 |
(block-size (expt 2 *block-bits*))) |
| 710 |
|
| 711 |
(call-with-values |
| 712 |
(lambda () |
| 713 |
(compute-compact-table |
| 714 |
(make-consecutive-info-source |
| 715 |
(expanded-code-point-info-source infos) |
| 716 |
(lambda (code-point) |
| 717 |
(make-code-point-info code-point |
| 718 |
"<unassigned>" |
| 719 |
(general-category unassigned) |
| 720 |
#f #f #f #f #f #f #f #f #f #f |
| 721 |
code-point code-point code-point)) |
| 722 |
(lambda (info) |
| 723 |
(code-point-info->case+general-category-encoding |
| 724 |
info |
| 725 |
(table-ref specialcasings-table |
| 726 |
(code-point-info-code-point info)) |
| 727 |
special-lowercase-table special-uppercase-table |
| 728 |
uppercase-offsets lowercase-offsets titlecase-offsets |
| 729 |
uppercase-index-width lowercase-index-width titlecase-index-width))) |
| 730 |
block-size)) |
| 731 |
(lambda (indices encodings) |
| 732 |
(values indices encodings |
| 733 |
uppercase-offsets lowercase-offsets titlecase-offsets)))))) |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
(define (write-vector-code/rll name vector port) |
| 738 |
(write `(define ,name (make-vector ,(vector-length vector))) |
| 739 |
port) |
| 740 |
(newline port) |
| 741 |
(let loop ((values (vector->list vector)) |
| 742 |
(index 0)) |
| 743 |
(cond |
| 744 |
((null? values)) |
| 745 |
((or (null? (cdr values)) |
| 746 |
(not (equal? (car values) (cadr values)))) |
| 747 |
(write `(vector-set! ,name ,index ,(car values)) |
| 748 |
port) |
| 749 |
(newline port) |
| 750 |
(loop (cdr values) (+ 1 index))) |
| 751 |
(else |
| 752 |
(let ((value (car values))) |
| 753 |
(let inner-loop ((values values) |
| 754 |
(last-index index)) |
| 755 |
(cond |
| 756 |
((or (null? values) |
| 757 |
(not (equal? (car values) value))) |
| 758 |
(write |
| 759 |
`(do ((i ,index (+ 1 i))) |
| 760 |
((>= i ,last-index)) |
| 761 |
(vector-set! ,name i ,value)) |
| 762 |
port) |
| 763 |
(newline port) |
| 764 |
(loop values last-index)) |
| 765 |
(else |
| 766 |
(inner-loop (cdr values) (+ 1 last-index)))))))))) |
| 767 |
|
| 768 |
(define (create-unicode-tables unicode-data-filename |
| 769 |
proplist-filename |
| 770 |
specialcasing-filename |
| 771 |
casefolding-filename |
| 772 |
composition-exclusions-filename |
| 773 |
category-output-file |
| 774 |
syntax-info-output-file |
| 775 |
normalization-output-file |
| 776 |
srfi-14-base-output-file) |
| 777 |
(let ((infos (parse-unicode-data unicode-data-filename)) |
| 778 |
(specialcasings (parse-specialcasing+casefolding specialcasing-filename |
| 779 |
casefolding-filename))) |
| 780 |
(call-with-values |
| 781 |
(lambda () |
| 782 |
(parse-proplist-for-upper/lowercase proplist-filename)) |
| 783 |
(lambda (special-uppercase-table special-lowercase-table) |
| 784 |
(call-with-output-file category-output-file |
| 785 |
(lambda (port) |
| 786 |
(display "; Automatically generated by WRITE-UNICODE-CATEGORY-TABLES; do not edit." |
| 787 |
port) |
| 788 |
(newline port) |
| 789 |
(newline port) |
| 790 |
(write-unicode-category-tables infos |
| 791 |
special-uppercase-table special-lowercase-table |
| 792 |
specialcasings |
| 793 |
port) |
| 794 |
(write-specialcasings-tables specialcasings port))) |
| 795 |
|
| 796 |
(call-with-output-file syntax-info-output-file |
| 797 |
(lambda (port) |
| 798 |
(display "; Automatically generated by WRITE-UNICODE-CATEGORY-TABLES; do not edit." |
| 799 |
port) |
| 800 |
(newline port) |
| 801 |
(newline port) |
| 802 |
(write-syntax-info infos port) |
| 803 |
(newline port))) |
| 804 |
|
| 805 |
(write-srfi-14-base-char-sets infos srfi-14-base-output-file) |
| 806 |
(call-with-output-file normalization-output-file |
| 807 |
(lambda (port) |
| 808 |
(display "; Automatically generated by WRITE-UNICODE-CATEGORY-TABLES; do not edit." |
| 809 |
port) |
| 810 |
(newline port) |
| 811 |
(newline port) |
| 812 |
(write-normalization-tables |
| 813 |
infos |
| 814 |
(parse-composition-exclusions composition-exclusions-filename) |
| 815 |
port))))))) |
| 816 |
|
| 817 |
(define *block-bits* 8) |
| 818 |
|
| 819 |
(define (write-unicode-category-tables infos |
| 820 |
special-uppercase-table special-lowercase-table |
| 821 |
specialcasings |
| 822 |
port) |
| 823 |
(call-with-values |
| 824 |
(lambda () |
| 825 |
(make-scalar-value-case+general-category-encoding-tables |
| 826 |
infos |
| 827 |
special-lowercase-table special-uppercase-table |
| 828 |
specialcasings)) |
| 829 |
(lambda (indices |
| 830 |
encodings |
| 831 |
uppercase-offsets lowercase-offsets titlecase-offsets) |
| 832 |
|
| 833 |
(write `(define *encoding-table-block-bits* ,*block-bits*) |
| 834 |
port) |
| 835 |
(newline port) |
| 836 |
(newline port) |
| 837 |
|
| 838 |
(write `(define *uppercase-index-width* |
| 839 |
,(bits-necessary (vector-length uppercase-offsets))) |
| 840 |
port) |
| 841 |
(newline port) |
| 842 |
(write `(define *lowercase-index-width* |
| 843 |
,(bits-necessary (vector-length lowercase-offsets))) |
| 844 |
port) |
| 845 |
(newline port) |
| 846 |
(write `(define *titlecase-index-width* |
| 847 |
,(bits-necessary (vector-length titlecase-offsets))) |
| 848 |
port) |
| 849 |
(newline port) |
| 850 |
(newline port) |
| 851 |
|
| 852 |
(write `(define *scalar-value-info-indices* ',indices) |
| 853 |
port) |
| 854 |
(newline port) |
| 855 |
(write `(define *scalar-value-info-encodings* ',encodings) |
| 856 |
port) |
| 857 |
(newline port) |
| 858 |
(newline port) |
| 859 |
|
| 860 |
(write `(define *uppercase-offsets* ',uppercase-offsets) |
| 861 |
port) |
| 862 |
(newline port) |
| 863 |
(write `(define *lowercase-offsets* ',lowercase-offsets) |
| 864 |
port) |
| 865 |
(newline port) |
| 866 |
(write `(define *titlecase-offsets* ',titlecase-offsets) |
| 867 |
port) |
| 868 |
(newline port) |
| 869 |
(newline port)))) |
| 870 |
|
| 871 |
(define (write-specialcasings-tables specialcasings port) |
| 872 |
(call-with-values |
| 873 |
(lambda () (specialcasing-encoding specialcasings)) |
| 874 |
(lambda (encodings casings) |
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
(write |
| 879 |
'(define-record-type specialcasing :specialcasing |
| 880 |
(make-specialcasing scalar-value |
| 881 |
lowercase-start lowercase-length |
| 882 |
titlecase-start titlecase-length |
| 883 |
uppercase-start uppercase-length |
| 884 |
foldcase-start foldcase-length |
| 885 |
final-sigma?) |
| 886 |
specialcasing? |
| 887 |
(scalar-value specialcasing-scalar-value) |
| 888 |
(lowercase-start specialcasing-lowercase-start) |
| 889 |
(lowercase-length specialcasing-lowercase-length) |
| 890 |
(titlecase-start specialcasing-titlecase-start) |
| 891 |
(titlecase-length specialcasing-titlecase-length) |
| 892 |
(uppercase-start specialcasing-uppercase-start) |
| 893 |
(uppercase-length specialcasing-uppercase-length) |
| 894 |
(foldcase-start specialcasing-foldcase-start) |
| 895 |
(foldcase-length specialcasing-foldcase-length) |
| 896 |
(final-sigma? specialcasing-final-sigma?)) |
| 897 |
port) |
| 898 |
(newline port) |
| 899 |
(newline port) |
| 900 |
|
| 901 |
(write `(define *specialcasing-table* (make-integer-table)) port) |
| 902 |
(newline port) |
| 903 |
(newline port) |
| 904 |
|
| 905 |
(for-each |
| 906 |
(lambda (c) |
| 907 |
(write |
| 908 |
`(table-set! *specialcasing-table* |
| 909 |
,(specialcasing-scalar-value c) |
| 910 |
(make-specialcasing |
| 911 |
,(specialcasing-scalar-value c) |
| 912 |
,(car (specialcasing-lowercase c)) |
| 913 |
,(cdr (specialcasing-lowercase c)) |
| 914 |
,(car (specialcasing-titlecase c)) |
| 915 |
,(cdr (specialcasing-titlecase c)) |
| 916 |
,(car (specialcasing-uppercase c)) |
| 917 |
,(cdr (specialcasing-uppercase c)) |
| 918 |
,(car (specialcasing-foldcase c)) |
| 919 |
,(cdr (specialcasing-foldcase c)) |
| 920 |
,(specialcasing-final-sigma? c))) |
| 921 |
port) |
| 922 |
(newline port)) |
| 923 |
encodings) |
| 924 |
|
| 925 |
(newline port) |
| 926 |
|
| 927 |
(write `(define *specialcasings* (list->string (map scalar-value->char ',casings))) port) |
| 928 |
(newline port) |
| 929 |
(newline port)))) |
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
(define (write-syntax-info infos port) |
| 934 |
(write `(define *non-symbol-constituents-above-127* |
| 935 |
',(list->vector (non-symbol-constituents-above-127 infos))) |
| 936 |
port) |
| 937 |
(newline port) |
| 938 |
(newline port) |
| 939 |
(write `(define *whitespaces* |
| 940 |
',(list->vector (whitespaces infos))) |
| 941 |
port) |
| 942 |
(newline port)) |
| 943 |
|
| 944 |
(define *symbol-constituent-general-categories* |
| 945 |
(list (general-category uppercase-letter) |
| 946 |
(general-category lowercase-letter) |
| 947 |
(general-category titlecase-letter) |
| 948 |
(general-category modified-letter) |
| 949 |
(general-category other-letter) |
| 950 |
(general-category non-spacing-mark) |
| 951 |
(general-category combining-spacing-mark) |
| 952 |
(general-category enclosing-mark) |
| 953 |
(general-category decimal-digit-number) |
| 954 |
(general-category letter-number) |
| 955 |
(general-category other-number) |
| 956 |
(general-category dash-punctuation) |
| 957 |
(general-category connector-punctuation) |
| 958 |
(general-category other-punctuation) |
| 959 |
(general-category currency-symbol) |
| 960 |
(general-category mathematical-symbol) |
| 961 |
(general-category modifier-symbol) |
| 962 |
(general-category other-symbol) |
| 963 |
(general-category private-use-character))) |
| 964 |
|
| 965 |
(define (symbol-constituent-above-127? info) |
| 966 |
(memq (code-point-info-general-category info) |
| 967 |
*symbol-constituent-general-categories*)) |
| 968 |
|
| 969 |
(define (non-symbol-constituents-above-127 infos) |
| 970 |
(let ((reverse-non-constituents '())) |
| 971 |
(for-each-expanded-code-point-info |
| 972 |
(lambda (info) |
| 973 |
(let ((cp (code-point-info-code-point info))) |
| 974 |
(if (and (> cp 127) |
| 975 |
(not (eq? (general-category surrogate) |
| 976 |
(code-point-info-general-category info))) |
| 977 |
(not (symbol-constituent-above-127? info))) |
| 978 |
(set! reverse-non-constituents |
| 979 |
(cons cp reverse-non-constituents))))) |
| 980 |
infos) |
| 981 |
(reverse reverse-non-constituents))) |
| 982 |
|
| 983 |
(define (whitespaces infos) |
| 984 |
(let ((reverse-whitespaces '())) |
| 985 |
(for-each-expanded-code-point-info |
| 986 |
(lambda (info) |
| 987 |
(if (eq? (general-category-primary-category |
| 988 |
(code-point-info-general-category info)) |
| 989 |
(primary-category separator)) |
| 990 |
(set! reverse-whitespaces |
| 991 |
(cons (code-point-info-code-point info) |
| 992 |
reverse-whitespaces)))) |
| 993 |
infos) |
| 994 |
(sort-list (append '(#x009 #x00a #x00b #x00c #x00d #x085) |
| 995 |
reverse-whitespaces) |
| 996 |
<))) |
| 997 |
|
| 998 |
(define (write-srfi-14-base-char-sets infos output-file) |
| 999 |
(call-with-output-file output-file |
| 1000 |
(lambda (port) |
| 1001 |
|
| 1002 |
(display "; Automatically generated by WRITE-SRFI-14-BASE-CHAR-SETS; do not edit." |
| 1003 |
port) |
| 1004 |
(newline port) |
| 1005 |
(newline port) |
| 1006 |
|
| 1007 |
(let-syntax |
| 1008 |
((general-category-predicate |
| 1009 |
(syntax-rules () |
| 1010 |
((general-category-predicate ?name) |
| 1011 |
(lambda (info) |
| 1012 |
(eq? (code-point-info-general-category info) |
| 1013 |
(general-category ?name)))))) |
| 1014 |
(primary-category-predicate |
| 1015 |
(syntax-rules () |
| 1016 |
((primary-category-predicate ?name) |
| 1017 |
(lambda (info) |
| 1018 |
(eq? (general-category-primary-category |
| 1019 |
(code-point-info-general-category info)) |
| 1020 |
(primary-category ?name))))))) |
| 1021 |
|
| 1022 |
(write-srfi-14-base-char-set-definition |
| 1023 |
'char-set:lower-case |
| 1024 |
srfi-14-lower-case? |
| 1025 |
infos port) |
| 1026 |
(write-srfi-14-base-char-set-definition |
| 1027 |
'char-set:upper-case |
| 1028 |
srfi-14-upper-case? |
| 1029 |
infos port) |
| 1030 |
(write-srfi-14-base-char-set-definition |
| 1031 |
'char-set:title-case |
| 1032 |
(general-category-predicate titlecase-letter) |
| 1033 |
infos port) |
| 1034 |
(write-srfi-14-base-char-set-definition |
| 1035 |
'char-set:letter |
| 1036 |
(primary-category-predicate letter) |
| 1037 |
infos port) |
| 1038 |
(write-srfi-14-base-char-set-definition |
| 1039 |
'char-set:digit |
| 1040 |
(general-category-predicate decimal-digit-number) |
| 1041 |
infos port) |
| 1042 |
(write-srfi-14-base-char-set-definition |
| 1043 |
'char-set:mark |
| 1044 |
(primary-category-predicate mark) |
| 1045 |
infos port) |
| 1046 |
(write-srfi-14-base-char-set-definition |
| 1047 |
'char-set:separator |
| 1048 |
(primary-category-predicate separator) |
| 1049 |
infos port) |
| 1050 |
(write-srfi-14-base-char-set-definition |
| 1051 |
'char-set:punctuation |
| 1052 |
(primary-category-predicate punctuation) |
| 1053 |
infos port) |
| 1054 |
(write-srfi-14-base-char-set-definition |
| 1055 |
'char-set:symbol |
| 1056 |
(primary-category-predicate symbol) |
| 1057 |
infos port) |
| 1058 |
(write-srfi-14-base-char-set-definition |
| 1059 |
'char-set:space-separator |
| 1060 |
(general-category-predicate space-separator) |
| 1061 |
infos port))))) |
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
(define (srfi-14-lower-case? info) |
| 1066 |
(let ((cp (code-point-info-code-point info))) |
| 1067 |
(and (not (and (>= cp #x2000) |
| 1068 |
(<= cp #x2fff))) |
| 1069 |
(= cp (code-point-info-lowercase-code-point info)) |
| 1070 |
(or (not (= cp (code-point-info-uppercase-code-point info))) |
| 1071 |
(string-contains (code-point-info-name info) |
| 1072 |
"SMALL LETTER") |
| 1073 |
(string-contains (code-point-info-name info) |
| 1074 |
"SMALL LIGATURE"))))) |
| 1075 |
|
| 1076 |
(define (srfi-14-upper-case? info) |
| 1077 |
(let ((cp (code-point-info-code-point info))) |
| 1078 |
(and (not (and (>= cp #x2000) |
| 1079 |
(<= cp #x2fff))) |
| 1080 |
(= cp (code-point-info-uppercase-code-point info)) |
| 1081 |
(or (not (= cp (code-point-info-lowercase-code-point info))) |
| 1082 |
(string-contains (code-point-info-name info) |
| 1083 |
"CAPITAL LETTER") |
| 1084 |
(string-contains (code-point-info-name info) |
| 1085 |
"CAPITAL LIGATURE"))))) |
| 1086 |
|
| 1087 |
(define (write-srfi-14-base-char-set-definition name pred infos port) |
| 1088 |
(write (srfi-14-base-char-set-definition name pred infos) |
| 1089 |
port) |
| 1090 |
(newline port)) |
| 1091 |
|
| 1092 |
(define (ranges->range-vector ranges) |
| 1093 |
(let* ((range-count (length ranges)) |
| 1094 |
(range-vector (make-vector (* 2 (length ranges))))) |
| 1095 |
|
| 1096 |
(let loop ((i 0) (ranges ranges)) |
| 1097 |
(if (< i range-count) |
| 1098 |
(begin |
| 1099 |
(vector-set! range-vector (* 2 i) (caar ranges)) |
| 1100 |
(vector-set! range-vector (+ 1 (* 2 i)) (cdar ranges)) |
| 1101 |
(loop (+ 1 i) (cdr ranges))))) |
| 1102 |
range-vector)) |
| 1103 |
|
| 1104 |
(define (srfi-14-base-char-set-definition name pred infos) |
| 1105 |
(let ((accumulator (make-ranges-accumulator pred))) |
| 1106 |
(for-each-expanded-code-point-info accumulator infos) |
| 1107 |
`(define ,name |
| 1108 |
(range-vector->char-set |
| 1109 |
',(ranges->range-vector (accumulator 'ranges)))))) |
| 1110 |
|
| 1111 |
(define (make-ranges-accumulator pred) |
| 1112 |
(let ((rev-ranges '()) |
| 1113 |
(current-left #f) |
| 1114 |
(current-right #f)) |
| 1115 |
|
| 1116 |
(lambda (message) |
| 1117 |
(cond |
| 1118 |
((not (code-point-info? message)) |
| 1119 |
(if current-left |
| 1120 |
(reverse (cons (cons current-left current-right) |
| 1121 |
rev-ranges)) |
| 1122 |
(reverse rev-ranges))) |
| 1123 |
((pred message) |
| 1124 |
(let ((scalar-value (code-point-info-code-point message))) |
| 1125 |
(cond |
| 1126 |
((not current-left) |
| 1127 |
(set! current-left scalar-value) |
| 1128 |
(set! current-right (+ 1 scalar-value))) |
| 1129 |
((= scalar-value current-right) |
| 1130 |
(set! current-right (+ 1 current-right))) |
| 1131 |
(else |
| 1132 |
(set! rev-ranges |
| 1133 |
(cons (cons current-left current-right) |
| 1134 |
rev-ranges)) |
| 1135 |
(set! current-left scalar-value) |
| 1136 |
(set! current-right (+ 1 scalar-value)))))))))) |
| 1137 |
|
| 1138 |
|
| 1139 |
(define (write-normalization-tables infos excluded port) |
| 1140 |
(call-with-values |
| 1141 |
(lambda () |
| 1142 |
(make-normalization-encoding-tables infos)) |
| 1143 |
(lambda (indices encodings) |
| 1144 |
(write `(define *normalization-info-block-bits* ,*block-bits*) |
| 1145 |
port) |
| 1146 |
(newline port) |
| 1147 |
(write `(define *normalization-info-indices* ',indices) |
| 1148 |
port) |
| 1149 |
(newline port) |
| 1150 |
(write `(define *normalization-info-encodings* ',encodings) |
| 1151 |
port) |
| 1152 |
(newline port))) |
| 1153 |
|
| 1154 |
(newline port) |
| 1155 |
|
| 1156 |
(let ((canonical-pairs (canonical-decomposition-pairs infos))) |
| 1157 |
(write `(define *canonical-decomposition-scalar-values* |
| 1158 |
',(list->vector (map car canonical-pairs))) |
| 1159 |
port) |
| 1160 |
(newline port) |
| 1161 |
(write `(define *canonical-decompositions* |
| 1162 |
',(list->vector (map cdr canonical-pairs))) |
| 1163 |
port) |
| 1164 |
(newline port)) |
| 1165 |
|
| 1166 |
(newline port) |
| 1167 |
|
| 1168 |
(call-with-values |
| 1169 |
(lambda () |
| 1170 |
(compatibility-decomposition-tables infos)) |
| 1171 |
(lambda (decompositions scalar-values indices) |
| 1172 |
(write `(define *compatibility-decompositions* ',decompositions) |
| 1173 |
port) |
| 1174 |
(newline port) |
| 1175 |
(write `(define *compatibility-scalar-values* ',scalar-values) |
| 1176 |
port) |
| 1177 |
(newline port) |
| 1178 |
(write `(define *compatibility-indices* ',indices) |
| 1179 |
port) |
| 1180 |
(newline port))) |
| 1181 |
|
| 1182 |
(newline port) |
| 1183 |
|
| 1184 |
(let ((composition-pairs (composition-pairs infos excluded))) |
| 1185 |
(write `(define *composition-scalar-values* |
| 1186 |
',(list->vector (map car composition-pairs))) |
| 1187 |
port) |
| 1188 |
(newline port) |
| 1189 |
(write `(define *composition-encodings* |
| 1190 |
',(list->vector (map cdr composition-pairs))) |
| 1191 |
port) |
| 1192 |
(newline port))) |
| 1193 |
|
| 1194 |
(define (parse-composition-exclusions filename) |
| 1195 |
(call-with-input-file filename |
| 1196 |
(lambda (port) |
| 1197 |
(let loop ((exclusions '())) |
| 1198 |
(let ((thing (read-line port))) |
| 1199 |
(cond |
| 1200 |
((eof-object? thing) exclusions) |
| 1201 |
((and (not (string=? "" thing)) |
| 1202 |
(not (char=? #\# (string-ref thing 0)))) |
| 1203 |
(let ((end (or (string-skip thing char-set:hex-digit) |
| 1204 |
(string-length thing)))) |
| 1205 |
(loop |
| 1206 |
(cons (string->number (substring thing 0 end) 16) |
| 1207 |
exclusions)))) |
| 1208 |
(else (loop exclusions)))))))) |
| 1209 |
|
| 1210 |
(define (make-normalization-encoding-tables infos) |
| 1211 |
(compute-compact-table |
| 1212 |
(make-consecutive-info-source |
| 1213 |
(expanded-code-point-info-source infos) |
| 1214 |
(lambda (code-point) |
| 1215 |
(make-code-point-info code-point |
| 1216 |
"<unassigned>" |
| 1217 |
(general-category unassigned) |
| 1218 |
0 #f #f #f #f #f #f #f #f #f |
| 1219 |
code-point code-point code-point)) |
| 1220 |
(lambda (info) |
| 1221 |
(bitwise-ior (code-point-info-combining-class info) |
| 1222 |
(if (code-point-info-canonical-decomposition info) |
| 1223 |
#x100 |
| 1224 |
0) |
| 1225 |
(if (code-point-info-compatibility-decomposition info) |
| 1226 |
#x200 |
| 1227 |
0)))) |
| 1228 |
(expt 2 *block-bits*))) |
| 1229 |
|
| 1230 |
(define (encode-canonical-decomposition l) |
| 1231 |
(cond |
| 1232 |
((null? (cdr l)) |
| 1233 |
(if (> (car l) #xffff) |
| 1234 |
l |
| 1235 |
(car l))) |
| 1236 |
(else |
| 1237 |
(let ((a (car l)) |
| 1238 |
(b (cadr l))) |
| 1239 |
(if (or (> a #xffff) |
| 1240 |
(> b #xffff)) |
| 1241 |
(cons a b) |
| 1242 |
(bitwise-ior (arithmetic-shift b 16) a)))))) |
| 1243 |
|
| 1244 |
|
| 1245 |
(define (canonical-decomposition-pairs infos) |
| 1246 |
(let ((pairs '())) |
| 1247 |
(for-each-expanded-code-point-info |
| 1248 |
(lambda (info) |
| 1249 |
(cond |
| 1250 |
((code-point-info-canonical-decomposition info) |
| 1251 |
=> (lambda (d) |
| 1252 |
(set! pairs |
| 1253 |
(cons |
| 1254 |
(cons (code-point-info-code-point info) |
| 1255 |
(encode-canonical-decomposition d)) |
| 1256 |
pairs)))))) |
| 1257 |
infos) |
| 1258 |
(reverse pairs))) |
| 1259 |
|
| 1260 |
(define (compatibility-decomposition-tables infos) |
| 1261 |
(let ((reverse-decomps '()) |
| 1262 |
(decomp-index 0) |
| 1263 |
(rev-infos '())) |
| 1264 |
(for-each-expanded-code-point-info |
| 1265 |
(lambda (info) |
| 1266 |
(cond |
| 1267 |
((code-point-info-compatibility-decomposition info) |
| 1268 |
=> (lambda (d) |
| 1269 |
(let ((size (length d))) |
| 1270 |
(set! reverse-decomps |
| 1271 |
(append (reverse d) reverse-decomps)) |
| 1272 |
(set! rev-infos |
| 1273 |
(cons (cons (code-point-info-code-point info) |
| 1274 |
decomp-index) |
| 1275 |
rev-infos)) |
| 1276 |
(set! decomp-index (+ decomp-index size))))))) |
| 1277 |
infos) |
| 1278 |
(let ((decomps (list->vector (reverse reverse-decomps)))) |
| 1279 |
(values decomps |
| 1280 |
(list->vector (map car (reverse rev-infos))) |
| 1281 |
(list->vector |
| 1282 |
(map cdr (reverse (cons (cons #f (vector-length decomps)) rev-infos)))))))) |
| 1283 |
|
| 1284 |
(define (composition-pairs infos excluded) |
| 1285 |
(let ((pairs '())) |
| 1286 |
(for-each-expanded-code-point-info |
| 1287 |
(lambda (info) |
| 1288 |
(cond |
| 1289 |
((code-point-info-canonical-decomposition info) |
| 1290 |
=> (lambda (d) |
| 1291 |
(if (and (pair? (cdr d)) |
| 1292 |
(not (member (code-point-info-code-point info) excluded)) |
| 1293 |
(code-point-info-combining-class |
| 1294 |
(find-code-point-info (car d) infos))) |
| 1295 |
(set! pairs |
| 1296 |
(cons (cons (code-point-info-code-point info) |
| 1297 |
(encode-composition d)) |
| 1298 |
pairs))))))) |
| 1299 |
infos) |
| 1300 |
|
| 1301 |
(sort-list pairs |
| 1302 |
(lambda (p1 p2) |
| 1303 |
(< (cdr p1) (cdr p2)))))) |
| 1304 |
|
| 1305 |
(define (encode-composition l) |
| 1306 |
(if (or (> (car l) #xffff) |
| 1307 |
(> (cadr l) #xffff)) |
| 1308 |
(error "non-BMP composition")) |
| 1309 |
(bitwise-ior (arithmetic-shift (cadr l) 16) |
| 1310 |
(car l))) |
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
(define (test-code-point-case+general-category-encoding-tables |
| 1315 |
infos special-uppercase-table special-lowercase-table |
| 1316 |
specialcasings |
| 1317 |
indices encodings |
| 1318 |
uppercase-offsets lowercase-offsets titlecase-offsets) |
| 1319 |
|
| 1320 |
(let ((lower-mask (- (arithmetic-shift 1 *block-bits*) 1)) |
| 1321 |
(uppercase-index-width (bits-necessary (vector-length uppercase-offsets))) |
| 1322 |
(lowercase-index-width (bits-necessary (vector-length lowercase-offsets))) |
| 1323 |
(titlecase-index-width (bits-necessary (vector-length titlecase-offsets)))) |
| 1324 |
|
| 1325 |
(for-each-expanded-code-point-info |
| 1326 |
(lambda (info) |
| 1327 |
(let* ((code-point (code-point-info-code-point info)) |
| 1328 |
(base-index (vector-ref indices |
| 1329 |
(arithmetic-shift code-point (- *block-bits*)))) |
| 1330 |
(index (+ base-index (bitwise-and code-point lower-mask))) |
| 1331 |
(encoding (vector-ref encodings index))) |
| 1332 |
|
| 1333 |
(if (not (eq? (code-point-info-general-category info) |
| 1334 |
(general-category surrogate))) |
| 1335 |
(begin |
| 1336 |
|
| 1337 |
(if (not (eq? (code-point-info-general-category info) |
| 1338 |
(code-point-encoding-general-category encoding))) |
| 1339 |
(error "general category mismatch" |
| 1340 |
info |
| 1341 |
(code-point-encoding-general-category encoding))) |
| 1342 |
|
| 1343 |
(let ((uppercase-code-point |
| 1344 |
(code-point-encoding-uppercase-code-point |
| 1345 |
code-point encoding |
| 1346 |
uppercase-offsets |
| 1347 |
uppercase-index-width lowercase-index-width titlecase-index-width)) |
| 1348 |
(lowercase-code-point |
| 1349 |
(code-point-encoding-lowercase-code-point |
| 1350 |
code-point encoding |
| 1351 |
lowercase-offsets |
| 1352 |
uppercase-index-width lowercase-index-width titlecase-index-width)) |
| 1353 |
(titlecase-code-point |
| 1354 |
(code-point-encoding-titlecase-code-point |
| 1355 |
code-point encoding |
| 1356 |
titlecase-offsets |
| 1357 |
uppercase-index-width lowercase-index-width titlecase-index-width)) |
| 1358 |
(uppercase? |
| 1359 |
(code-point-encoding-uppercase? |
| 1360 |
encoding |
| 1361 |
uppercase-index-width lowercase-index-width titlecase-index-width)) |
| 1362 |
(lowercase? |
| 1363 |
(code-point-encoding-lowercase? |
| 1364 |
encoding |
| 1365 |
uppercase-index-width lowercase-index-width titlecase-index-width))) |
| 1366 |
|
| 1367 |
|
| 1368 |
(if (not (= (code-point-info-uppercase-code-point info) |
| 1369 |
uppercase-code-point)) |
| 1370 |
(error "uppercase mismatch" info uppercase-code-point)) |
| 1371 |
|
| 1372 |
(if (not (= (code-point-info-lowercase-code-point info) |
| 1373 |
lowercase-code-point)) |
| 1374 |
(error "lowercase mismatch" info lowercase-code-point)) |
| 1375 |
|
| 1376 |
(if (not (= (code-point-info-titlecase-code-point info) |
| 1377 |
titlecase-code-point)) |
| 1378 |
(error "titlecase mismatch" info titlecase-code-point)) |
| 1379 |
|
| 1380 |
(if (not (eq? (or (table-ref special-uppercase-table code-point) |
| 1381 |
(eq? (code-point-info-general-category info) |
| 1382 |
(general-category uppercase-letter))) |
| 1383 |
uppercase?)) |
| 1384 |
(error "uppercase? mismatch" info code-point)) |
| 1385 |
(if (not (eq? (or (table-ref special-lowercase-table code-point) |
| 1386 |
(eq? (code-point-info-general-category info) |
| 1387 |
(general-category lowercase-letter))) |
| 1388 |
lowercase?)) |
| 1389 |
(error "lowercase? mismatch" info code-point)) |
| 1390 |
))))) |
| 1391 |
infos))) |
| 1392 |
|
| 1393 |
(define (check-unicode-tables unicode-data-filename |
| 1394 |
proplist-filename |
| 1395 |
specialcasing-filename) |
| 1396 |
(let ((infos (parse-unicode-data unicode-data-filename)) |
| 1397 |
(specialcasings (parse-specialcasing specialcasing-filename))) |
| 1398 |
(call-with-values |
| 1399 |
(lambda () |
| 1400 |
(parse-proplist-for-upper/lowercase proplist-filename)) |
| 1401 |
(lambda (special-uppercase-table special-lowercase-table) |
| 1402 |
(call-with-values |
| 1403 |
(lambda () |
| 1404 |
(make-scalar-value-case+general-category-encoding-tables |
| 1405 |
infos |
| 1406 |
special-lowercase-table special-uppercase-table |
| 1407 |
specialcasings)) |
| 1408 |
(lambda (indices |
| 1409 |
encodings |
| 1410 |
uppercase-offsets lowercase-offsets titlecase-offsets) |
| 1411 |
(test-code-point-case+general-category-encoding-tables |
| 1412 |
infos special-uppercase-table special-lowercase-table |
| 1413 |
specialcasings |
| 1414 |
indices encodings |
| 1415 |
uppercase-offsets lowercase-offsets titlecase-offsets))))))) |
| 1416 |
|
| 1417 |
(define (find-code-point-info code-point infos) |
| 1418 |
(call-with-current-continuation |
| 1419 |
(lambda (return) |
| 1420 |
(for-each-expanded-code-point-info |
| 1421 |
(lambda (info) |
| 1422 |
(if (= code-point (code-point-info-code-point info)) |
| 1423 |
(return info))) |
| 1424 |
infos)))) |