]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser-scheme.cc
Merge commit 'origin/dev/jneeman' into systems-per-page
[lilypond.git] / lily / lily-parser-scheme.cc
1 /*
2   lily-parser-scheme.cc -- implement Lily_parser bindings
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include <unistd.h>
10
11 #include "lily-parser.hh"
12
13 #include "file-name-map.hh"
14 #include "file-name.hh"
15 #include "file-path.hh"
16 #include "international.hh"
17 #include "lily-lexer.hh"
18 #include "ly-module.hh"
19 #include "main.hh"
20 #include "program-option.hh"
21 #include "sources.hh"
22 #include "warn.hh"
23
24 /* Do not append `!' suffix, since 1st argument is not modified. */
25 LY_DEFINE (ly_set_point_and_click, "ly:set-point-and-click",
26            1, 0, 0, (SCM what),
27            "Deprecated.")
28 {
29   (void) what;
30   warning (_f ("deprecated function called: %s", "ly:set-point-and-click"));
31   return SCM_UNSPECIFIED;
32 }
33
34 LY_DEFINE (ly_parse_file, "ly:parse-file",
35            1, 0, 0, (SCM name),
36            "Parse a single @code{.ly} file."
37            "  Upon failure, throw @code{ly-file-failed} key.")
38 {
39   LY_ASSERT_TYPE (scm_is_string, name, 1);
40   string file = ly_scm2string (name);
41   char const *extensions[] = {"ly", "", 0};
42
43   string file_name = global_path.find (file, extensions);
44
45   /* By default, use base name of input file for output file name,
46      write output to cwd; do not use root and directory parts of input
47      file name.  */
48   File_name out_file_name (file_name);
49
50   out_file_name.ext_ = "";
51   out_file_name.root_ = "";
52   if (ly_get_option (ly_symbol2scm ("gui")) != SCM_BOOL_T
53       && ly_get_option (ly_symbol2scm ("strip-output-dir")) == SCM_BOOL_T) {
54     out_file_name.dir_ = "";
55   }
56   
57   /* When running from gui, generate output in .ly source directory.  */
58   string output_name = output_name_global;
59   if (!output_name.empty ())
60     {
61       /* Interpret --output=DIR to mean --output=DIR/BASE.  */
62       string dir;
63       if (is_dir (output_name))
64         {
65           dir = output_name;
66           output_name = "";
67         }
68       else
69         {
70           File_name out (output_name);
71           if (is_dir (out.dir_part ()))
72             {
73               dir = out.dir_part ();
74               out_file_name = out.file_part ();
75             }
76         }         
77
78       if (dir != "" && dir != "." && dir != get_working_directory ())
79         {
80           global_path.prepend (get_working_directory ());
81           message (_f ("Changing working directory to: `%s'",
82                        dir.c_str ()));
83           chdir (dir.c_str ());
84         }
85       else
86         out_file_name = File_name (output_name);
87     }
88
89   string init;
90   if (!init_name_global.empty ())
91     init = init_name_global;
92   else
93     init = "init.ly";
94
95   string out_file = out_file_name.to_string ();
96   if (init.length () && global_path.find (init).empty ())
97     {
98       warning (_f ("cannot find init file: `%s'", init));
99       warning (_f ("(search path: `%s')",
100                    global_path.to_string ().c_str ()));
101       exit (2);
102     }
103
104
105   bool error = false;
106   if ((file_name != "-") && file_name.empty ())
107     {
108       warning (_f ("cannot find file: `%s'", file));
109       error = true;
110     }
111   else
112     {
113       Sources sources;
114       sources.set_path (&global_path);
115
116       string mapped_fn = map_file_name (file_name);
117       message (_f ("Processing `%s'", mapped_fn.c_str ()));
118       progress_indication ("\n");
119
120       Lily_parser *parser = new Lily_parser (&sources);
121
122       parser->parse_file (init, file_name, out_file);
123
124       error = parser->error_level_;
125
126       parser->clear ();
127       parser->unprotect ();
128     }
129
130   /*
131     outside the if-else to ensure cleanup fo Sources object, 
132   */
133   if (error)
134     /* TODO: pass renamed input file too.  */
135     scm_throw (ly_symbol2scm ("ly-file-failed"),
136                scm_list_1 (ly_string2scm (file_name)));
137   
138   return SCM_UNSPECIFIED;
139 }
140
141
142 LY_DEFINE (ly_parser_lexer, "ly:parser-lexer",
143            1, 0, 0, (SCM parser_smob),
144            "Return the lexer for @var{parser-smob}.")
145 {
146   Lily_parser *parser = unsmob_lily_parser (parser_smob);
147   return parser->lexer_->self_scm ();
148 }
149
150 LY_DEFINE (ly_parser_clone, "ly:parser-clone",
151            1, 0, 0, (SCM parser_smob),
152            "Return a clone of @var{parser-smob}.")
153 {
154   LY_ASSERT_SMOB (Lily_parser, parser_smob, 1);
155   Lily_parser *parser = unsmob_lily_parser (parser_smob);
156   Lily_parser *clone = new Lily_parser (*parser);
157
158   return clone->unprotect ();
159 }
160
161 LY_DEFINE (ly_parser_define_x, "ly:parser-define!",
162            3, 0, 0, (SCM parser_smob, SCM symbol, SCM val),
163            "Bind @var{symbol} to @var{val} in @var{parser-smob}'s module.")
164 {
165   
166   LY_ASSERT_SMOB (Lily_parser, parser_smob, 1);
167   Lily_parser *parser = unsmob_lily_parser (parser_smob);
168
169   LY_ASSERT_TYPE (ly_is_symbol, symbol, 2);
170     
171   parser->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
172   return SCM_UNSPECIFIED;
173 }
174
175 LY_DEFINE (ly_parser_lookup, "ly:parser-lookup",
176            2, 0, 0, (SCM parser_smob, SCM symbol),
177            "Look up @var{symbol} in @var{parser-smob}'s module."
178            "  Return @code{'()} if not defined.")
179 {
180   LY_ASSERT_SMOB (Lily_parser, parser_smob, 1);
181
182   Lily_parser *parser = unsmob_lily_parser (parser_smob);
183  
184   LY_ASSERT_TYPE (ly_is_symbol, symbol, 2);
185
186   SCM val = parser->lexer_->lookup_identifier (ly_scm2string (scm_symbol_to_string (symbol)));
187   if (val != SCM_UNDEFINED)
188     return val;
189   else
190     return SCM_EOL;
191 }
192
193 LY_DEFINE (ly_parser_parse_string, "ly:parser-parse-string",
194            2, 0, 0, (SCM parser_smob, SCM ly_code),
195            "Parse the string @var{ly-code} with @var{parser-smob}."
196            "  Upon failure, throw @code{ly-file-failed} key.")
197 {
198   LY_ASSERT_SMOB (Lily_parser, parser_smob, 1);
199   Lily_parser *parser = unsmob_lily_parser (parser_smob); 
200   LY_ASSERT_TYPE (scm_is_string, ly_code, 2);
201
202   parser->parse_string (ly_scm2string (ly_code));
203
204   return SCM_UNSPECIFIED;
205 }
206
207 LY_DEFINE (ly_parser_set_note_names, "ly:parser-set-note-names",
208            2, 0, 0, (SCM parser, SCM names),
209            "Replace current note names in @var{parser}."
210            "  @var{names} is an alist of symbols.  This only has effect"
211            " if the current mode is notes.")
212 {
213   LY_ASSERT_SMOB (Lily_parser, parser, 1);
214   Lily_parser *p = unsmob_lily_parser (parser);
215
216   if (p->lexer_->is_note_state ())
217     {
218       p->lexer_->pop_state ();
219       p->lexer_->push_note_state (alist_to_hashq (names));
220     }
221
222   return SCM_UNSPECIFIED;
223 }
224
225 LY_DEFINE (ly_parser_output_name, "ly:parser-output-name",
226            1, 0, 0, (SCM parser),
227            "Return the base name of the output file.")
228 {
229   LY_ASSERT_SMOB (Lily_parser, parser, 1);
230   Lily_parser *p = unsmob_lily_parser (parser);
231
232   return ly_string2scm (p->output_basename_);
233 }
234
235 LY_DEFINE (ly_parser_error, "ly:parser-error",
236            2, 1, 0, (SCM parser, SCM msg, SCM input),
237            "Display an error message and make the parser fail.")
238 {
239   LY_ASSERT_SMOB (Lily_parser, parser, 1);
240   Lily_parser *p = unsmob_lily_parser (parser);
241   
242   LY_ASSERT_TYPE (scm_is_string, msg, 2);
243   string s = ly_scm2string (msg);
244   
245   Input *i = unsmob_input (input);
246   if (i)
247     p->parser_error (*i, s);
248   else
249     p->parser_error (s);
250
251   return parser;
252 }
253
254 LY_DEFINE (ly_parser_clear_error, "ly:parser-clear-error",
255            1, 0, 0, (SCM parser),
256            "Clear the error flag for the parser.")
257 {
258   LY_ASSERT_SMOB (Lily_parser, parser, 1);
259   Lily_parser *p = unsmob_lily_parser (parser);
260   
261
262   p->error_level_ = 0;
263   p->lexer_->error_level_ = 0;
264   
265   return SCM_UNSPECIFIED;
266 }
267
268 LY_DEFINE (ly_parser_has_error_p, "ly:parser-has-error?",
269            1, 0, 0, (SCM parser),
270            "Does @var{parser} have an error flag?")
271 {
272   LY_ASSERT_SMOB (Lily_parser, parser, 1);
273   Lily_parser *p = unsmob_lily_parser (parser);
274
275   return scm_from_bool (p->error_level_ || p->lexer_->error_level_);
276 }