]> git.donarmstrong.com Git - lilypond.git/blob - scm/parser-ly-from-scheme.scm
Fix some bugs in the dynamic engraver and PostScript backend
[lilypond.git] / scm / parser-ly-from-scheme.scm
1 ;;;; ly-from-scheme.scm -- parsing LilyPond music expressions from scheme
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 2004--2006  Nicolas Sceaux  <nicolas.sceaux@free.fr>
6 ;;;;           Jan Nieuwenhuizen <janneke@gnu.org>
7
8 (define gen-lily-sym
9   ;; Generate a lilyvartmpXX symbol, that may be (hopefully) unique.
10   (let ((var-idx -1))
11     (lambda ()
12       (set! var-idx (1+ var-idx))
13       (string->symbol (format #f "lilyvartmp~a"
14                               (list->string (map (lambda (chr)
15                                                    (integer->char (+ (char->integer #\a)
16                                                                      (- (char->integer chr)
17                                                                         (char->integer #\0)))))
18                                                  (string->list (number->string var-idx)))))))))
19
20 (define-public (ly:parse-string-result str parser)
21   "Parse `str', which is supposed to contain a music expression."
22   (ly:parser-parse-string
23    parser
24    (format #f "parseStringResult = \\notemode { ~a }" str))
25   (ly:parser-lookup parser 'parseStringResult))
26
27 (define-public (read-lily-expression chr port)
28   "Read a #{ lily music expression #} from port and return
29 the scheme music expression. The $ character may be used to introduce
30 scheme forms, typically symbols. $$ may be used to simply write a `$'
31 character."
32   (let ((bindings '()))
33
34     (define (create-binding! val)
35       "Create a new symbol, bind it to `val' and return it."
36       (let ((tmp-symbol (gen-lily-sym)))
37         (set! bindings (cons (cons tmp-symbol val) bindings))
38         tmp-symbol))
39     
40     (define (remove-dollars! form)
41       "Generate a form where `$variable' and `$ value' mottos are replaced
42       by new symbols, which are binded to the adequate values."
43       (cond (;; $variable
44              (and (symbol? form)
45                   (string=? (substring (symbol->string form) 0 1) "$")
46                   (not (and (<= 2 (string-length (symbol->string form)))
47                             (string=? (substring (symbol->string form) 1 2) "$"))))
48              (create-binding! (string->symbol (substring (symbol->string form) 1))))
49             (;; atom
50              (not (pair? form)) form)
51             (;; ($ value ...)
52              (eqv? (car form) '$)
53              (cons (create-binding! (cadr form)) (remove-dollars! (cddr form))))
54             (else ;; (something ...)
55              (cons (remove-dollars! (car form)) (remove-dollars! (cdr form))))))
56     
57     (let ((lily-string (call-with-output-string
58                         (lambda (out)
59                           (do ((c (read-char port) (read-char port)))
60                               ((and (char=? c #\#)
61                                     (char=? (peek-char port) #\})) ;; we stop when #} is encoutered
62                                (read-char port))
63                             (cond
64                              ;; a $form expression
65                              ((and (char=? c #\$) (not (char=? (peek-char port) #\$)))
66                               (format out "\\~a" (create-binding! (read port))))
67                              ;; just a $ character
68                              ((and (char=? c #\$) (char=? (peek-char port) #\$))
69                               ;; pop the second $
70                               (display (read-char port) out))
71                              ;; a #scheme expression
72                              ((char=? c #\#)
73                               (let ((expr (read port)))
74                                 (format out "#~s" (if (eq? '$ expr)
75                                                       (create-binding! (read port))
76                                                       (remove-dollars! expr)))))
77                              ;; other caracters
78                              (else
79                               (display c out))))))))
80       `(let ((parser-clone (ly:clone-parser parser)))
81          ,@(map (lambda (binding)
82                   `(ly:parser-define! parser-clone ',(car binding) ,(cdr binding)))
83                 (reverse bindings))
84          (ly:parse-string-result ,lily-string parser-clone)))))
85
86 (read-hash-extend #\{ read-lily-expression)