]> git.donarmstrong.com Git - lilypond.git/blob - lily/translator-def.cc
f48e78d3aa3a9d0c979bcb1e0888bbeb9ca9f720
[lilypond.git] / lily / translator-def.cc
1 /*   
2   translator-def.cc --  implement Context_def
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 2000--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "lily-proto.hh"
11 #include "context-def.hh"
12 #include "translator-group.hh"
13 #include "warn.hh"
14 #include "music-output-def.hh"
15 #include "ly-smobs.icc"
16
17 int
18 Context_def::print_smob (SCM smob, SCM port, scm_print_state*)
19 {
20   Context_def* me = (Context_def*) SCM_CELL_WORD_1 (smob);
21
22   scm_puts ("#<Context_def ", port);
23   scm_display (me->context_name_, port);
24   scm_puts (">", port);
25   return 1;
26 }
27
28
29 SCM
30 Context_def::mark_smob (SCM smob)
31 {
32   Context_def* me = (Context_def*) SCM_CELL_WORD_1 (smob);
33
34   scm_gc_mark (me->description_);
35   scm_gc_mark (me->context_aliases_);
36   scm_gc_mark (me->accept_mods_);
37   scm_gc_mark (me->translator_mods_);
38   scm_gc_mark (me->property_ops_);  
39   scm_gc_mark (me->translator_group_type_);
40   return me->context_name_;
41 }
42
43
44 Context_def::Context_def ()
45 {
46   context_aliases_ = SCM_EOL;
47   translator_group_type_ = SCM_EOL;
48   accept_mods_ = SCM_EOL;
49   translator_mods_ = SCM_EOL;
50   property_ops_ = SCM_EOL;
51   context_name_ = SCM_EOL;
52   description_ = SCM_EOL;
53
54   smobify_self();
55 }
56
57 Context_def::~Context_def ()
58 {
59 }
60
61 Context_def::Context_def (Context_def const & s)
62   : Input (s)
63 {
64   context_aliases_ = SCM_EOL;
65   translator_group_type_ = SCM_EOL;
66   accept_mods_ = SCM_EOL;   
67   translator_mods_ = SCM_EOL;
68   property_ops_ = SCM_EOL;
69   context_name_ = SCM_EOL;
70   description_ = SCM_EOL;
71   
72   smobify_self();
73   description_ = s.description_;
74
75   accept_mods_ = s.accept_mods_;
76   property_ops_ = s.property_ops_;
77   translator_mods_ = s.translator_mods_;
78   context_aliases_ = s.context_aliases_;
79   translator_group_type_ = s.translator_group_type_;
80   context_name_ = s.context_name_;
81 }
82
83
84 void
85 Context_def::add_context_mod (SCM mod)
86 {
87   SCM tag  = gh_car (mod);
88   if (ly_symbol2scm ("description")  == tag)
89     {
90       description_ = gh_cadr (mod);
91       return ;
92     }
93
94   SCM sym = gh_cadr (mod);
95   if (gh_string_p (sym))
96     sym = scm_string_to_symbol (sym);
97   
98   if (ly_symbol2scm ("consists") == tag
99       || ly_symbol2scm ("consists-end") == tag
100       || ly_symbol2scm ("remove") == tag)
101     {
102       if (!get_translator (sym))
103         error (_f ("Program has no such type: `%s'", ly_symbol2string (sym).to_str0 ()));
104       else
105         translator_mods_ = gh_cons (scm_list_2 (tag, sym), translator_mods_ );
106     }
107   else if (ly_symbol2scm ("accepts") == tag
108            || ly_symbol2scm ("denies") == tag)
109     {
110       accept_mods_ = gh_cons (scm_list_2 (tag, sym), accept_mods_); 
111     }
112   else if (ly_symbol2scm ("poppush") == tag
113            || ly_symbol2scm ("pop") == tag
114            || ly_symbol2scm ("push") == tag
115            || ly_symbol2scm ("assign") == tag
116            || ly_symbol2scm ("unset") == tag)
117     {
118       property_ops_ = gh_cons (mod, property_ops_);
119     }
120   else if (ly_symbol2scm ("alias") == tag)
121     {
122       context_aliases_ = gh_cons (sym, context_aliases_);
123     }
124   else if (ly_symbol2scm ("translator-type")  == tag)
125     {
126       translator_group_type_ = sym;
127     }
128   else if (ly_symbol2scm ("context-name")  == tag)
129     {
130       context_name_ = sym;
131     }
132   else
133     {
134       programming_error ("Unknown context mod tag.");
135     }
136 }
137
138
139
140 SCM
141 Context_def::get_context_name () const
142 {
143   return context_name_;
144 }
145
146 SCM
147 Context_def::get_accepted (SCM user_mod) const
148 {
149   SCM mods = scm_reverse_x (scm_list_copy (accept_mods_),
150                             user_mod);
151   SCM acc = SCM_EOL;
152   for (SCM s = mods; gh_pair_p (s); s = gh_cdr (s))
153     {
154       SCM tag = gh_caar (s);
155       SCM sym = gh_cadar (s);
156       if (tag == ly_symbol2scm ("accepts"))
157         acc = gh_cons (sym, acc);
158       else if (tag == ly_symbol2scm ("denies"))
159         acc = scm_delete_x (sym, acc);
160     }
161   return acc;
162 }
163
164            
165 Link_array<Context_def>
166 Context_def::path_to_acceptable_translator (SCM type_sym, Music_output_def* odef) const
167 {
168   assert (gh_symbol_p (type_sym));
169   
170   SCM accepted = get_accepted (SCM_EOL);
171
172   Link_array<Context_def> accepteds;
173   for (SCM s = accepted; gh_pair_p (s); s = ly_cdr (s))
174     {
175       Context_def *t = unsmob_context_def (odef->find_translator (ly_car (s)));
176       if (!t)
177         continue;
178       accepteds.push (t);
179     }
180
181   Link_array<Context_def> best_result;
182   for (int i=0; i < accepteds.size (); i++)
183     {
184       /*
185         don't check aliases, because \context Staff should not create RhythmicStaff.
186       */
187       if (gh_equal_p (accepteds[i]->get_context_name (), type_sym))
188         {
189           best_result.push (accepteds[i]);
190           return best_result;
191         }
192     }
193       
194   int best_depth= INT_MAX;
195   for (int i=0; i < accepteds.size (); i++)
196     {
197       Context_def * g = accepteds[i];
198
199       Link_array<Context_def> result
200         = g->path_to_acceptable_translator (type_sym, odef);
201       if (result.size () && result.size () < best_depth)
202         {
203           result.insert (g,0);
204           best_result = result;
205
206           /*
207             this following line was added in 1.9.3, but hsould've been
208             there all along... Let's hope it doesn't cause nightmares.
209            */
210           best_depth = result.size();
211         }
212     }
213
214   return best_result;
215 }
216
217 IMPLEMENT_SMOBS (Context_def);
218 IMPLEMENT_DEFAULT_EQUAL_P (Context_def);
219
220
221
222
223 SCM
224 Context_def::get_translator_names (SCM user_mod) const
225 {
226   SCM l1 = SCM_EOL;
227   SCM l2 = SCM_EOL;
228
229   SCM mods = scm_reverse_x (scm_list_copy (translator_mods_),
230                             user_mod);
231   
232   for (SCM s = mods; gh_pair_p (s); s = gh_cdr (s))
233     {
234       SCM tag = gh_caar (s);
235       SCM arg = gh_cadar (s);
236
237       if (gh_string_p (arg))
238         arg = scm_string_to_symbol (arg);
239       
240       if (ly_symbol2scm ("consists") == tag)
241         l1 = gh_cons (arg, l1);
242       else if (ly_symbol2scm ("consists-end") == tag)
243         l2 = gh_cons (arg, l2);
244       else if (ly_symbol2scm ("remove") == tag)
245         {
246           l1 = scm_delete_x (arg, l1);
247           l2 = scm_delete_x (arg, l2);
248         }
249     }
250
251   return scm_append_x (scm_list_2 (l1, l2));
252 }
253
254
255 Translator_group *
256 Context_def::instantiate (SCM ops)
257 {
258   Translator * g = get_translator (translator_group_type_);
259   g = g->clone (); 
260
261   Translator_group *tg = dynamic_cast<Translator_group*> (g);
262   tg->definition_ = self_scm ();
263
264   SCM trans_names = get_translator_names (ops); 
265   tg->simple_trans_list_ = names_to_translators (trans_names, tg);
266   tg->accepts_list_ = get_accepted  (ops);
267   return tg;
268 }
269
270
271 SCM
272 Context_def::clone_scm () const
273 {
274   Context_def * t = new Context_def (*this);
275   scm_gc_unprotect_object (t->self_scm());
276   return t->self_scm();
277 }
278
279 SCM
280 Context_def::make_scm ()
281 {
282   Context_def* t = new Context_def;
283   scm_gc_unprotect_object (t->self_scm());
284   return t->self_scm();
285 }
286
287 void
288 Context_def::apply_default_property_operations (Translator_group *tg)
289 {
290   apply_property_operations (tg , property_ops_);
291 }
292
293 SCM
294 Context_def::to_alist () const
295 {
296   SCM l = SCM_EOL;
297
298   l = gh_cons (gh_cons (ly_symbol2scm ("consists"),
299                         get_translator_names (SCM_EOL)), l);
300   l = gh_cons (gh_cons (ly_symbol2scm ("description"),  description_), l);
301   l = gh_cons (gh_cons (ly_symbol2scm ("aliases"),  context_aliases_), l);
302   l = gh_cons (gh_cons (ly_symbol2scm ("accepts"),  get_accepted (SCM_EOL)), l);
303   l = gh_cons (gh_cons (ly_symbol2scm ("property-ops"),  property_ops_), l);
304   l = gh_cons (gh_cons (ly_symbol2scm ("context-name"),  context_name_), l);
305   l = gh_cons (gh_cons (ly_symbol2scm ("group-type"),  translator_group_type_), l);    
306
307   return l;  
308 }
309
310 bool
311 Context_def::is_alias (SCM sym) const
312 {
313   bool b  = sym == context_name_;
314
315   for (SCM a = context_aliases_; !b && gh_pair_p (a); a = ly_cdr (a))
316     b = b || sym == ly_car (a);
317
318   return b;
319 }