]> git.donarmstrong.com Git - lilypond.git/blob - lily/parse-scm.cc
(ly_module_define): only define variable if
[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)
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         answer = scm_primitive_eval (form);
33     }
34  
35   /* Reset read_buf for scm_ftell.
36      Shouldn't scm_read () do this for us?  */
37   scm_fill_input (port);
38   SCM to = scm_ftell (port);
39   ps->nchars = ly_scm2int (to) - ly_scm2int (from);
40
41   /* Don't close the port here; if we re-enter this function via a
42      continuation, then the next time we enter it, we'll get an error.
43      It's a string port anyway, so there's no advantage to closing it
44      early. */
45   // scm_close_port (port);
46
47   return answer;
48 }
49
50 SCM
51 catch_protected_parse_body (void *p)
52 {
53   Parse_start *ps = (Parse_start*) p;
54   return internal_ly_parse_scm (ps);
55 }
56
57 SCM
58 safe_catch_protected_parse_body (void *p)
59 {
60   Parse_start *ps = (Parse_start*) p;
61   return internal_ly_parse_scm (ps);
62 }
63
64 SCM 
65 parse_handler (void * data, SCM tag, SCM args)
66 {
67   Parse_start* ps = (Parse_start*) data;
68   (void) tag;                   // prevent warning
69   
70   ps->start_location_.error (_("GUILE signaled an error for the expression beginning here"));
71
72   if (scm_ilength (args) > 2)
73     scm_display_error_message (ly_cadr (args), ly_caddr (args), scm_current_error_port ());
74
75   /*
76     The following is a kludge; we should probably search for
77     [a-z][0-9] (a note), and start before that.
78    */
79   ps->nchars = 1;
80     
81   return SCM_UNDEFINED;
82 }
83
84 /*
85   Do some magical incantations: if not, lily will exit on the first
86   GUILE error, leaving no location trace. 
87  */
88
89
90 #if GUILE_MINOR_VERSION < 7
91   #define READ_ERROR "misc-error"
92   #else
93   #define READ_ERROR "read-error"
94 #endif
95
96 SCM
97 protected_ly_parse_scm (Parse_start *ps)
98 {
99   return scm_internal_catch (ly_symbol2scm (READ_ERROR),
100                              &catch_protected_parse_body,
101                              (void*) ps,
102                              &parse_handler, (void*) ps);
103 }
104
105 bool parse_protect_global = true; 
106
107 /* Try parsing.  Upon failure return SCM_UNDEFINED. */
108 SCM
109 ly_parse_scm (char const* s, int *n, Input i, bool safe)
110 {
111   Parse_start ps ;
112   ps.str = s;
113   ps.start_location_ = i;
114
115   SCM ans = parse_protect_global ? protected_ly_parse_scm (&ps)
116     : internal_ly_parse_scm (&ps);
117   *n = ps.nchars;
118
119   return ans;  
120 }
121