]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser-scheme.cc
*** empty log message ***
[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 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "file-name.hh"
10 #include "file-path.hh"
11 #include "main.hh"
12 #include "lily-parser.hh"
13 #include "warn.hh"
14 #include "source.hh"
15 #include "lily-lexer.hh"
16 #include "score.hh"
17 #include "lilypond-key.hh"
18 #include "ly-module.hh"
19 #include "output-def.hh"
20 #include "book.hh"
21 #include "paper-book.hh"
22 #include "file-name-map.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   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
40   char const *file = scm_i_string_chars (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   global_path.append (out_file_name.dir_);
51
52   out_file_name.ext_ = "";
53   out_file_name.root_ = "";
54   out_file_name.dir_ = "";
55
56   if (!output_name_global.is_empty ())
57     {
58       if (is_dir (output_name_global))
59         {
60           char cwd[PATH_MAX];
61           getcwd (cwd, PATH_MAX);
62
63           if (output_name_global != cwd)
64             {
65               global_path.prepend (cwd);
66               message (_f ("Changing working directory to `%s'",
67                            output_name_global.to_str0 ()));
68               chdir (output_name_global.to_str0 ());
69               
70             }
71           output_name_global = "";
72         }
73       else      
74         out_file_name = File_name (output_name_global);
75     }
76
77   String init;
78   if (!init_name_global.is_empty ())
79     init = init_name_global;
80   else
81     init = "init.ly";
82
83   String out_file = out_file_name.to_string ();
84
85   if (init.length () && global_path.find (init).is_empty ())
86     {
87       warning (_f ("can't find init file: `%s'", init));
88       warning (_f ("(search path: `%s')",
89                    global_path.to_string ().to_str0 ()));
90       exit (2);
91     }
92
93   if ((file_name != "-") && global_path.find (file_name).is_empty ())
94     {
95       warning (_f ("can't find file: `%s'", file_name));
96       scm_throw (ly_symbol2scm ("ly-file-failed"),
97                  scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
98     }
99   else
100     {
101       Sources sources;
102       sources.set_path (&global_path);
103
104       String mapped_fn = map_file_name (file_name);
105       message (_f ("Processing `%s'", mapped_fn.to_str0 ()));
106       progress_indication ("\n");
107
108       Lily_parser *parser = new Lily_parser (&sources);
109
110       parser->parse_file (init, file_name, out_file);
111
112       bool error = parser->error_level_;
113       scm_gc_unprotect_object (parser->self_scm ());
114       parser = 0;
115       if (error)
116         /* TODO: pass renamed input file too.  */
117         scm_throw (ly_symbol2scm ("ly-file-failed"),
118                    scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
119     }
120   return SCM_UNSPECIFIED;
121 }
122
123 LY_DEFINE (ly_parse_string, "ly:parse-string",
124            1, 0, 0, (SCM ly_code),
125            "Parse the string LY_CODE.  "
126            "Upon failure, throw @code{ly-file-failed} key.")
127 {
128   SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
129
130   Sources sources;
131   sources.set_path (&global_path);
132   Lily_parser *parser = new Lily_parser (&sources);
133   scm_module_define (global_lily_module, ly_symbol2scm ("parser"),
134                      parser->self_scm ());
135   parser->parse_string (ly_scm2string (ly_code));
136   scm_gc_unprotect_object (parser->self_scm ());
137   parser = 0;
138
139   return SCM_UNSPECIFIED;
140 }
141
142 LY_DEFINE (ly_clone_parser, "ly:clone-parser",
143            1, 0, 0, (SCM parser_smob),
144            "Return a clone of PARSER_SMOB.")
145 {
146   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
147   Lily_parser *clone = new Lily_parser (*parser);
148
149   /* FIXME: should copy scopes too. */
150   return scm_gc_unprotect_object (clone->self_scm ());
151 }
152
153 LY_DEFINE (ly_parser_define, "ly:parser-define",
154            3, 0, 0, (SCM parser_smob, SCM symbol, SCM val),
155            "Bind SYMBOL to VAL in PARSER_SMOB's module.")
156 {
157   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
158   SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
159   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
160
161   parser->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
162   return SCM_UNSPECIFIED;
163 }
164
165 LY_DEFINE (ly_parser_lookup, "ly:parser-lookup",
166            2, 0, 0, (SCM parser_smob, SCM symbol),
167            "Lookup @var{symbol} in @var{parser_smob}'s module.  "
168            "Undefined is '().")
169 {
170   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
171
172   SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
173   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
174
175   SCM val = parser->lexer_->lookup_identifier (ly_scm2string (scm_symbol_to_string (symbol)));
176   if (val != SCM_UNDEFINED)
177     return val;
178   else
179     return SCM_EOL;
180 }
181
182 LY_DEFINE (ly_parser_parse_string, "ly:parser-parse-string",
183            2, 0, 0, (SCM parser_smob, SCM ly_code),
184            "Parse the string LY_CODE with PARSER_SMOB."
185            "Upon failure, throw @code{ly-file-failed} key.")
186 {
187   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
188
189   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
190   SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG2, __FUNCTION__, "string");
191
192   parser->parse_string (ly_scm2string (ly_code));
193
194   return SCM_UNSPECIFIED;
195 }
196
197 /* TODO: move this to Scheme?  Why take the parser arg, and all the back
198    & forth between scm and c++? */
199 LY_DEFINE (ly_parser_print_score, "ly:parser-print-score",
200            2, 0, 0,
201            (SCM parser_smob, SCM score_smob),
202            "Print score, i.e., the classic way.")
203 {
204   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
205   Score *score = unsmob_score (score_smob);
206
207   Object_key *key = new Lilypond_general_key (0, score->user_key_, 0);
208
209   if (score->error_found_)
210     return SCM_UNSPECIFIED;
211
212   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
213   SCM_ASSERT_TYPE (score, score_smob, SCM_ARG2, __FUNCTION__, "score");
214
215   SCM header = ly_c_module_p (score->header_)
216     ? score->header_
217     : parser->lexer_->lookup_identifier ("$globalheader");
218
219   File_name outname (parser->output_basename_);
220   int *c = &parser->book_count_;
221   if (*c)
222     outname.base_ += "-" + to_string (*c);
223   (*c)++;
224
225   SCM os = scm_makfrom0str (outname.to_string ().to_str0 ());
226   SCM paper = get_paper (parser)->self_scm ();
227   for (int i = 0; i < score->defs_.size (); i++)
228     default_rendering (score->get_music (), score->defs_[i]->self_scm (),
229                        paper, header, os, key->self_scm ());
230
231   if (score->defs_.is_empty ())
232     {
233       Output_def *layout = get_layout (parser);
234       default_rendering (score->get_music (),
235                          layout->self_scm (),
236                          paper,
237                          header, os, key->self_scm ());
238       
239       scm_gc_unprotect_object (layout->self_scm ());
240     }
241
242   scm_gc_unprotect_object (paper);
243   scm_gc_unprotect_object (key->self_scm ());
244   return SCM_UNSPECIFIED;
245 }
246
247 LY_DEFINE (ly_parser_set_note_names, "ly:parser-set-note-names",
248            2, 0, 0, (SCM parser, SCM names),
249            "Replace current note names in @var{parser}. "
250            "@var{names} is an alist of symbols.  "
251            "This only has effect if the current mode is notes.")
252 {
253   Lily_parser *p = unsmob_my_lily_parser (parser);
254   SCM_ASSERT_TYPE (p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
255
256   if (p->lexer_->is_note_state ())
257     {
258       p->lexer_->pop_state ();
259       p->lexer_->push_note_state (alist_to_hashq (names));
260     }
261
262   return SCM_UNSPECIFIED;
263 }
264
265 LY_DEFINE (ly_parser_print_book, "ly:parser-print-book",
266            2, 0, 0, (SCM parser_smob, SCM book_smob),
267            "Print book.")
268 {
269   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
270   Book *book = unsmob_book (book_smob);
271   Output_def *bp = unsmob_output_def (parser->lexer_->lookup_identifier ("$defaultpaper"));
272
273   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "Lilypond parser");
274   SCM_ASSERT_TYPE (book, book_smob, SCM_ARG2, __FUNCTION__, "Book");
275
276   /*  ugh. changing argument.*/
277   book->paper_ = bp;
278
279   File_name outname (parser->output_basename_);
280   int *c = &parser->book_count_;
281   if (*c)
282     outname.base_ += "-" + to_string (*c);
283   (*c)++;
284
285   Output_def *layout = get_layout (parser);
286   Paper_book *pb = book->process (outname.to_string (), layout);
287
288   if (pb)
289     {
290       pb->output (outname.to_string ());
291       scm_gc_unprotect_object (pb->self_scm ());
292     }
293
294   scm_gc_unprotect_object (layout->self_scm ());
295   return SCM_UNSPECIFIED;
296 }
297