]> git.donarmstrong.com Git - lilypond.git/blob - lily/parse-scm.cc
* buildscripts/guile-gnome.sh: Build without gcc libtool version
[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       SCM function = ly_scheme_function ("make-safe-lilypond-module");
42       if (ps->safe_)
43         answer = scm_eval (form, function);
44       else
45         answer = scm_primitive_eval (form);
46     }
47  
48   /* Reset read_buf for scm_ftell.
49      Shouldn't scm_read () do this for us?  */
50   scm_fill_input (port);
51   SCM to = scm_ftell (port);
52   ps->nchars = ly_scm2int (to) - ly_scm2int (from);
53
54   /* Don't close the port here; if we re-enter this function via a
55      continuation, then the next time we enter it, we'll get an error.
56      It's a string port anyway, so there's no advantage to closing it
57      early. */
58   // scm_close_port (port);
59
60   return answer;
61 }
62
63 SCM
64 catch_protected_parse_body (void *p)
65 {
66   Parse_start *ps = (Parse_start*) p;
67   
68   return internal_ly_parse_scm (ps);
69 }
70
71 SCM 
72 parse_handler (void *data, SCM tag, SCM args)
73 {
74   Parse_start* ps = (Parse_start *) data;
75   (void) tag;
76   
77   ps->start_location_.error (_("GUILE signaled an error for the expression beginning here"));
78
79   if (scm_ilength (args) > 2)
80     scm_display_error_message (ly_cadr (args), ly_caddr (args), scm_current_error_port ());
81
82   /*
83     The following is a kludge; we should probably search for
84     [a-z][0-9] (a note), and start before that.
85    */
86   ps->nchars = 1;
87     
88   return SCM_UNDEFINED;
89 }
90
91 /*
92   Do some magical incantations: if not, lily will exit on the first
93   GUILE error, leaving no location trace. 
94  */
95
96
97 #if GUILE_MINOR_VERSION < 7
98   #define READ_ERROR "misc-error"
99   #else
100   #define READ_ERROR "read-error"
101 #endif
102
103 SCM
104 protected_ly_parse_scm (Parse_start *ps)
105 {
106   return scm_internal_catch (ly_symbol2scm (READ_ERROR),
107                              &catch_protected_parse_body,
108                              (void*) ps,
109                              &parse_handler, (void*) ps);
110 }
111
112 bool parse_protect_global = true; 
113
114 /* Try parsing.  Upon failure return SCM_UNDEFINED.
115    FIXME: shouldn't we return SCM_UNSCPECIFIED -- jcn  */
116 SCM
117 ly_parse_scm (char const *s, int *n, Input i, bool safe)
118 {
119   Parse_start ps;
120   ps.str = s;
121   ps.start_location_ = i;
122   ps.safe_ = safe;
123   
124   SCM ans = parse_protect_global ? protected_ly_parse_scm (&ps)
125     : internal_ly_parse_scm (&ps);
126   *n = ps.nchars;
127
128   return ans;  
129 }
130