]> git.donarmstrong.com Git - lilypond.git/blob - lily/parse-scm.cc
*** empty log message ***
[lilypond.git] / lily / parse-scm.cc
1 /*
2   parse-scm --
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9
10 #include <stdio.h>
11
12 #include "ly-module.hh"
13 #include "lily-guile.hh"
14 #include "main.hh"
15 #include "paper-book.hh"
16 #include "parse-scm.hh"
17 #include "string.hh"
18 #include "source-file.hh"
19
20 /* Pass string to scm parser, evaluate one expression.
21    Return result value and #chars read.
22    
23    Thanks to Gary Houston <ghouston@freewire.co.uk>  */
24 SCM
25 internal_ly_parse_scm (Parse_start * ps)
26 {
27   Source_file *sf =ps->start_location_.source_file_;
28   SCM port = sf->get_port ();
29
30   int off = ps->start_location_.defined_str0_ - sf->to_str0 ();
31   
32   scm_seek (port, scm_long2num (off), scm_long2num (SEEK_SET));
33   SCM from = scm_ftell (port);
34
35   SCM form;
36   SCM answer = SCM_UNSPECIFIED;
37
38   /* Read expression from port.  */
39   if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
40     {
41       if (ps->safe_)
42         {
43           static SCM module = SCM_BOOL_F;
44           if (module == SCM_BOOL_F)
45             {
46               SCM function = ly_scheme_function ("make-safe-lilypond-module");
47               module = scm_call_0 (function);
48             }
49           answer = scm_eval (form, module);
50         }
51       else
52         answer = scm_primitive_eval (form);
53     }
54  
55   /* Reset read_buf for scm_ftell.
56      Shouldn't scm_read () do this for us?  */
57   scm_fill_input (port);
58   SCM to = scm_ftell (port);
59   ps->nchars = scm_to_int (to) - scm_to_int (from);
60
61   /* Don't close the port here; if we re-enter this function via a
62      continuation, then the next time we enter it, we'll get an error.
63      It's a string port anyway, so there's no advantage to closing it
64      early. */
65   // scm_close_port (port);
66
67   return answer;
68 }
69
70 SCM
71 catch_protected_parse_body (void *p)
72 {
73   Parse_start *ps = (Parse_start*) p;
74   
75   return internal_ly_parse_scm (ps);
76 }
77
78 SCM 
79 parse_handler (void *data, SCM tag, SCM args)
80 {
81   Parse_start* ps = (Parse_start *) data;
82   (void) tag;
83   
84   ps->start_location_.error (_("GUILE signaled an error for the expression beginning here"));
85
86   if (scm_ilength (args) > 2)
87     scm_display_error_message (scm_cadr (args), scm_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_UNDEFINED;
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
103
104 #if GUILE_MINOR_VERSION < 7
105   #define READ_ERROR "misc-error"
106   #else
107   #define READ_ERROR "read-error"
108 #endif
109
110 SCM
111 protected_ly_parse_scm (Parse_start *ps)
112 {
113   return scm_internal_catch (ly_symbol2scm (READ_ERROR),
114                              &catch_protected_parse_body,
115                              (void*) ps,
116                              &parse_handler, (void*) ps);
117 }
118
119 bool parse_protect_global = true; 
120
121 /* Try parsing.  Upon failure return SCM_UNDEFINED.
122    FIXME: shouldn't we return SCM_UNSCPECIFIED -- jcn  */
123 SCM
124 ly_parse_scm (char const *s, int *n, Input i, bool safe)
125 {
126   Parse_start ps;
127   ps.str = s;
128   ps.start_location_ = i;
129   ps.safe_ = safe;
130   
131   SCM ans = parse_protect_global ? protected_ly_parse_scm (&ps)
132     : internal_ly_parse_scm (&ps);
133   *n = ps.nchars;
134
135   return ans;  
136 }
137