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