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