]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/translator.cc
* lily/lexer.ll, lily/parser.yy: Add EXPECT_NO_MORE_ARGS token, to
[lilypond.git] / lily / translator.cc
index 7bd0e6b47e7f19e69452e9d1e2c3e38663d7f598..d90c8be045b4b3526ab778e5d54862037171469e 100644 (file)
@@ -8,11 +8,12 @@
 
 #include "translator.hh"
 
-#include "warn.hh"
-#include "translator-group.hh"
 #include "context-def.hh"
 #include "dispatcher.hh"
 #include "global-context.hh"
+#include "international.hh"
+#include "translator-group.hh"
+#include "warn.hh"
 
 #include "translator.icc"
 #include "ly-smobs.icc"
@@ -51,12 +52,6 @@ Translator::Translator (Translator const &src)
   must_be_last_ = src.must_be_last_;
 }
 
-bool
-Translator::try_music (Music *)
-{
-  return false;
-}
-
 Moment
 Translator::now_mom () const
 {
@@ -93,12 +88,9 @@ Translator::stop_translation_timestep ()
 }
 
 /*
-  this function has 2 properties
-
-  - It is called before try_music ()
-
-  - It is called before any user information enters the translators.
-  (i.e. any \property or event is not processed yet.)
+  this function is called once each moment, before any user
+  information enters the translators.  (i.e. no \property or event has
+  been processed yet.)
 */
 void
 Translator::start_translation_timestep ()
@@ -129,8 +121,41 @@ Translator::disconnect_from_context (Context *c)
     c->events_below ()->remove_listener (r->get_listener_ (this), r->event_class_);
 }
 
+static SCM listened_event_class_table;
+void
+ensure_listened_hash ()
+{
+  if (!listened_event_class_table)
+    listened_event_class_table = scm_permanent_object (scm_c_make_hash_table (61));
+}
+
+
+LY_DEFINE (ly_get_listened_event_classes, "ly:get-listened-event-classes",
+          0, 0, 0, (),
+          "Returns a list of all event classes that some translator listens to.")
+{
+  ensure_listened_hash ();
+  return ly_hash_table_keys (listened_event_class_table);
+}
+
+LY_DEFINE (ly_is_listened_event_class, "ly:is-listened-event-class",
+          1, 0, 0, (SCM sym),
+          "Is @var{sym} a listened event class?")
+{
+  ensure_listened_hash ();
+  return scm_hashq_ref (listened_event_class_table, sym, SCM_BOOL_F);
+}
+
+void
+add_listened_event_class (SCM sym)
+{
+  ensure_listened_hash ();
+  scm_hashq_set_x (listened_event_class_table, sym, SCM_BOOL_T);
+}
+
+
 /*
-  Internally called once, statically, for each translator
+  internally called once, statically, for each translator
   listener. Connects the name of an event class with a procedure that
   fetches the corresponding listener.
 
@@ -139,22 +164,39 @@ Translator::disconnect_from_context (Context *c)
  */
 void
 Translator::add_translator_listener (translator_listener_record **listener_list,
-                        translator_listener_record *r,
-                        Listener (*get_listener) (void *), 
-                        const char *ev_class)
+                                    translator_listener_record *r,
+                                    Listener (*get_listener) (void *), 
+                                    const char *ev_class)
 {
   /* ev_class is the C++ identifier name. Convert to scm symbol */
   string name = string (ev_class);
   name = replace_all (name, '_', '-');
-  name = name + "-event";
-  /* It's OK to use scm_gc_protect_object for protection, because r is
-     statically allocated. */
-  r->event_class_ = scm_gc_protect_object (scm_str2symbol (name.c_str ()));
+  name += "-event";
+  
+  SCM class_sym = scm_str2symbol (name.c_str ());
+  
+  add_listened_event_class (class_sym);
+
+  r->event_class_ = class_sym;
   r->get_listener_ = get_listener;
   r->next_ = *listener_list;
   *listener_list = r;
 }
 
+/*
+  Used by ADD_THIS_TRANSLATOR to extract a list of event-class names
+  for each translator.  This list is used by the internals
+  documentation.
+*/
+SCM
+Translator::get_listened_class_list (const translator_listener_record *listeners) const
+{
+  SCM list = SCM_EOL;
+  for (; listeners; listeners = listeners->next_)
+    list = scm_cons (listeners->event_class_, list);
+  return list;
+}
+
 /*
   SMOBS
 */
@@ -216,6 +258,9 @@ add_acknowledger (Engraver_void_function_engraver_grob_info ptr,
   interface_name = replace_all (interface_name, '_', '-');
   interface_name += "-interface";
 
+  /*
+    this is only called during program init, so safe to use scm_gc_protect_object()
+  */
   inf.symbol_ = scm_gc_protect_object (ly_symbol2scm (interface_name.c_str ()));
   ack_array->push_back (inf);
 }
@@ -231,6 +276,48 @@ generic_get_acknowledger (SCM sym, vector<Acknowledge_information> const *ack_ar
   return 0;
 }
 
+Moment
+get_event_length (Stream_event *e)
+{
+  Moment *m = unsmob_moment (e->get_property ("length"));
+  if (m)
+    return *m;
+  else
+    return Moment (0);
+}
+
+/*
+  Helper, used through ASSIGN_EVENT_ONCE to throw warnings for
+  simultaneous events. The helper is only useful in listen_* methods
+  of translators.
+*/
+bool
+internal_event_assignment (Stream_event **old_ev, Stream_event *new_ev, const char *function)
+{
+  if (*old_ev)
+    {
+      /* extract event class from function name */
+      const char *prefix = "listen_";
+      string ev_class = function;
+      /* This assertion fails if EVENT_ASSIGNMENT was called outside a
+        translator listener. Don't do that. */
+      assert (0 == ev_class.find (prefix));
+
+      /* "listen_foo_bar" -> "foo-bar" */
+      ev_class.erase (0, strlen(prefix));
+      replace_all (ev_class, '_', '-');
+
+      new_ev->origin ()->warning (_f ("Two simultaneous %s events, junking this one", ev_class.c_str ()));
+      (*old_ev)->origin ()->warning (_f ("Previous %s event here", ev_class.c_str ()));
+      return false;
+    }
+  else
+    {
+      *old_ev = new_ev;
+      return true;
+    }
+}
+
 ADD_TRANSLATOR (Translator,
                "Base class. Unused",
                "",