2019-03-27 20:38:45 -04:00
|
|
|
(define (tex-command-char? ch)
|
|
|
|
(or (char-alphabetic? ch)
|
|
|
|
(char-numeric? ch)))
|
|
|
|
|
|
|
|
(define (not-tex-special-char? ch)
|
|
|
|
(not (or (equal? ch #\{)
|
|
|
|
(equal? ch #\})
|
|
|
|
(equal? ch #\\))))
|
|
|
|
|
|
|
|
(define (read-tex-command-args)
|
|
|
|
(let loop ((args '()))
|
|
|
|
(if (not (read-char? #\{))
|
|
|
|
args
|
|
|
|
(loop (append args (list (read-tex-until #\})))))))
|
|
|
|
|
|
|
|
(define (read-tex-thing)
|
|
|
|
(cond ((read-char? #\\)
|
|
|
|
(let ((command (read-char* tex-command-char?)))
|
2019-03-28 06:37:15 -04:00
|
|
|
(if command
|
|
|
|
(cons (string->symbol command)
|
|
|
|
(read-tex-command-args))
|
|
|
|
(read-char* not-tex-special-char?))))
|
2019-03-27 20:38:45 -04:00
|
|
|
((read-char? #\{)
|
|
|
|
(cons 'math (read-tex-until #\})))
|
|
|
|
(else (read-char* not-tex-special-char?))))
|
|
|
|
|
|
|
|
(define (read-tex-until sentinel)
|
|
|
|
(let loop ((things '()))
|
|
|
|
(if (read-char? sentinel)
|
|
|
|
things
|
|
|
|
(let ((thing (read-tex-thing)))
|
2019-03-28 06:36:18 -04:00
|
|
|
(if (not thing)
|
|
|
|
things
|
|
|
|
(loop (append things (list thing))))))))
|
2019-03-27 20:38:45 -04:00
|
|
|
|
2019-03-28 06:33:25 -04:00
|
|
|
(define (read-tex-document)
|
|
|
|
(read-tex-until eof-object?))
|