]> git.donarmstrong.com Git - lilypond.git/blob - lily/parse-scm.cc
ly- -> ly:
[lilypond.git] / lily / parse-scm.cc
1 #include <stdio.h>
2
3 #include "lily-guile.hh"
4 #include "parse-scm.hh"
5 #include "string.hh"
6 #include "source-file.hh"
7
8 /*
9   Pass string to scm parser, evaluate one expression.
10   Return result value and #chars read.
11
12   Thanks to Gary Houston <ghouston@freewire.co.uk>
13
14   Need guile-1.3.4 (>1.3 anyway) for ftell on str ports -- jcn
15 */
16 SCM
17 internal_ly_parse_scm (Parse_start * ps)
18 {
19   Source_file* sf =ps->start_location_.source_file_;
20   SCM port = sf->get_port();
21
22   int off = ps->start_location_.defined_str0_ - sf->to_str0();
23   
24   scm_seek (port, scm_long2num (off), scm_long2num (SEEK_SET));
25   SCM from = scm_ftell (port);
26
27   SCM form;
28   SCM answer = SCM_UNSPECIFIED;
29
30   /* Read expression from port */
31   if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
32     answer = scm_primitive_eval (form);
33  
34   /*
35    After parsing
36
37  (begin (foo 1 2))
38
39    all seems fine, but after parsing
40
41  (foo 1 2)
42
43    read_buf has been advanced to read_pos - 1,
44    so that scm_ftell returns 1, instead of #parsed chars
45    */
46   
47   /*
48     urg: reset read_buf for scm_ftell
49     shouldn't scm_read () do this for us?
50   */
51   scm_fill_input (port);
52   SCM to = scm_ftell (port);
53   ps->nchars = gh_scm2int (to) - gh_scm2int (from);
54
55   /* Don't close the port here; if we re-enter this function via a
56      continuation, then the next time we enter it, we'll get an error.
57      It's a string port anyway, so there's no advantage to closing it
58      early.
59
60      scm_close_port (port);
61   */
62
63   return answer;
64 }
65
66
67 SCM
68 catch_protected_parse_body (void *p)
69 {
70   Parse_start *ps = (Parse_start*) p;
71   return internal_ly_parse_scm (ps);
72 }
73
74 SCM 
75 parse_handler (void * data, SCM tag, SCM args)
76 {
77   Parse_start* ps = (Parse_start*) data;
78
79   ps->start_location_.error (_("GUILE signaled an error for the expression begining here"));
80
81   if (scm_ilength (args) > 2)
82     scm_display_error_message (gh_cadr (args), gh_caddr(args), scm_current_error_port());
83
84   /*
85     The following is a kludge; we should probably search for
86     [a-z][0-9] (a note), and start before that.
87    */
88   ps->nchars = 1;
89     
90   return SCM_EOL;
91 }
92
93 /*
94   Do some magical incantations: if not, lily will exit on the first
95   GUILE error, leaving no location trace. 
96  */
97
98
99 #if GUILE_MINOR_VERSION < 7
100   #define READ_ERROR "misc-error"
101   #else
102   #define READ_ERROR "read-error"
103 #endif
104
105 SCM
106 protected_ly_parse_scm (Parse_start *ps)
107 {
108   return scm_internal_catch (ly_symbol2scm (READ_ERROR),
109                              &catch_protected_parse_body,
110                              (void*)ps,
111                              &parse_handler, (void*)ps);
112
113 }
114
115
116 static bool protect = true;
117
118 LY_DEFINE(set_parse_protect, "ly:set-parse-protect",
119           1,0,0, (SCM t),
120           "If protection is switched on, errors in inline scheme are caught.
121 If off, GUILE will halt on errors, and give a stack trace. Default is protected evaluation.")
122 {
123   protect =  (t == SCM_BOOL_T);
124   return SCM_UNSPECIFIED;
125 }
126
127 SCM
128 ly_parse_scm (char const* s, int *n, Input i)
129 {
130   
131   Parse_start ps ;
132   ps.str = s;
133   ps.start_location_ = i;
134
135   SCM ans = protect ? protected_ly_parse_scm (&ps)
136     : internal_ly_parse_scm (&ps);
137   *n = ps.nchars;
138   return ans;  
139 }
140