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)
19 This is actually pretty wasteful: we stuff the rest of the entire
20 file down GUILE, while we usually need only a bit of it.
22 SCM str = ly_str02scm (ps->str);
23 SCM port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG,
26 scm_set_port_filename_x (port, scm_makfrom0str (ps->start_location.file_string ().get_str0()));
27 scm_set_port_line_x (port, gh_int2scm (ps->start_location.line_number ()));
28 scm_set_port_column_x (port, gh_int2scm (ps->start_location.column_number()));
30 SCM from = scm_ftell (port);
33 SCM answer = SCM_UNSPECIFIED;
35 /* Read expression from port */
36 if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
37 answer = scm_primitive_eval (form);
44 all seems fine, but after parsing
48 read_buf has been advanced to read_pos - 1,
49 so that scm_ftell returns 1, instead of #parsed chars
53 urg: reset read_buf for scm_ftell
54 shouldn't scm_read () do this for us?
56 scm_fill_input (port);
57 SCM to = scm_ftell (port);
58 ps->nchars = gh_scm2int (to) - gh_scm2int (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
65 scm_close_port (port);
73 catch_protected_parse_body (void *p)
75 Parse_start *ps = (Parse_start*) p;
76 return internal_ly_parse_scm (ps);
80 parse_handler (void * data, SCM tag, SCM args)
82 Parse_start* ps = (Parse_start*) data;
84 ps->start_location.error (_("GUILE signaled an error for the expression begining here"));
86 if (scm_ilength (args) > 2)
87 scm_display_error_message (gh_cadr (args), gh_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.
103 protected_ly_parse_scm (Parse_start *ps)
105 return scm_internal_catch (ly_symbol2scm ("misc-error"), &catch_protected_parse_body,
107 &parse_handler, (void*)ps);
113 ly_parse_scm (char const* s, int *n, Input i)
117 ps.start_location = i;
119 SCM ans = protected_ly_parse_scm (&ps);