]> git.donarmstrong.com Git - lilypond.git/blob - lily/context.cc
* lily/parser.yy (Music_list): add error-found to music with errors.
[lilypond.git] / lily / context.cc
1 /*   
2   context.cc --  implement Context
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004 Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8 */
9
10 #include "translator-group.hh"
11 #include "context-def.hh"
12 #include "context.hh"
13 #include "warn.hh"
14 #include "output-def.hh"
15 #include "scm-hash.hh"
16 #include "main.hh"
17 #include "ly-smobs.icc"
18 #include "score-context.hh"
19
20 bool
21 Context::is_removable () const
22 {
23   return context_list_ == SCM_EOL && ! iterator_count_ &&
24           !dynamic_cast<Score_context const*> (this);
25 }
26
27 void
28 Context::check_removal ()
29 {
30   for (SCM p = context_list_; scm_is_pair (p); p = scm_cdr (p))
31     {
32       Context *trg =  unsmob_context (scm_car (p));
33
34       trg->check_removal ();
35       if (trg->is_removable ())
36         {
37           recurse_over_translators (trg, &Translator::finalize, UP);
38           remove_context (trg);
39         }
40     }
41 }
42
43 Context::Context (Context const&)
44 {
45   assert (false);
46 }
47
48 Scheme_hash_table*
49 Context::properties_dict () const
50 {
51   return Scheme_hash_table::unsmob (properties_scm_);
52 }
53
54 void
55 Context::add_context (Context*t)
56 {
57   SCM ts = t->self_scm ();
58   context_list_ = ly_append2 (context_list_,
59                               scm_cons (ts, SCM_EOL));
60   
61   t->daddy_context_ = this;
62   if (!t->init_)
63     {
64       t->init_ = true;
65       
66       scm_gc_unprotect_object (ts);
67       Context_def * td = unsmob_context_def (t->definition_);
68
69       /*
70         this can not move before add_context (), because \override
71         operations require that we are in the hierarchy.
72       */
73       td->apply_default_property_operations (t);
74
75       recurse_over_translators (t, &Translator::initialize, DOWN);
76     }
77 }
78
79 Context::Context ()
80 {
81   daddy_context_ = 0;
82   init_ = false;
83   aliases_ = SCM_EOL;
84   iterator_count_  = 0;
85   implementation_ = SCM_EOL;
86   properties_scm_ = SCM_EOL;
87   accepts_list_ = SCM_EOL;
88   context_list_ = SCM_EOL;
89   definition_ = SCM_EOL;
90   
91   smobify_self ();
92
93   properties_scm_ = (new Scheme_hash_table)->self_scm ();
94   scm_gc_unprotect_object (properties_scm_);
95 }
96
97
98
99 Context*
100 Context::find_create_context (SCM n, String id,
101                               SCM operations)
102 {
103   /*
104     Don't create multiple score contexts.
105    */
106   if (dynamic_cast<Global_context*> (this)
107       && dynamic_cast<Global_context*> (this)->get_score_context ())
108     return get_score_context ()->find_create_context (n, id, operations);
109     
110   
111   Context * existing = find_context_below (this, n,id);
112   if (existing)
113     return existing;
114
115   if (n == ly_symbol2scm ("Bottom"))
116     {
117       Context* tg = get_default_interpreter ();
118       tg->id_string_ = id;
119       return tg;
120     }
121
122   /*
123     TODO: use accepts_list_.
124    */
125   Link_array<Context_def> path
126     = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
127
128   if (path.size ())
129     {
130       Context * current = this;
131
132       // start at 1.  The first one (index 0) will be us.
133       for (int i=0; i < path.size (); i++)
134         {
135           SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
136
137           Context * new_group
138             = path[i]->instantiate (ops);
139
140           if (i == path.size () -1)
141             {
142               new_group->id_string_ = id;
143             }
144
145           current->add_context (new_group);
146           apply_property_operations (new_group, ops);
147           
148           current = new_group;
149         }
150
151       return current;
152     }
153
154   /*
155     Don't go up to Global_context, because global goes down to
156     Score_context
157    */
158   Context *ret = 0;
159   if (daddy_context_ && !dynamic_cast<Global_context*> (daddy_context_))
160     ret = daddy_context_->find_create_context (n, id, operations);
161   else
162     {
163       warning (_f ("Cannot find or create `%s' called `%s'",
164                    ly_symbol2string (n).to_str0 (), id));
165       ret =0;
166     }
167   return ret;
168 }
169
170 /*
171   Default child context as a SCM string, or something else if there is
172   none.
173 */
174 SCM
175 Context::default_child_context_name () const
176 {
177   return scm_is_pair (accepts_list_)
178     ? scm_car (scm_last_pair (accepts_list_))
179     : SCM_EOL;
180 }
181
182
183 bool
184 Context::is_bottom_context () const
185 {
186   return !scm_is_symbol (default_child_context_name ());
187 }
188
189 Context*
190 Context::get_default_interpreter ()
191 {
192   if (!is_bottom_context ())
193     {
194       SCM nm = default_child_context_name ();
195       SCM st = find_context_def (get_output_def (), nm);
196
197       Context_def *t = unsmob_context_def (st);
198       if (!t)
199         {
200           warning (_f ("can't find or create: `%s'", ly_symbol2string (nm).to_str0 ()));
201           t = unsmob_context_def (this->definition_);
202         }
203       Context *tg = t->instantiate (SCM_EOL);
204       add_context (tg);
205       if (!tg->is_bottom_context ())
206         return tg->get_default_interpreter ();
207       else
208         return tg;
209     }
210   return this;
211 }
212
213 /*
214   PROPERTIES
215  */
216 Context*
217 Context::where_defined (SCM sym) const
218 {
219   if (properties_dict ()->contains (sym))
220     {
221       return (Context*)this;
222     }
223
224   return (daddy_context_) ? daddy_context_->where_defined (sym) : 0;
225 }
226
227 /*
228   return SCM_EOL when not found.
229 */
230 SCM
231 Context::internal_get_property (SCM sym) const
232 {
233   SCM val =SCM_EOL;
234   if (properties_dict ()->try_retrieve (sym, &val))
235     return val;
236
237   if (daddy_context_)
238     return daddy_context_->internal_get_property (sym);
239   
240   return val;
241 }
242
243 bool
244 Context::is_alias (SCM sym) const
245 {
246   if (sym == ly_symbol2scm ("Bottom")
247       && !scm_is_pair (accepts_list_))
248     return true;
249   if (sym == unsmob_context_def (definition_)->get_context_name ())
250     return true;
251   
252   return scm_c_memq (sym, aliases_) != SCM_BOOL_F;
253 }
254
255 void
256 Context::add_alias (SCM sym)
257 {
258   aliases_ = scm_cons (sym, aliases_);
259 }
260
261
262
263 void
264 Context::internal_set_property (SCM sym, SCM val)
265 {
266 #ifndef NDEBUG
267   if (internal_type_checking_global_b)
268     assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
269 #endif
270   
271   properties_dict ()->set (sym, val);
272 }
273
274 /*
275   TODO: look up to check whether we have inherited var? 
276  */
277 void
278 Context::unset_property (SCM sym)
279 {
280   properties_dict ()->remove (sym);
281 }
282
283 /**
284    Remove a context from the hierarchy.
285  */
286 Context *
287 Context::remove_context (Context*trans)
288 {
289   assert (trans);
290
291   context_list_ = scm_delq_x (trans->self_scm (), context_list_);
292   trans->daddy_context_ = 0;
293   return trans;
294 }
295
296 /*
297   ID == "" means accept any ID.
298  */
299 Context *
300 find_context_below (Context * where,
301                     SCM type, String id)
302 {
303   if (where->is_alias (type))
304     {
305       if (id == "" || where->id_string () == id)
306         return where;
307     }
308   
309   Context * found = 0;
310   for (SCM s = where->children_contexts ();
311        !found && scm_is_pair (s); s = scm_cdr (s))
312     {
313       Context * tr = unsmob_context (scm_car (s));
314
315       found = find_context_below (tr, type, id);
316     }
317
318   return found; 
319 }
320
321 SCM
322 Context::properties_as_alist () const
323 {
324   return properties_dict ()->to_alist ();
325 }
326
327 String
328 Context::context_name () const
329 {
330   Context_def * td = unsmob_context_def (definition_ );
331   return ly_symbol2string (td->get_context_name ());
332 }
333
334
335 Score_context*
336 Context::get_score_context () const
337 {
338   if (Score_context *sc =dynamic_cast<Score_context*> ((Context*)this))
339     return sc;
340   else if (daddy_context_)
341     return daddy_context_->get_score_context ();
342   else
343     return 0;
344 }
345
346 Output_def *
347 Context::get_output_def () const
348 {
349   return  (daddy_context_)
350     ? daddy_context_->get_output_def () : 0;
351 }
352
353 Context::~Context ()
354 {
355   
356 }
357
358 Moment
359 Context::now_mom () const
360 {
361   return daddy_context_->now_mom ();
362 }
363
364 int
365 Context::print_smob (SCM s, SCM port, scm_print_state *)
366 {
367   Context *sc = (Context *) scm_cdr (s);
368      
369   scm_puts ("#<", port);
370   scm_puts (classname (sc), port);
371   if (Context_def *d=unsmob_context_def (sc->definition_))
372     {
373       scm_puts (" ", port);
374       scm_display (d->get_context_name (), port);
375     }
376
377   if (Context *td=dynamic_cast<Context *> (sc))
378     {
379       scm_puts ("=", port);
380       scm_puts (td->id_string_.to_str0 (), port);
381     }
382   
383
384   scm_puts (" ", port);
385
386   scm_display (sc->context_list_, port);
387   scm_puts (" >", port);
388   
389   return 1;
390 }
391
392 SCM
393 Context::mark_smob (SCM sm)
394 {
395   Context * me = (Context*) SCM_CELL_WORD_1 (sm);
396   
397   scm_gc_mark (me->context_list_);
398   scm_gc_mark (me->aliases_);
399   scm_gc_mark (me->definition_);  
400   scm_gc_mark (me->properties_scm_);  
401   scm_gc_mark (me->accepts_list_);
402   scm_gc_mark (me->implementation_);
403
404   return me->properties_scm_;
405 }
406
407 IMPLEMENT_SMOBS (Context);
408 IMPLEMENT_DEFAULT_EQUAL_P (Context);
409 IMPLEMENT_TYPE_P (Context,"ly:context?");
410
411 bool
412 Context::try_music (Music* m)
413 {
414   Translator*  t = implementation ();
415   if (!t)
416     return false;
417   
418   bool b = t->try_music (m);
419   if (!b && daddy_context_)
420     b = daddy_context_->try_music (m);
421
422   return b;
423 }
424
425
426 Global_context*
427 Context::get_global_context () const
428 {
429   if (dynamic_cast<Global_context *>((Context*) this))
430     return dynamic_cast<Global_context *> ((Context*) this);
431
432   if (daddy_context_)
433     return daddy_context_->get_global_context ();
434
435   programming_error ("No Global context!");
436   return 0;
437 }
438
439 Context*
440 Context::get_parent_context () const
441 {
442   return daddy_context_;
443 }
444
445 Translator_group*
446 Context::implementation () const
447 {
448   return dynamic_cast<Translator_group*> (unsmob_translator (implementation_));
449 }