]> git.donarmstrong.com Git - lilypond.git/blob - lily/context-def.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / context-def.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2000--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /* TODO: should junk this class an replace by
21    a single list of context modifications?  */
22
23 #include "context-def.hh"
24
25 #include "context.hh"
26 #include "context-mod.hh"
27 #include "international.hh"
28 #include "output-def.hh"
29 #include "translator.hh"
30 #include "warn.hh"
31
32 using std::set;
33 using std::string;
34 using std::vector;
35
36 Context_def::Context_def ()
37 {
38   context_aliases_ = SCM_EOL;
39   translator_group_type_ = SCM_EOL;
40   accept_mods_ = SCM_EOL;
41   translator_mods_ = SCM_EOL;
42   property_ops_ = SCM_EOL;
43   context_name_ = SCM_EOL;
44   default_child_ = SCM_EOL;
45   description_ = SCM_EOL;
46   input_location_ = SCM_EOL;
47
48   smobify_self ();
49
50   input_location_ = Input ().smobbed_copy ();
51   context_name_ = ly_symbol2scm ("");
52 }
53
54 Input *
55 Context_def::origin () const
56 {
57   return unsmob<Input> (input_location_);
58 }
59
60 Context_def::Context_def (Context_def const &s)
61   : Smob<Context_def> ()
62 {
63   context_aliases_ = SCM_EOL;
64   translator_group_type_ = SCM_EOL;
65   accept_mods_ = SCM_EOL;
66   translator_mods_ = SCM_EOL;
67   property_ops_ = SCM_EOL;
68   context_name_ = SCM_EOL;
69   description_ = SCM_EOL;
70   default_child_ = SCM_EOL;
71   input_location_ = SCM_EOL;
72   smobify_self ();
73
74   description_ = s.description_;
75   input_location_ = s.origin ()->smobbed_copy ();
76   default_child_ = s.default_child_;
77   accept_mods_ = s.accept_mods_;
78   property_ops_ = s.property_ops_;
79   translator_mods_ = s.translator_mods_;
80   context_aliases_ = s.context_aliases_;
81   translator_group_type_ = s.translator_group_type_;
82   context_name_ = s.context_name_;
83 }
84
85 Context_def::~Context_def ()
86 {
87 }
88
89 const char Context_def::type_p_name_[] = "ly:context-def?";
90
91 int
92 Context_def::print_smob (SCM port, scm_print_state *) const
93 {
94   scm_puts ("#<Context_def ", port);
95   scm_display (context_name_, port);
96   scm_puts (" ", port);
97   string loc = origin ()->location_string ();
98   scm_puts (loc.c_str (), port);
99   scm_puts (">", port);
100   return 1;
101 }
102
103 SCM
104 Context_def::mark_smob () const
105 {
106   ASSERT_LIVE_IS_ALLOWED (self_scm ());
107
108   scm_gc_mark (description_);
109   scm_gc_mark (context_aliases_);
110   scm_gc_mark (accept_mods_);
111   scm_gc_mark (translator_mods_);
112   scm_gc_mark (property_ops_);
113   scm_gc_mark (translator_group_type_);
114   scm_gc_mark (default_child_);
115   scm_gc_mark (input_location_);
116
117   return context_name_;
118 }
119
120 void
121 Context_def::add_context_mod (SCM mod)
122 {
123   SCM tag = scm_car (mod);
124   if (scm_is_eq (tag, ly_symbol2scm ("description")))
125     {
126       description_ = scm_cadr (mod);
127       return;
128     }
129
130   /*
131     other modifiers take symbols as argument.
132   */
133   SCM sym = scm_cadr (mod);
134   if (scm_is_string (sym))
135     sym = scm_string_to_symbol (sym);
136
137   if (scm_is_eq (tag, ly_symbol2scm ("default-child")))
138     default_child_ = sym;
139   else if (scm_is_eq (tag, ly_symbol2scm ("consists"))
140            || scm_is_eq (tag, ly_symbol2scm ("remove")))
141     {
142       translator_mods_ = scm_cons (scm_list_2 (tag, sym), translator_mods_);
143     }
144   else if (scm_is_eq (tag, ly_symbol2scm ("accepts"))
145            || scm_is_eq (tag, ly_symbol2scm ("denies")))
146     accept_mods_ = scm_cons (scm_list_2 (tag, sym), accept_mods_);
147   else if (scm_is_eq (tag, ly_symbol2scm ("pop"))
148            || scm_is_eq (tag, ly_symbol2scm ("push"))
149            || scm_is_eq (tag, ly_symbol2scm ("assign"))
150            || scm_is_eq (tag, ly_symbol2scm ("unset"))
151            || scm_is_eq (tag, ly_symbol2scm ("apply")))
152     property_ops_ = scm_cons (mod, property_ops_);
153   else if (scm_is_eq (tag, ly_symbol2scm ("alias")))
154     context_aliases_ = scm_cons (sym, context_aliases_);
155   else if (scm_is_eq (tag, ly_symbol2scm ("translator-type")))
156     translator_group_type_ = sym;
157   else if (scm_is_eq (tag, ly_symbol2scm ("context-name")))
158     context_name_ = sym;
159   else
160     programming_error ("unknown context mod tag");
161 }
162
163 SCM
164 Context_def::get_accepted (SCM user_mod) const
165 {
166   SCM mods = scm_reverse_x (scm_list_copy (accept_mods_), user_mod);
167   SCM acc = SCM_EOL;
168   for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
169     {
170       SCM tag = scm_caar (s);
171       SCM sym = scm_cadar (s);
172       if (scm_is_eq (tag, ly_symbol2scm ("accepts")))
173         acc = scm_cons (sym, acc);
174       else if (scm_is_eq (tag, ly_symbol2scm ("denies")))
175         acc = scm_delete_x (sym, acc);
176     }
177
178   SCM def = get_default_child (user_mod);
179   if (scm_is_symbol (def))
180     {
181       acc = scm_delete_x (def, acc);
182       acc = scm_cons (def, acc);
183     }
184
185   return acc;
186 }
187
188 SCM
189 Context_def::get_default_child (SCM user_mod) const
190 {
191   SCM name = default_child_;
192   for (SCM s = user_mod; scm_is_pair (s); s = scm_cdr (s))
193     {
194       SCM entry = scm_car (s);
195       if (scm_is_eq (scm_car (entry), ly_symbol2scm ("default-child")))
196         {
197           name = scm_cadr (entry);
198           break;
199         }
200     }
201
202   return name;
203 }
204
205 /*
206   Given a name of a context that we want to create, finds a list of context
207   definitions such that:
208    - the first element in the list defines a context that is a valid child of
209      the context defined by this Context_def
210    - each subsequent element in the list defines a context that is a valid child
211      of the context defined by the preceding element in the list
212    - the last element in the list defines a context with the given name
213
214   The ADDITIONAL_ACCEPTS parameter is a list of additional contexts that this
215   specific context def (but not any of the child context defs) should accept.
216 */
217 vector<Context_def *>
218 Context_def::path_to_acceptable_context (SCM type_sym,
219                                          Output_def *odef,
220                                          SCM additional_accepts) const
221 {
222   set<const Context_def *> seen;
223   return internal_path_to_acceptable_context (type_sym, odef, additional_accepts, &seen);
224 }
225
226 /*
227 The SEEN parameter is a set which keeps track of visited contexts, allowing
228 contexts of the same type to be nested.
229 */
230 vector<Context_def *>
231 Context_def::internal_path_to_acceptable_context (SCM type_sym,
232                                                   Output_def *odef,
233                                                   SCM additional_accepts,
234                                                   set<const Context_def *> *seen) const
235 {
236   assert (scm_is_symbol (type_sym));
237
238   SCM accepted = get_accepted (additional_accepts);
239
240   vector<Context_def *> accepteds;
241   for (SCM s = accepted; scm_is_pair (s); s = scm_cdr (s))
242     if (Context_def *t = unsmob<Context_def> (find_context_def (odef,
243                                                                scm_car (s))))
244       accepteds.push_back (t);
245
246   vector<Context_def *> best_result;
247   for (vsize i = 0; i < accepteds.size (); i++)
248     {
249       /* do not check aliases, because \context Staff should not
250          create RhythmicStaff. */
251       if (ly_is_equal (accepteds[i]->get_context_name (), type_sym))
252         {
253           best_result.push_back (accepteds[i]);
254           return best_result;
255         }
256     }
257
258   seen->insert (this);
259   vsize best_depth = INT_MAX;
260   for (vsize i = 0; i < accepteds.size (); i++)
261     {
262       Context_def *g = accepteds[i];
263
264       if (!seen->count (g))
265         {
266           vector<Context_def *> result
267             = g->internal_path_to_acceptable_context (type_sym, odef, SCM_EOL, seen);
268           if (result.size () && result.size () < best_depth)
269             {
270               best_depth = result.size ();
271               result.insert (result.begin (), g);
272               best_result = result;
273             }
274         }
275     }
276   seen->erase (this);
277
278   return best_result;
279 }
280
281 SCM
282 Context_def::get_translator_names (SCM user_mod) const
283 {
284   SCM l1 = SCM_EOL;
285
286   SCM mods = scm_reverse_x (scm_list_copy (translator_mods_), user_mod);
287
288   for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
289     {
290       SCM tag = scm_caar (s);
291       SCM arg = scm_cadar (s);
292
293       if (scm_is_string (arg))
294         arg = scm_string_to_symbol (arg);
295
296       if (scm_is_eq (tag, ly_symbol2scm ("consists")))
297         l1 = scm_cons (arg, l1);
298       else if (scm_is_eq (tag, ly_symbol2scm ("remove"))
299                && (scm_is_pair (arg)
300                    || ly_is_procedure (arg)
301                    || get_translator (arg)))
302         l1 = scm_delete_x (arg, l1);
303     }
304
305   return l1;
306 }
307
308 Context *
309 Context_def::instantiate (SCM ops)
310 {
311   Context *context = new Context ();
312
313   context->definition_ = self_scm ();
314   context->definition_mods_ = ops;
315   context->aliases_ = context_aliases_;
316   context->accepts_list_ = get_accepted (ops);
317   context->default_child_ = get_default_child (ops);
318
319   return context;
320 }
321
322 SCM
323 Context_def::make_scm ()
324 {
325   Context_def *t = new Context_def;
326   return t->unprotect ();
327 }
328
329 void
330 Context_def::apply_default_property_operations (Context *tg)
331 {
332   apply_property_operations (tg, scm_reverse (property_ops_));
333 }
334
335 SCM
336 Context_def::to_alist () const
337 {
338   SCM ell = SCM_EOL;
339
340   ell = scm_cons (scm_cons (ly_symbol2scm ("consists"),
341                             get_translator_names (SCM_EOL)), ell);
342   ell = scm_cons (scm_cons (ly_symbol2scm ("description"), description_), ell);
343   ell = scm_cons (scm_cons (ly_symbol2scm ("aliases"), context_aliases_), ell);
344   ell = scm_cons (scm_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)),
345                   ell);
346   if (scm_is_symbol (default_child_))
347     ell = scm_acons (ly_symbol2scm ("default-child"), default_child_, ell);
348   ell = scm_cons (scm_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)),
349                   ell);
350   ell = scm_cons (scm_cons (ly_symbol2scm ("property-ops"), property_ops_),
351                   ell);
352   ell = scm_cons (scm_cons (ly_symbol2scm ("context-name"), context_name_),
353                   ell);
354
355   if (scm_is_symbol (translator_group_type_))
356     ell = scm_cons (scm_cons (ly_symbol2scm ("group-type"),
357                               translator_group_type_), ell);
358   return ell;
359 }
360
361 SCM
362 Context_def::lookup (SCM sym) const
363 {
364   if (scm_is_eq (ly_symbol2scm ("default-child"), sym))
365     return default_child_;
366   else if (scm_is_eq (ly_symbol2scm ("consists"), sym))
367     return get_translator_names (SCM_EOL);
368   else if (scm_is_eq (ly_symbol2scm ("description"), sym))
369     return description_;
370   else if (scm_is_eq (ly_symbol2scm ("aliases"), sym))
371     return context_aliases_;
372   else if (scm_is_eq (ly_symbol2scm ("accepts"), sym))
373     return get_accepted (SCM_EOL);
374   else if (scm_is_eq (ly_symbol2scm ("property-ops"), sym))
375     return property_ops_;
376   else if (scm_is_eq (ly_symbol2scm ("context-name"), sym))
377     return context_name_;
378   else if (scm_is_eq (ly_symbol2scm ("group-type"), sym))
379     return translator_group_type_;
380   return SCM_UNDEFINED;
381 }
382
383 bool
384 Context_def::is_alias (SCM sym) const
385 {
386   if (scm_is_eq (sym, ly_symbol2scm ("Bottom")))
387     return !scm_is_symbol (get_default_child (SCM_EOL));
388
389   if (scm_is_eq (sym, get_context_name ()))
390     return true;
391
392   return scm_is_true (scm_c_memq (sym, context_aliases_));
393 }
394
395 LY_DEFINE (ly_context_def_lookup, "ly:context-def-lookup",
396            2, 1, 0, (SCM def, SCM sym, SCM val),
397            "Return the value of @var{sym} in context definition @var{def}"
398            " (e.g., @code{\\Voice}).  If no value is found, return"
399            " @var{val} or @code{'()} if @var{val} is undefined."
400            " @var{sym} can be any of @samp{default-child}, @samp{consists},"
401            " @samp{description}, @samp{aliases}, @samp{accepts},"
402            " @samp{property-ops}, @samp{context-name}, @samp{group-type}.")
403 {
404   LY_ASSERT_SMOB (Context_def, def, 1);
405   Context_def *cd = unsmob<Context_def> (def);
406   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
407
408   SCM res = cd->lookup (sym);
409
410   scm_remember_upto_here_1 (def);
411
412   if (SCM_UNBNDP (res))
413     res = SCM_EOL;
414
415   if (scm_is_null (res) && !SCM_UNBNDP (val))
416     return val;
417
418   return res;
419 }
420
421 LY_DEFINE (ly_context_def_modify, "ly:context-def-modify",
422            2, 0, 0, (SCM def, SCM mod),
423            "Return the result of applying the context-mod @var{mod} to"
424            " the context definition @var{def}.  Does not change @var{def}.")
425 {
426   LY_ASSERT_SMOB (Context_def, def, 1);
427   LY_ASSERT_SMOB (Context_mod, mod, 2);
428
429   Context_def *cd = unsmob<Context_def> (def)->clone ();
430
431   for (SCM s = unsmob<Context_mod> (mod)->get_mods ();
432        scm_is_pair (s);
433        s = scm_cdr (s))
434     cd->add_context_mod (scm_car (s));
435
436   return cd->unprotect ();
437 }