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