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>
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_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);
40 /* Read expression from port. */
41 if (!SCM_EOF_OBJECT_P (form))
45 static SCM module = SCM_BOOL_F;
46 if (module == SCM_BOOL_F)
48 SCM function = ly_lily_module_constant ("make-safe-lilypond-module");
49 module = scm_call_0 (function);
51 answer = scm_eval (form, module);
54 answer = scm_primitive_eval (form);
57 /* Reset read_buf for scm_ftell.
58 Shouldn't scm_read () do this for us? */
59 scm_fill_input (port);
60 SCM to = scm_ftell (port);
61 ps->nchars = scm_to_int (to) - scm_to_int (from);
63 /* Don't close the port here; if we re-enter this function via a
64 continuation, then the next time we enter it, we'll get an error.
65 It's a string port anyway, so there's no advantage to closing it
67 // scm_close_port (port);
73 catch_protected_parse_body (void *p)
75 Parse_start *ps = (Parse_start *) p;
77 return internal_ly_parse_scm (ps);
81 parse_handler (void *data, SCM tag, SCM args)
83 Parse_start *ps = (Parse_start *) data;
86 ps->start_location_.error (_ ("GUILE signaled an error for the expression beginning here"));
88 if (scm_ilength (args) > 2)
89 scm_display_error_message (scm_cadr (args), scm_caddr (args), scm_current_error_port ());
92 The following is a kludge; we should probably search for
93 [a-z][0-9] (a note), and start before that.
101 protected_ly_parse_scm (Parse_start *ps)
104 Catch #t : catch all Scheme level errors.
106 return scm_internal_catch (SCM_BOOL_T,
107 &catch_protected_parse_body,
109 &parse_handler, (void *) ps);
112 bool parse_protect_global = true;
113 bool parsed_objects_should_be_dead = false;
115 /* Try parsing. Upon failure return SCM_UNDEFINED.
116 FIXME: shouldn't we return SCM_UNSCPECIFIED -- jcn */
118 ly_parse_scm (char const *s, int *n, Input i, bool safe)
122 ps.start_location_ = i;
125 SCM ans = parse_protect_global ? protected_ly_parse_scm (&ps)
126 : internal_ly_parse_scm (&ps);