]> git.donarmstrong.com Git - lilypond.git/blob - lily/parse-scm.cc
Run grand replace for 2015.
[lilypond.git] / lily / parse-scm.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2004--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
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
20 #include "parse-scm.hh"
21
22 #include <cstdio>
23 using namespace std;
24
25 #include "lily-parser.hh"
26 #include "lily-lexer.hh"
27 #include "international.hh"
28 #include "main.hh"
29 #include "paper-book.hh"
30 #include "source-file.hh"
31
32 /* Pass string to scm parser, read one expression.
33    Return result value and #chars read.
34
35    Thanks to Gary Houston <ghouston@freewire.co.uk>  */
36 SCM
37 internal_ly_parse_scm (Parse_start *ps)
38 {
39   Input &hi = ps->location_;
40   Source_file *sf = hi.get_source_file ();
41   SCM port = sf->get_port ();
42
43   long off = hi.start () - sf->c_str ();
44
45   scm_seek (port, scm_from_long (off), scm_from_long (SEEK_SET));
46   SCM from = scm_ftell (port);
47
48   scm_set_port_line_x (port, scm_from_int (hi.line_number () - 1));
49   scm_set_port_column_x (port, scm_from_int (hi.column_number () - 1));
50
51   bool multiple = ly_is_equal (scm_peek_char (port), SCM_MAKE_CHAR ('@'));
52
53   if (multiple)
54     (void) scm_read_char (port);
55
56   SCM form = scm_read (port);
57   SCM to = scm_ftell (port);
58
59   hi.set (hi.get_source_file (),
60           hi.start (),
61           hi.start () + scm_to_int (scm_difference (to, from)));
62
63   if (!SCM_EOF_OBJECT_P (form))
64     {
65       if (ps->parser_->lexer_->top_input ())
66         {
67           // Find any precompiled form.
68           SCM c = scm_assv_ref (ps->parser_->closures_, from);
69           if (scm_is_true (c))
70             // Replace form with a call to previously compiled closure
71             form = scm_list_1 (c);
72         }
73       if (multiple)
74         form = scm_list_3 (ly_symbol2scm ("apply"),
75                            ly_symbol2scm ("values"),
76                            form);
77       return form;
78     }
79
80   /* Don't close the port here; if we re-enter this function via a
81      continuation, then the next time we enter it, we'll get an error.
82      It's a string port anyway, so there's no advantage to closing it
83      early. */
84   // scm_close_port (port);
85
86   return SCM_UNDEFINED;
87 }
88
89 SCM
90 internal_ly_eval_scm (Parse_start *ps)
91 {
92   if (ps->safe_)
93     {
94       static SCM module = SCM_BOOL_F;
95       if (module == SCM_BOOL_F)
96         {
97           SCM function = ly_lily_module_constant ("make-safe-lilypond-module");
98           module = scm_gc_protect_object (scm_call_0 (function));
99         }
100
101       // We define the parser so trusted Scheme functions can
102       // access the real namespace underlying the parser.
103       if (ps->parser_)
104         scm_module_define (module, ly_symbol2scm ("parser"),
105                            ps->parser_->self_scm ());
106       return scm_eval (ps->form_, module);
107     }
108   return scm_primitive_eval (ps->form_);
109 }
110
111 SCM
112 catch_protected_parse_body (void *p)
113 {
114   return internal_ly_parse_scm (static_cast<Parse_start *> (p));
115 }
116
117 SCM
118 catch_protected_eval_body (void *p)
119 {
120   return internal_ly_eval_scm (static_cast<Parse_start *> (p));
121 }
122
123 SCM
124 parse_handler (void *data, SCM /*tag*/, SCM args)
125 {
126   Parse_start *ps = (Parse_start *) data;
127
128   ps->location_.error (_ ("GUILE signaled an error for the expression beginning here"));
129
130   if (scm_ilength (args) > 2)
131     scm_display_error_message (scm_cadr (args), scm_caddr (args), scm_current_error_port ());
132
133   return SCM_UNDEFINED;
134 }
135
136 SCM
137 protected_ly_parse_scm (Parse_start *ps)
138 {
139   /*
140     Catch #t : catch all Scheme level errors.
141    */
142   return scm_internal_catch (SCM_BOOL_T,
143                              catch_protected_parse_body,
144                              (void *) ps,
145                              &parse_handler, (void *) ps);
146 }
147
148 SCM
149 protected_ly_eval_scm (Parse_start *ps)
150 {
151   /*
152     Catch #t : catch all Scheme level errors.
153    */
154   return scm_internal_catch (SCM_BOOL_T,
155                              catch_protected_eval_body,
156                              (void *) ps,
157                              &parse_handler, (void *) ps);
158 }
159
160 bool parse_protect_global = true;
161 bool parsed_objects_should_be_dead = false;
162
163 /* Try parsing.  Upon failure return SCM_UNDEFINED. */
164
165 SCM
166 ly_parse_scm (Input &i, bool safe, Lily_parser *parser)
167 {
168   Parse_start ps (SCM_UNDEFINED, i, safe, parser);
169
170   SCM ans = parse_protect_global ? protected_ly_parse_scm (&ps)
171             : internal_ly_parse_scm (&ps);
172
173   return ans;
174 }
175
176 SCM
177 ly_eval_scm (SCM form, Input i, bool safe, Lily_parser *parser)
178 {
179   Parse_start ps (form, i, safe, parser);
180
181   SCM ans = parse_protect_global ? protected_ly_eval_scm (&ps)
182             : internal_ly_eval_scm (&ps);
183   scm_remember_upto_here_1 (form);
184   return ans;
185 }