2 translator-def.cc -- implement Context_def
4 source file of the GNU LilyPond music typesetter
6 (c) 2000--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 /* TODO: should junk this class an replace by
10 a single list of context modifications? */
12 #include "context-def.hh"
14 #include "engraver.hh"
15 #include "output-def.hh"
16 #include "performer.hh"
17 #include "score-context.hh"
18 #include "translator-group.hh"
21 Context_def::Context_def ()
23 context_aliases_ = SCM_EOL;
24 translator_group_type_ = SCM_EOL;
25 accept_mods_ = SCM_EOL;
26 translator_mods_ = SCM_EOL;
27 property_ops_ = SCM_EOL;
28 context_name_ = SCM_EOL;
29 default_child_ = SCM_EOL;
30 description_ = SCM_EOL;
34 context_name_ = ly_symbol2scm ("");
37 Context_def::Context_def (Context_def const &s)
40 context_aliases_ = SCM_EOL;
41 translator_group_type_ = SCM_EOL;
42 accept_mods_ = SCM_EOL;
43 translator_mods_ = SCM_EOL;
44 property_ops_ = SCM_EOL;
45 context_name_ = SCM_EOL;
46 description_ = SCM_EOL;
47 default_child_ = SCM_EOL;
50 description_ = s.description_;
52 default_child_ = s.default_child_;
53 accept_mods_ = s.accept_mods_;
54 property_ops_ = s.property_ops_;
55 translator_mods_ = s.translator_mods_;
56 context_aliases_ = s.context_aliases_;
57 translator_group_type_ = s.translator_group_type_;
58 context_name_ = s.context_name_;
61 Context_def::~Context_def ()
65 #include "ly-smobs.icc"
66 IMPLEMENT_SMOBS (Context_def);
67 IMPLEMENT_DEFAULT_EQUAL_P (Context_def);
70 Context_def::print_smob (SCM smob, SCM port, scm_print_state*)
72 Context_def *me = (Context_def *) SCM_CELL_WORD_1 (smob);
74 scm_puts ("#<Context_def ", port);
75 scm_display (me->context_name_, port);
81 Context_def::mark_smob (SCM smob)
83 Context_def *me = (Context_def *) SCM_CELL_WORD_1 (smob);
85 scm_gc_mark (me->description_);
86 scm_gc_mark (me->context_aliases_);
87 scm_gc_mark (me->accept_mods_);
88 scm_gc_mark (me->translator_mods_);
89 scm_gc_mark (me->property_ops_);
90 scm_gc_mark (me->translator_group_type_);
91 scm_gc_mark (me->default_child_);
93 return me->context_name_;
97 Context_def::add_context_mod (SCM mod)
99 SCM tag = scm_car (mod);
100 if (ly_symbol2scm ("description") == tag)
102 description_ = scm_cadr (mod);
107 other modifiers take symbols as argument.
109 SCM sym = scm_cadr (mod);
110 if (scm_is_string (sym))
111 sym = scm_string_to_symbol (sym);
113 if (ly_symbol2scm ("default-child") == tag)
115 default_child_ = sym;
117 else if (ly_symbol2scm ("consists") == tag
118 || ly_symbol2scm ("consists-end") == tag
119 || ly_symbol2scm ("remove") == tag)
121 if (!get_translator (sym))
122 error (_f ("program has no such type: `%s'",
123 ly_symbol2string (sym).to_str0 ()));
125 translator_mods_ = scm_cons (scm_list_2 (tag, sym), translator_mods_);
127 else if (ly_symbol2scm ("accepts") == tag
128 || ly_symbol2scm ("denies") == tag)
129 accept_mods_ = scm_cons (scm_list_2 (tag, sym), accept_mods_);
130 else if (ly_symbol2scm ("poppush") == tag
131 || ly_symbol2scm ("pop") == tag
132 || ly_symbol2scm ("push") == tag
133 || ly_symbol2scm ("assign") == tag
134 || ly_symbol2scm ("unset") == tag)
135 property_ops_ = scm_cons (mod, property_ops_);
136 else if (ly_symbol2scm ("alias") == tag)
137 context_aliases_ = scm_cons (sym, context_aliases_);
138 else if (ly_symbol2scm ("translator-type") == tag)
139 translator_group_type_ = sym;
140 else if (ly_symbol2scm ("context-name") == tag)
143 programming_error ("unknown context mod tag");
147 Context_def::get_context_name () const
149 return context_name_;
153 Context_def::get_accepted (SCM user_mod) const
155 SCM mods = scm_reverse_x (scm_list_copy (accept_mods_), user_mod);
157 for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
159 SCM tag = scm_caar (s);
160 SCM sym = scm_cadar (s);
161 if (tag == ly_symbol2scm ("accepts"))
162 acc = scm_cons (sym, acc);
163 else if (tag == ly_symbol2scm ("denies"))
164 acc = scm_delete_x (sym, acc);
167 SCM def = get_default_child (user_mod);
168 if (scm_is_symbol (def))
170 if (scm_memq (def, acc))
171 acc = scm_delete_x (def, acc);
172 acc = scm_cons (def, acc);
180 Context_def::get_default_child (SCM user_mod) const
182 SCM name = default_child_;
183 for (SCM s = user_mod; scm_is_pair (s); s = scm_cdr (s))
185 SCM entry = scm_car (s);
186 if (scm_car (entry) == ly_symbol2scm ("default-child"))
188 name = scm_cadr (entry);
196 Link_array<Context_def>
197 Context_def::path_to_acceptable_context (SCM type_sym, Output_def *odef) const
199 assert (scm_is_symbol (type_sym));
201 SCM accepted = get_accepted (SCM_EOL);
203 Link_array<Context_def> accepteds;
204 for (SCM s = accepted; scm_is_pair (s); s = scm_cdr (s))
205 if (Context_def *t = unsmob_context_def (find_context_def (odef,
209 Link_array<Context_def> best_result;
210 for (int i = 0; i < accepteds.size (); i++)
212 /* do not check aliases, because \context Staff should not
213 create RhythmicStaff. */
214 if (ly_is_equal (accepteds[i]->get_context_name (), type_sym))
216 best_result.push (accepteds[i]);
221 int best_depth = INT_MAX;
222 for (int i = 0; i < accepteds.size (); i++)
224 Context_def *g = accepteds[i];
226 Link_array<Context_def> result
227 = g->path_to_acceptable_context (type_sym, odef);
228 if (result.size () && result.size () < best_depth)
230 best_depth = result.size ();
231 result.insert (g, 0);
232 best_result = result;
240 Context_def::get_translator_names (SCM user_mod) const
244 SCM mods = scm_reverse_x (scm_list_copy (translator_mods_), user_mod);
246 for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
248 SCM tag = scm_caar (s);
249 SCM arg = scm_cadar (s);
251 if (scm_is_string (arg))
252 arg = scm_string_to_symbol (arg);
254 if (ly_symbol2scm ("consists") == tag)
255 l1 = scm_cons (arg, l1);
256 else if (ly_symbol2scm ("remove") == tag)
258 l1 = scm_delete_x (arg, l1);
266 filter_performers (SCM ell)
269 for (SCM p = ell; scm_is_pair (p); p = scm_cdr (p))
271 if (dynamic_cast<Performer *> (unsmob_translator (scm_car (*tail))))
272 *tail = scm_cdr (*tail);
274 tail = SCM_CDRLOC(*tail);
280 filter_engravers (SCM ell)
283 for (SCM p = ell; scm_is_pair (p); p = scm_cdr (p))
285 if (dynamic_cast<Engraver *> (unsmob_translator (scm_car (*tail))))
286 *tail = scm_cdr (*tail);
288 tail = SCM_CDRLOC(*tail);
294 Context_def::instantiate (SCM ops, Object_key const *key)
296 Context *context = 0;
298 if (context_name_ == ly_symbol2scm ("Score"))
299 context = new Score_context (key);
301 context = new Context (key);
303 context->definition_ = self_scm ();
305 SCM trans_names = get_translator_names (ops);
307 Translator_group *g = get_translator_group (translator_group_type_);
308 SCM trans_list = SCM_EOL;
310 for (SCM s = trans_names; scm_is_pair (s); s = scm_cdr (s))
312 Translator *t = get_translator (scm_car (s));
314 warning (_f ("can't find: `%s'", s));
317 Translator *tr = t->clone ();
318 SCM str = tr->self_scm ();
320 if (tr->must_be_last ())
322 SCM cons = scm_cons (str, SCM_EOL);
323 if (scm_is_pair (trans_list))
324 scm_set_cdr_x (scm_last_pair (trans_list), cons);
330 trans_list = scm_cons (str, trans_list);
333 tr->daddy_context_ = context;
334 scm_gc_unprotect_object (str);
340 Ugh, todo: should just make a private
341 copy of Context_def with the user mods.
344 g->simple_trans_list_ = trans_list;
346 context->implementation_ = g->self_scm ();
347 if (dynamic_cast<Engraver *> (g))
348 g->simple_trans_list_ = filter_performers (g->simple_trans_list_);
349 else if (dynamic_cast<Performer *> (g))
350 g->simple_trans_list_ = filter_engravers (g->simple_trans_list_);
352 g->context_ = context;
353 context->aliases_ = context_aliases_;
355 scm_gc_unprotect_object (g->self_scm ());
357 context->accepts_list_ = get_accepted (ops);
363 Context_def::clone_scm () const
365 Context_def *t = new Context_def (*this);
366 SCM x = t->self_scm ();
367 scm_gc_unprotect_object (x);
372 Context_def::make_scm ()
374 Context_def *t = new Context_def;
375 SCM x = t->self_scm ();
376 scm_gc_unprotect_object (x);
381 Context_def::apply_default_property_operations (Context *tg)
383 apply_property_operations (tg, property_ops_);
387 Context_def::to_alist () const
391 ell = scm_cons (scm_cons (ly_symbol2scm ("consists"),
392 get_translator_names (SCM_EOL)), ell);
393 ell = scm_cons (scm_cons (ly_symbol2scm ("description"), description_), ell);
394 ell = scm_cons (scm_cons (ly_symbol2scm ("aliases"), context_aliases_), ell);
395 ell = scm_cons (scm_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)),
397 ell = scm_cons (scm_cons (ly_symbol2scm ("property-ops"), property_ops_),
399 ell = scm_cons (scm_cons (ly_symbol2scm ("context-name"), context_name_),
402 if (scm_is_symbol (translator_group_type_))
403 ell = scm_cons (scm_cons (ly_symbol2scm ("group-type"),
404 translator_group_type_), ell);