]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser-scheme.cc
(parse_symbol_list): Bugfix.
[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 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include <unistd.h>
10
11 #include "file-name.hh"
12 #include "file-path.hh"
13 #include "main.hh"
14 #include "lily-parser.hh"
15 #include "warn.hh"
16 #include "source.hh"
17 #include "lily-lexer.hh"
18 #include "ly-module.hh"
19 #include "file-name-map.hh"
20
21 /* Do not append `!' suffix, since 1st argument is not modified. */
22 LY_DEFINE (ly_set_point_and_click, "ly:set-point-and-click",
23            1, 0, 0, (SCM what),
24            "Deprecated.")
25 {
26   (void) what;
27   warning (_f ("deprecated function called: %s", "ly:set-point-and-click"));
28   return SCM_UNSPECIFIED;
29 }
30
31 LY_DEFINE (ly_parse_file, "ly:parse-file",
32            1, 0, 0, (SCM name),
33            "Parse a single @code{.ly} file.  "
34            "Upon failure, throw @code{ly-file-failed} key.")
35 {
36   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
37   char const *file = scm_i_string_chars (name);
38   char const *extensions[] = {"ly", "", 0};
39
40   String file_name = global_path.find (file, extensions);
41
42   /* By default, use base name of input file for output file name,
43      write output to cwd; do not use root and directory parts of input
44      file name.  */
45   File_name out_file_name (file_name);
46
47   global_path.append (out_file_name.dir_);
48
49   out_file_name.ext_ = "";
50   out_file_name.root_ = "";
51   out_file_name.dir_ = "";
52
53   /* When running from gui, generate output in .ly source directory.  */
54   if (output_name_global.is_empty ()
55       && scm_call_0 (ly_lily_module_constant ("running-from-gui?")) == SCM_BOOL_T)
56     {
57       File_name f (file);
58       f.base_ = "";
59       f.ext_ = "";
60       output_name_global = f.to_string ();
61     }
62
63   if (!output_name_global.is_empty ())
64     {
65       /* Interpret --output=DIR to mean --output=DIR/BASE.  */
66       if (is_dir (output_name_global))
67         {
68           char cwd[PATH_MAX];
69           getcwd (cwd, PATH_MAX);
70
71           if (output_name_global != cwd)
72             {
73               global_path.prepend (cwd);
74               message (_f ("Changing working directory to `%s'",
75                            output_name_global.to_str0 ()));
76               chdir (output_name_global.to_str0 ());
77             }
78           output_name_global = "";
79         }
80       else
81         out_file_name = File_name (output_name_global);
82     }
83
84   String init;
85   if (!init_name_global.is_empty ())
86     init = init_name_global;
87   else
88     init = "init.ly";
89
90   String out_file = out_file_name.to_string ();
91
92   if (init.length () && global_path.find (init).is_empty ())
93     {
94       warning (_f ("can't find init file: `%s'", init));
95       warning (_f ("(search path: `%s')",
96                    global_path.to_string ().to_str0 ()));
97       exit (2);
98     }
99
100   if ((file_name != "-") && global_path.find (file_name).is_empty ())
101     {
102       warning (_f ("can't find file: `%s'", file_name));
103       scm_throw (ly_symbol2scm ("ly-file-failed"),
104                  scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
105     }
106   else
107     {
108       Sources sources;
109       sources.set_path (&global_path);
110
111       String mapped_fn = map_file_name (file_name);
112       message (_f ("Processing `%s'", mapped_fn.to_str0 ()));
113       progress_indication ("\n");
114
115       Lily_parser *parser = new Lily_parser (&sources);
116
117       parser->parse_file (init, file_name, out_file);
118
119       bool error = parser->error_level_;
120       parser->unprotect ();
121       parser = 0;
122       if (error)
123         /* TODO: pass renamed input file too.  */
124         scm_throw (ly_symbol2scm ("ly-file-failed"),
125                    scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
126     }
127   return SCM_UNSPECIFIED;
128 }
129
130 LY_DEFINE (ly_parse_string, "ly:parse-string",
131            1, 0, 0, (SCM ly_code),
132            "Parse the string LY_CODE.  "
133            "Upon failure, throw @code{ly-file-failed} key.")
134 {
135   SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
136
137   Sources sources;
138   sources.set_path (&global_path);
139   Lily_parser *parser = new Lily_parser (&sources);
140   parser->parse_string (ly_scm2string (ly_code));
141   parser->unprotect ();
142   parser = 0;
143
144   return SCM_UNSPECIFIED;
145 }
146
147 LY_DEFINE (ly_clone_parser, "ly:clone-parser",
148            1, 0, 0, (SCM parser_smob),
149            "Return a clone of PARSER_SMOB.")
150 {
151   Lily_parser *parser = unsmob_lily_parser (parser_smob);
152   Lily_parser *clone = new Lily_parser (*parser);
153
154   return clone->unprotect ();
155 }
156
157 LY_DEFINE (ly_parser_define, "ly:parser-define!",
158            3, 0, 0, (SCM parser_smob, SCM symbol, SCM val),
159            "Bind SYMBOL to VAL in PARSER_SMOB's module.")
160 {
161   Lily_parser *parser = unsmob_lily_parser (parser_smob);
162   SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
163   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
164
165   parser->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
166   return SCM_UNSPECIFIED;
167 }
168
169 LY_DEFINE (ly_parser_lookup, "ly:parser-lookup",
170            2, 0, 0, (SCM parser_smob, SCM symbol),
171            "Lookup @var{symbol} in @var{parser_smob}'s module.  "
172            "Undefined is '().")
173 {
174   Lily_parser *parser = unsmob_lily_parser (parser_smob);
175
176   SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
177   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
178
179   SCM val = parser->lexer_->lookup_identifier (ly_scm2string (scm_symbol_to_string (symbol)));
180   if (val != SCM_UNDEFINED)
181     return val;
182   else
183     return SCM_EOL;
184 }
185
186 LY_DEFINE (ly_parser_parse_string, "ly:parser-parse-string",
187            2, 0, 0, (SCM parser_smob, SCM ly_code),
188            "Parse the string LY_CODE with PARSER_SMOB."
189            "Upon failure, throw @code{ly-file-failed} key.")
190 {
191   Lily_parser *parser = unsmob_lily_parser (parser_smob);
192
193   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
194   SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG2, __FUNCTION__, "string");
195
196   parser->parse_string (ly_scm2string (ly_code));
197
198   return SCM_UNSPECIFIED;
199 }
200
201 LY_DEFINE (ly_parser_set_note_names, "ly:parser-set-note-names",
202            2, 0, 0, (SCM parser, SCM names),
203            "Replace current note names in @var{parser}. "
204            "@var{names} is an alist of symbols.  "
205            "This only has effect if the current mode is notes.")
206 {
207   Lily_parser *p = unsmob_lily_parser (parser);
208   SCM_ASSERT_TYPE (p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
209
210   if (p->lexer_->is_note_state ())
211     {
212       p->lexer_->pop_state ();
213       p->lexer_->push_note_state (alist_to_hashq (names));
214     }
215
216   return SCM_UNSPECIFIED;
217 }
218
219 LY_DEFINE (ly_parser_output_name, "ly:parser-output-name",
220            1, 0, 0, (SCM parser),
221            "Return the base name of the output file.")
222 {
223   Lily_parser *p = unsmob_lily_parser (parser);
224   SCM_ASSERT_TYPE (p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
225
226   return scm_makfrom0str (p->output_basename_.to_str0 ());
227 }
228