]> git.donarmstrong.com Git - lilypond.git/blob - scm/parser-ly-from-scheme.scm
* The grand 2005-2006 replace.
[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) (- (char->integer chr)
16                                                                                             (char->integer #\0)))))
17                                                  (string->list (number->string var-idx)))))))))
18
19 (define-public (ly:parse-string-result str parser)
20   "Parse `str', which is supposed to contain a music expression."
21   (let ((music-sym (gen-lily-sym)))
22     (ly:parser-parse-string
23      parser
24      (format #f "parseStringResult = { ~a }" str))
25
26     (ly:parser-lookup parser 'parseStringResult)))
27
28 (define-public (read-lily-expression chr port)
29   "Read a #{ lily music expression #} from port and return
30 the scheme music expression. The $ character may be used to introduce
31 scheme forms, typically symbols. $$ may be used to simply write a `$'
32 character."
33   (let ((bindings '()))
34
35     (define (create-binding! val)
36       "Create a new symbol, bind it to `val' and return it."
37       (let ((tmp-symbol (gen-lily-sym)))
38
39         (set! bindings (cons (cons tmp-symbol val) bindings))
40         tmp-symbol))
41     
42     (define (remove-dollars! form)
43       "Generate a form where `$variable' and `$ value' mottos are replaced
44       by new symbols, which are binded to the adequate values."
45       (cond (;; $variable
46              (and (symbol? form)
47                   (string=? (substring (symbol->string form) 0 1) "$")
48                   (not (and (<= 2 (string-length (symbol->string form)))
49                             (string=? (substring (symbol->string form) 1 2) "$"))))
50              (create-binding! (string->symbol (substring (symbol->string form) 1))))
51             (;; atom
52              (not (pair? form)) form)
53             (;; ($ value ...)
54              (eqv? (car form) '$)
55              (cons (create-binding! (cadr form)) (remove-dollars! (cddr form))))
56             (else ;; (something ...)
57              (cons (remove-dollars! (car form)) (remove-dollars! (cdr form))))))
58     (let*
59         ((lily-string (call-with-output-string
60                         (lambda (out)
61                           (do ((c (read-char port) (read-char port)))
62                              ((and (char=? c #\#)
63                                    (char=? (peek-char port) #\})) ;; we stop when #} is encoutered
64                               (read-char port))
65                            (cond
66                             ;; a $form expression
67                             ((and (char=? c #\$) (not (char=? (peek-char port) #\$)))
68                              (format out "\\~a" (create-binding! (read port))))
69                             ;; just a $ character
70                             ((and (char=? c #\$) (char=? (peek-char port) #\$))
71                              ;; pop the second $
72                              (display (read-char port) out))
73                             ;; a #scheme expression
74                             ((char=? c #\#)
75                              (let ((expr (read port)))
76                                (format out "#~s" (if (eq? '$ expr)
77                                                      (create-binding! (read port))
78                                                      (remove-dollars! expr)))))
79                             ;; other caracters
80                             (else
81                              (display c out)))))))
82
83           (result
84            `(let ((parser-clone (ly:clone-parser parser)))
85               ,@(map (lambda (binding)
86                        `(ly:parser-define! parser-clone ',(car binding) ,(cdr binding)))
87                      (reverse bindings))
88               (ly:parse-string-result ,lily-string parser-clone))
89           ))
90
91       
92              
93       result
94       )))
95
96 (read-hash-extend #\{ read-lily-expression)