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 ())
57 out_file_name = File_name (output_name_global);
60 if (!init_name_global.is_empty ())
61 init = init_name_global;
65 String out_file = out_file_name.to_string ();
67 if (init.length () && global_path.find (init).is_empty ())
69 warning (_f ("can't find init file: `%s'", init));
70 warning (_f ("(search path: `%s')",
71 global_path.to_string ().to_str0 ()));
75 if ((file_name != "-") && global_path.find (file_name).is_empty ())
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 ())));
84 sources.set_path (&global_path);
86 String mapped_fn = map_file_name (file_name);
87 message (_f ("Processing `%s'", mapped_fn.to_str0 ()));
88 progress_indication ("\n");
90 Lily_parser *parser = new Lily_parser (&sources);
92 parser->parse_file (init, file_name, out_file);
94 bool error = parser->error_level_;
95 scm_gc_unprotect_object (parser->self_scm ());
98 /* TODO: pass renamed input file too. */
99 scm_throw (ly_symbol2scm ("ly-file-failed"),
100 scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
102 return SCM_UNSPECIFIED;
105 LY_DEFINE (ly_parse_string, "ly:parse-string",
106 1, 0, 0, (SCM ly_code),
107 "Parse the string LY_CODE. "
108 "Upon failure, throw @code{ly-file-failed} key.")
110 SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
113 sources.set_path (&global_path);
114 Lily_parser *parser = new Lily_parser (&sources);
115 scm_module_define (global_lily_module, ly_symbol2scm ("parser"),
116 parser->self_scm ());
117 parser->parse_string (ly_scm2string (ly_code));
118 scm_gc_unprotect_object (parser->self_scm ());
121 return SCM_UNSPECIFIED;
124 LY_DEFINE (ly_clone_parser, "ly:clone-parser",
125 1, 0, 0, (SCM parser_smob),
126 "Return a clone of PARSER_SMOB.")
128 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
129 Lily_parser *clone = new Lily_parser (*parser);
131 /* FIXME: should copy scopes too. */
132 return scm_gc_unprotect_object (clone->self_scm ());
135 LY_DEFINE (ly_parser_define, "ly:parser-define",
136 3, 0, 0, (SCM parser_smob, SCM symbol, SCM val),
137 "Bind SYMBOL to VAL in PARSER_SMOB's module.")
139 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
140 SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
141 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
143 parser->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
144 return SCM_UNSPECIFIED;
147 LY_DEFINE (ly_parser_lookup, "ly:parser-lookup",
148 2, 0, 0, (SCM parser_smob, SCM symbol),
149 "Lookup @var{symbol} in @var{parser_smob}'s module. "
152 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 SCM val = parser->lexer_->lookup_identifier (ly_scm2string (scm_symbol_to_string (symbol)));
158 if (val != SCM_UNDEFINED)
164 LY_DEFINE (ly_parser_parse_string, "ly:parser-parse-string",
165 2, 0, 0, (SCM parser_smob, SCM ly_code),
166 "Parse the string LY_CODE with PARSER_SMOB."
167 "Upon failure, throw @code{ly-file-failed} key.")
169 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
171 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
172 SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG2, __FUNCTION__, "string");
174 parser->parse_string (ly_scm2string (ly_code));
176 return SCM_UNSPECIFIED;
179 /* TODO: move this to Scheme? Why take the parser arg, and all the back
180 & forth between scm and c++? */
181 LY_DEFINE (ly_parser_print_score, "ly:parser-print-score",
183 (SCM parser_smob, SCM score_smob),
184 "Print score, i.e., the classic way.")
186 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
187 Score *score = unsmob_score (score_smob);
189 Object_key *key = new Lilypond_general_key (0, score->user_key_, 0);
191 if (score->error_found_)
192 return SCM_UNSPECIFIED;
194 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
195 SCM_ASSERT_TYPE (score, score_smob, SCM_ARG2, __FUNCTION__, "score");
197 SCM header = ly_c_module_p (score->header_) ? score->header_
198 : parser->lexer_->lookup_identifier ("$globalheader");
200 File_name outname (parser->output_basename_);
201 int *c = &parser->book_count_;
203 outname.base_ += "-" + to_string (*c);
206 SCM os = scm_makfrom0str (outname.to_string ().to_str0 ());
207 SCM paper = get_paper (parser)->self_scm ();
208 for (int i = 0; i < score->defs_.size (); i++)
209 default_rendering (score->get_music (), score->defs_[i]->self_scm (),
210 paper, header, os, key->self_scm ());
212 if (score->defs_.is_empty ())
214 Output_def *layout = get_layout (parser);
215 default_rendering (score->get_music (), layout->self_scm (),
216 get_paper (parser)->self_scm (),
217 header, os, key->self_scm ());
218 scm_gc_unprotect_object (layout->self_scm ());
221 scm_gc_unprotect_object (key->self_scm ());
222 return SCM_UNSPECIFIED;
225 LY_DEFINE (ly_parser_set_note_names, "ly:parser-set-note-names",
226 2, 0, 0, (SCM parser, SCM names),
227 "Replace current note names in @var{parser}. "
228 "@var{names} is an alist of symbols. "
229 "This only has effect if the current mode is notes.")
231 Lily_parser *p = unsmob_my_lily_parser (parser);
232 SCM_ASSERT_TYPE (p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
234 if (p->lexer_->is_note_state ())
236 p->lexer_->pop_state ();
237 p->lexer_->push_note_state (alist_to_hashq (names));
240 return SCM_UNSPECIFIED;
243 LY_DEFINE (ly_parser_print_book, "ly:parser-print-book",
244 2, 0, 0, (SCM parser_smob, SCM book_smob),
247 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
248 Book *book = unsmob_book (book_smob);
249 Output_def *bp = unsmob_output_def (parser->lexer_->lookup_identifier ("$defaultpaper"));
251 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "Lilypond parser");
252 SCM_ASSERT_TYPE (book, book_smob, SCM_ARG2, __FUNCTION__, "Book");
254 /* ugh. changing argument.*/
257 File_name outname (parser->output_basename_);
258 int *c = &parser->book_count_;
260 outname.base_ += "-" + to_string (*c);
263 Output_def *layout = get_layout (parser);
264 Paper_book *pb = book->process (outname.to_string (), layout);
268 pb->output (outname.to_string ());
269 scm_gc_unprotect_object (pb->self_scm ());
272 scm_gc_unprotect_object (layout->self_scm ());
273 return SCM_UNSPECIFIED;