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 "lily-lexer.hh"
15 #include "lily-parser.hh"
16 #include "ly-module.hh"
18 #include "program-option.hh"
22 /* Do not append `!' suffix, since 1st argument is not modified. */
23 LY_DEFINE (ly_set_point_and_click, "ly:set-point-and-click",
28 warning (_f ("deprecated function called: %s", "ly:set-point-and-click"));
29 return SCM_UNSPECIFIED;
32 LY_DEFINE (ly_parse_file, "ly:parse-file",
34 "Parse a single @code{.ly} file. "
35 "Upon failure, throw @code{ly-file-failed} key.")
37 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
38 char const *file = scm_i_string_chars (name);
39 char const *extensions[] = {"ly", "", 0};
41 String file_name = global_path.find (file, extensions);
43 /* By default, use base name of input file for output file name,
44 write output to cwd; do not use root and directory parts of input
46 File_name out_file_name (file_name);
48 global_path.append (out_file_name.dir_);
50 out_file_name.ext_ = "";
51 out_file_name.root_ = "";
52 out_file_name.dir_ = "";
54 /* When running from gui, generate output in .ly source directory. */
55 if (output_name_global.is_empty ()
56 && ly_get_option (ly_symbol2scm ("gui")) == SCM_BOOL_T)
61 output_name_global = f.to_string ();
64 if (!output_name_global.is_empty ())
66 /* Interpret --output=DIR to mean --output=DIR/BASE. */
67 if (is_dir (output_name_global))
70 getcwd (cwd, PATH_MAX);
72 if (output_name_global != cwd)
74 global_path.prepend (cwd);
75 message (_f ("Changing working directory to `%s'",
76 output_name_global.to_str0 ()));
77 chdir (output_name_global.to_str0 ());
79 output_name_global = "";
82 out_file_name = File_name (output_name_global);
86 if (!init_name_global.is_empty ())
87 init = init_name_global;
91 String out_file = out_file_name.to_string ();
93 if (init.length () && global_path.find (init).empty ())
95 warning (_f ("can't find init file: `%s'", init));
96 warning (_f ("(search path: `%s')",
97 global_path.to_string ().c_str ()));
101 if ((file_name != "-") && global_path.find (file_name).empty ())
103 warning (_f ("can't find file: `%s'", file_name));
104 scm_throw (ly_symbol2scm ("ly-file-failed"),
105 scm_list_1 (scm_makfrom0str (file_name.c_str ())));
110 sources.set_path (&global_path);
112 String mapped_fn = map_file_name (file_name);
113 message (_f ("Processing `%s'", mapped_fn.to_str0 ()));
114 progress_indication ("\n");
116 Lily_parser *parser = new Lily_parser (&sources);
118 parser->parse_file (init, file_name, out_file);
120 bool error = parser->error_level_;
121 parser->unprotect ();
124 /* TODO: pass renamed input file too. */
125 scm_throw (ly_symbol2scm ("ly-file-failed"),
126 scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
128 return SCM_UNSPECIFIED;
131 LY_DEFINE (ly_parse_string, "ly:parse-string",
132 1, 0, 0, (SCM ly_code),
133 "Parse the string LY_CODE. "
134 "Upon failure, throw @code{ly-file-failed} key.")
136 SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
139 sources.set_path (&global_path);
140 Lily_parser *parser = new Lily_parser (&sources);
141 parser->parse_string (ly_scm2string (ly_code));
142 parser->unprotect ();
145 return SCM_UNSPECIFIED;
148 LY_DEFINE (ly_clone_parser, "ly:clone-parser",
149 1, 0, 0, (SCM parser_smob),
150 "Return a clone of PARSER_SMOB.")
152 Lily_parser *parser = unsmob_lily_parser (parser_smob);
153 Lily_parser *clone = new Lily_parser (*parser);
155 return clone->unprotect ();
158 LY_DEFINE (ly_parser_define, "ly:parser-define!",
159 3, 0, 0, (SCM parser_smob, SCM symbol, SCM val),
160 "Bind SYMBOL to VAL in PARSER_SMOB's module.")
162 Lily_parser *parser = unsmob_lily_parser (parser_smob);
163 SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
164 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
166 parser->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
167 return SCM_UNSPECIFIED;
170 LY_DEFINE (ly_parser_lookup, "ly:parser-lookup",
171 2, 0, 0, (SCM parser_smob, SCM symbol),
172 "Lookup @var{symbol} in @var{parser_smob}'s module. "
175 Lily_parser *parser = unsmob_lily_parser (parser_smob);
177 SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
178 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
180 SCM val = parser->lexer_->lookup_identifier (ly_scm2string (scm_symbol_to_string (symbol)));
181 if (val != SCM_UNDEFINED)
187 LY_DEFINE (ly_parser_parse_string, "ly:parser-parse-string",
188 2, 0, 0, (SCM parser_smob, SCM ly_code),
189 "Parse the string LY_CODE with PARSER_SMOB."
190 "Upon failure, throw @code{ly-file-failed} key.")
192 Lily_parser *parser = unsmob_lily_parser (parser_smob);
194 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
195 SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG2, __FUNCTION__, "string");
197 parser->parse_string (ly_scm2string (ly_code));
199 return SCM_UNSPECIFIED;
202 LY_DEFINE (ly_parser_set_note_names, "ly:parser-set-note-names",
203 2, 0, 0, (SCM parser, SCM names),
204 "Replace current note names in @var{parser}. "
205 "@var{names} is an alist of symbols. "
206 "This only has effect if the current mode is notes.")
208 Lily_parser *p = unsmob_lily_parser (parser);
209 SCM_ASSERT_TYPE (p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
211 if (p->lexer_->is_note_state ())
213 p->lexer_->pop_state ();
214 p->lexer_->push_note_state (alist_to_hashq (names));
217 return SCM_UNSPECIFIED;
220 LY_DEFINE (ly_parser_output_name, "ly:parser-output-name",
221 1, 0, 0, (SCM parser),
222 "Return the base name of the output file.")
224 Lily_parser *p = unsmob_lily_parser (parser);
225 SCM_ASSERT_TYPE (p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
227 return scm_makfrom0str (p->output_basename_.to_str0 ());