151 lines
3.8 KiB
Scheme
151 lines
3.8 KiB
Scheme
#!@GUILE@ --no-auto-compile
|
|
-*- scheme -*-
|
|
!#
|
|
|
|
;; Can be called with a trailing argument pointing to the file on disk.
|
|
|
|
(use-modules
|
|
(uniseg internal)
|
|
(ice-9 pretty-print)
|
|
(ice-9 peg)
|
|
(ice-9 format)
|
|
(ice-9 exceptions)
|
|
(ice-9 match)
|
|
(srfi srfi-1))
|
|
|
|
(define stdout (current-output-port))
|
|
|
|
(define emoji-url
|
|
"https://unicode.org/Public/13.0.0/ucd/emoji/emoji-data.txt")
|
|
|
|
(define-peg-pattern @emoji-category all
|
|
(* (peg "[a-zA-Z_]")))
|
|
|
|
(define-peg-pattern @emoji-datum body
|
|
(and @codepoint-range (* @ws) (ignore ";") (* @ws) @emoji-category))
|
|
|
|
(define-peg-pattern @emoji-line body
|
|
(and @emoji-datum (* @ws) @comment))
|
|
|
|
(define emoji-ht (make-hash-table 5))
|
|
|
|
(define emoji-sets
|
|
'(emoji
|
|
emoji-presentation
|
|
emoji-modifier
|
|
emoji-modifier-base
|
|
emoji-component
|
|
emoji-extended-pictographic))
|
|
|
|
(define emoji-symbol-names
|
|
(map
|
|
(λ (set)
|
|
(string->symbol
|
|
(string-concatenate
|
|
(list "char-set:"
|
|
(symbol->string set)))))
|
|
emoji-sets))
|
|
|
|
(define emoji-sets-and-symbols
|
|
(zip emoji-sets emoji-symbol-names))
|
|
|
|
(define (process-emoji-line line)
|
|
(define (string->category str)
|
|
(match str
|
|
("Emoji" 'emoji)
|
|
("Emoji_Presentation" 'emoji-presentation)
|
|
("Emoji_Modifier" 'emoji-modifier)
|
|
("Emoji_Modifier_Base" 'emoji-modifier-base)
|
|
("Emoji_Component" 'emoji-component)
|
|
("Extended_Pictographic" 'emoji-extended-pictographic)))
|
|
|
|
(define tree (peg:tree (match-pattern @emoji-line line)))
|
|
|
|
(unless (or (not tree)
|
|
(null? tree)
|
|
(eq? '@comment (car tree)))
|
|
|
|
(match tree
|
|
(((('@codepoint-range
|
|
('@codepoint codepoints) ...)
|
|
('@emoji-category cat-str))
|
|
('@comment comment))
|
|
|
|
(with-exception-handler
|
|
(λ (err)
|
|
(format stdout "Skipping line due to error :: ")
|
|
(format-exception-msg stdout err))
|
|
(λ ()
|
|
(let ((f (hex-string->integer (first codepoints)))
|
|
(l (hex-string->integer (last codepoints)))
|
|
(category (string->category cat-str)))
|
|
|
|
(when (or (in-surrogate-range f)
|
|
(in-surrogate-range l))
|
|
(error (format #f "chars in surrogate range ~x -> ~x" f l)))
|
|
|
|
(cons-hash-list! emoji-ht category f l)))
|
|
#:unwind? #t)))))
|
|
|
|
(define line-func
|
|
(if (= 2 (length (command-line)))
|
|
(λ ()
|
|
(file-to-lines (last (command-line)) stdout))
|
|
(λ ()
|
|
(wget-to-lines emoji-url stdout))))
|
|
|
|
(define file "uniseg/emoji.scm")
|
|
|
|
(format stdout "Writing to ~a...\n" file)
|
|
|
|
(with-output-to-file file
|
|
(λ ()
|
|
(format #t ";; Code generated by ~a. DO NOT EDIT\n\n" (first (command-line)))
|
|
|
|
(for-each process-emoji-line (line-func))
|
|
|
|
(pretty-print
|
|
`(define-module (uniseg emoji)
|
|
#:use-module (uniseg internal)
|
|
#:use-module (ice-9 hash-table)
|
|
#:use-module (srfi srfi-1)
|
|
#:export (,@emoji-symbol-names
|
|
emoji-charsets)))
|
|
|
|
(pretty-print
|
|
`(define emoji-ht
|
|
(alist->hashq-table ',(hash-map->list cons emoji-ht))))
|
|
|
|
(display "\n")
|
|
|
|
(for-each
|
|
(λ (sym)
|
|
(pretty-print
|
|
`(define ,sym (char-set))))
|
|
emoji-symbol-names)
|
|
|
|
(display "\n")
|
|
|
|
(pretty-print
|
|
`(define emoji-charsets
|
|
(list
|
|
,@(map
|
|
(λ (pair)
|
|
(let ((f (first pair))
|
|
(s (second pair)))
|
|
`(list ',f ,s)))
|
|
emoji-sets-and-symbols))))
|
|
|
|
(display "\n")
|
|
|
|
(for-each
|
|
(λ (set-pair)
|
|
(let ((name (first set-pair))
|
|
(symbol (second set-pair)))
|
|
(pretty-print
|
|
`(ranges->charset! emoji-ht ',name ,symbol))))
|
|
emoji-sets-and-symbols)
|
|
|
|
(display "Code generation complete.\n" stdout)))
|
|
|
|
(format stdout "Written to ~a.\n" file)
|