]> git.donarmstrong.com Git - lilypond.git/blob - lily/parse-scm.cc
* configure.in: Test for and accept lmodern if EC fonts not found.
[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 #include "parse-scm.hh"
10
11 #include <cstdio>
12
13 #include "ly-module.hh"
14 #include "main.hh"
15 #include "paper-book.hh"
16 #include "source-file.hh"
17
18 /* Pass string to scm parser, evaluate one expression.
19    Return result value and #chars read.
20    
21    Thanks to Gary Houston <ghouston@freewire.co.uk>  */
22 SCM
23 internal_ly_parse_scm (Parse_start * ps)
24 {
25   Source_file *sf =ps->start_location_.source_file_;
26   SCM port = sf->get_port ();
27
28   int off = ps->start_location_.defined_str0_ - sf->to_str0 ();
29   
30   scm_seek (port, scm_long2num (off), scm_long2num (SEEK_SET));
31   SCM from = scm_ftell (port);
32
33   SCM form;
34   SCM answer = SCM_UNSPECIFIED;
35
36   /* Read expression from port.  */
37   if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
38     {
39       if (ps->safe_)
40         {
41           static SCM module = SCM_BOOL_F;
42           if (module == SCM_BOOL_F)
43             {
44               SCM function = ly_scheme_function ("make-safe-lilypond-module");
45               module = scm_call_0 (function);
46             }
47           answer = scm_eval (form, module);
48         }
49       else
50         answer = scm_primitive_eval (form);
51     }
52  
53   /* Reset read_buf for scm_ftell.
54      Shouldn't scm_read () do this for us?  */
55   scm_fill_input (port);
56   SCM to = scm_ftell (port);
57   ps->nchars = scm_to_int (to) - scm_to_int (from);
58
59   /* Don't close the port here; if we re-enter this function via a
60      continuation, then the next time we enter it, we'll get an error.
61      It's a string port anyway, so there's no advantage to closing it
62      early. */
63   // scm_close_port (port);
64
65   return answer;
66 }
67
68 SCM
69 catch_protected_parse_body (void *p)
70 {
71   Parse_start *ps = (Parse_start*) p;
72   
73   return internal_ly_parse_scm (ps);
74 }
75
76 SCM 
77 parse_handler (void *data, SCM tag, SCM args)
78 {
79   Parse_start* ps = (Parse_start *) data;
80   (void) tag;
81   
82   ps->start_location_.error (_("GUILE signaled an error for the expression beginning here"));
83
84   if (scm_ilength (args) > 2)
85     scm_display_error_message (scm_cadr (args), scm_caddr (args), scm_current_error_port ());
86
87   /*
88     The following is a kludge; we should probably search for
89     [a-z][0-9] (a note), and start before that.
90    */
91   ps->nchars = 1;
92     
93   return SCM_UNDEFINED;
94 }
95
96 /*
97   Do some magical incantations: if not, lily will exit on the first
98   GUILE error, leaving no location trace. 
99  */
100
101
102 #if GUILE_MINOR_VERSION < 7
103   #define READ_ERROR "misc-error"
104   #else
105   #define READ_ERROR "read-error"
106 #endif
107
108 SCM
109 protected_ly_parse_scm (Parse_start *ps)
110 {
111   return scm_internal_catch (ly_symbol2scm (READ_ERROR),
112                              &catch_protected_parse_body,
113                              (void*) ps,
114                              &parse_handler, (void*) ps);
115 }
116
117 bool parse_protect_global = true; 
118
119 /* Try parsing.  Upon failure return SCM_UNDEFINED.
120    FIXME: shouldn't we return SCM_UNSCPECIFIED -- jcn  */
121 SCM
122 ly_parse_scm (char const *s, int *n, Input i, bool safe)
123 {
124   Parse_start ps;
125   ps.str = s;
126   ps.start_location_ = i;
127   ps.safe_ = safe;
128   
129   SCM ans = parse_protect_global ? protected_ly_parse_scm (&ps)
130     : internal_ly_parse_scm (&ps);
131   *n = ps.nchars;
132
133   return ans;  
134 }
135