]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser-scheme.cc
* lily/lily-parser-scheme.cc: print mapped file name for progress
[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 ("ly:set-point-and-click called");
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     out_file_name = File_name (output_name_global);
58
59   String init;
60   if (!init_name_global.is_empty ())
61     init = init_name_global;
62   else
63     init = "init.ly";
64
65   String out_file = out_file_name.to_string ();
66
67   if (init.length () && global_path.find (init).is_empty ())
68     {
69       warning (_f ("can't find init file: `%s'", init));
70       warning (_f ("(search path: `%s')",
71                    global_path.to_string ().to_str0 ()));
72       exit (2);
73     }
74
75   if ((file_name != "-") && global_path.find (file_name).is_empty ())
76     {
77       warning (_f ("can't find file: `%s'", file_name));
78       scm_throw (ly_symbol2scm ("ly-file-failed"),
79                  scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
80     }
81   else
82     {
83       Sources sources;
84       sources.set_path (&global_path);
85
86       String mapped_fn = map_file_name (file_name);
87       progress_indication (_f ("Processing `%s'", mapped_fn.to_str0 ()));
88       progress_indication ("\n");
89
90       Lily_parser *parser = new Lily_parser (&sources);
91
92       parser->parse_file (init, file_name, out_file);
93
94       bool error = parser->error_level_;
95       scm_gc_unprotect_object (parser->self_scm ());
96       parser = 0;
97       if (error)
98         /* TODO: pass renamed input file too.  */
99         scm_throw (ly_symbol2scm ("ly-file-failed"),
100                    scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
101     }
102   return SCM_UNSPECIFIED;
103 }
104
105 LY_DEFINE (ly_parse_string, "ly:parse-string",
106            1, 0, 0, (SCM ly_code),
107            "Parse the string LY_CODE.  "
108            "Upon failure, throw @code{ly-file-failed} key.")
109 {
110   SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
111
112   Sources sources;
113   sources.set_path (&global_path);
114   Lily_parser *parser = new Lily_parser (&sources);
115   scm_module_define (global_lily_module, ly_symbol2scm ("parser"),
116                      parser->self_scm ());
117   parser->parse_string (ly_scm2string (ly_code));
118   scm_gc_unprotect_object (parser->self_scm ());
119   parser = 0;
120
121   return SCM_UNSPECIFIED;
122 }
123
124 LY_DEFINE (ly_clone_parser, "ly:clone-parser",
125            1, 0, 0, (SCM parser_smob),
126            "Return a clone of PARSER_SMOB.")
127 {
128   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
129   Lily_parser *clone = new Lily_parser (*parser);
130
131   /* FIXME: should copy scopes too. */
132   return scm_gc_unprotect_object (clone->self_scm ());
133 }
134
135 LY_DEFINE (ly_parser_define, "ly:parser-define",
136            3, 0, 0, (SCM parser_smob, SCM symbol, SCM val),
137            "Bind SYMBOL to VAL in PARSER_SMOB's module.")
138 {
139   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
140   SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
141   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
142
143   parser->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
144   return SCM_UNSPECIFIED;
145 }
146
147 LY_DEFINE (ly_parser_lookup, "ly:parser-lookup",
148            2, 0, 0, (SCM parser_smob, SCM symbol),
149            "Lookup @var{symbol} in @var{parser_smob}'s module.  "
150            "Undefined is '().")
151 {
152   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
153
154   SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
155   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
156
157   SCM val = parser->lexer_->lookup_identifier (ly_scm2string (scm_symbol_to_string (symbol)));
158   if (val != SCM_UNDEFINED)
159     return val;
160   else
161     return SCM_EOL;
162 }
163
164 LY_DEFINE (ly_parser_parse_string, "ly:parser-parse-string",
165            2, 0, 0, (SCM parser_smob, SCM ly_code),
166            "Parse the string LY_CODE with PARSER_SMOB."
167            "Upon failure, throw @code{ly-file-failed} key.")
168 {
169   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
170
171   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
172   SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG2, __FUNCTION__, "string");
173
174   parser->parse_string (ly_scm2string (ly_code));
175
176   return SCM_UNSPECIFIED;
177 }
178
179 /* TODO: move this to Scheme?  Why take the parser arg, and all the back
180    & forth between scm and c++? */
181 LY_DEFINE (ly_parser_print_score, "ly:parser-print-score",
182            2, 0, 0,
183            (SCM parser_smob, SCM score_smob),
184            "Print score, i.e., the classic way.")
185 {
186   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
187   Score *score = unsmob_score (score_smob);
188
189   Object_key *key = new Lilypond_general_key (0, score->user_key_, 0);
190
191   if (score->error_found_)
192     return SCM_UNSPECIFIED;
193
194   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
195   SCM_ASSERT_TYPE (score, score_smob, SCM_ARG2, __FUNCTION__, "score");
196
197   SCM header = ly_c_module_p (score->header_) ? score->header_
198     : parser->lexer_->lookup_identifier ("$globalheader");
199
200   File_name outname (parser->output_basename_);
201   int *c = &parser->book_count_;
202   if (*c)
203     outname.base_ += "-" + to_string (*c);
204   (*c)++;
205
206   SCM os = scm_makfrom0str (outname.to_string ().to_str0 ());
207   SCM paper = get_paper (parser)->self_scm ();
208   for (int i = 0; i < score->defs_.size (); i++)
209     default_rendering (score->get_music (), score->defs_[i]->self_scm (),
210                        paper, header, os, key->self_scm ());
211
212   if (score->defs_.is_empty ())
213     {
214       Output_def *layout = get_layout (parser);
215       default_rendering (score->get_music (), layout->self_scm (),
216                          get_paper (parser)->self_scm (),
217                          header, os, key->self_scm ());
218       scm_gc_unprotect_object (layout->self_scm ());
219     }
220
221   scm_gc_unprotect_object (key->self_scm ());
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.  "
229            "This only has effect if the current mode is notes.")
230 {
231   Lily_parser *p = unsmob_my_lily_parser (parser);
232   SCM_ASSERT_TYPE (p, parser, SCM_ARG1, __FUNCTION__, "Lilypond 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_print_book, "ly:parser-print-book",
244            2, 0, 0, (SCM parser_smob, SCM book_smob),
245            "Print book.")
246 {
247   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
248   Book *book = unsmob_book (book_smob);
249   Output_def *bp = unsmob_output_def (parser->lexer_->lookup_identifier ("$defaultpaper"));
250
251   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "Lilypond parser");
252   SCM_ASSERT_TYPE (book, book_smob, SCM_ARG2, __FUNCTION__, "Book");
253
254   /*  ugh. changing argument.*/
255   book->paper_ = bp;
256
257   File_name outname (parser->output_basename_);
258   int *c = &parser->book_count_;
259   if (*c)
260     outname.base_ += "-" + to_string (*c);
261   (*c)++;
262
263   Output_def *layout = get_layout (parser);
264   Paper_book *pb = book->process (outname.to_string (), layout);
265
266   if (pb)
267     {
268       pb->output (outname.to_string ());
269       scm_gc_unprotect_object (pb->self_scm ());
270     }
271
272   scm_gc_unprotect_object (layout->self_scm ());
273   return SCM_UNSPECIFIED;
274 }
275