4 source file of the GNU LilyPond music typesetter
6 (c) 2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
12 #include "ly-module.hh"
13 #include "lily-guile.hh"
15 #include "paper-book.hh"
16 #include "parse-scm.hh"
18 #include "source-file.hh"
20 /* Pass string to scm parser, evaluate one expression.
21 Return result value and #chars read.
23 Thanks to Gary Houston <ghouston@freewire.co.uk> */
25 internal_ly_parse_scm (Parse_start * ps)
27 Source_file *sf =ps->start_location_.source_file_;
28 SCM port = sf->get_port ();
30 int off = ps->start_location_.defined_str0_ - sf->to_str0 ();
32 scm_seek (port, scm_long2num (off), scm_long2num (SEEK_SET));
33 SCM from = scm_ftell (port);
36 SCM answer = SCM_UNSPECIFIED;
38 /* Read expression from port. */
39 if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
41 SCM function = ly_scheme_function ("make-safe-lilypond-module");
43 answer = scm_eval (form, function);
45 answer = scm_primitive_eval (form);
48 /* Reset read_buf for scm_ftell.
49 Shouldn't scm_read () do this for us? */
50 scm_fill_input (port);
51 SCM to = scm_ftell (port);
52 ps->nchars = scm_to_int (to) - scm_to_int (from);
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
58 // scm_close_port (port);
64 catch_protected_parse_body (void *p)
66 Parse_start *ps = (Parse_start*) p;
68 return internal_ly_parse_scm (ps);
72 parse_handler (void *data, SCM tag, SCM args)
74 Parse_start* ps = (Parse_start *) data;
77 ps->start_location_.error (_("GUILE signaled an error for the expression beginning here"));
79 if (scm_ilength (args) > 2)
80 scm_display_error_message (ly_cadr (args), ly_caddr (args), scm_current_error_port ());
83 The following is a kludge; we should probably search for
84 [a-z][0-9] (a note), and start before that.
92 Do some magical incantations: if not, lily will exit on the first
93 GUILE error, leaving no location trace.
97 #if GUILE_MINOR_VERSION < 7
98 #define READ_ERROR "misc-error"
100 #define READ_ERROR "read-error"
104 protected_ly_parse_scm (Parse_start *ps)
106 return scm_internal_catch (ly_symbol2scm (READ_ERROR),
107 &catch_protected_parse_body,
109 &parse_handler, (void*) ps);
112 bool parse_protect_global = true;
114 /* Try parsing. Upon failure return SCM_UNDEFINED.
115 FIXME: shouldn't we return SCM_UNSCPECIFIED -- jcn */
117 ly_parse_scm (char const *s, int *n, Input i, bool safe)
121 ps.start_location_ = i;
124 SCM ans = parse_protect_global ? protected_ly_parse_scm (&ps)
125 : internal_ly_parse_scm (&ps);