]> git.donarmstrong.com Git - lilypond.git/blob - lily/parse-scm.cc
* lily/include/lily-guile.hh: many new ly_ functions. Thanks to
[lilypond.git] / lily / parse-scm.cc
1 #include <stdio.h>
2
3 #include "ly-module.hh"
4 #include "lily-guile.hh"
5 #include "main.hh"
6 #include "paper-book.hh"
7 #include "parse-scm.hh"
8 #include "string.hh"
9 #include "source-file.hh"
10
11 /* Pass string to scm parser, evaluate one expression.
12    Return result value and #chars read.
13    
14    Thanks to Gary Houston <ghouston@freewire.co.uk>  */
15 SCM
16 internal_ly_parse_scm (Parse_start * ps, bool safe)
17 {
18   Source_file* sf =ps->start_location_.source_file_;
19   SCM port = sf->get_port ();
20
21   int off = ps->start_location_.defined_str0_ - sf->to_str0();
22   
23   scm_seek (port, scm_long2num (off), scm_long2num (SEEK_SET));
24   SCM from = scm_ftell (port);
25
26   SCM form;
27   SCM answer = SCM_UNSPECIFIED;
28
29   /* Read expression from port */
30   if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
31     {
32       if (safe)
33         {
34           static SCM safe_module;
35           if (!safe_module)
36             {
37               safe_module = scm_primitive_eval (ly_symbol2scm ("safe-module"));
38               if (output_format_global == PAGE_LAYOUT)
39                 ly_import_module (safe_module, scm_c_resolve_module ("lily"));
40             }
41           answer = scm_eval (form, safe_module);
42         }
43       else
44         answer = scm_primitive_eval (form);
45     }
46  
47   /* Reset read_buf for scm_ftell.
48      Shouldn't scm_read () do this for us?  */
49   scm_fill_input (port);
50   SCM to = scm_ftell (port);
51   ps->nchars = ly_scm2int (to) - ly_scm2int (from);
52
53   /* Don't close the port here; if we re-enter this function via a
54      continuation, then the next time we enter it, we'll get an error.
55      It's a string port anyway, so there's no advantage to closing it
56      early. */
57   // scm_close_port (port);
58
59   return answer;
60 }
61
62 SCM
63 catch_protected_parse_body (void *p)
64 {
65   Parse_start *ps = (Parse_start*) p;
66   return internal_ly_parse_scm (ps, false);
67 }
68
69 SCM
70 safe_catch_protected_parse_body (void *p)
71 {
72   Parse_start *ps = (Parse_start*) p;
73   return internal_ly_parse_scm (ps, true);
74 }
75
76 SCM 
77 parse_handler (void * data, SCM tag, SCM args)
78 {
79   Parse_start* ps = (Parse_start*) data;
80   (void) tag;                   // prevent warning
81   
82   ps->start_location_.error (_("GUILE signaled an error for the expression beginning here"));
83
84   if (scm_ilength (args) > 2)
85     scm_display_error_message (ly_cadr (args), ly_caddr (args), scm_current_error_port ());
86
87   /*
88     The following is a kludge; we should probably search for
89     [a-z][0-9] (a note), and start before that.
90    */
91   ps->nchars = 1;
92     
93   return SCM_UNDEFINED;
94 }
95
96 /*
97   Do some magical incantations: if not, lily will exit on the first
98   GUILE error, leaving no location trace. 
99  */
100
101
102 #if GUILE_MINOR_VERSION < 7
103   #define READ_ERROR "misc-error"
104   #else
105   #define READ_ERROR "read-error"
106 #endif
107
108 SCM
109 protected_ly_parse_scm (Parse_start *ps, bool safe)
110 {
111   return scm_internal_catch (ly_symbol2scm (READ_ERROR),
112                              (safe ? &safe_catch_protected_parse_body
113                               : catch_protected_parse_body),
114                              (void*) ps,
115                              &parse_handler, (void*) ps);
116 }
117
118 bool parse_protect_global = true; 
119
120 /* Try parsing.  Upon failure return SCM_UNDEFINED. */
121 SCM
122 ly_parse_scm (char const* s, int *n, Input i, bool safe)
123 {
124   Parse_start ps ;
125   ps.str = s;
126   ps.start_location_ = i;
127
128   SCM ans = parse_protect_global ? protected_ly_parse_scm (&ps, safe)
129     : internal_ly_parse_scm (&ps, safe);
130   *n = ps.nchars;
131
132   return ans;  
133 }
134