gdritter repos when-computer / 4b79120
Added comments to telml.scm and allowed numbers in tag names Getty Ritter 7 years ago
1 changed file(s) with 19 addition(s) and 3 deletion(s). Collapse all Expand all
1616 (title meta-title)
1717 (tags meta-tags))
1818
19 ;; This builds a stream abstraction, where a stream is a stream of
20 ;; characters. This makes writing a parser a _lot_ easier.
1921 (define (mk-stream s)
2022 (let ((lst (string->list s)))
2123 (lambda (msg)
3234 (cadr lst)))
3335 ((eq? 'done msg) (null? lst))))))
3436
37 ;; Special characters in TeLML are in the set [\{}|], and this also
38 ;; considers EOF to be a special character.
3539 (define (special-char? c)
3640 (or (eq? c #\\)
3741 (eq? c #\{)
3943 (eq? c #\|)
4044 (not c)))
4145
46 ;; This turns a stream back into a proper list, mostly for debugging
47 ;; purposes.
4248 (define (stream->list s)
4349 (if (s 'peek) (cons (s 'next) (stream->list s)) '()))
4450
51 ;; Identifiers consist of the characters [A-Za-z0-9_-], which are
52 ;; matched here by number.
4553 (define (ident-char? c)
4654 (let ((i (char->integer c)))
47 (or (= i 45) (= i 95)
55 (or (= i 45)
56 (= i 95)
57 (and (>= i 48) (<= i 57))
4858 (and (>= i 64) (<= i 90))
4959 (and (>= i 97) (<= i 122)))))
5060
61 ;; Whitespace is in [\t\r\n ]
5162 (define (whitespace? c)
5263 (let ((i (char->integer c)))
5364 (or (= i 9) (= i 10) (= i 13) (= i 32))))
5465
66 ;; Our parsing function consists of a number of mutually recursive
67 ;; functions that operate over a stream of characters.
5568 (define (parse str)
5669 (let ((s (mk-stream str)))
5770
5871 (define (match-char c)
59 (if (eq? (s 'next) c) #t (raise c)))
72 (if (eq? (s 'next) c) #t (throw 'unexpected-char c)))
6073
6174 (define (parse-tag)
6275 (let* ((_ (match-char #\\))
187200 (partition (go document) '(())))
188201
189202 (define (string->sexp str)
190 (call-with-input-string str read))
203 (catch 'read-error
204 (lambda () (call-with-input-string str read))
205 (lambda _ #f)))
191206
192207 (define (escape-chars str)
193208 (define (go c rest)
204219 (define (telml->sxml telml tags)
205220 (define (rec arg)
206221 (cond ((string? arg) (escape-chars arg))
222 ((null? arg) '())
207223 ((symbol? (car arg))
208224 (let ((args (map rec (cdr arg)))
209225 (func (assoc (car arg) tags)))