3 #include "lily-guile.hh"
4 #include "parse-scm.hh"
6 #include "source-file.hh"
9 Pass string to scm parser, evaluate one expression.
10 Return result value and #chars read.
12 Thanks to Gary Houston <ghouston@freewire.co.uk>
14 Need guile-1.3.4 (>1.3 anyway) for ftell on str ports -- jcn
17 internal_ly_parse_scm (Parse_start * ps)
19 Source_file* sf =ps->start_location_.source_file_;
20 SCM port = sf->get_port();
22 int off = ps->start_location_.defined_str0_ - sf->to_str0();
25 scm_seek (port, scm_long2num (off), scm_long2num (SEEK_SET));
26 SCM from = scm_ftell (port);
29 SCM answer = SCM_UNSPECIFIED;
31 /* Read expression from port */
32 if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
33 answer = scm_primitive_eval (form);
40 all seems fine, but after parsing
44 read_buf has been advanced to read_pos - 1,
45 so that scm_ftell returns 1, instead of #parsed chars
49 urg: reset read_buf for scm_ftell
50 shouldn't scm_read () do this for us?
52 scm_fill_input (port);
53 SCM to = scm_ftell (port);
54 ps->nchars = gh_scm2int (to) - gh_scm2int (from);
56 /* Don't close the port here; if we re-enter this function via a
57 continuation, then the next time we enter it, we'll get an error.
58 It's a string port anyway, so there's no advantage to closing it
61 scm_close_port (port);
69 catch_protected_parse_body (void *p)
71 Parse_start *ps = (Parse_start*) p;
72 return internal_ly_parse_scm (ps);
76 parse_handler (void * data, SCM tag, SCM args)
78 Parse_start* ps = (Parse_start*) data;
80 ps->start_location_.error (_("GUILE signaled an error for the expression begining here"));
82 if (scm_ilength (args) > 2)
83 scm_display_error_message (gh_cadr (args), gh_caddr(args), scm_current_error_port());
86 The following is a kludge; we should probably search for
87 [a-z][0-9] (a note), and start before that.
95 Do some magical incantations: if not, lily will exit on the first
96 GUILE error, leaving no location trace.
100 #if GUILE_MINOR_VERSION < 7
101 #define READ_ERROR "misc-error"
103 #define READ_ERROR "read-error"
107 protected_ly_parse_scm (Parse_start *ps)
109 return scm_internal_catch (ly_symbol2scm (READ_ERROR),
110 &catch_protected_parse_body,
112 &parse_handler, (void*)ps);
118 ly_parse_scm (char const* s, int *n, Input i)
122 ps.start_location_ = i;
124 SCM ans = protected_ly_parse_scm (&ps);