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)))
43 static SCM module = SCM_BOOL_F;
44 if (module == SCM_BOOL_F)
46 SCM function = ly_scheme_function ("make-safe-lilypond-module");
47 module = scm_call_0 (function);
49 answer = scm_eval (form, module);
52 answer = scm_primitive_eval (form);
55 /* Reset read_buf for scm_ftell.
56 Shouldn't scm_read () do this for us? */
57 scm_fill_input (port);
58 SCM to = scm_ftell (port);
59 ps->nchars = scm_to_int (to) - scm_to_int (from);
61 /* Don't close the port here; if we re-enter this function via a
62 continuation, then the next time we enter it, we'll get an error.
63 It's a string port anyway, so there's no advantage to closing it
65 // scm_close_port (port);
71 catch_protected_parse_body (void *p)
73 Parse_start *ps = (Parse_start*) p;
75 return internal_ly_parse_scm (ps);
79 parse_handler (void *data, SCM tag, SCM args)
81 Parse_start* ps = (Parse_start *) data;
84 ps->start_location_.error (_("GUILE signaled an error for the expression beginning here"));
86 if (scm_ilength (args) > 2)
87 scm_display_error_message (ly_cadr (args), ly_caddr (args), scm_current_error_port ());
90 The following is a kludge; we should probably search for
91 [a-z][0-9] (a note), and start before that.
99 Do some magical incantations: if not, lily will exit on the first
100 GUILE error, leaving no location trace.
104 #if GUILE_MINOR_VERSION < 7
105 #define READ_ERROR "misc-error"
107 #define READ_ERROR "read-error"
111 protected_ly_parse_scm (Parse_start *ps)
113 return scm_internal_catch (ly_symbol2scm (READ_ERROR),
114 &catch_protected_parse_body,
116 &parse_handler, (void*) ps);
119 bool parse_protect_global = true;
121 /* Try parsing. Upon failure return SCM_UNDEFINED.
122 FIXME: shouldn't we return SCM_UNSCPECIFIED -- jcn */
124 ly_parse_scm (char const *s, int *n, Input i, bool safe)
128 ps.start_location_ = i;
131 SCM ans = parse_protect_global ? protected_ly_parse_scm (&ps)
132 : internal_ly_parse_scm (&ps);