2 parse-scm -- Parse a single SCM expression exactly.
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "parse-scm.hh"
14 #include "international.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> */
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 answer = SCM_UNSPECIFIED;
35 SCM form = scm_read (port);
37 /* Read expression from port. */
38 if (!SCM_EOF_OBJECT_P (form))
42 static SCM module = SCM_BOOL_F;
43 if (module == SCM_BOOL_F)
45 SCM function = ly_lily_module_constant ("make-safe-lilypond-module");
46 module = scm_call_0 (function);
48 answer = scm_eval (form, module);
51 answer = scm_primitive_eval (form);
54 /* Reset read_buf for scm_ftell.
55 Shouldn't scm_read () do this for us? */
56 scm_fill_input (port);
57 SCM to = scm_ftell (port);
58 ps->nchars = scm_to_int (to) - scm_to_int (from);
60 /* Don't close the port here; if we re-enter this function via a
61 continuation, then the next time we enter it, we'll get an error.
62 It's a string port anyway, so there's no advantage to closing it
64 // scm_close_port (port);
70 catch_protected_parse_body (void *p)
72 Parse_start *ps = (Parse_start *) p;
74 return internal_ly_parse_scm (ps);
78 parse_handler (void *data, SCM tag, SCM args)
80 Parse_start *ps = (Parse_start *) data;
83 ps->start_location_.error (_ ("GUILE signaled an error for the expression beginning here"));
85 if (scm_ilength (args) > 2)
86 scm_display_error_message (scm_cadr (args), scm_caddr (args), scm_current_error_port ());
89 The following is a kludge; we should probably search for
90 [a-z][0-9] (a note), and start before that.
98 Do some magical incantations: if not, lily will exit on the first
99 GUILE error, leaving no location trace.
102 #if GUILE_MINOR_VERSION < 7
103 #define READ_ERROR "misc-error"
105 #define READ_ERROR "read-error"
109 protected_ly_parse_scm (Parse_start *ps)
111 return scm_internal_catch (ly_symbol2scm (READ_ERROR),
112 &catch_protected_parse_body,
114 &parse_handler, (void *) ps);
117 bool parse_protect_global = true;
119 /* Try parsing. Upon failure return SCM_UNDEFINED.
120 FIXME: shouldn't we return SCM_UNSCPECIFIED -- jcn */
122 ly_parse_scm (char const *s, int *n, Input i, bool safe)
126 ps.start_location_ = i;
129 SCM ans = parse_protect_global ? protected_ly_parse_scm (&ps)
130 : internal_ly_parse_scm (&ps);