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