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