]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser.cc
16af0c3cdf818135dc8686c97b362eed0a0e0fde
[lilypond.git] / lily / lily-parser.cc
1 /*
2   lily-parser.cc -- implement Lily_parser
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
7   Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 #include "lily-parser.hh"
11
12 #include "book.hh"
13 #include "file-name.hh"
14 #include "file-path.hh"
15 #include "international.hh"
16 #include "lily-lexer.hh"
17 #include "lily-version.hh"
18 #include "ly-module.hh"
19 #include "main.hh"
20 #include "output-def.hh"
21 #include "paper-book.hh"
22 #include "parser.hh"
23 #include "score.hh"
24 #include "sources.hh"
25 #include "text-metrics.hh"
26 #include "warn.hh"
27 #include "program-option.hh"
28
29 #include "ly-smobs.icc"
30
31 Lily_parser::Lily_parser (Sources *sources)
32 {
33   lexer_ = 0;
34   sources_ = sources;
35   default_duration_ = Duration (2, 0);
36   error_level_ = 0;
37
38   smobify_self ();
39
40   lexer_ = new Lily_lexer (sources_, this);
41   lexer_->unprotect ();
42 }
43
44 Lily_parser::Lily_parser (Lily_parser const &src)
45 {
46   lexer_ = 0;
47   sources_ = src.sources_;
48   default_duration_ = src.default_duration_;
49   error_level_ = src.error_level_;
50   output_basename_ = src.output_basename_;
51
52   smobify_self ();
53   if (src.lexer_)
54     {
55       lexer_ = new Lily_lexer (*src.lexer_, this);
56     }
57   
58   lexer_->unprotect ();
59 }
60
61 Lily_parser::~Lily_parser ()
62 {
63 }
64
65
66 SCM
67 Lily_parser::mark_smob (SCM s)
68 {
69   Lily_parser *parser = (Lily_parser *) SCM_CELL_WORD_1 (s);
70   return (parser->lexer_) ? parser->lexer_->self_scm () : SCM_EOL;
71 }
72
73 int
74 Lily_parser::print_smob (SCM s, SCM port, scm_print_state*)
75 {
76   scm_puts ("#<Lily_parser ", port);
77   Lily_parser *parser = (Lily_parser *) SCM_CELL_WORD_1 (s);
78   if (parser->lexer_)
79     scm_display (parser->lexer_->self_scm (), port);
80   else
81     scm_puts ("(no lexer yet)", port);
82   scm_puts (" >", port);
83   return 1;
84 }
85
86 /* Process one .ly file, or book.  */
87 void
88 Lily_parser::parse_file (string init, string name, string out_name)
89 {
90   if (get_output_backend_name () == "tex")
91     try_load_text_metrics (out_name);
92
93   // TODO: use $parser 
94   lexer_->set_identifier (ly_symbol2scm ("parser"), self_scm ());
95   output_basename_ = out_name;
96
97   lexer_->main_input_name_ = name;
98
99   message (_ ("Parsing..."));
100
101   set_yydebug (0);
102
103   lexer_->new_input (init, sources_);
104
105   File_name f (name);
106   string s = global_path.find (f.base_ + ".twy");
107   s = gulp_file_to_string (s, false, -1);
108   scm_eval_string (ly_string2scm (s));
109
110   /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
111      OUT_FILE (unless IN_FILE redefines output file name).  */
112
113   SCM mod = lexer_->set_current_scope ();
114   do_yyparse ();
115
116   /*
117     Don't mix cyclic pointers with weak tables.
118   */
119   lexer_->set_identifier (ly_symbol2scm ("parser"),
120                           SCM_EOL);
121   ly_reexport_module (scm_current_module ());
122
123   scm_set_current_module (mod);
124
125   if (!define_spots_.empty ())
126     {
127       define_spots_.back ().warning (_ ("braces do not match"));
128       error_level_ = 1;
129     }
130
131   error_level_ = error_level_ | lexer_->error_level_;
132   clear ();
133 }
134
135 void
136 Lily_parser::parse_string (string ly_code)
137 {
138   // TODO: use $parser 
139   lexer_->set_identifier (ly_symbol2scm ("parser"),
140                           self_scm ());
141
142   lexer_->main_input_name_ = "<string>";
143   lexer_->is_main_input_ = true; 
144   lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
145
146   SCM mod = lexer_->set_current_scope ();
147   do_yyparse ();
148   scm_set_current_module (mod);
149
150   if (!define_spots_.empty ())
151     {
152       if (define_spots_.empty ()
153           && !error_level_)
154         programming_error ("define_spots_ don't match, but error_level_ not set.");
155     }
156
157   error_level_ = error_level_ | lexer_->error_level_;
158 }
159
160 void
161 Lily_parser::clear ()
162 {
163   if (lexer_)
164     {
165       while (lexer_->has_scope ())
166         lexer_->remove_scope ();
167     }
168
169   lexer_ = 0;  
170 }
171
172 char const *
173 Lily_parser::here_str0 () const
174 {
175   return lexer_->here_str0 ();
176 }
177
178 void
179 Lily_parser::parser_error (string s)
180 {
181   lexer_->here_input ().error (_ (s.c_str ()));
182   error_level_ = 1;
183 }
184
185 void
186 Lily_parser::parser_error (Input const &i, string s)
187 {
188   i.error (s);
189   error_level_ = 1;
190 }
191
192
193
194 IMPLEMENT_SMOBS (Lily_parser);
195 IMPLEMENT_TYPE_P (Lily_parser, "ly:lily-parser?");
196 IMPLEMENT_DEFAULT_EQUAL_P (Lily_parser);
197
198
199 /****************************************************************
200   OUTPUT-DEF 
201  ****************************************************************/
202
203 Output_def *
204 get_layout (Lily_parser *parser)
205 {
206   SCM id = parser->lexer_->lookup_identifier ("$defaultlayout");
207   Output_def *layout = unsmob_output_def (id);
208   layout = layout ? layout->clone () : new Output_def;
209   layout->set_variable (ly_symbol2scm ("is-layout"), SCM_BOOL_T);
210     
211   return layout;
212 }
213
214 Output_def *
215 get_midi (Lily_parser *parser)
216 {
217   SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
218   Output_def *layout = unsmob_output_def (id);
219   layout = layout ? layout->clone () : new Output_def;
220   layout->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
221   return layout;
222 }
223
224 /* Return a copy of the top of $papers stack, or $defaultpaper if the
225  * stack is empty */
226 Output_def *
227 get_paper (Lily_parser *parser)
228 {
229   SCM papers = parser->lexer_->lookup_identifier ("$papers");
230   Output_def *layout = ((papers == SCM_UNDEFINED) || scm_is_null (papers)) ?
231     0 : unsmob_output_def (scm_car (papers));
232   SCM default_paper = parser->lexer_->lookup_identifier ("$defaultpaper");
233   layout = layout ? layout : unsmob_output_def (default_paper);
234
235   layout = layout ? dynamic_cast<Output_def *> (layout->clone ()) : new Output_def;
236   layout->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
237   return layout;
238 }
239
240 /* Initialize (reset) the $papers stack */
241 void
242 init_papers (Lily_parser *parser)
243 {
244   parser->lexer_->set_identifier (ly_symbol2scm ("$papers"), SCM_EOL);
245 }
246
247 /* Push a paper on top of $papers stack */
248 void
249 push_paper (Lily_parser *parser, Output_def *paper)
250 {
251   parser->lexer_->set_identifier (ly_symbol2scm ("$papers"),
252                                   scm_cons (paper->self_scm (),
253                                             parser->lexer_->lookup_identifier ("$papers")));
254 }
255
256 /* Pop a paper from $papers stack */
257 void
258 pop_paper (Lily_parser *parser)
259 {
260   if (! scm_is_null (parser->lexer_->lookup_identifier ("$papers")))
261     parser->lexer_->set_identifier (ly_symbol2scm ("$papers"),
262                                     scm_cdr (parser->lexer_->lookup_identifier ("$papers")));
263 }
264
265 /* Change the paper on top of $papers stack */
266 void
267 set_paper (Lily_parser *parser, Output_def *paper)
268 {
269   scm_set_car_x (parser->lexer_->lookup_identifier ("$papers"), paper->self_scm ());
270 }
271
272 SCM
273 get_header (Lily_parser *parser)
274 {
275   SCM id = parser->lexer_->lookup_identifier ("$defaultheader");
276   if (!ly_is_module (id))
277     id = parser->make_scope ();
278   else
279     {
280       SCM nid = parser->make_scope ();
281       ly_module_copy (nid, id);
282       id = nid;
283     }
284   
285   return id;
286 }
287
288 SCM 
289 Lily_parser::make_scope () const
290 {
291   SCM module = ly_make_anonymous_module (be_safe_global);
292   return module;    
293 }