]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser-scheme.cc
Made most music creations in the parser go through syntax constructors.
[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 "input-smob.hh"
15 #include "international.hh"
16 #include "lily-lexer.hh"
17 #include "lily-parser.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   char const *file = scm_i_string_chars (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       /* 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         dir = dir_name (output_name_global);
77       if (dir != "" && dir != "." && dir != get_working_directory ())
78         {
79           global_path.prepend (get_working_directory ());
80           message (_f ("Changing working directory to `%s'",
81                        dir.c_str ()));
82           chdir (dir.c_str ());
83         }
84       else
85         out_file_name = File_name (output_name_global);
86     }
87
88   string init;
89   if (!init_name_global.empty ())
90     init = init_name_global;
91   else
92     init = "init.ly";
93
94   string out_file = out_file_name.to_string ();
95
96   if (init.length () && global_path.find (init).empty ())
97     {
98       warning (_f ("can't find init file: `%s'", init));
99       warning (_f ("(search path: `%s')",
100                    global_path.to_string ().c_str ()));
101       exit (2);
102     }
103
104   if ((file_name != "-") && global_path.find (file_name).empty ())
105     {
106       warning (_f ("can't find file: `%s'", file_name));
107       scm_throw (ly_symbol2scm ("ly-file-failed"),
108                  scm_list_1 (scm_makfrom0str (file_name.c_str ())));
109     }
110   else
111     {
112       Sources sources;
113       sources.set_path (&global_path);
114
115       string mapped_fn = map_file_name (file_name);
116       message (_f ("Processing `%s'", mapped_fn.c_str ()));
117       progress_indication ("\n");
118
119       Lily_parser *parser = new Lily_parser (&sources);
120
121       parser->parse_file (init, file_name, out_file);
122
123       bool error = parser->error_level_;
124       parser->unprotect ();
125       parser = 0;
126       if (error)
127         /* TODO: pass renamed input file too.  */
128         scm_throw (ly_symbol2scm ("ly-file-failed"),
129                    scm_list_1 (scm_makfrom0str (file_name.c_str ())));
130     }
131   return SCM_UNSPECIFIED;
132 }
133
134 LY_DEFINE (ly_parse_string, "ly:parse-string",
135            1, 0, 0, (SCM ly_code),
136            "Parse the string LY_CODE.  "
137            "Upon failure, throw @code{ly-file-failed} key.")
138 {
139   SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
140
141   Sources sources;
142   sources.set_path (&global_path);
143   Lily_parser *parser = new Lily_parser (&sources);
144   parser->parse_string (ly_scm2string (ly_code));
145   parser->unprotect ();
146   parser = 0;
147
148   return SCM_UNSPECIFIED;
149 }
150
151 LY_DEFINE (ly_clone_parser, "ly:clone-parser",
152            1, 0, 0, (SCM parser_smob),
153            "Return a clone of PARSER_SMOB.")
154 {
155   Lily_parser *parser = unsmob_lily_parser (parser_smob);
156   Lily_parser *clone = new Lily_parser (*parser);
157
158   return clone->unprotect ();
159 }
160
161 LY_DEFINE (ly_parser_define, "ly:parser-define!",
162            3, 0, 0, (SCM parser_smob, SCM symbol, SCM val),
163            "Bind SYMBOL to VAL in PARSER_SMOB's module.")
164 {
165   Lily_parser *parser = unsmob_lily_parser (parser_smob);
166   SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
167   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
168
169   parser->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
170   return SCM_UNSPECIFIED;
171 }
172
173 LY_DEFINE (ly_parser_lookup, "ly:parser-lookup",
174            2, 0, 0, (SCM parser_smob, SCM symbol),
175            "Lookup @var{symbol} in @var{parser_smob}'s module.  "
176            "Undefined is '().")
177 {
178   Lily_parser *parser = unsmob_lily_parser (parser_smob);
179
180   SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
181   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
182
183   SCM val = parser->lexer_->lookup_identifier (ly_scm2string (scm_symbol_to_string (symbol)));
184   if (val != SCM_UNDEFINED)
185     return val;
186   else
187     return SCM_EOL;
188 }
189
190 LY_DEFINE (ly_parser_parse_string, "ly:parser-parse-string",
191            2, 0, 0, (SCM parser_smob, SCM ly_code),
192            "Parse the string LY_CODE with PARSER_SMOB."
193            "Upon failure, throw @code{ly-file-failed} key.")
194 {
195   Lily_parser *parser = unsmob_lily_parser (parser_smob);
196
197   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
198   SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG2, __FUNCTION__, "string");
199
200   parser->parse_string (ly_scm2string (ly_code));
201
202   return SCM_UNSPECIFIED;
203 }
204
205 LY_DEFINE (ly_parser_set_note_names, "ly:parser-set-note-names",
206            2, 0, 0, (SCM parser, SCM names),
207            "Replace current note names in @var{parser}. "
208            "@var{names} is an alist of symbols.  "
209            "This only has effect if the current mode is notes.")
210 {
211   Lily_parser *p = unsmob_lily_parser (parser);
212   SCM_ASSERT_TYPE (p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
213
214   if (p->lexer_->is_note_state ())
215     {
216       p->lexer_->pop_state ();
217       p->lexer_->push_note_state (alist_to_hashq (names));
218     }
219
220   return SCM_UNSPECIFIED;
221 }
222
223 LY_DEFINE (ly_parser_output_name, "ly:parser-output-name",
224            1, 0, 0, (SCM parser),
225            "Return the base name of the output file.")
226 {
227   Lily_parser *p = unsmob_lily_parser (parser);
228   SCM_ASSERT_TYPE (p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
229
230   return scm_makfrom0str (p->output_basename_.c_str ());
231 }
232
233 LY_DEFINE (ly_parser_error, "ly:parser-error",
234            2, 1, 0, (SCM parser, SCM msg, SCM input),
235            "Display an error message, and make the parser fail")
236 {
237   Lily_parser *p = unsmob_lily_parser (parser);
238   SCM_ASSERT_TYPE (p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
239   SCM_ASSERT_TYPE (scm_is_string (msg), msg, SCM_ARG2, __FUNCTION__, "string");
240   string s = ly_scm2string (msg);
241   
242   Input *i = unsmob_input (input);
243   if (i)
244     p->parser_error (*i, s);
245   else
246     p->parser_error (s);
247
248   return parser;
249 }