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