]> git.donarmstrong.com Git - lilypond.git/blob - lily/parse-scm.cc
Separate read/eval for Scheme expressions.
[lilypond.git] / lily / parse-scm.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2004--2011 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 "international.hh"
27 #include "main.hh"
28 #include "paper-book.hh"
29 #include "source-file.hh"
30
31 /* Pass string to scm parser, read one expression.
32    Return result value and #chars read.
33
34    Thanks to Gary Houston <ghouston@freewire.co.uk>  */
35 SCM
36 internal_ly_parse_scm (Parse_start *ps)
37 {
38   Source_file *sf = ps->start_location_.get_source_file ();
39   SCM port = sf->get_port ();
40
41   int off = ps->start_location_.start () - sf->c_str ();
42
43   scm_seek (port, scm_from_long (off), scm_from_long (SEEK_SET));
44   SCM from = scm_ftell (port);
45
46   scm_set_port_line_x (port, scm_from_int (ps->start_location_.line_number () - 1));
47   scm_set_port_column_x (port, scm_from_int (ps->start_location_.column_number () - 1));
48
49   SCM form = scm_read (port);
50
51   SCM to = scm_ftell (port);
52   ps->nchars = scm_to_int (to) - scm_to_int (from);
53
54   /* Don't close the port here; if we re-enter this function via a
55      continuation, then the next time we enter it, we'll get an error.
56      It's a string port anyway, so there's no advantage to closing it
57      early. */
58   // scm_close_port (port);
59
60   if (!SCM_EOF_OBJECT_P (form))
61     return scm_cons (form, make_input (ps->start_location_));
62
63   return SCM_UNDEFINED;
64 }
65
66 SCM
67 internal_ly_eval_scm (Parse_start *ps)
68 {
69   if (ps->parser_ && !SCM_UNBNDP (ps->parser_->local_environment_))
70     return scm_local_eval (ps->form_, ps->parser_->local_environment_);
71   if (ps->safe_)
72     {
73       static SCM module = SCM_BOOL_F;
74       if (module == SCM_BOOL_F)
75         {
76           SCM function = ly_lily_module_constant ("make-safe-lilypond-module");
77           module = scm_call_0 (function);
78         }
79       
80       // We define the parser so trusted Scheme functions can
81       // access the real namespace underlying the parser.
82       if (ps->parser_)
83         scm_module_define (module, ly_symbol2scm ("parser"),
84                            ps->parser_->self_scm ());
85       return scm_eval (ps->form_, module);
86     }
87   return scm_primitive_eval (ps->form_);
88 }
89
90
91 SCM
92 catch_protected_parse_body (void *p)
93 {
94   Parse_start *ps = (Parse_start *) p;
95
96   return (*ps->func_) (ps);
97 }
98
99 SCM
100 parse_handler (void *data, SCM tag, SCM args)
101 {
102   Parse_start *ps = (Parse_start *) data;
103
104   ps->start_location_.error (_ ("GUILE signaled an error for the expression beginning here"));
105
106   if (scm_ilength (args) > 2)
107     scm_display_error_message (scm_cadr (args), scm_caddr (args), scm_current_error_port ());
108
109   if (tag == ly_symbol2scm ("read-error"))
110     ps->nchars = 1;
111
112   return SCM_UNDEFINED;
113 }
114
115 SCM
116 protected_ly_parse_scm (Parse_start *ps)
117 {
118   /*
119     Catch #t : catch all Scheme level errors.
120    */
121   return scm_internal_catch (SCM_BOOL_T,
122                              &catch_protected_parse_body,
123                              (void *) ps,
124                              &parse_handler, (void *) ps);
125 }
126
127 bool parse_protect_global = true;
128 bool parsed_objects_should_be_dead = false;
129
130 /* Try parsing.  Upon failure return SCM_UNDEFINED. */
131
132 SCM
133 ly_parse_scm (char const *s, int *n, Input i, bool safe, Lily_parser *parser)
134 {
135   Parse_start ps;
136   ps.str = s;
137   ps.start_location_ = i;
138   ps.safe_ = safe;
139   ps.form_ = SCM_UNDEFINED;
140   ps.parser_ = parser;
141   ps.func_ = internal_ly_parse_scm;
142
143   SCM ans = parse_protect_global ? protected_ly_parse_scm (&ps)
144             : internal_ly_parse_scm (&ps);
145   *n = ps.nchars;
146
147   return ans;
148 }
149
150 SCM
151 ly_eval_scm (SCM form, Input i, bool safe, Lily_parser *parser)
152 {
153   Parse_start ps;
154   ps.str = 0;
155   ps.start_location_ = i;
156   ps.safe_ = safe;
157   ps.form_ = form;
158   ps.parser_ = parser;
159   ps.func_ = internal_ly_eval_scm;
160
161   SCM ans = parse_protect_global ? protected_ly_parse_scm (&ps)
162             : internal_ly_eval_scm (&ps);
163   scm_remember_upto_here_1 (form);
164   return ans;
165 }
166