2 lily-parser-scheme.cc -- implement Lily_parser bindings
4 source file of the GNU LilyPond music typesetter
6 (c) 2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "file-name.hh"
10 #include "file-path.hh"
12 #include "lily-parser.hh"
15 #include "lily-lexer.hh"
17 #include "lilypond-key.hh"
18 #include "ly-module.hh"
19 #include "output-def.hh"
21 #include "paper-book.hh"
22 #include "file-name-map.hh"
24 /* Do not append `!' suffix, since 1st argument is not modified. */
25 LY_DEFINE (ly_set_point_and_click, "ly:set-point-and-click",
30 warning (_f ("deprecated function called: %s", "ly:set-point-and-click"));
31 return SCM_UNSPECIFIED;
34 LY_DEFINE (ly_parse_file, "ly:parse-file",
36 "Parse a single @code{.ly} file. "
37 "Upon failure, throw @code{ly-file-failed} key.")
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};
43 String file_name = global_path.find (file, extensions);
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
48 File_name out_file_name (file_name);
50 global_path.append (out_file_name.dir_);
52 out_file_name.ext_ = "";
53 out_file_name.root_ = "";
54 out_file_name.dir_ = "";
56 if (!output_name_global.is_empty ())
58 if (is_dir (output_name_global))
61 getcwd (cwd, PATH_MAX);
63 if (output_name_global != cwd)
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 ());
71 output_name_global = "";
74 out_file_name = File_name (output_name_global);
78 if (!init_name_global.is_empty ())
79 init = init_name_global;
83 String out_file = out_file_name.to_string ();
85 if (init.length () && global_path.find (init).is_empty ())
87 warning (_f ("can't find init file: `%s'", init));
88 warning (_f ("(search path: `%s')",
89 global_path.to_string ().to_str0 ()));
93 if ((file_name != "-") && global_path.find (file_name).is_empty ())
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 ())));
102 sources.set_path (&global_path);
104 String mapped_fn = map_file_name (file_name);
105 message (_f ("Processing `%s'", mapped_fn.to_str0 ()));
106 progress_indication ("\n");
108 Lily_parser *parser = new Lily_parser (&sources);
110 parser->parse_file (init, file_name, out_file);
112 bool error = parser->error_level_;
113 scm_gc_unprotect_object (parser->self_scm ());
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 ())));
120 return SCM_UNSPECIFIED;
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.")
128 SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
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 ());
139 return SCM_UNSPECIFIED;
142 LY_DEFINE (ly_clone_parser, "ly:clone-parser",
143 1, 0, 0, (SCM parser_smob),
144 "Return a clone of PARSER_SMOB.")
146 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
147 Lily_parser *clone = new Lily_parser (*parser);
149 /* FIXME: should copy scopes too. */
150 return scm_gc_unprotect_object (clone->self_scm ());
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.")
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");
161 parser->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
162 return SCM_UNSPECIFIED;
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. "
170 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
172 SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
173 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
175 SCM val = parser->lexer_->lookup_identifier (ly_scm2string (scm_symbol_to_string (symbol)));
176 if (val != SCM_UNDEFINED)
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.")
187 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
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");
192 parser->parse_string (ly_scm2string (ly_code));
194 return SCM_UNSPECIFIED;
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",
201 (SCM parser_smob, SCM score_smob),
202 "Print score, i.e., the classic way.")
204 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
205 Score *score = unsmob_score (score_smob);
207 Object_key *key = new Lilypond_general_key (0, score->user_key_, 0);
209 if (score->error_found_)
210 return SCM_UNSPECIFIED;
212 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
213 SCM_ASSERT_TYPE (score, score_smob, SCM_ARG2, __FUNCTION__, "score");
215 SCM header = ly_c_module_p (score->header_)
217 : parser->lexer_->lookup_identifier ("$globalheader");
219 File_name outname (parser->output_basename_);
220 int *c = &parser->book_count_;
222 outname.base_ += "-" + to_string (*c);
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 ());
231 if (score->defs_.is_empty ())
233 Output_def *layout = get_layout (parser);
234 default_rendering (score->get_music (),
237 header, os, key->self_scm ());
239 scm_gc_unprotect_object (layout->self_scm ());
242 scm_gc_unprotect_object (paper);
243 scm_gc_unprotect_object (key->self_scm ());
244 return SCM_UNSPECIFIED;
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.")
253 Lily_parser *p = unsmob_my_lily_parser (parser);
254 SCM_ASSERT_TYPE (p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
256 if (p->lexer_->is_note_state ())
258 p->lexer_->pop_state ();
259 p->lexer_->push_note_state (alist_to_hashq (names));
262 return SCM_UNSPECIFIED;
265 LY_DEFINE (ly_parser_print_book, "ly:parser-print-book",
266 2, 0, 0, (SCM parser_smob, SCM book_smob),
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"));
273 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "Lilypond parser");
274 SCM_ASSERT_TYPE (book, book_smob, SCM_ARG2, __FUNCTION__, "Book");
276 /* ugh. changing argument.*/
279 File_name outname (parser->output_basename_);
280 int *c = &parser->book_count_;
282 outname.base_ += "-" + to_string (*c);
285 Output_def *layout = get_layout (parser);
286 Paper_book *pb = book->process (outname.to_string (), layout);
290 pb->output (outname.to_string ());
291 scm_gc_unprotect_object (pb->self_scm ());
294 scm_gc_unprotect_object (layout->self_scm ());
295 return SCM_UNSPECIFIED;