]> git.donarmstrong.com Git - lilypond.git/blob - scm/parser-ly-from-scheme.scm
Let #{ ... #} pass its $ handling to environment cloning
[lilypond.git] / scm / parser-ly-from-scheme.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2004--2011  Nicolas Sceaux  <nicolas.sceaux@free.fr>
4 ;;;;           Jan Nieuwenhuizen <janneke@gnu.org>
5 ;;;;
6 ;;;; LilyPond is free software: you can redistribute it and/or modify
7 ;;;; it under the terms of the GNU General Public License as published by
8 ;;;; the Free Software Foundation, either version 3 of the License, or
9 ;;;; (at your option) any later version.
10 ;;;;
11 ;;;; LilyPond is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ;;;; GNU General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18
19 (define-public (read-lily-expression chr port)
20   "Read a lilypond music expression enclosed within @code{#@{} and @code{#@}}
21 from @var{port} and return the corresponding Scheme music expression.
22 @samp{$} and @samp{#} introduce immediate and normal Scheme forms."
23   (let ((lily-string (call-with-output-string
24                       (lambda (out)
25                         (do ((c (read-char port) (read-char port)))
26                             ((and (char=? c #\#)
27                                   (char=? (peek-char port) #\})) ;; we stop when #} is encountered
28                              (read-char port))
29                           ;; a #scheme or $scheme expression
30                           (if (or (char=? c #\#) (char=? c #\$))
31                               (format out "~a~s" c (read port))
32                               ;; other characters
33                               (display c out)))))))
34     `(let* ((clone
35              (ly:parser-clone parser (procedure-environment (lambda () '()))))
36             (result (begin
37                       (ly:parser-clear-error clone)
38                       (ly:parse-string-expression clone ,lily-string))))
39        (if (ly:parser-has-error? clone)
40            (ly:parser-error parser (_ "error in #{ ... #}")))
41        result)))
42
43 (read-hash-extend #\{ read-lily-expression)