]> git.donarmstrong.com Git - lilypond.git/blob - lily/context-def.cc
Merge branch 'lilypond/translation' of ssh://trettig@git.sv.gnu.org/srv/git/lilypond...
[lilypond.git] / lily / context-def.cc
1 /*
2   translator-def.cc -- implement Context_def
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2000--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 /* TODO: should junk this class an replace by
10    a single list of context modifications?  */
11
12 #include "context-def.hh"
13
14 #include "context.hh"
15 #include "international.hh"
16 #include "output-def.hh"
17 #include "translator.hh"
18 #include "warn.hh"
19
20 Context_def::Context_def ()
21 {
22   context_aliases_ = SCM_EOL;
23   translator_group_type_ = SCM_EOL;
24   accept_mods_ = SCM_EOL;
25   translator_mods_ = SCM_EOL;
26   property_ops_ = SCM_EOL;
27   context_name_ = SCM_EOL;
28   default_child_ = SCM_EOL;
29   description_ = SCM_EOL;
30   input_location_ = SCM_EOL;
31
32   smobify_self ();
33
34   input_location_ = make_input (Input ());
35   context_name_ = ly_symbol2scm ("");
36 }
37
38 Input *
39 Context_def::origin () const
40 {
41   return unsmob_input (input_location_);
42 }
43
44 Context_def::Context_def (Context_def const &s)
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   default_child_ = SCM_EOL;
54   input_location_ = SCM_EOL;
55   smobify_self ();
56
57   description_ = s.description_;
58   input_location_ = make_input (*s.origin ()); 
59   default_child_ = s.default_child_;
60   accept_mods_ = s.accept_mods_;
61   property_ops_ = s.property_ops_;
62   translator_mods_ = s.translator_mods_;
63   context_aliases_ = s.context_aliases_;
64   translator_group_type_ = s.translator_group_type_;
65   context_name_ = s.context_name_;
66 }
67
68 Context_def::~Context_def ()
69 {
70 }
71
72 #include "ly-smobs.icc"
73 IMPLEMENT_SMOBS (Context_def);
74 IMPLEMENT_DEFAULT_EQUAL_P (Context_def);
75
76 int
77 Context_def::print_smob (SCM smob, SCM port, scm_print_state*)
78 {
79   Context_def *me = (Context_def *) SCM_CELL_WORD_1 (smob);
80
81   scm_puts ("#<Context_def ", port);
82   scm_display (me->context_name_, port);
83   scm_puts (">", port);
84   return 1;
85 }
86
87 SCM
88 Context_def::mark_smob (SCM smob)
89 {
90   ASSERT_LIVE_IS_ALLOWED ();
91   
92   Context_def *me = (Context_def *) SCM_CELL_WORD_1 (smob);
93
94   scm_gc_mark (me->description_);
95   scm_gc_mark (me->context_aliases_);
96   scm_gc_mark (me->accept_mods_);
97   scm_gc_mark (me->translator_mods_);
98   scm_gc_mark (me->property_ops_);
99   scm_gc_mark (me->translator_group_type_);
100   scm_gc_mark (me->default_child_);
101   scm_gc_mark (me->input_location_);
102
103   return me->context_name_;
104 }
105
106 void
107 Context_def::add_context_mod (SCM mod)
108 {
109   SCM tag = scm_car (mod);
110   if (ly_symbol2scm ("description") == tag)
111     {
112       description_ = scm_cadr (mod);
113       return;
114     }
115
116   /*
117     other modifiers take symbols as argument.
118   */
119   SCM sym = scm_cadr (mod);
120   if (scm_is_string (sym))
121     sym = scm_string_to_symbol (sym);
122
123   if (ly_symbol2scm ("default-child") == tag)
124     default_child_ = sym;
125   else if (ly_symbol2scm ("consists") == tag
126            || ly_symbol2scm ("consists-end") == tag
127            || ly_symbol2scm ("remove") == tag)
128     {
129       if (!get_translator (sym))
130         error (_f ("program has no such type: `%s'",
131                    ly_symbol2string (sym).c_str ()));
132       else
133         translator_mods_ = scm_cons (scm_list_2 (tag, sym), translator_mods_);
134     }
135   else if (ly_symbol2scm ("accepts") == tag
136            || ly_symbol2scm ("denies") == tag)
137     accept_mods_ = scm_cons (scm_list_2 (tag, sym), accept_mods_);
138   else if (ly_symbol2scm ("pop") == tag
139            || ly_symbol2scm ("push") == tag
140            || ly_symbol2scm ("assign") == tag
141            || ly_symbol2scm ("unset") == tag)
142     property_ops_ = scm_cons (mod, property_ops_);
143   else if (ly_symbol2scm ("alias") == tag)
144     context_aliases_ = scm_cons (sym, context_aliases_);
145   else if (ly_symbol2scm ("translator-type") == tag)
146     translator_group_type_ = sym;
147   else if (ly_symbol2scm ("context-name") == tag)
148     context_name_ = sym;
149   else
150     programming_error ("unknown context mod tag");
151 }
152
153 SCM
154 Context_def::get_accepted (SCM user_mod) const
155 {
156   SCM mods = scm_reverse_x (scm_list_copy (accept_mods_), user_mod);
157   SCM acc = SCM_EOL;
158   for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
159     {
160       SCM tag = scm_caar (s);
161       SCM sym = scm_cadar (s);
162       if (tag == ly_symbol2scm ("accepts"))
163         acc = scm_cons (sym, acc);
164       else if (tag == ly_symbol2scm ("denies"))
165         acc = scm_delete_x (sym, acc);
166     }
167
168   SCM def = get_default_child (user_mod);
169   if (scm_is_symbol (def))
170     {
171       if (scm_memq (def, acc))
172         acc = scm_delete_x (def, acc);
173       acc = scm_cons (def, acc);
174     }
175
176   return acc;
177 }
178
179 SCM
180 Context_def::get_default_child (SCM user_mod) const
181 {
182   SCM name = default_child_;
183   for (SCM s = user_mod; scm_is_pair (s); s = scm_cdr (s))
184     {
185       SCM entry = scm_car (s);
186       if (scm_car (entry) == ly_symbol2scm ("default-child"))
187         {
188           name = scm_cadr (entry);
189           break;
190         }
191     }
192
193   return name;
194 }
195
196 /*
197   Given a name of a context that we want to create, finds a list of context
198   definitions such that:
199    - the first element in the list defines a context that is a valid child of
200      the context defined by this Context_def
201    - each subsequent element in the list defines a context that is a valid child
202      of the the context defined by the preceding element in the list
203    - the last element in the list defines a context with the given name
204
205   The ADDITIONAL_ACCEPTS parameter is a list of additional contexts that this
206   specific context def (but not any of the child context defs) should accept.
207 */
208 vector<Context_def*>
209 Context_def::path_to_acceptable_context (SCM type_sym, Output_def *odef, SCM additional_accepts) const
210 {
211   assert (scm_is_symbol (type_sym));
212
213   SCM accepted = get_accepted (additional_accepts);
214
215   vector<Context_def*> accepteds;
216   for (SCM s = accepted; scm_is_pair (s); s = scm_cdr (s))
217     if (Context_def *t = unsmob_context_def (find_context_def (odef,
218                                                                scm_car (s))))
219       accepteds.push_back (t);
220
221   vector<Context_def*> best_result;
222   for (vsize i = 0; i < accepteds.size (); i++)
223     {
224       /* do not check aliases, because \context Staff should not
225          create RhythmicStaff. */
226       if (ly_is_equal (accepteds[i]->get_context_name (), type_sym))
227         {
228           best_result.push_back (accepteds[i]);
229           return best_result;
230         }
231     }
232
233   vsize best_depth = INT_MAX;
234   for (vsize i = 0; i < accepteds.size (); i++)
235     {
236       Context_def *g = accepteds[i];
237
238       vector<Context_def*> result
239         = g->path_to_acceptable_context (type_sym, odef, SCM_EOL);
240       if (result.size () && result.size () < best_depth)
241         {
242           best_depth = result.size ();
243           result.insert (result.begin (), g);
244           best_result = result;
245         }
246     }
247
248   return best_result;
249 }
250
251 SCM
252 Context_def::get_translator_names (SCM user_mod) const
253 {
254   SCM l1 = SCM_EOL;
255
256   SCM mods = scm_reverse_x (scm_list_copy (translator_mods_), user_mod);
257
258   for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
259     {
260       SCM tag = scm_caar (s);
261       SCM arg = scm_cadar (s);
262
263       if (scm_is_string (arg))
264         arg = scm_string_to_symbol (arg);
265
266       if (ly_symbol2scm ("consists") == tag)
267         l1 = scm_cons (arg, l1);
268       else if (ly_symbol2scm ("remove") == tag)
269         l1 = scm_delete_x (arg, l1);
270     }
271
272   return l1;
273 }
274
275 Context *
276 Context_def::instantiate (SCM ops)
277 {
278   Context *context = new Context ();
279
280   context->definition_ = self_scm ();
281   context->definition_mods_ = ops;
282   context->aliases_ = context_aliases_;
283   context->accepts_list_ = get_accepted (ops);
284
285   return context;
286 }
287
288 SCM
289 Context_def::make_scm ()
290 {
291   Context_def *t = new Context_def;
292   return t->unprotect ();
293 }
294
295 void
296 Context_def::apply_default_property_operations (Context *tg)
297 {
298   apply_property_operations (tg, property_ops_);
299 }
300
301 SCM
302 Context_def::to_alist () const
303 {
304   SCM ell = SCM_EOL;
305
306   ell = scm_cons (scm_cons (ly_symbol2scm ("consists"),
307                             get_translator_names (SCM_EOL)), ell);
308   ell = scm_cons (scm_cons (ly_symbol2scm ("description"), description_), ell);
309   ell = scm_cons (scm_cons (ly_symbol2scm ("aliases"), context_aliases_), ell);
310   ell = scm_cons (scm_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)),
311                   ell);
312   ell = scm_cons (scm_cons (ly_symbol2scm ("property-ops"), property_ops_),
313                   ell);
314   ell = scm_cons (scm_cons (ly_symbol2scm ("context-name"), context_name_),
315                   ell);
316
317   if (scm_is_symbol (translator_group_type_))
318     ell = scm_cons (scm_cons (ly_symbol2scm ("group-type"),
319                               translator_group_type_), ell);
320   return ell;
321 }
322