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"
24 /* Do not append `!' suffix, since 1st argument is not modified. */
25 LY_DEFINE (ly_set_point_and_click, "ly:set-point-and-click",
29 warning ("ly:set-point-and-click called");
30 return SCM_UNSPECIFIED;
33 LY_DEFINE (ly_parse_file, "ly:parse-file",
35 "Parse a single @code{.ly} file. "
36 "Upon failure, throw @code{ly-file-failed} key.")
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};
42 String file_name = global_path.find (file, extensions);
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
47 File_name out_file_name (file_name);
49 global_path.append (out_file_name.dir_);
51 out_file_name.ext_ = "";
52 out_file_name.root_ = "";
53 out_file_name.dir_ = "";
55 if (!output_name_global.is_empty ())
56 out_file_name = File_name (output_name_global);
59 if (!init_name_global.is_empty ())
60 init = init_name_global;
64 String out_file = out_file_name.to_string ();
66 if (init.length () && global_path.find (init).is_empty ())
68 warning (_f ("can't find init file: `%s'", init));
69 warning (_f ("(search path: `%s')",
70 global_path.to_string ().to_str0 ()));
74 if ((file_name != "-") && global_path.find (file_name).is_empty ())
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 ())));
83 sources.set_path (&global_path);
85 progress_indication (_f ("Processing `%s'", file_name.to_str0 ()));
86 progress_indication ("\n");
88 Lily_parser *parser = new Lily_parser (&sources);
90 parser->parse_file (init, file_name, out_file);
92 bool error = parser->error_level_;
93 scm_gc_unprotect_object (parser->self_scm ());
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 ())));
100 return SCM_UNSPECIFIED;
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.")
108 SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
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 ());
119 return SCM_UNSPECIFIED;
122 LY_DEFINE (ly_clone_parser, "ly:clone-parser",
123 1, 0, 0, (SCM parser_smob),
124 "Return a clone of PARSER_SMOB.")
126 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
127 Lily_parser *clone = new Lily_parser (*parser);
129 /* FIXME: should copy scopes too. */
130 return scm_gc_unprotect_object (clone->self_scm ());
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.")
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");
141 parser->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
142 return SCM_UNSPECIFIED;
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. "
150 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
152 SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
153 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
155 SCM val = parser->lexer_->lookup_identifier (ly_scm2string (scm_symbol_to_string (symbol)));
156 if (val != SCM_UNDEFINED)
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.")
167 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
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");
172 parser->parse_string (ly_scm2string (ly_code));
174 return SCM_UNSPECIFIED;
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",
181 (SCM parser_smob, SCM score_smob),
182 "Print score, i.e., the classic way.")
184 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
185 Score *score = unsmob_score (score_smob);
187 Object_key * key = new Lilypond_general_key (0, score->user_key_, 0);
189 if (score->error_found_)
190 return SCM_UNSPECIFIED;
192 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
193 SCM_ASSERT_TYPE (score, score_smob, SCM_ARG2, __FUNCTION__, "score");
195 SCM header = ly_c_module_p (score->header_) ? score->header_
196 : parser->lexer_->lookup_identifier ("$globalheader");
198 File_name outname (parser->output_basename_);
199 int *c = &parser->book_count_;
201 outname.base_ += "-" + to_string (*c);
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 ());
211 if (score->defs_.is_empty ())
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 ());
221 scm_gc_unprotect_object (key->self_scm ());
222 return SCM_UNSPECIFIED;
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.")
233 Lily_parser *p = unsmob_my_lily_parser (parser);
234 SCM_ASSERT_TYPE(p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
236 if (p->lexer_->is_note_state ())
238 p->lexer_->pop_state ();
239 p->lexer_->push_note_state (alist_to_hashq (names));
242 return SCM_UNSPECIFIED;
245 LY_DEFINE (ly_parser_print_book, "ly:parser-print-book",
246 2, 0, 0, (SCM parser_smob, SCM book_smob),
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"));
253 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "Lilypond parser");
254 SCM_ASSERT_TYPE (book, book_smob, SCM_ARG2, __FUNCTION__, "Book");
256 /* ugh. changing argument.*/
259 File_name outname (parser->output_basename_);
260 int *c = &parser->book_count_;
262 outname.base_ += "-" + to_string (*c);
265 Output_def *layout = get_layout (parser);
266 Paper_book* pb = book->process (outname.to_string (), layout);
270 pb->output (outname.to_string ());
271 scm_gc_unprotect_object (pb->self_scm ());
274 scm_gc_unprotect_object (layout->self_scm ());
275 return SCM_UNSPECIFIED;