Merge branch 'master' of git://git.sv.gnu.org/lilypond
[lilypond.git] / lily / parse-scm.cc
blob51acd6e6ca658793d5b41e2f146bad608d45bd6e
1 /*
2 parse-scm -- Parse a single SCM expression exactly.
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
9 #include "parse-scm.hh"
11 #include <cstdio>
12 using namespace std;
14 #include "international.hh"
15 #include "main.hh"
16 #include "paper-book.hh"
17 #include "source-file.hh"
19 /* Pass string to scm parser, evaluate one expression.
20 Return result value and #chars read.
22 Thanks to Gary Houston <ghouston@freewire.co.uk> */
23 SCM
24 internal_ly_parse_scm (Parse_start *ps)
26 Source_file *sf = ps->start_location_.get_source_file ();
27 SCM port = sf->get_port ();
29 int off = ps->start_location_.start () - sf->c_str ();
31 scm_seek (port, scm_long2num (off), scm_long2num (SEEK_SET));
32 SCM from = scm_ftell (port);
34 scm_set_port_line_x (port, scm_from_int (ps->start_location_.line_number () -1));
35 scm_set_port_column_x (port, scm_from_int (ps->start_location_.column_number () -1));
37 SCM answer = SCM_UNSPECIFIED;
38 SCM form = scm_read (port);
41 /* Reset read_buf for scm_ftell.
42 Shouldn't scm_read () do this for us? */
43 scm_fill_input (port);
44 SCM to = scm_ftell (port);
45 ps->nchars = scm_to_int (to) - scm_to_int (from);
48 /* Read expression from port. */
49 if (!SCM_EOF_OBJECT_P (form))
51 if (ps->safe_)
53 static SCM module = SCM_BOOL_F;
54 if (module == SCM_BOOL_F)
56 SCM function = ly_lily_module_constant ("make-safe-lilypond-module");
57 module = scm_call_0 (function);
59 answer = scm_eval (form, module);
61 else
62 answer = scm_primitive_eval (form);
65 /* Don't close the port here; if we re-enter this function via a
66 continuation, then the next time we enter it, we'll get an error.
67 It's a string port anyway, so there's no advantage to closing it
68 early. */
69 // scm_close_port (port);
71 return answer;
74 SCM
75 catch_protected_parse_body (void *p)
77 Parse_start *ps = (Parse_start *) p;
79 return internal_ly_parse_scm (ps);
82 SCM
83 parse_handler (void *data, SCM tag, SCM args)
85 Parse_start *ps = (Parse_start *) data;
87 ps->start_location_.error (_ ("GUILE signaled an error for the expression beginning here"));
89 if (scm_ilength (args) > 2)
90 scm_display_error_message (scm_cadr (args), scm_caddr (args), scm_current_error_port ());
92 if (tag == ly_symbol2scm ("read-error"))
93 ps->nchars = 1;
95 return SCM_UNDEFINED;
98 SCM
99 protected_ly_parse_scm (Parse_start *ps)
102 Catch #t : catch all Scheme level errors.
104 return scm_internal_catch (SCM_BOOL_T,
105 &catch_protected_parse_body,
106 (void *) ps,
107 &parse_handler, (void *) ps);
110 bool parse_protect_global = true;
111 bool parsed_objects_should_be_dead = false;
113 /* Try parsing. Upon failure return SCM_UNDEFINED.
114 FIXME: shouldn't we return SCM_UNSCPECIFIED -- jcn */
116 ly_parse_scm (char const *s, int *n, Input i, bool safe)
118 Parse_start ps;
119 ps.str = s;
120 ps.start_location_ = i;
121 ps.safe_ = safe;
123 SCM ans = parse_protect_global ? protected_ly_parse_scm (&ps)
124 : internal_ly_parse_scm (&ps);
125 *n = ps.nchars;
127 return ans;