2 lily-parser-scheme.cc -- implement Lily_parser bindings
4 source file of the GNU LilyPond music typesetter
6 (c) 2005--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
11 #include "lily-parser.hh"
13 #include "file-name-map.hh"
14 #include "file-name.hh"
15 #include "file-path.hh"
16 #include "international.hh"
17 #include "lily-lexer.hh"
18 #include "ly-module.hh"
20 #include "program-option.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 string file = ly_scm2string (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 /* When running from gui, generate output in .ly source directory. */
57 if (output_name_global.empty ()
58 && ly_get_option (ly_symbol2scm ("gui")) == SCM_BOOL_T)
63 output_name_global = f.to_string ();
66 if (!output_name_global.empty ())
69 /* Interpret --output=DIR to mean --output=DIR/BASE. */
71 if (is_dir (output_name_global))
73 dir = output_name_global;
74 output_name_global = "";
78 File_name out (output_name_global);
79 if (is_dir (out.dir_part ()))
81 dir = out.dir_part ();
82 out_file_name = out.file_part ();
86 if (dir != "" && dir != "." && dir != get_working_directory ())
88 global_path.prepend (get_working_directory ());
89 message (_f ("Changing working directory to: `%s'",
94 out_file_name = File_name (output_name_global);
98 if (!init_name_global.empty ())
99 init = init_name_global;
103 string out_file = out_file_name.to_string ();
105 if (init.length () && global_path.find (init).empty ())
107 warning (_f ("cannot find init file: `%s'", init));
108 warning (_f ("(search path: `%s')",
109 global_path.to_string ().c_str ()));
115 if ((file_name != "-") && file_name.empty ())
117 warning (_f ("cannot find file: `%s'", file));
123 sources.set_path (&global_path);
125 string mapped_fn = map_file_name (file_name);
126 message (_f ("Processing `%s'", mapped_fn.c_str ()));
127 progress_indication ("\n");
129 Lily_parser *parser = new Lily_parser (&sources);
131 parser->parse_file (init, file_name, out_file);
133 error = parser->error_level_;
136 parser->unprotect ();
140 outside the if-else to ensure cleanup fo Sources object,
143 /* TODO: pass renamed input file too. */
144 scm_throw (ly_symbol2scm ("ly-file-failed"),
145 scm_list_1 (ly_string2scm (file_name)));
147 return SCM_UNSPECIFIED;
151 LY_DEFINE (ly_parser_lexer, "ly:parser-lexer",
152 1, 0, 0, (SCM parser_smob),
153 "Return the lexer for @var{parser-smob}.")
155 Lily_parser *parser = unsmob_lily_parser (parser_smob);
156 return parser->lexer_->self_scm ();
159 LY_DEFINE (ly_parser_clone, "ly:parser-clone",
160 1, 0, 0, (SCM parser_smob),
161 "Return a clone of @var{parser-smob}.")
163 Lily_parser *parser = unsmob_lily_parser (parser_smob);
164 Lily_parser *clone = new Lily_parser (*parser);
166 return clone->unprotect ();
169 LY_DEFINE (ly_parser_define, "ly:parser-define!",
170 3, 0, 0, (SCM parser_smob, SCM symbol, SCM val),
171 "Bind @var{symbol} to @var{val} in @var{parser-smob}'s module.")
173 Lily_parser *parser = unsmob_lily_parser (parser_smob);
174 SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
175 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
177 parser->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
178 return SCM_UNSPECIFIED;
181 LY_DEFINE (ly_parser_lookup, "ly:parser-lookup",
182 2, 0, 0, (SCM parser_smob, SCM symbol),
183 "Lookup @var{symbol} in @var{parser-smob}'s module. "
186 Lily_parser *parser = unsmob_lily_parser (parser_smob);
188 SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
189 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
191 SCM val = parser->lexer_->lookup_identifier (ly_scm2string (scm_symbol_to_string (symbol)));
192 if (val != SCM_UNDEFINED)
198 LY_DEFINE (ly_parser_parse_string, "ly:parser-parse-string",
199 2, 0, 0, (SCM parser_smob, SCM ly_code),
200 "Parse the string @code{ly-code} with @code{parser-smob}."
201 "Upon failure, throw @code{ly-file-failed} key.")
203 Lily_parser *parser = unsmob_lily_parser (parser_smob);
205 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
206 SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG2, __FUNCTION__, "string");
208 parser->parse_string (ly_scm2string (ly_code));
210 return SCM_UNSPECIFIED;
213 LY_DEFINE (ly_parser_set_note_names, "ly:parser-set-note-names",
214 2, 0, 0, (SCM parser, SCM names),
215 "Replace current note names in @var{parser}. "
216 "@var{names} is an alist of symbols. "
217 "This only has effect if the current mode is notes.")
219 Lily_parser *p = unsmob_lily_parser (parser);
220 SCM_ASSERT_TYPE (p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
222 if (p->lexer_->is_note_state ())
224 p->lexer_->pop_state ();
225 p->lexer_->push_note_state (alist_to_hashq (names));
228 return SCM_UNSPECIFIED;
231 LY_DEFINE (ly_parser_output_name, "ly:parser-output-name",
232 1, 0, 0, (SCM parser),
233 "Return the base name of the output file.")
235 Lily_parser *p = unsmob_lily_parser (parser);
236 SCM_ASSERT_TYPE (p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
238 return ly_string2scm (p->output_basename_);
241 LY_DEFINE (ly_parser_error, "ly:parser-error",
242 2, 1, 0, (SCM parser, SCM msg, SCM input),
243 "Display an error message, and make the parser fail")
245 Lily_parser *p = unsmob_lily_parser (parser);
246 SCM_ASSERT_TYPE (p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
247 SCM_ASSERT_TYPE (scm_is_string (msg), msg, SCM_ARG2, __FUNCTION__, "string");
248 string s = ly_scm2string (msg);
250 Input *i = unsmob_input (input);
252 p->parser_error (*i, s);
259 LY_DEFINE (ly_parser_clear_error, "ly:parser-clear-error",
260 1, 0, 0, (SCM parser),
261 "Clear the error flag for the parser.")
263 Lily_parser *p = unsmob_lily_parser (parser);
264 SCM_ASSERT_TYPE (p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
267 p->lexer_->error_level_ = 0;
269 return SCM_UNSPECIFIED;