]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser-scheme.cc
doc string nits.
[lilypond.git] / lily / lily-parser-scheme.cc
1 /*
2   lily-parser-scheme.cc -- implement Lily_parser bindings
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include <unistd.h>
10
11 #include "lily-parser.hh"
12 6
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"
19 #include "main.hh"
20 #include "program-option.hh"
21 #include "source.hh"
22 #include "warn.hh"
23
24 /* Do not append `!' suffix, since 1st argument is not modified. */
25 LY_DEFINE (ly_set_point_and_click, "ly:set-point-and-click",
26            1, 0, 0, (SCM what),
27            "Deprecated.")
28 {
29   (void) what;
30   warning (_f ("deprecated function called: %s", "ly:set-point-and-click"));
31   return SCM_UNSPECIFIED;
32 }
33
34 LY_DEFINE (ly_parse_file, "ly:parse-file",
35            1, 0, 0, (SCM name),
36            "Parse a single @code{.ly} file.  "
37            "Upon failure, throw @code{ly-file-failed} key.")
38 {
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};
42
43   string file_name = global_path.find (file, extensions);
44
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
47      file name.  */
48   File_name out_file_name (file_name);
49
50   global_path.append (out_file_name.dir_);
51
52   out_file_name.ext_ = "";
53   out_file_name.root_ = "";
54   out_file_name.dir_ = "";
55
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)
59     {
60       File_name f (file);
61       f.base_ = "";
62       f.ext_ = "";
63       output_name_global = f.to_string ();
64     }
65
66   if (!output_name_global.empty ())
67     {
68       
69       /* Interpret --output=DIR to mean --output=DIR/BASE.  */
70       string dir;
71       if (is_dir (output_name_global))
72         {
73           dir = output_name_global;
74           output_name_global = "";
75         }
76       else
77         {
78           File_name out (output_name_global);
79           if (is_dir (out.dir_part ()))
80             {
81               dir = out.dir_part ();
82               out_file_name = out.file_part ();
83             }
84         }         
85
86       if (dir != "" && dir != "." && dir != get_working_directory ())
87         {
88           global_path.prepend (get_working_directory ());
89           message (_f ("Changing working directory to: `%s'",
90                        dir.c_str ()));
91           chdir (dir.c_str ());
92         }
93       else
94         out_file_name = File_name (output_name_global);
95     }
96
97   string init;
98   if (!init_name_global.empty ())
99     init = init_name_global;
100   else
101     init = "init.ly";
102
103   string out_file = out_file_name.to_string ();
104
105   if (init.length () && global_path.find (init).empty ())
106     {
107       warning (_f ("cannot find init file: `%s'", init));
108       warning (_f ("(search path: `%s')",
109                    global_path.to_string ().c_str ()));
110       exit (2);
111     }
112
113
114   bool error = false;
115   if ((file_name != "-") && file_name.empty ())
116     {
117       warning (_f ("cannot find file: `%s'", file));
118       error = true;
119     }
120   else
121     {
122       Sources sources;
123       sources.set_path (&global_path);
124
125       string mapped_fn = map_file_name (file_name);
126       message (_f ("Processing `%s'", mapped_fn.c_str ()));
127       progress_indication ("\n");
128
129       Lily_parser *parser = new Lily_parser (&sources);
130
131       parser->parse_file (init, file_name, out_file);
132
133       error = parser->error_level_;
134
135       parser->clear ();
136       parser->unprotect ();
137     }
138
139   /*
140     outside the if-else to ensure cleanup fo Sources object, 
141    */
142   if (error)
143     /* TODO: pass renamed input file too.  */
144     scm_throw (ly_symbol2scm ("ly-file-failed"),
145                scm_list_1 (scm_makfrom0str (file_name.c_str ())));
146   
147   return SCM_UNSPECIFIED;
148 }
149
150 LY_DEFINE (ly_parse_string, "ly:parse-string",
151            1, 0, 0, (SCM ly_code),
152            "Parse the string LY_CODE.  "
153            "Upon failure, throw @code{ly-file-failed} key.")
154 {
155   SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
156
157   Sources sources;
158   sources.set_path (&global_path);
159   Lily_parser *parser = new Lily_parser (&sources);
160   parser->parse_string (ly_scm2string (ly_code));
161   parser->unprotect ();
162   parser = 0;
163
164   return SCM_UNSPECIFIED;
165 }
166
167 LY_DEFINE (ly_parser_lexer, "ly:parser-lexer",
168            1, 0, 0, (SCM parser_smob),
169            "Return the lexer for @var{parser-smob}.")
170 {
171   Lily_parser *parser = unsmob_lily_parser (parser_smob);
172   return parser->lexer_->self_scm ();
173 }
174
175 LY_DEFINE (ly_parser_clone, "ly:parser-clone",
176            1, 0, 0, (SCM parser_smob),
177            "Return a clone of @var{parser-smob}.")
178 {
179   Lily_parser *parser = unsmob_lily_parser (parser_smob);
180   Lily_parser *clone = new Lily_parser (*parser);
181
182   return clone->unprotect ();
183 }
184
185 LY_DEFINE (ly_parser_define, "ly:parser-define!",
186            3, 0, 0, (SCM parser_smob, SCM symbol, SCM val),
187            "Bind @var{symbol} to @var{val} in @var{parser-smob}'s module.")
188 {
189   Lily_parser *parser = unsmob_lily_parser (parser_smob);
190   SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
191   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
192
193   parser->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
194   return SCM_UNSPECIFIED;
195 }
196
197 LY_DEFINE (ly_parser_lookup, "ly:parser-lookup",
198            2, 0, 0, (SCM parser_smob, SCM symbol),
199            "Lookup @var{symbol} in @var{parser-smob}'s module.  "
200            "Undefined is '().")
201 {
202   Lily_parser *parser = unsmob_lily_parser (parser_smob);
203
204   SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
205   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
206
207   SCM val = parser->lexer_->lookup_identifier (ly_scm2string (scm_symbol_to_string (symbol)));
208   if (val != SCM_UNDEFINED)
209     return val;
210   else
211     return SCM_EOL;
212 }
213
214 LY_DEFINE (ly_parser_parse_string, "ly:parser-parse-string",
215            2, 0, 0, (SCM parser_smob, SCM ly_code),
216            "Parse the string @code{ly-code} with @code{parser-smob}."
217            "Upon failure, throw @code{ly-file-failed} key.")
218 {
219   Lily_parser *parser = unsmob_lily_parser (parser_smob);
220
221   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
222   SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG2, __FUNCTION__, "string");
223
224   parser->parse_string (ly_scm2string (ly_code));
225
226   return SCM_UNSPECIFIED;
227 }
228
229 LY_DEFINE (ly_parser_set_note_names, "ly:parser-set-note-names",
230            2, 0, 0, (SCM parser, SCM names),
231            "Replace current note names in @var{parser}. "
232            "@var{names} is an alist of symbols.  "
233            "This only has effect if the current mode is notes.")
234 {
235   Lily_parser *p = unsmob_lily_parser (parser);
236   SCM_ASSERT_TYPE (p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
237
238   if (p->lexer_->is_note_state ())
239     {
240       p->lexer_->pop_state ();
241       p->lexer_->push_note_state (alist_to_hashq (names));
242     }
243
244   return SCM_UNSPECIFIED;
245 }
246
247 LY_DEFINE (ly_parser_output_name, "ly:parser-output-name",
248            1, 0, 0, (SCM parser),
249            "Return the base name of the output file.")
250 {
251   Lily_parser *p = unsmob_lily_parser (parser);
252   SCM_ASSERT_TYPE (p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
253
254   return scm_makfrom0str (p->output_basename_.c_str ());
255 }
256
257 LY_DEFINE (ly_parser_error, "ly:parser-error",
258            2, 1, 0, (SCM parser, SCM msg, SCM input),
259            "Display an error message, and make the parser fail")
260 {
261   Lily_parser *p = unsmob_lily_parser (parser);
262   SCM_ASSERT_TYPE (p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
263   SCM_ASSERT_TYPE (scm_is_string (msg), msg, SCM_ARG2, __FUNCTION__, "string");
264   string s = ly_scm2string (msg);
265   
266   Input *i = unsmob_input (input);
267   if (i)
268     p->parser_error (*i, s);
269   else
270     p->parser_error (s);
271
272   return parser;
273 }