2 #include "lily-guile.hh"
3 #include "parse-scm.hh"
8 Pass string to scm parser, evaluate one expression.
9 Return result value and #chars read.
11 Thanks to Gary Houston <ghouston@freewire.co.uk>
13 Need guile-1.3.4 (>1.3 anyway) for ftell on str ports -- jcn
16 internal_ly_parse_scm (Parse_start * ps)
20 This is actually pretty wasteful: we stuff the rest of the entire
21 file down GUILE, while we usually need only a bit of it.
23 TODO: implement read_only_string_port(), (preferably in GUILE ?)
26 SCM str = scm_makfrom0str (ps->str);
27 SCM port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG,
30 scm_set_port_filename_x (port, scm_makfrom0str (ps->start_location.file_string ().get_str0()));
31 scm_set_port_line_x (port, gh_int2scm (ps->start_location.line_number ()));
32 scm_set_port_column_x (port, gh_int2scm (ps->start_location.column_number()));
34 SCM from = scm_ftell (port);
37 SCM answer = SCM_UNSPECIFIED;
39 /* Read expression from port */
40 if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
41 answer = scm_primitive_eval (form);
48 all seems fine, but after parsing
52 read_buf has been advanced to read_pos - 1,
53 so that scm_ftell returns 1, instead of #parsed chars
57 urg: reset read_buf for scm_ftell
58 shouldn't scm_read () do this for us?
60 scm_fill_input (port);
61 SCM to = scm_ftell (port);
62 ps->nchars = gh_scm2int (to) - gh_scm2int (from);
64 /* Don't close the port here; if we re-enter this function via a
65 continuation, then the next time we enter it, we'll get an error.
66 It's a string port anyway, so there's no advantage to closing it
69 scm_close_port (port);
77 catch_protected_parse_body (void *p)
79 Parse_start *ps = (Parse_start*) p;
80 return internal_ly_parse_scm (ps);
84 parse_handler (void * data, SCM tag, SCM args)
86 Parse_start* ps = (Parse_start*) data;
88 ps->start_location.error (_("GUILE signaled an error for the expression begining here"));
90 if (scm_ilength (args) > 2)
91 scm_display_error_message (gh_cadr (args), gh_caddr(args), scm_current_error_port());
94 The following is a kludge; we should probably search for
95 [a-z][0-9] (a note), and start before that.
103 Do some magical incantations: if not, lily will exit on the first
104 GUILE error, leaving no location trace.
108 #if GUILE_MINOR_VERSION < 7
109 #define READ_ERROR "misc-error"
111 #define READ_ERROR "read-error"
115 protected_ly_parse_scm (Parse_start *ps)
117 return scm_internal_catch (ly_symbol2scm (READ_ERROR),
118 &catch_protected_parse_body,
120 &parse_handler, (void*)ps);
126 ly_parse_scm (char const* s, int *n, Input i)
130 ps.start_location = i;
132 SCM ans = protected_ly_parse_scm (&ps);