Use scalar instead of embedded_scm for context mod overrides.
[lilypond/mpolesky.git] / lily / parse-scm.cc
blobd1dad300e37a0b61169010bf17343f5b687bb7de
1 /*
2 parse-scm -- Parse a single SCM expression exactly.
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2009 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 SCM to = scm_ftell (port);
42 ps->nchars = scm_to_int (to) - scm_to_int (from);
45 /* Read expression from port. */
46 if (!SCM_EOF_OBJECT_P (form))
48 if (ps->safe_)
50 static SCM module = SCM_BOOL_F;
51 if (module == SCM_BOOL_F)
53 SCM function = ly_lily_module_constant ("make-safe-lilypond-module");
54 module = scm_call_0 (function);
57 // We define the parser so trusted Scheme functions can
58 // access the real namespace underlying the parser.
59 if (ps->parser_)
60 scm_module_define (module, ly_symbol2scm ("parser"),
61 ps->parser_->self_scm());
62 answer = scm_eval (form, module);
64 else
65 answer = scm_primitive_eval (form);
68 /* Don't close the port here; if we re-enter this function via a
69 continuation, then the next time we enter it, we'll get an error.
70 It's a string port anyway, so there's no advantage to closing it
71 early. */
72 // scm_close_port (port);
74 return answer;
77 SCM
78 catch_protected_parse_body (void *p)
80 Parse_start *ps = (Parse_start *) p;
82 return internal_ly_parse_scm (ps);
85 SCM
86 parse_handler (void *data, SCM tag, SCM args)
88 Parse_start *ps = (Parse_start *) data;
90 ps->start_location_.error (_ ("GUILE signaled an error for the expression beginning here"));
92 if (scm_ilength (args) > 2)
93 scm_display_error_message (scm_cadr (args), scm_caddr (args), scm_current_error_port ());
95 if (tag == ly_symbol2scm ("read-error"))
96 ps->nchars = 1;
98 return SCM_UNDEFINED;
102 protected_ly_parse_scm (Parse_start *ps)
105 Catch #t : catch all Scheme level errors.
107 return scm_internal_catch (SCM_BOOL_T,
108 &catch_protected_parse_body,
109 (void *) ps,
110 &parse_handler, (void *) ps);
113 bool parse_protect_global = true;
114 bool parsed_objects_should_be_dead = false;
116 /* Try parsing. Upon failure return SCM_UNDEFINED.
117 FIXME: shouldn't we return SCM_UNSCPECIFIED -- jcn */
119 ly_parse_scm (char const *s, int *n, Input i, bool safe, Lily_parser *parser)
121 Parse_start ps;
122 ps.str = s;
123 ps.start_location_ = i;
124 ps.safe_ = safe;
125 ps.parser_ = parser;
127 SCM ans = parse_protect_global ? protected_ly_parse_scm (&ps)
128 : internal_ly_parse_scm (&ps);
129 *n = ps.nchars;
131 return ans;