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