]> git.donarmstrong.com Git - lilypond.git/blob - lily/parse-scm.cc
Merge commit 'origin' into beamlets2
[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--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "parse-scm.hh"
10
11 #include <cstdio>
12 using namespace std;
13
14 #include "lily-parser.hh"
15 #include "international.hh"
16 #include "main.hh"
17 #include "paper-book.hh"
18 #include "source-file.hh"
19
20 /* Pass string to scm parser, evaluate one expression.
21    Return result value and #chars read.
22
23    Thanks to Gary Houston <ghouston@freewire.co.uk>  */
24 SCM
25 internal_ly_parse_scm (Parse_start *ps)
26 {
27   Source_file *sf = ps->start_location_.get_source_file ();
28   SCM port = sf->get_port ();
29
30   int off = ps->start_location_.start () - sf->c_str ();
31
32   scm_seek (port, scm_long2num (off), scm_long2num (SEEK_SET));
33   SCM from = scm_ftell (port);
34
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));
37   
38   SCM answer = SCM_UNSPECIFIED;
39   SCM form = scm_read (port);
40
41   SCM to = scm_ftell (port);
42   ps->nchars = scm_to_int (to) - scm_to_int (from);
43   
44
45   /* Read expression from port.  */
46   if (!SCM_EOF_OBJECT_P (form))
47     {
48       if (ps->safe_)
49         {
50           static SCM module = SCM_BOOL_F;
51           if (module == SCM_BOOL_F)
52             {
53               SCM function = ly_lily_module_constant ("make-safe-lilypond-module");
54               module = scm_call_0 (function);
55             }
56           
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);
63         }
64       else
65         answer = scm_primitive_eval (form);
66     }
67
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);
73
74   return answer;
75 }
76
77 SCM
78 catch_protected_parse_body (void *p)
79 {
80   Parse_start *ps = (Parse_start *) p;
81
82   return internal_ly_parse_scm (ps);
83 }
84
85 SCM
86 parse_handler (void *data, SCM tag, SCM args)
87 {
88   Parse_start *ps = (Parse_start *) data;
89
90   ps->start_location_.error (_ ("GUILE signaled an error for the expression beginning here"));
91
92   if (scm_ilength (args) > 2)
93     scm_display_error_message (scm_cadr (args), scm_caddr (args), scm_current_error_port ());
94
95   if (tag == ly_symbol2scm ("read-error"))
96     ps->nchars = 1;
97
98   return SCM_UNDEFINED;
99 }
100
101 SCM
102 protected_ly_parse_scm (Parse_start *ps)
103 {
104   /*
105     Catch #t : catch all Scheme level errors.
106    */
107   return scm_internal_catch (SCM_BOOL_T, 
108                              &catch_protected_parse_body,
109                              (void *) ps,
110                              &parse_handler, (void *) ps);
111 }
112
113 bool parse_protect_global = true;
114 bool parsed_objects_should_be_dead = false;
115
116 /* Try parsing.  Upon failure return SCM_UNDEFINED.
117    FIXME: shouldn't we return SCM_UNSCPECIFIED -- jcn  */
118 SCM
119 ly_parse_scm (char const *s, int *n, Input i, bool safe, Lily_parser *parser)
120 {
121   Parse_start ps;
122   ps.str = s;
123   ps.start_location_ = i;
124   ps.safe_ = safe;
125   ps.parser_ = parser;
126
127   SCM ans = parse_protect_global ? protected_ly_parse_scm (&ps)
128     : internal_ly_parse_scm (&ps);
129   *n = ps.nchars;
130
131   return ans;
132 }
133