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>
10 #include "file-name.hh"
11 #include "file-path.hh"
13 #include "lily-parser.hh"
16 #include "lily-lexer.hh"
18 #include "lilypond-key.hh"
19 #include "ly-module.hh"
20 #include "output-def.hh"
22 #include "paper-book.hh"
27 bool store_locations_global;
29 /* Do not append `!' suffix, since 1st argument is not modified. */
30 LY_DEFINE (ly_set_point_and_click, "ly:set-point-and-click",
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}")
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");
43 scm_module_define (global_lily_module, ly_symbol2scm ("point-and-click"),
45 store_locations_global = ly_c_procedure_p (val);
46 return SCM_UNSPECIFIED;
49 LY_DEFINE (ly_parse_file, "ly:parse-file",
51 "Parse a single @code{.ly} file. "
52 "Upon failure, throw @code{ly-file-failed} key.")
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};
58 String file_name = global_path.find (file, extensions);
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
63 File_name out_file_name (file_name);
65 global_path.append (out_file_name.dir_);
67 out_file_name.ext_ = "";
68 out_file_name.root_ = "";
69 out_file_name.dir_ = "";
71 if (!output_name_global.is_empty ())
72 out_file_name = File_name (output_name_global);
75 if (!init_name_global.is_empty ())
76 init = init_name_global;
80 String out_file = out_file_name.to_string ();
82 if (init.length () && global_path.find (init).is_empty ())
84 warning (_f ("can't find init file: `%s'", init));
85 warning (_f ("(search path: `%s')",
86 global_path.to_string ().to_str0 ()));
90 if ((file_name != "-") && global_path.find (file_name).is_empty ())
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 ())));
99 sources.set_path (&global_path);
101 progress_indication (_f ("Processing `%s'", file_name.to_str0 ()));
102 progress_indication ("\n");
104 Lily_parser *parser = new Lily_parser (&sources);
106 parser->parse_file (init, file_name, out_file);
108 bool error = parser->error_level_;
109 scm_gc_unprotect_object (parser->self_scm ());
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 ())));
116 return SCM_UNSPECIFIED;
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.")
124 SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
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 ());
135 return SCM_UNSPECIFIED;
138 LY_DEFINE (ly_clone_parser, "ly:clone-parser",
139 1, 0, 0, (SCM parser_smob),
140 "Return a clone of PARSER_SMOB.")
142 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
143 Lily_parser *clone = new Lily_parser (*parser);
145 /* FIXME: should copy scopes too. */
146 return scm_gc_unprotect_object (clone->self_scm ());
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.")
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");
157 parser->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
158 return SCM_UNSPECIFIED;
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. "
166 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
168 SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
169 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
171 SCM val = parser->lexer_->lookup_identifier (ly_scm2string (scm_symbol_to_string (symbol)));
172 if (val != SCM_UNDEFINED)
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.")
183 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
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");
188 parser->parse_string (ly_scm2string (ly_code));
190 return SCM_UNSPECIFIED;
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",
197 (SCM parser_smob, SCM score_smob),
198 "Print score, i.e., the classic way.")
200 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
201 Score *score = unsmob_score (score_smob);
203 Object_key * key = new Lilypond_general_key (0, score->user_key_, 0);
205 if (score->error_found_)
206 return SCM_UNSPECIFIED;
208 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
209 SCM_ASSERT_TYPE (score, score_smob, SCM_ARG2, __FUNCTION__, "score");
211 SCM header = ly_c_module_p (score->header_) ? score->header_
212 : parser->lexer_->lookup_identifier ("$globalheader");
214 File_name outname (parser->output_basename_);
215 int *c = &parser->book_count_;
217 outname.base_ += "-" + to_string (*c);
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 (),
228 if (score->defs_.is_empty ())
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 ());
237 scm_gc_unprotect_object (key->self_scm ());
238 return SCM_UNSPECIFIED;
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.")
249 Lily_parser *p = unsmob_my_lily_parser (parser);
250 SCM_ASSERT_TYPE(p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
252 if (p->lexer_->is_note_state ())
254 p->lexer_->pop_state ();
255 p->lexer_->push_note_state (alist_to_hashq (names));
258 return SCM_UNSPECIFIED;
261 LY_DEFINE (ly_parser_print_book, "ly:parser-print-book",
262 2, 0, 0, (SCM parser_smob, SCM book_smob),
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"));
269 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "Lilypond parser");
270 SCM_ASSERT_TYPE (book, book_smob, SCM_ARG2, __FUNCTION__, "Book");
272 /* ugh. changing argument.*/
275 File_name outname (parser->output_basename_);
276 int *c = &parser->book_count_;
278 outname.base_ += "-" + to_string (*c);
281 Output_def *layout = get_layout (parser);
282 Paper_book* pb = book->process (outname.to_string (), layout);
286 pb->output (outname.to_string ());
287 scm_gc_unprotect_object (pb->self_scm ());
290 scm_gc_unprotect_object (layout->self_scm ());
291 return SCM_UNSPECIFIED;