Fix InstrumentSwitch grob definition.
[lilypond.git] / lily / parse-scm.cc
blob087d2e5951e09940c5aa9fecd947c0111ed6911e
1 /*
2 parse-scm -- Parse a single SCM expression exactly.
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
9 #include "parse-scm.hh"
11 #include <cstdio>
12 using namespace std;
14 #include "lily-parser.hh"
15 #include "international.hh"
16 #include "main.hh"
17 #include "paper-book.hh"
18 #include "source-file.hh"
20 /* Pass string to scm parser, evaluate one expression.
21 Return result value and #chars read.
23 Thanks to Gary Houston <ghouston@freewire.co.uk> */
24 SCM
25 internal_ly_parse_scm (Parse_start *ps)
27 Source_file *sf = ps->start_location_.get_source_file ();
28 SCM port = sf->get_port ();
30 int off = ps->start_location_.start () - sf->c_str ();
32 scm_seek (port, scm_long2num (off), scm_long2num (SEEK_SET));
33 SCM from = scm_ftell (port);
35 scm_set_port_line_x (port, scm_from_int (ps->start_location_.line_number () -1));
36 scm_set_port_column_x (port, scm_from_int (ps->start_location_.column_number () -1));
38 SCM answer = SCM_UNSPECIFIED;
39 SCM form = scm_read (port);
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);
48 /* Read expression from port. */
49 if (!SCM_EOF_OBJECT_P (form))
51 if (ps->safe_)
53 static SCM module = SCM_BOOL_F;
54 if (module == SCM_BOOL_F)
56 SCM function = ly_lily_module_constant ("make-safe-lilypond-module");
57 module = scm_call_0 (function);
60 // We define the parser so trusted Scheme functions can
61 // access the real namespace underlying the parser.
62 if (ps->parser_)
63 scm_module_define (module, ly_symbol2scm ("parser"),
64 ps->parser_->self_scm());
65 answer = scm_eval (form, module);
67 else
68 answer = scm_primitive_eval (form);
71 /* Don't close the port here; if we re-enter this function via a
72 continuation, then the next time we enter it, we'll get an error.
73 It's a string port anyway, so there's no advantage to closing it
74 early. */
75 // scm_close_port (port);
77 return answer;
80 SCM
81 catch_protected_parse_body (void *p)
83 Parse_start *ps = (Parse_start *) p;
85 return internal_ly_parse_scm (ps);
88 SCM
89 parse_handler (void *data, SCM tag, SCM args)
91 Parse_start *ps = (Parse_start *) data;
93 ps->start_location_.error (_ ("GUILE signaled an error for the expression beginning here"));
95 if (scm_ilength (args) > 2)
96 scm_display_error_message (scm_cadr (args), scm_caddr (args), scm_current_error_port ());
98 if (tag == ly_symbol2scm ("read-error"))
99 ps->nchars = 1;
101 return SCM_UNDEFINED;
105 protected_ly_parse_scm (Parse_start *ps)
108 Catch #t : catch all Scheme level errors.
110 return scm_internal_catch (SCM_BOOL_T,
111 &catch_protected_parse_body,
112 (void *) ps,
113 &parse_handler, (void *) ps);
116 bool parse_protect_global = true;
117 bool parsed_objects_should_be_dead = false;
119 /* Try parsing. Upon failure return SCM_UNDEFINED.
120 FIXME: shouldn't we return SCM_UNSCPECIFIED -- jcn */
122 ly_parse_scm (char const *s, int *n, Input i, bool safe, Lily_parser *parser)
124 Parse_start ps;
125 ps.str = s;
126 ps.start_location_ = i;
127 ps.safe_ = safe;
128 ps.parser_ = parser;
130 SCM ans = parse_protect_global ? protected_ly_parse_scm (&ps)
131 : internal_ly_parse_scm (&ps);
132 *n = ps.nchars;
134 return ans;