]> git.donarmstrong.com Git - lilypond.git/blob - lily/parse-scm.cc
Run `make grand-replace'.
[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   /* 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           
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);
66         }
67       else
68         answer = scm_primitive_eval (form);
69     }
70
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);
76
77   return answer;
78 }
79
80 SCM
81 catch_protected_parse_body (void *p)
82 {
83   Parse_start *ps = (Parse_start *) p;
84
85   return internal_ly_parse_scm (ps);
86 }
87
88 SCM
89 parse_handler (void *data, SCM tag, SCM args)
90 {
91   Parse_start *ps = (Parse_start *) data;
92
93   ps->start_location_.error (_ ("GUILE signaled an error for the expression beginning here"));
94
95   if (scm_ilength (args) > 2)
96     scm_display_error_message (scm_cadr (args), scm_caddr (args), scm_current_error_port ());
97
98   if (tag == ly_symbol2scm ("read-error"))
99     ps->nchars = 1;
100
101   return SCM_UNDEFINED;
102 }
103
104 SCM
105 protected_ly_parse_scm (Parse_start *ps)
106 {
107   /*
108     Catch #t : catch all Scheme level errors.
109    */
110   return scm_internal_catch (SCM_BOOL_T, 
111                              &catch_protected_parse_body,
112                              (void *) ps,
113                              &parse_handler, (void *) ps);
114 }
115
116 bool parse_protect_global = true;
117 bool parsed_objects_should_be_dead = false;
118
119 /* Try parsing.  Upon failure return SCM_UNDEFINED.
120    FIXME: shouldn't we return SCM_UNSCPECIFIED -- jcn  */
121 SCM
122 ly_parse_scm (char const *s, int *n, Input i, bool safe, Lily_parser *parser)
123 {
124   Parse_start ps;
125   ps.str = s;
126   ps.start_location_ = i;
127   ps.safe_ = safe;
128   ps.parser_ = parser;
129
130   SCM ans = parse_protect_global ? protected_ly_parse_scm (&ps)
131     : internal_ly_parse_scm (&ps);
132   *n = ps.nchars;
133
134   return ans;
135 }
136