]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser-scheme.cc
Merge branch 'lilypond/translation'
[lilypond.git] / lily / lily-parser-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <unistd.h>
21
22 #include "lily-parser.hh"
23
24 #include "file-name-map.hh"
25 #include "file-name.hh"
26 #include "file-path.hh"
27 #include "international.hh"
28 #include "lily-lexer.hh"
29 #include "ly-module.hh"
30 #include "main.hh"
31 #include "program-option.hh"
32 #include "sources.hh"
33 #include "warn.hh"
34
35 LY_DEFINE (ly_parse_file, "ly:parse-file",
36            1, 0, 0, (SCM name),
37            "Parse a single @code{.ly} file."
38            "  Upon failure, throw @code{ly-file-failed} key.")
39 {
40   LY_ASSERT_TYPE (scm_is_string, name, 1);
41   string file = ly_scm2string (name);
42   char const *extensions[] = {"ly", "", 0};
43
44   string file_name = global_path.find (file, extensions);
45
46   /* By default, use base name of input file for output file name,
47      write output to cwd; do not use root and directory parts of input
48      file name.  */
49   File_name out_file_name (file_name);
50
51   out_file_name.ext_ = "";
52   out_file_name.root_ = "";
53   if (ly_get_option (ly_symbol2scm ("gui")) != SCM_BOOL_T
54       && ly_get_option (ly_symbol2scm ("strip-output-dir")) == SCM_BOOL_T)
55     {
56       out_file_name.dir_ = "";
57     }
58
59   /* When running from gui, generate output in .ly source directory.  */
60   string output_name = output_name_global;
61   if (!output_name.empty ())
62     {
63       /* Interpret --output=DIR to mean --output=DIR/BASE.  */
64       string dir;
65       if (is_dir (output_name))
66         {
67           dir = output_name;
68           output_name = "";
69         }
70       else
71         {
72           File_name out (output_name);
73           if (is_dir (out.dir_part ()))
74             {
75               dir = out.dir_part ();
76               out_file_name = out.file_part ();
77             }
78         }
79
80       if (dir != "" && dir != "." && dir != get_working_directory ())
81         {
82           global_path.prepend (get_working_directory ());
83           message (_f ("Changing working directory to: `%s'",
84                        dir.c_str ()));
85           chdir (dir.c_str ());
86         }
87       else
88         out_file_name = File_name (output_name);
89     }
90
91   string init;
92   if (!init_name_global.empty ())
93     init = init_name_global;
94   else
95     init = "init.ly";
96
97   string out_file = out_file_name.to_string ();
98   if (init.length () && global_path.find (init).empty ())
99     {
100       warning (_f ("cannot find init file: `%s'", init));
101       warning (_f ("(search path: `%s')",
102                    global_path.to_string ().c_str ()));
103       exit (2);
104     }
105
106   bool error = false;
107   if ((file_name != "-") && file_name.empty ())
108     {
109       warning (_f ("cannot find file: `%s'", file));
110       error = true;
111     }
112   else
113     {
114       Sources sources;
115       sources.set_path (&global_path);
116
117       string mapped_fn = map_file_name (file_name);
118       message (_f ("Processing `%s'", mapped_fn.c_str ()));
119       progress_indication ("\n");
120
121       Lily_parser *parser = new Lily_parser (&sources);
122
123       parser->parse_file (init, file_name, out_file);
124
125       error = parser->error_level_;
126
127       parser->clear ();
128       parser->unprotect ();
129     }
130
131   /*
132     outside the if-else to ensure cleanup fo Sources object,
133   */
134   if (error)
135     /* TODO: pass renamed input file too.  */
136     scm_throw (ly_symbol2scm ("ly-file-failed"),
137                scm_list_1 (ly_string2scm (file_name)));
138
139   return SCM_UNSPECIFIED;
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   if (!parser->lexer_->is_clean ())
203     parser->parser_error (_ ("ly:parser-parse-string is only valid with a new parser."
204                              "  Use ly:parser-include-string instead."));
205   else
206     parser->parse_string (ly_scm2string (ly_code));
207
208   return SCM_UNSPECIFIED;
209 }
210
211 LY_DEFINE (ly_parser_include_string, "ly:parser-include-string",
212            2, 0, 0, (SCM parser_smob, SCM ly_code),
213            "Include the string @var{ly-code} into the input stream"
214            " for @var{parser-smob}.")
215 {
216   LY_ASSERT_SMOB (Lily_parser, parser_smob, 1);
217   Lily_parser *parser = unsmob_lily_parser (parser_smob);
218   LY_ASSERT_TYPE (scm_is_string, ly_code, 2);
219
220   parser->include_string (ly_scm2string (ly_code));
221
222   return SCM_UNSPECIFIED;
223 }
224
225 LY_DEFINE (ly_parser_set_note_names, "ly:parser-set-note-names",
226            2, 0, 0, (SCM parser, SCM names),
227            "Replace current note names in @var{parser}."
228            "  @var{names} is an alist of symbols.  This only has effect"
229            " if the current mode is notes.")
230 {
231   LY_ASSERT_SMOB (Lily_parser, parser, 1);
232   Lily_parser *p = unsmob_lily_parser (parser);
233
234   if (p->lexer_->is_note_state ())
235     {
236       p->lexer_->pop_state ();
237       p->lexer_->push_note_state (alist_to_hashq (names));
238     }
239
240   return SCM_UNSPECIFIED;
241 }
242
243 LY_DEFINE (ly_parser_set_repetition_symbol, "ly:parser-set-repetition-symbol",
244            2, 0, 0, (SCM parser, SCM sym),
245            "Replace the current repetition symbol in @var{parser}."
246            "  @var{sym} is the new repetition symbol.")
247 {
248   LY_ASSERT_SMOB (Lily_parser, parser, 1);
249   Lily_parser *p = unsmob_lily_parser (parser);
250
251   p->lexer_->chord_repetition_.repetition_symbol_ = sym;
252
253   return SCM_UNSPECIFIED;
254 }
255
256 LY_DEFINE (ly_parser_set_repetition_function, "ly:parser-set-repetition-function",
257            2, 0, 0, (SCM parser, SCM fun),
258            "Replace the current repetition function in @var{parser}."
259            "  @var{fun} is the new repetition function.")
260 {
261   LY_ASSERT_SMOB (Lily_parser, parser, 1);
262   Lily_parser *p = unsmob_lily_parser (parser);
263
264   p->lexer_->chord_repetition_.repetition_function_ = fun;
265
266   return SCM_UNSPECIFIED;
267 }
268
269 LY_DEFINE (ly_parser_output_name, "ly:parser-output-name",
270            1, 0, 0, (SCM parser),
271            "Return the base name of the output file.")
272 {
273   LY_ASSERT_SMOB (Lily_parser, parser, 1);
274   Lily_parser *p = unsmob_lily_parser (parser);
275
276   return ly_string2scm (p->output_basename_);
277 }
278
279 LY_DEFINE (ly_parser_error, "ly:parser-error",
280            2, 1, 0, (SCM parser, SCM msg, SCM input),
281            "Display an error message and make the parser fail.")
282 {
283   LY_ASSERT_SMOB (Lily_parser, parser, 1);
284   Lily_parser *p = unsmob_lily_parser (parser);
285
286   LY_ASSERT_TYPE (scm_is_string, msg, 2);
287   string s = ly_scm2string (msg);
288
289   Input *i = unsmob_input (input);
290   if (i)
291     p->parser_error (*i, s);
292   else
293     p->parser_error (s);
294
295   return parser;
296 }
297
298 LY_DEFINE (ly_parser_clear_error, "ly:parser-clear-error",
299            1, 0, 0, (SCM parser),
300            "Clear the error flag for the parser.")
301 {
302   LY_ASSERT_SMOB (Lily_parser, parser, 1);
303   Lily_parser *p = unsmob_lily_parser (parser);
304
305   p->error_level_ = 0;
306   p->lexer_->error_level_ = 0;
307
308   return SCM_UNSPECIFIED;
309 }
310
311 LY_DEFINE (ly_parser_has_error_p, "ly:parser-has-error?",
312            1, 0, 0, (SCM parser),
313            "Does @var{parser} have an error flag?")
314 {
315   LY_ASSERT_SMOB (Lily_parser, parser, 1);
316   Lily_parser *p = unsmob_lily_parser (parser);
317
318   return scm_from_bool (p->error_level_ || p->lexer_->error_level_);
319 }