]> git.donarmstrong.com Git - lilypond.git/blob - lily/parse-scm.cc
*** empty log message ***
[lilypond.git] / lily / parse-scm.cc
1 /*
2   parse-scm -- Parse a single SCM expression exactly.
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "parse-scm.hh"
10
11 #include <cstdio>
12 using namespace std;
13
14 #include "international.hh"
15 #include "main.hh"
16 #include "paper-book.hh"
17 #include "source-file.hh"
18
19 /* Pass string to scm parser, evaluate one expression.
20    Return result value and #chars read.
21
22    Thanks to Gary Houston <ghouston@freewire.co.uk>  */
23 SCM
24 internal_ly_parse_scm (Parse_start *ps)
25 {
26   Source_file *sf = ps->start_location_.get_source_file ();
27   SCM port = sf->get_port ();
28
29   int off = ps->start_location_.start () - sf->c_str ();
30
31   scm_seek (port, scm_long2num (off), scm_long2num (SEEK_SET));
32   SCM from = scm_ftell (port);
33
34   SCM answer = SCM_UNSPECIFIED;
35   SCM form = scm_read (port);
36
37   /* Read expression from port.  */
38   if (!SCM_EOF_OBJECT_P (form))
39     {
40       if (ps->safe_)
41         {
42           static SCM module = SCM_BOOL_F;
43           if (module == SCM_BOOL_F)
44             {
45               SCM function = ly_lily_module_constant ("make-safe-lilypond-module");
46               module = scm_call_0 (function);
47             }
48           answer = scm_eval (form, module);
49         }
50       else
51         answer = scm_primitive_eval (form);
52     }
53
54   /* Reset read_buf for scm_ftell.
55      Shouldn't scm_read () do this for us?  */
56   scm_fill_input (port);
57   SCM to = scm_ftell (port);
58   ps->nchars = scm_to_int (to) - scm_to_int (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   // scm_close_port (port);
65
66   return answer;
67 }
68
69 SCM
70 catch_protected_parse_body (void *p)
71 {
72   Parse_start *ps = (Parse_start *) p;
73
74   return internal_ly_parse_scm (ps);
75 }
76
77 SCM
78 parse_handler (void *data, SCM tag, SCM args)
79 {
80   Parse_start *ps = (Parse_start *) data;
81   (void) tag;
82
83   ps->start_location_.error (_ ("GUILE signaled an error for the expression beginning here"));
84
85   if (scm_ilength (args) > 2)
86     scm_display_error_message (scm_cadr (args), scm_caddr (args), scm_current_error_port ());
87
88   /*
89     The following is a kludge; we should probably search for
90     [a-z][0-9] (a note), and start before that.
91   */
92   ps->nchars = 1;
93
94   return SCM_UNDEFINED;
95 }
96
97 /*
98   Do some magical incantations: if not, lily will exit on the first
99   GUILE error, leaving no location trace.
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