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 "file-name-map.hh"
12 #include "file-name.hh"
13 #include "file-path.hh"
14 #include "international.hh"
15 #include "lily-lexer.hh"
16 #include "lily-parser.hh"
17 #include "ly-module.hh"
19 #include "program-option.hh"
23 /* Do not append `!' suffix, since 1st argument is not modified. */
24 LY_DEFINE (ly_set_point_and_click, "ly:set-point-and-click",
29 warning (_f ("deprecated function called: %s", "ly:set-point-and-click"));
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 /* When running from gui, generate output in .ly source directory. */
56 if (output_name_global.empty ()
57 && ly_get_option (ly_symbol2scm ("gui")) == SCM_BOOL_T)
62 output_name_global = f.to_string ();
65 if (!output_name_global.empty ())
67 /* Interpret --output=DIR to mean --output=DIR/BASE. */
69 if (is_dir (output_name_global))
71 dir = output_name_global;
72 output_name_global = "";
75 dir = dir_name (output_name_global);
76 if (dir != "" && dir != "." && dir != get_working_directory ())
78 global_path.prepend (get_working_directory ());
79 message (_f ("Changing working directory to `%s'",
84 out_file_name = File_name (output_name_global);
88 if (!init_name_global.empty ())
89 init = init_name_global;
93 string out_file = out_file_name.to_string ();
95 if (init.length () && global_path.find (init).empty ())
97 warning (_f ("can't find init file: `%s'", init));
98 warning (_f ("(search path: `%s')",
99 global_path.to_string ().c_str ()));
103 if ((file_name != "-") && global_path.find (file_name).empty ())
105 warning (_f ("can't find file: `%s'", file_name));
106 scm_throw (ly_symbol2scm ("ly-file-failed"),
107 scm_list_1 (scm_makfrom0str (file_name.c_str ())));
112 sources.set_path (&global_path);
114 string mapped_fn = map_file_name (file_name);
115 message (_f ("Processing `%s'", mapped_fn.c_str ()));
116 progress_indication ("\n");
118 Lily_parser *parser = new Lily_parser (&sources);
120 parser->parse_file (init, file_name, out_file);
122 bool error = parser->error_level_;
123 parser->unprotect ();
126 /* TODO: pass renamed input file too. */
127 scm_throw (ly_symbol2scm ("ly-file-failed"),
128 scm_list_1 (scm_makfrom0str (file_name.c_str ())));
130 return SCM_UNSPECIFIED;
133 LY_DEFINE (ly_parse_string, "ly:parse-string",
134 1, 0, 0, (SCM ly_code),
135 "Parse the string LY_CODE. "
136 "Upon failure, throw @code{ly-file-failed} key.")
138 SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
141 sources.set_path (&global_path);
142 Lily_parser *parser = new Lily_parser (&sources);
143 parser->parse_string (ly_scm2string (ly_code));
144 parser->unprotect ();
147 return SCM_UNSPECIFIED;
150 LY_DEFINE (ly_clone_parser, "ly:clone-parser",
151 1, 0, 0, (SCM parser_smob),
152 "Return a clone of PARSER_SMOB.")
154 Lily_parser *parser = unsmob_lily_parser (parser_smob);
155 Lily_parser *clone = new Lily_parser (*parser);
157 return clone->unprotect ();
160 LY_DEFINE (ly_parser_define, "ly:parser-define!",
161 3, 0, 0, (SCM parser_smob, SCM symbol, SCM val),
162 "Bind SYMBOL to VAL in PARSER_SMOB's module.")
164 Lily_parser *parser = unsmob_lily_parser (parser_smob);
165 SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
166 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
168 parser->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
169 return SCM_UNSPECIFIED;
172 LY_DEFINE (ly_parser_lookup, "ly:parser-lookup",
173 2, 0, 0, (SCM parser_smob, SCM symbol),
174 "Lookup @var{symbol} in @var{parser_smob}'s module. "
177 Lily_parser *parser = unsmob_lily_parser (parser_smob);
179 SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
180 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
182 SCM val = parser->lexer_->lookup_identifier (ly_scm2string (scm_symbol_to_string (symbol)));
183 if (val != SCM_UNDEFINED)
189 LY_DEFINE (ly_parser_parse_string, "ly:parser-parse-string",
190 2, 0, 0, (SCM parser_smob, SCM ly_code),
191 "Parse the string LY_CODE with PARSER_SMOB."
192 "Upon failure, throw @code{ly-file-failed} key.")
194 Lily_parser *parser = unsmob_lily_parser (parser_smob);
196 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
197 SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG2, __FUNCTION__, "string");
199 parser->parse_string (ly_scm2string (ly_code));
201 return SCM_UNSPECIFIED;
204 LY_DEFINE (ly_parser_set_note_names, "ly:parser-set-note-names",
205 2, 0, 0, (SCM parser, SCM names),
206 "Replace current note names in @var{parser}. "
207 "@var{names} is an alist of symbols. "
208 "This only has effect if the current mode is notes.")
210 Lily_parser *p = unsmob_lily_parser (parser);
211 SCM_ASSERT_TYPE (p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
213 if (p->lexer_->is_note_state ())
215 p->lexer_->pop_state ();
216 p->lexer_->push_note_state (alist_to_hashq (names));
219 return SCM_UNSPECIFIED;
222 LY_DEFINE (ly_parser_output_name, "ly:parser-output-name",
223 1, 0, 0, (SCM parser),
224 "Return the base name of the output file.")
226 Lily_parser *p = unsmob_lily_parser (parser);
227 SCM_ASSERT_TYPE (p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
229 return scm_makfrom0str (p->output_basename_.c_str ());