]> git.donarmstrong.com Git - lilypond.git/blob - lily/parse-scm.cc
(sane_putenv): Oops. Should supply a private copy
[lilypond.git] / lily / parse-scm.cc
1
2 #include "lily-guile.hh"
3 #include "parse-scm.hh"
4 #include "string.hh"
5
6
7 /*
8   Pass string to scm parser, evaluate one expression.
9   Return result value and #chars read.
10
11   Thanks to Gary Houston <ghouston@freewire.co.uk>
12
13   Need guile-1.3.4 (>1.3 anyway) for ftell on str ports -- jcn
14 */
15 SCM
16 internal_ly_parse_scm (Parse_start * ps)
17 {
18   /*
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.
21    */
22   SCM str = ly_str02scm (ps->str);
23   SCM port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG,
24                             "ly_eval_scm_0str");
25
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()));
29   
30   SCM from = scm_ftell (port);
31
32   SCM form;
33   SCM answer = SCM_UNSPECIFIED;
34
35   /* Read expression from port */
36   if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
37     answer = scm_primitive_eval (form);
38  
39   /*
40    After parsing
41
42  (begin (foo 1 2))
43
44    all seems fine, but after parsing
45
46  (foo 1 2)
47
48    read_buf has been advanced to read_pos - 1,
49    so that scm_ftell returns 1, instead of #parsed chars
50    */
51   
52   /*
53     urg: reset read_buf for scm_ftell
54     shouldn't scm_read () do this for us?
55   */
56   scm_fill_input (port);
57   SCM to = scm_ftell (port);
58   ps->nchars = gh_scm2int (to) - gh_scm2int (from);
59
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
63      early.
64
65      scm_close_port (port);
66   */
67
68   return answer;
69 }
70
71
72 SCM
73 catch_protected_parse_body (void *p)
74 {
75   Parse_start *ps = (Parse_start*) p;
76   return internal_ly_parse_scm (ps);
77 }
78
79 SCM 
80 parse_handler (void * data, SCM tag, SCM args)
81 {
82   Parse_start* ps = (Parse_start*) data;
83
84   ps->start_location.error (_("GUILE signaled an error for the expression begining here"));
85
86   if (scm_ilength (args) > 2)
87     scm_display_error_message (gh_cadr (args), gh_caddr(args), scm_current_error_port());
88
89   /*
90     The following is a kludge; we should probably search for
91     [a-z][0-9] (a note), and start before that.
92    */
93   ps->nchars = 1;
94     
95   return SCM_EOL;
96 }
97
98 /*
99   Do some magical incantations: if not, lily will exit on the first
100   GUILE error, leaving no location trace. 
101  */
102 SCM
103 protected_ly_parse_scm (Parse_start *ps)
104 {
105   return scm_internal_catch (ly_symbol2scm ("misc-error"), &catch_protected_parse_body,
106                              (void*)ps,
107                              &parse_handler, (void*)ps);
108
109 }
110
111
112 SCM
113 ly_parse_scm (char const* s, int *n, Input i)
114 {
115   Parse_start ps ;
116   ps.str = s;
117   ps.start_location = i;
118
119   SCM ans = protected_ly_parse_scm (&ps);
120   *n = ps.nchars;
121   return ans;  
122 }
123