]> git.donarmstrong.com Git - lilypond.git/blob - lily/parse-scm.cc
81c4ad3a29e00afa28ff840e359c120f801ecc6b
[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 "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   Source_file *sf = ps->start_location_.get_source_file ();
40   SCM port = sf->get_port ();
41
42   long off = ps->start_location_.start () - sf->c_str ();
43
44   scm_seek (port, scm_from_long (off), scm_from_long (SEEK_SET));
45   SCM from = scm_ftell (port);
46
47   scm_set_port_line_x (port, scm_from_int (ps->start_location_.line_number () - 1));
48   scm_set_port_column_x (port, scm_from_int (ps->start_location_.column_number () - 1));
49
50   SCM form = scm_read (port);
51
52   SCM to = scm_ftell (port);
53   ps->nchars = scm_to_int (to) - scm_to_int (from);
54
55
56   if (!SCM_EOF_OBJECT_P (form)) {
57     if (ps->parser_->lexer_->top_input ())
58       {
59         // Find any precompiled form.
60         SCM c = scm_assv_ref (ps->parser_->closures_, from);
61         if (scm_is_true (c))
62           // Replace form with a call to previously compiled closure
63           form = scm_list_1 (c);
64       }
65     return scm_cons (form, make_input (ps->start_location_));
66 }
67
68   /* Don't close the port here; if we re-enter this function via a
69      continuation, then the next time we enter it, we'll get an error.
70      It's a string port anyway, so there's no advantage to closing it
71      early. */
72   // scm_close_port (port);
73
74   return SCM_UNDEFINED;
75 }
76
77 SCM
78 internal_ly_eval_scm (Parse_start *ps)
79 {
80   if (ps->safe_)
81     {
82       static SCM module = SCM_BOOL_F;
83       if (module == SCM_BOOL_F)
84         {
85           SCM function = ly_lily_module_constant ("make-safe-lilypond-module");
86           module = scm_gc_protect_object (scm_call_0 (function));
87         }
88       
89       // We define the parser so trusted Scheme functions can
90       // access the real namespace underlying the parser.
91       if (ps->parser_)
92         scm_module_define (module, ly_symbol2scm ("parser"),
93                            ps->parser_->self_scm ());
94       return scm_eval (ps->form_, module);
95     }
96   return scm_primitive_eval (ps->form_);
97 }
98
99
100 SCM
101 catch_protected_parse_body (void *p)
102 {
103   Parse_start *ps = (Parse_start *) p;
104
105   return (*ps->func_) (ps);
106 }
107
108 SCM
109 parse_handler (void *data, SCM tag, SCM args)
110 {
111   Parse_start *ps = (Parse_start *) data;
112
113   ps->start_location_.error (_ ("GUILE signaled an error for the expression beginning here"));
114
115   if (scm_ilength (args) > 2)
116     scm_display_error_message (scm_cadr (args), scm_caddr (args), scm_current_error_port ());
117
118   if (tag == ly_symbol2scm ("read-error"))
119     ps->nchars = 1;
120
121   return SCM_UNDEFINED;
122 }
123
124 SCM
125 protected_ly_parse_scm (Parse_start *ps)
126 {
127   /*
128     Catch #t : catch all Scheme level errors.
129    */
130   return scm_internal_catch (SCM_BOOL_T,
131                              &catch_protected_parse_body,
132                              (void *) ps,
133                              &parse_handler, (void *) ps);
134 }
135
136 bool parse_protect_global = true;
137 bool parsed_objects_should_be_dead = false;
138
139 /* Try parsing.  Upon failure return SCM_UNDEFINED. */
140
141 SCM
142 ly_parse_scm (char const *s, int *n, Input i, bool safe, Lily_parser *parser)
143 {
144   Parse_start ps;
145   ps.str = s;
146   ps.start_location_ = i;
147   ps.safe_ = safe;
148   ps.form_ = SCM_UNDEFINED;
149   ps.parser_ = parser;
150   ps.func_ = internal_ly_parse_scm;
151
152   SCM ans = parse_protect_global ? protected_ly_parse_scm (&ps)
153             : internal_ly_parse_scm (&ps);
154   *n = ps.nchars;
155
156   return ans;
157 }
158
159 SCM
160 ly_eval_scm (SCM form, Input i, bool safe, Lily_parser *parser)
161 {
162   Parse_start ps;
163   ps.str = 0;
164   ps.start_location_ = i;
165   ps.safe_ = safe;
166   ps.form_ = form;
167   ps.parser_ = parser;
168   ps.func_ = internal_ly_eval_scm;
169
170   SCM ans = parse_protect_global ? protected_ly_parse_scm (&ps)
171             : internal_ly_eval_scm (&ps);
172   scm_remember_upto_here_1 (form);
173   return ans;
174 }
175