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)
114 default_child_ = sym;
115 else if (ly_symbol2scm ("consists") == tag
116 || ly_symbol2scm ("consists-end") == tag
117 || ly_symbol2scm ("remove") == tag)
119 if (!get_translator (sym))
120 error (_f ("program has no such type: `%s'",
121 ly_symbol2string (sym).to_str0 ()));
123 translator_mods_ = scm_cons (scm_list_2 (tag, sym), translator_mods_);
125 else if (ly_symbol2scm ("accepts") == tag
126 || ly_symbol2scm ("denies") == tag)
127 accept_mods_ = scm_cons (scm_list_2 (tag, sym), accept_mods_);
128 else if (ly_symbol2scm ("poppush") == tag
129 || ly_symbol2scm ("pop") == tag
130 || ly_symbol2scm ("push") == tag
131 || ly_symbol2scm ("assign") == tag
132 || ly_symbol2scm ("unset") == tag)
133 property_ops_ = scm_cons (mod, property_ops_);
134 else if (ly_symbol2scm ("alias") == tag)
135 context_aliases_ = scm_cons (sym, context_aliases_);
136 else if (ly_symbol2scm ("translator-type") == tag)
137 translator_group_type_ = sym;
138 else if (ly_symbol2scm ("context-name") == tag)
141 programming_error ("unknown context mod tag");
145 Context_def::get_context_name () const
147 return context_name_;
151 Context_def::get_accepted (SCM user_mod) const
153 SCM mods = scm_reverse_x (scm_list_copy (accept_mods_), user_mod);
155 for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
157 SCM tag = scm_caar (s);
158 SCM sym = scm_cadar (s);
159 if (tag == ly_symbol2scm ("accepts"))
160 acc = scm_cons (sym, acc);
161 else if (tag == ly_symbol2scm ("denies"))
162 acc = scm_delete_x (sym, acc);
165 SCM def = get_default_child (user_mod);
166 if (scm_is_symbol (def))
168 if (scm_memq (def, acc))
169 acc = scm_delete_x (def, acc);
170 acc = scm_cons (def, acc);
177 Context_def::get_default_child (SCM user_mod) const
179 SCM name = default_child_;
180 for (SCM s = user_mod; scm_is_pair (s); s = scm_cdr (s))
182 SCM entry = scm_car (s);
183 if (scm_car (entry) == ly_symbol2scm ("default-child"))
185 name = scm_cadr (entry);
193 Link_array<Context_def>
194 Context_def::path_to_acceptable_context (SCM type_sym, Output_def *odef) const
196 assert (scm_is_symbol (type_sym));
198 SCM accepted = get_accepted (SCM_EOL);
200 Link_array<Context_def> accepteds;
201 for (SCM s = accepted; scm_is_pair (s); s = scm_cdr (s))
202 if (Context_def *t = unsmob_context_def (find_context_def (odef,
206 Link_array<Context_def> best_result;
207 for (int i = 0; i < accepteds.size (); i++)
209 /* do not check aliases, because \context Staff should not
210 create RhythmicStaff. */
211 if (ly_is_equal (accepteds[i]->get_context_name (), type_sym))
213 best_result.push (accepteds[i]);
218 int best_depth = INT_MAX;
219 for (int i = 0; i < accepteds.size (); i++)
221 Context_def *g = accepteds[i];
223 Link_array<Context_def> result
224 = g->path_to_acceptable_context (type_sym, odef);
225 if (result.size () && result.size () < best_depth)
227 best_depth = result.size ();
228 result.insert (g, 0);
229 best_result = result;
237 Context_def::get_translator_names (SCM user_mod) const
241 SCM mods = scm_reverse_x (scm_list_copy (translator_mods_), user_mod);
243 for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
245 SCM tag = scm_caar (s);
246 SCM arg = scm_cadar (s);
248 if (scm_is_string (arg))
249 arg = scm_string_to_symbol (arg);
251 if (ly_symbol2scm ("consists") == tag)
252 l1 = scm_cons (arg, l1);
253 else if (ly_symbol2scm ("remove") == tag)
254 l1 = scm_delete_x (arg, l1);
261 filter_performers (SCM ell)
264 for (SCM p = ell; scm_is_pair (p); p = scm_cdr (p))
266 if (dynamic_cast<Performer *> (unsmob_translator (scm_car (*tail))))
267 *tail = scm_cdr (*tail);
269 tail = SCM_CDRLOC (*tail);
275 filter_engravers (SCM ell)
278 for (SCM p = ell; scm_is_pair (p); p = scm_cdr (p))
280 if (dynamic_cast<Engraver *> (unsmob_translator (scm_car (*tail))))
281 *tail = scm_cdr (*tail);
283 tail = SCM_CDRLOC (*tail);
289 Context_def::instantiate (SCM ops, Object_key const *key)
291 Context *context = 0;
293 if (context_name_ == ly_symbol2scm ("Score"))
294 context = new Score_context (key);
296 context = new Context (key);
298 context->definition_ = self_scm ();
300 SCM trans_names = get_translator_names (ops);
302 Translator_group *g = get_translator_group (translator_group_type_);
303 SCM trans_list = SCM_EOL;
305 for (SCM s = trans_names; scm_is_pair (s); s = scm_cdr (s))
307 Translator *t = get_translator (scm_car (s));
309 warning (_f ("can't find: `%s'", s));
312 Translator *tr = t->clone ();
313 SCM str = tr->self_scm ();
315 if (tr->must_be_last ())
317 SCM cons = scm_cons (str, SCM_EOL);
318 if (scm_is_pair (trans_list))
319 scm_set_cdr_x (scm_last_pair (trans_list), cons);
324 trans_list = scm_cons (str, trans_list);
326 tr->daddy_context_ = context;
332 Ugh, todo: should just make a private
333 copy of Context_def with the user mods.
336 g->simple_trans_list_ = trans_list;
338 context->implementation_ = g;
339 if (dynamic_cast<Engraver *> (g))
340 g->simple_trans_list_ = filter_performers (g->simple_trans_list_);
341 else if (dynamic_cast<Performer *> (g))
342 g->simple_trans_list_ = filter_engravers (g->simple_trans_list_);
344 g->context_ = context;
345 context->aliases_ = context_aliases_;
349 context->accepts_list_ = get_accepted (ops);
355 Context_def::clone_scm () const
357 Context_def *t = new Context_def (*this);
358 return t->unprotect ();
362 Context_def::make_scm ()
364 Context_def *t = new Context_def;
365 return t->unprotect ();
369 Context_def::apply_default_property_operations (Context *tg)
371 apply_property_operations (tg, property_ops_);
375 Context_def::to_alist () const
379 ell = scm_cons (scm_cons (ly_symbol2scm ("consists"),
380 get_translator_names (SCM_EOL)), ell);
381 ell = scm_cons (scm_cons (ly_symbol2scm ("description"), description_), ell);
382 ell = scm_cons (scm_cons (ly_symbol2scm ("aliases"), context_aliases_), ell);
383 ell = scm_cons (scm_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)),
385 ell = scm_cons (scm_cons (ly_symbol2scm ("property-ops"), property_ops_),
387 ell = scm_cons (scm_cons (ly_symbol2scm ("context-name"), context_name_),
390 if (scm_is_symbol (translator_group_type_))
391 ell = scm_cons (scm_cons (ly_symbol2scm ("group-type"),
392 translator_group_type_), ell);