]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser.cc
0893045193fade186ecdb3bce5f6d7b125208839
[lilypond.git] / lily / lily-parser.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5   Jan Nieuwenhuizen <janneke@gnu.org>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "lily-parser.hh"
22
23 #include "book.hh"
24 #include "file-name.hh"
25 #include "file-path.hh"
26 #include "international.hh"
27 #include "lily-lexer.hh"
28 #include "lily-version.hh"
29 #include "ly-module.hh"
30 #include "main.hh"
31 #include "output-def.hh"
32 #include "paper-book.hh"
33 #include "parser.hh"
34 #include "score.hh"
35 #include "source-file.hh"
36 #include "sources.hh"
37 #include "warn.hh"
38 #include "program-option.hh"
39
40 using std::string;
41
42 Lily_parser::Lily_parser (Sources *sources)
43 {
44   lexer_ = 0;
45   sources_ = sources;
46   default_duration_ = Duration (2, 0);
47   default_tremolo_type_ = 8;
48   error_level_ = 0;
49   closures_ = SCM_EOL;
50
51   smobify_self ();
52
53   lexer_ = new Lily_lexer (sources_, this);
54   lexer_->unprotect ();
55 }
56
57 Lily_parser::Lily_parser (Lily_parser const &src, SCM closures, SCM location)
58   : Smob<Lily_parser> ()
59 {
60   lexer_ = 0;
61   sources_ = src.sources_;
62   default_duration_ = src.default_duration_;
63   default_tremolo_type_ = src.default_tremolo_type_;
64   error_level_ = 0;
65   output_basename_ = src.output_basename_;
66   closures_ = closures;
67
68   smobify_self ();
69   if (src.lexer_)
70     {
71       lexer_ = new Lily_lexer (*src.lexer_, this, location);
72       lexer_->unprotect ();
73     }
74 }
75
76 Lily_parser::~Lily_parser ()
77 {
78 }
79
80 SCM
81 Lily_parser::mark_smob () const
82 {
83   scm_gc_mark (closures_);
84   return (lexer_) ? lexer_->self_scm () : SCM_EOL;
85 }
86
87 int
88 Lily_parser::print_smob (SCM port, scm_print_state *) const
89 {
90   scm_puts ("#<Lily_parser ", port);
91   if (lexer_)
92     scm_display (lexer_->self_scm (), port);
93   else
94     scm_puts ("(no lexer yet)", port);
95   scm_puts (" >", port);
96   return 1;
97 }
98
99 /* Process one .ly file, or book.  */
100 void
101 Lily_parser::parse_file (const string &init, const string &name, const string &out_name)
102 {
103   output_basename_ = out_name;
104
105   lexer_->main_input_name_ = name;
106
107   message (_ ("Parsing..."));
108
109   set_yydebug (0);
110
111   lexer_->new_input (init, sources_);
112
113   File_name f (name);
114   string s = global_path.find (f.base_ + ".twy");
115   s = gulp_file_to_string (s, false, -1);
116   scm_eval_string (ly_string2scm (s));
117
118   /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
119      OUT_FILE (unless IN_FILE redefines output file name).  */
120
121   SCM mod = lexer_->set_current_scope ();
122   do
123     {
124       do_yyparse ();
125     }
126   while (!lexer_->is_clean ());
127
128   ly_reexport_module (scm_current_module ());
129
130   scm_set_current_module (mod);
131
132   error_level_ = error_level_ | lexer_->error_level_;
133   clear ();
134 }
135
136 void
137 Lily_parser::parse_string (const string &ly_code)
138 {
139   lexer_->main_input_name_ = "<string>";
140   lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
141
142   SCM mod = lexer_->set_current_scope ();
143
144   do_yyparse ();
145
146   scm_set_current_module (mod);
147
148   error_level_ = error_level_ | lexer_->error_level_;
149 }
150
151 SCM
152 Lily_parser::parse_string_expression (const string &ly_code, const string &filename,
153                                       int line)
154 {
155   lexer_->main_input_name_ = filename;
156   lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
157   if (line)
158     {
159       lexer_->get_source_file ()->set_line (0, line);
160     }
161   SCM mod = lexer_->set_current_scope ();
162
163   lexer_->push_extra_token (Input (), EMBEDDED_LILY);
164   SCM result = do_yyparse ();
165
166   scm_set_current_module (mod);
167
168   error_level_ = error_level_ | lexer_->error_level_;
169   return result;
170 }
171
172 void
173 Lily_parser::include_string (const string &ly_code)
174 {
175   lexer_->new_input ("<included string>", ly_code, sources_);
176 }
177
178 void
179 Lily_parser::clear ()
180 {
181   if (lexer_)
182     {
183       while (lexer_->has_scope ())
184         lexer_->remove_scope ();
185     }
186
187   lexer_ = 0;
188 }
189
190 void
191 Lily_parser::parser_error (const string &s)
192 {
193   lexer_->here_input ().non_fatal_error (_ (s.c_str ()));
194   error_level_ = 1;
195 }
196
197 void
198 Lily_parser::parser_error (Input const &i, const string &s)
199 {
200   i.non_fatal_error (s);
201   error_level_ = 1;
202 }
203
204 const char Lily_parser::type_p_name_[] = "ly:lily-parser?";
205
206 /****************************************************************
207   OUTPUT-DEF
208  ****************************************************************/
209
210 Output_def *
211 get_layout (Lily_parser *parser)
212 {
213   SCM id = parser->lexer_->lookup_identifier ("$defaultlayout");
214   Output_def *layout = unsmob<Output_def> (id);
215   layout = layout ? layout->clone () : new Output_def;
216   layout->set_variable (ly_symbol2scm ("is-layout"), SCM_BOOL_T);
217
218   return layout;
219 }
220
221 Output_def *
222 get_midi (Lily_parser *parser)
223 {
224   SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
225   Output_def *layout = unsmob<Output_def> (id);
226   layout = layout ? layout->clone () : new Output_def;
227   layout->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
228   return layout;
229 }
230
231 /* Return a copy of the top of $papers stack, or $defaultpaper if the
232  * stack is empty */
233 Output_def *
234 get_paper (Lily_parser *parser)
235 {
236   SCM papers = parser->lexer_->lookup_identifier ("$papers");
237   Output_def *layout = (SCM_UNBNDP (papers) || scm_is_null (papers))
238                        ? 0 : unsmob<Output_def> (scm_car (papers));
239   SCM default_paper = parser->lexer_->lookup_identifier ("$defaultpaper");
240   layout = layout ? layout : unsmob<Output_def> (default_paper);
241
242   layout = layout ? dynamic_cast<Output_def *> (layout->clone ()) : new Output_def;
243   layout->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
244   return layout;
245 }
246
247 /* Initialize (reset) the $papers stack */
248 void
249 init_papers (Lily_parser *parser)
250 {
251   parser->lexer_->set_identifier (ly_symbol2scm ("$papers"), SCM_EOL);
252 }
253
254 /* Push a paper on top of $papers stack */
255 void
256 push_paper (Lily_parser *parser, Output_def *paper)
257 {
258   parser->lexer_->set_identifier (ly_symbol2scm ("$papers"),
259                                   scm_cons (paper->self_scm (),
260                                             parser->lexer_->lookup_identifier ("$papers")));
261 }
262
263 /* Pop a paper from $papers stack */
264 void
265 pop_paper (Lily_parser *parser)
266 {
267   if (scm_is_pair (parser->lexer_->lookup_identifier ("$papers")))
268     parser->lexer_->set_identifier (ly_symbol2scm ("$papers"),
269                                     scm_cdr (parser->lexer_->lookup_identifier ("$papers")));
270 }
271
272 /* Change the paper on top of $papers stack */
273 void
274 set_paper (Lily_parser *parser, Output_def *paper)
275 {
276   scm_set_car_x (parser->lexer_->lookup_identifier ("$papers"), paper->self_scm ());
277 }
278
279 SCM
280 get_header (Lily_parser *parser)
281 {
282   SCM id = parser->lexer_->lookup_identifier ("$defaultheader");
283   if (!ly_is_module (id))
284     id = parser->make_scope ();
285   else
286     {
287       SCM nid = parser->make_scope ();
288       ly_module_copy (nid, id);
289       id = nid;
290     }
291
292   return id;
293 }
294
295 SCM
296 Lily_parser::make_scope () const
297 {
298   SCM module = ly_make_module (be_safe_global);
299   return module;
300 }