]> git.donarmstrong.com Git - lilypond.git/blob - lily/parse-scm.cc
set column & line numbers for embedded-Scheme reads.
[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--2007 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_set_port_line_x (port,  scm_from_int (ps->start_location_.line_number () -1));
35   scm_set_port_column_x (port,  scm_from_int (ps->start_location_.column_number () -1));
36   
37   SCM answer = SCM_UNSPECIFIED;
38   SCM form = scm_read (port);
39
40   /* Read expression from port.  */
41   if (!SCM_EOF_OBJECT_P (form))
42     {
43       if (ps->safe_)
44         {
45           static SCM module = SCM_BOOL_F;
46           if (module == SCM_BOOL_F)
47             {
48               SCM function = ly_lily_module_constant ("make-safe-lilypond-module");
49               module = scm_call_0 (function);
50             }
51           answer = scm_eval (form, module);
52         }
53       else
54         answer = scm_primitive_eval (form);
55     }
56
57   /* Reset read_buf for scm_ftell.
58      Shouldn't scm_read () do this for us?  */
59   scm_fill_input (port);
60   SCM to = scm_ftell (port);
61   ps->nchars = scm_to_int (to) - scm_to_int (from);
62
63   /* Don't close the port here; if we re-enter this function via a
64      continuation, then the next time we enter it, we'll get an error.
65      It's a string port anyway, so there's no advantage to closing it
66      early. */
67   // scm_close_port (port);
68
69   return answer;
70 }
71
72 SCM
73 catch_protected_parse_body (void *p)
74 {
75   Parse_start *ps = (Parse_start *) p;
76
77   return internal_ly_parse_scm (ps);
78 }
79
80 SCM
81 parse_handler (void *data, SCM tag, SCM args)
82 {
83   Parse_start *ps = (Parse_start *) data;
84   (void) tag;
85
86   ps->start_location_.error (_ ("GUILE signaled an error for the expression beginning here"));
87
88   if (scm_ilength (args) > 2)
89     scm_display_error_message (scm_cadr (args), scm_caddr (args), scm_current_error_port ());
90
91   /*
92     The following is a kludge; we should probably search for
93     [a-z][0-9] (a note), and start before that.
94   */
95   ps->nchars = 1;
96
97   return SCM_UNDEFINED;
98 }
99
100 SCM
101 protected_ly_parse_scm (Parse_start *ps)
102 {
103   /*
104     Catch #t : catch all Scheme level errors.
105    */
106   return scm_internal_catch (SCM_BOOL_T, 
107                              &catch_protected_parse_body,
108                              (void *) ps,
109                              &parse_handler, (void *) ps);
110 }
111
112 bool parse_protect_global = true;
113 bool parsed_objects_should_be_dead = false;
114
115 /* Try parsing.  Upon failure return SCM_UNDEFINED.
116    FIXME: shouldn't we return SCM_UNSCPECIFIED -- jcn  */
117 SCM
118 ly_parse_scm (char const *s, int *n, Input i, bool safe)
119 {
120   Parse_start ps;
121   ps.str = s;
122   ps.start_location_ = i;
123   ps.safe_ = safe;
124
125   SCM ans = parse_protect_global ? protected_ly_parse_scm (&ps)
126     : internal_ly_parse_scm (&ps);
127   *n = ps.nchars;
128
129   return ans;
130 }
131