]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/parser.yy
Issue 4938 (2/3) Refactor handling of MIDI control changes
[lilypond.git] / lily / parser.yy
index 54de4727b5b75d8f16776198ee3e0d94bff6fcce..159786b93c1dbc13268e72140f1cf7a485996177 100644 (file)
@@ -346,7 +346,6 @@ If we give names, Bison complains.
 %token BOOK_IDENTIFIER
 %token CHORD_MODIFIER
 %token CHORD_REPETITION
-%token CONTEXT_MOD_IDENTIFIER
 %token DRUM_PITCH
  /* Artificial token for durations in argument lists */
 %token DURATION_ARG
@@ -387,8 +386,7 @@ prec levels in different prods */
 start_symbol:
        lilypond
        | EMBEDDED_LILY {
-               SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
-               parser->lexer_->push_note_state (nn);
+               parser->lexer_->push_note_state (Lily::pitchnames);
        } embedded_lilypond {
                parser->lexer_->pop_state ();
                 *retval = $3;
@@ -410,10 +408,8 @@ lilypond:  /* empty */ { $$ = SCM_UNSPECIFIED; }
 
 
 toplevel_expression:
-       {
-               parser->lexer_->add_scope (get_header (parser));
-       } lilypond_header {
-               parser->lexer_->set_identifier (ly_symbol2scm ("$defaultheader"), $2);
+       header_block {
+               parser->lexer_->set_identifier (ly_symbol2scm ("$defaultheader"), $1);
        }
        | book_block {
                SCM proc = parser->lexer_->lookup_identifier ("toplevel-book-handler");
@@ -476,6 +472,12 @@ toplevel_expression:
                                id = ly_symbol2scm ("$defaultlayout");
 
                        parser->lexer_->set_identifier (id, $1);
+               } else if (ly_is_module ($1))
+               {
+                       SCM module = get_header (parser);
+                       ly_module_copy (module, $1);
+                       parser->lexer_->set_identifier
+                               (ly_symbol2scm ("$defaultheader"), module);
                } else if (!scm_is_eq ($1, SCM_UNSPECIFIED))
                        parser->parser_error (@1, _("bad expression type"));
        }
@@ -527,6 +529,7 @@ embedded_scm_bare_arg:
        | partial_markup
        | full_markup_list
        | context_modification
+       | header_block
        | score_block
        | context_def_spec_block
        | book_block
@@ -635,8 +638,15 @@ lilypond_header_body:
        | lilypond_header_body assignment  {
 
        }
-       | lilypond_header_body embedded_scm  {
-
+       | lilypond_header_body SCM_TOKEN {
+               // Evaluate and ignore #xxx, as opposed to \xxx
+               parser->lexer_->eval_scm_token ($2, @2);
+       }
+       | lilypond_header_body embedded_scm_active {
+               if (ly_is_module ($2))
+                       ly_module_copy (scm_current_module (), $2);
+               else if (!scm_is_eq ($2, SCM_UNSPECIFIED))
+                       parser->parser_error (@2, _("bad expression type"));
        }
        ;
 
@@ -646,6 +656,14 @@ lilypond_header:
        }
        ;
 
+header_block:
+       {
+               parser->lexer_->add_scope (get_header (parser));
+       } lilypond_header {
+               $$ = $2;
+       }
+       ;
+
 /*
        DECLARATIONS
 */
@@ -658,11 +676,6 @@ assignment:
                parser->lexer_->set_identifier ($1, $3);
                 $$ = SCM_UNSPECIFIED;
        }
-       | assignment_id property_path '=' identifier_init {
-               SCM path = scm_cons (scm_string_to_symbol ($1), $2);
-               parser->lexer_->set_identifier (path, $4);
-                $$ = SCM_UNSPECIFIED;
-       }
        | assignment_id '.' property_path '=' identifier_init {
                SCM path = scm_cons (scm_string_to_symbol ($1), $3);
                parser->lexer_->set_identifier (path, $5);
@@ -679,6 +692,14 @@ assignment:
 identifier_init:
        identifier_init_nonumber
        | number_expression
+       | symbol_list_part_bare '.' property_path
+       {
+               $$ = scm_reverse_x ($1, $3);
+       }
+       | symbol_list_part_bare ',' property_path
+       {
+               $$ = scm_reverse_x ($1, $3);
+       }
        | post_event_nofinger post_events
        {
                $$ = scm_reverse_x ($2, SCM_EOL);
@@ -704,7 +725,8 @@ identifier_init:
        ;
 
 identifier_init_nonumber:
-       score_block
+       header_block
+       | score_block
        | book_block
        | bookpart_block
        | output_def
@@ -821,8 +843,7 @@ context_mod_arg:
        embedded_scm
        |
        {
-               SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
-               parser->lexer_->push_note_state (nn);
+               parser->lexer_->push_note_state (Lily::pitchnames);
        }
        composite_music
        {
@@ -954,16 +975,15 @@ book_body:
                        SCM proc = parser->lexer_->lookup_identifier ("book-score-handler");
                        scm_call_2 (proc, $1, $2);
                } else if (Output_def *od = unsmob<Output_def> ($2)) {
-                       SCM id = SCM_EOL;
-
-                       if (to_boolean (od->c_variable ("is-paper")))
-                               id = ly_symbol2scm ("$defaultpaper");
-                       else if (to_boolean (od->c_variable ("is-midi")))
-                               id = ly_symbol2scm ("$defaultmidi");
-                       else if (to_boolean (od->c_variable ("is-layout")))
-                               id = ly_symbol2scm ("$defaultlayout");
-
-                       parser->lexer_->set_identifier (id, $2);
+                       if (to_boolean (od->lookup_variable (ly_symbol2scm ("is-paper")))) {
+                               unsmob<Book> ($1)->paper_ = od;
+                               set_paper (parser, od);
+                       } else {
+                               parser->parser_error (@2, _ ("need \\paper for paper block"));
+                       }
+               } else if (ly_is_module ($2))
+               {
+                       ly_module_copy (unsmob<Book> ($1)->header_, $2);
                } else if (!scm_is_eq ($2, SCM_UNSPECIFIED))
                        parser->parser_error (@2, _("bad expression type"));
        }
@@ -1035,16 +1055,16 @@ bookpart_body:
                        SCM proc = parser->lexer_->lookup_identifier ("bookpart-score-handler");
                        scm_call_2 (proc, $1, $2);
                } else if (Output_def *od = unsmob<Output_def> ($2)) {
-                       SCM id = SCM_EOL;
-
-                       if (to_boolean (od->c_variable ("is-paper")))
-                               id = ly_symbol2scm ("$defaultpaper");
-                       else if (to_boolean (od->c_variable ("is-midi")))
-                               id = ly_symbol2scm ("$defaultmidi");
-                       else if (to_boolean (od->c_variable ("is-layout")))
-                               id = ly_symbol2scm ("$defaultlayout");
-
-                       parser->lexer_->set_identifier (id, $2);
+                       if (to_boolean (od->lookup_variable (ly_symbol2scm ("is-paper")))) {
+                               unsmob<Book> ($1)->paper_ = od;
+                       } else {
+                               parser->parser_error (@2, _ ("need \\paper for paper block"));
+                       }
+               } else if (ly_is_module ($2)) {
+                       Book *book = unsmob<Book> ($1);
+                       if (!ly_is_module (book->header_))
+                               book->header_ = ly_make_module (false);
+                       ly_module_copy (book->header_, $2);
                } else if (!scm_is_eq ($2, SCM_UNSPECIFIED))
                        parser->parser_error (@2, _("bad expression type"));
        }
@@ -1143,6 +1163,22 @@ score_items:
                                scm_set_cdr_x ($$, scm_cons ($2, scm_cdr ($$)));
                        else
                                $$ = scm_cons ($2, $$);
+               } else if (ly_is_module ($2)) {
+                       SCM module = SCM_UNSPECIFIED;
+                       if (score) {
+                               module = score->get_header ();
+                               if (!ly_is_module (module))
+                               {
+                                       module = ly_make_module (false);
+                                       score->set_header (module);
+                               }
+                       } else if (scm_is_pair ($$) && ly_is_module (scm_car ($$)))
+                               module = scm_car ($$);
+                       else {
+                               module = ly_make_module (false);
+                               $$ = scm_cons (module, $$);
+                       }
+                       ly_module_copy (module, $2);
                } else if (!scm_is_eq ($2, SCM_UNSPECIFIED))
                        parser->parser_error (@2, _("Spurious expression in \\score"));
        }
@@ -1279,8 +1315,7 @@ output_def_body:
        {
                if (scm_is_pair ($1))
                        $1 = scm_car ($1);
-               SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
-               parser->lexer_->push_note_state (nn);
+               parser->lexer_->push_note_state (Lily::pitchnames);
        } music_or_context_def
        {
                parser->lexer_->pop_state ();
@@ -1452,21 +1487,12 @@ simple_music:
 context_modification:
         WITH
        {
-               SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
-               parser->lexer_->push_note_state (nn);
+               parser->lexer_->push_note_state (Lily::pitchnames);
        } '{' context_mod_list '}'
         {
                 parser->lexer_->pop_state ();
                 $$ = $4;
         }
-        | WITH CONTEXT_MOD_IDENTIFIER
-        {
-                $$ = $2;
-        }
-        | CONTEXT_MOD_IDENTIFIER
-        {
-                $$ = $1;
-        }
        | WITH context_modification_arg
        {
                if (unsmob<Music> ($2)) {
@@ -1476,7 +1502,10 @@ context_modification:
                if (unsmob<Context_mod> ($2))
                        $$ = $2;
                else {
-                       parser->parser_error (@2, _ ("not a context mod"));
+                       // let's permit \with #*unspecified* to go for
+                       // an empty context mod
+                       if (!scm_is_eq ($2, SCM_UNSPECIFIED))
+                               parser->parser_error (@2, _ ("not a context mod"));
                        $$ = Context_mod ().smobbed_copy ();
                }
        }
@@ -1487,16 +1516,40 @@ context_modification_arg:
        | MUSIC_IDENTIFIER
        ;
 
-optional_context_mod:
-        /**/ {
-            $$ = SCM_EOL;
-        }
-        | context_modification
+/* A list of single mods collected from a (possibly empty) sequence of
+ * context modifications, usually written as \with ... \with ...
+ */
+
+optional_context_mods:
+       context_modification_mods_list
         {
-              $$ = $1;
+               if (scm_is_pair ($1))
+                       $$ = scm_append_x (scm_reverse_x ($1, SCM_EOL));
         }
         ;
 
+/* The worker for optional_context_mods conses a (reversed) list where
+ * each element contains the list of single context mods from one
+ * context modification block.  Context_mod::get_mods creates fresh
+ * copies, so it's okay to use append! on them.
+ */
+
+context_modification_mods_list:
+       /**/ {
+               $$ = SCM_EOL;
+       }
+       | context_modification_mods_list context_modification
+       {
+               if (Context_mod *m = unsmob<Context_mod> ($2))
+                       $$ = scm_cons (m->get_mods (), $1);
+       }
+       ;
+
+/* A Context_mod is a container for a list of context mods like
+ * \consists ...  \override ... .  context_mod_list produces a
+ * Context_mod from the inside of a \with { ... } statement.
+ */
+
 context_mod_list:
         /**/ {
             $$ = Context_mod ().smobbed_copy ();
@@ -1505,58 +1558,34 @@ context_mod_list:
                if (!SCM_UNBNDP ($2))
                        unsmob<Context_mod> ($1)->add_context_mod ($2);
         }
-        | context_mod_list CONTEXT_MOD_IDENTIFIER {
-                 Context_mod *md = unsmob<Context_mod> ($2);
-                 if (md)
-                     unsmob<Context_mod> ($1)->add_context_mods (md->get_mods ());
-        }
        | context_mod_list context_mod_arg {
-               if (scm_is_eq ($2, SCM_UNSPECIFIED))
-                       ;
-               else if (unsmob<Music> ($2)) {
+               if (unsmob<Music> ($2)) {
                        SCM proc = parser->lexer_->lookup_identifier ("context-mod-music-handler");
                        $2 = scm_call_1 (proc, $2);
                }
                if (unsmob<Context_mod> ($2))
                        unsmob<Context_mod> ($$)->add_context_mods
                                (unsmob<Context_mod> ($2)->get_mods ());
-               else {
+               else if (!scm_is_eq ($2, SCM_UNSPECIFIED))
                        parser->parser_error (@2, _ ("not a context mod"));
-               }
         }
         ;
 
 context_prefix:
-       CONTEXT symbol optional_id optional_context_mod {
-                Context_mod *ctxmod = unsmob<Context_mod> ($4);
-                SCM mods = SCM_EOL;
-                if (ctxmod)
-                        mods = ctxmod->get_mods ();
-               $$ = START_MAKE_SYNTAX (context_specification, $2, $3, mods, SCM_BOOL_F);
+       CONTEXT symbol optional_id optional_context_mods {
+               $$ = START_MAKE_SYNTAX (context_specification, $2, $3, $4, SCM_BOOL_F);
        }
-       | NEWCONTEXT symbol optional_id optional_context_mod {
-                Context_mod *ctxmod = unsmob<Context_mod> ($4);
-                SCM mods = SCM_EOL;
-                if (ctxmod)
-                        mods = ctxmod->get_mods ();
-               $$ = START_MAKE_SYNTAX (context_specification, $2, $3, mods, SCM_BOOL_T);
+       | NEWCONTEXT symbol optional_id optional_context_mods {
+               $$ = START_MAKE_SYNTAX (context_specification, $2, $3, $4, SCM_BOOL_T);
        }
        ;
 
 new_lyrics:
-       ADDLYRICS optional_context_mod lyric_mode_music {
-               Context_mod *ctxmod = unsmob<Context_mod> ($2);
-               SCM mods = SCM_EOL;
-               if (ctxmod)
-                       mods = ctxmod->get_mods ();
-               $$ = scm_acons ($3, mods, SCM_EOL);
+       ADDLYRICS optional_context_mods lyric_mode_music {
+               $$ = scm_acons ($3, $2, SCM_EOL);
        }
-       | new_lyrics ADDLYRICS optional_context_mod lyric_mode_music {
-               Context_mod *ctxmod = unsmob<Context_mod> ($3);
-               SCM mods = SCM_EOL;
-               if (ctxmod)
-                       mods = ctxmod->get_mods ();
-               $$ = scm_acons ($4, mods, $1);
+       | new_lyrics ADDLYRICS optional_context_mods lyric_mode_music {
+               $$ = scm_acons ($4, $3, $1);
        }
        ;
 
@@ -1731,6 +1760,21 @@ symbol_list_element:
        | UNSIGNED
        ;
 
+symbol_list_part_bare:
+       STRING
+       {
+               $$ = try_string_variants (Lily::key_list_p, $1);
+               if (SCM_UNBNDP ($$)) {
+                       parser->parser_error (@1, _("not a key"));
+                       $$ = SCM_EOL;
+               } else
+                       $$ = scm_reverse ($$);
+       }
+       | UNSIGNED
+       {
+               $$ = scm_list_1 ($1);
+       }
+       ;
 
 function_arglist_nonbackup:
        function_arglist_common
@@ -1902,6 +1946,8 @@ function_arglist_nonbackup_reparse:
                if (scm_is_true (scm_call_1 ($2, $4)))
                        // May be 3 \cm or similar
                        MYREPARSE (@4, $2, REAL, $4);
+               else if (scm_is_true (scm_call_1 ($2, scm_list_1 ($4))))
+                       MYREPARSE (@4, $2, SYMBOL_LIST, scm_list_1 ($4));
                else {
                        SCM d = make_duration ($4);
                        if (!SCM_UNBNDP (d)) {
@@ -2008,6 +2054,8 @@ function_arglist_backup:
                if (scm_is_true (scm_call_1 ($2, $4)))
                        // May be 3 \cm or similar
                        MYREPARSE (@4, $2, REAL, $4);
+               else if (scm_is_true (scm_call_1 ($2, scm_list_1 ($4))))
+                       MYREPARSE (@4, $2, SYMBOL_LIST, scm_list_1 ($4));
                else {
                        SCM d = make_duration ($4);
                        if (!SCM_UNBNDP (d)) {
@@ -2378,6 +2426,8 @@ function_arglist_common_reparse:
                if (scm_is_true (scm_call_1 ($1, $3)))
                        // May be 3 \cm or similar
                        MYREPARSE (@3, $1, REAL, $3);
+               else if (scm_is_true (scm_call_1 ($1, scm_list_1 ($3))))
+                       MYREPARSE (@3, $1, SYMBOL_LIST, scm_list_1 ($3));
                else {
                        SCM d = make_duration ($3);
                        if (!SCM_UNBNDP (d)) {
@@ -2490,12 +2540,8 @@ mode_changed_music:
                }
                parser->lexer_->pop_state ();
        }
-       | mode_changing_head_with_context optional_context_mod grouped_music_list {
-                Context_mod *ctxmod = unsmob<Context_mod> ($2);
-                SCM mods = SCM_EOL;
-                if (ctxmod)
-                        mods = ctxmod->get_mods ();
-               $$ = MAKE_SYNTAX (context_specification, @$, $1, SCM_EOL, mods, SCM_BOOL_T, $3);
+       | mode_changing_head_with_context optional_context_mods grouped_music_list {
+               $$ = MAKE_SYNTAX (context_specification, @$, $1, SCM_EOL, $2, SCM_BOOL_T, $3);
                if (scm_is_eq ($1, ly_symbol2scm ("ChordNames")))
                {
                  $$ = MAKE_SYNTAX (unrelativable_music, @$, $$);
@@ -2506,15 +2552,13 @@ mode_changed_music:
 
 mode_changing_head:
        NOTEMODE {
-               SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
-               parser->lexer_->push_note_state (nn);
+               parser->lexer_->push_note_state (Lily::pitchnames);
 
                $$ = ly_symbol2scm ("notes");
        }
        | DRUMMODE
                {
-               SCM nn = parser->lexer_->lookup_identifier ("drumPitchNames");
-               parser->lexer_->push_note_state (nn);
+               parser->lexer_->push_note_state (Lily::drum_pitch_names);
 
                $$ = ly_symbol2scm ("drums");
        }
@@ -2524,10 +2568,9 @@ mode_changing_head:
                $$ = ly_symbol2scm ("figures");
        }
        | CHORDMODE {
-               SCM nn = parser->lexer_->lookup_identifier ("chordmodifiers");
-               parser->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
-               nn = parser->lexer_->lookup_identifier ("pitchnames");
-               parser->lexer_->push_chord_state (nn);
+               parser->lexer_->chordmodifier_tab_ =
+                       alist_to_hashq (Lily::chordmodifiers);
+               parser->lexer_->push_chord_state (Lily::pitchnames);
                $$ = ly_symbol2scm ("chords");
 
        }
@@ -2539,8 +2582,7 @@ mode_changing_head:
 
 mode_changing_head_with_context:
        DRUMS {
-               SCM nn = parser->lexer_->lookup_identifier ("drumPitchNames");
-               parser->lexer_->push_note_state (nn);
+               parser->lexer_->push_note_state (Lily::drum_pitch_names);
 
                $$ = ly_symbol2scm ("DrumStaff");
        }
@@ -2550,10 +2592,9 @@ mode_changing_head_with_context:
                $$ = ly_symbol2scm ("FiguredBass");
        }
        | CHORDS {
-               SCM nn = parser->lexer_->lookup_identifier ("chordmodifiers");
-               parser->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
-               nn = parser->lexer_->lookup_identifier ("pitchnames");
-               parser->lexer_->push_chord_state (nn);
+               parser->lexer_->chordmodifier_tab_ =
+                       alist_to_hashq (Lily::chordmodifiers);
+               parser->lexer_->push_chord_state (Lily::pitchnames);
                $$ = ly_symbol2scm ("ChordNames");
        }
        | LYRICS
@@ -2573,9 +2614,6 @@ property_path:
        symbol_list_rev  {
                $$ = scm_reverse_x ($1, SCM_EOL);
        }
-       | symbol_list_rev property_path {
-               $$ = scm_reverse_x ($1, $2);
-       }
        ;
 
 property_operation:
@@ -2585,7 +2623,7 @@ property_operation:
        | UNSET symbol {
                $$ = scm_list_2 (ly_symbol2scm ("unset"), $2);
        }
-       | OVERRIDE property_path '=' scalar {
+       | OVERRIDE revert_arg '=' scalar {
                if (scm_ilength ($2) < 2) {
                        parser->parser_error (@2, _("bad grob property path"));
                        $$ = SCM_UNDEFINED;
@@ -2885,6 +2923,14 @@ scalar:
                $$ = scm_difference ($2, SCM_UNDEFINED);
        }
        | string
+       | symbol_list_part_bare '.' property_path
+       {
+               $$ = scm_reverse_x ($1, $3);
+       }
+       | symbol_list_part_bare ',' property_path
+       {
+               $$ = scm_reverse_x ($1, $3);
+       }
        ;
 
 event_chord:
@@ -2896,15 +2942,11 @@ event_chord:
                }
        } %prec ':'
        | CHORD_REPETITION optional_notemode_duration post_events {
-               Input i;
-               i.set_location (@1, @3);
-               $$ = MAKE_SYNTAX (repetition_chord, i,
+               $$ = MAKE_SYNTAX (repetition_chord, @$,
                                  $2, scm_reverse_x ($3, SCM_EOL));
        } %prec ':'
        | MULTI_MEASURE_REST optional_notemode_duration post_events {
-               Input i;
-               i.set_location (@1, @3);
-               $$ = MAKE_SYNTAX (multi_measure_rest, i, $2,
+               $$ = MAKE_SYNTAX (multi_measure_rest, @$, $2,
                                  scm_reverse_x ($3, SCM_EOL));
        } %prec ':'
        | tempo_event
@@ -3832,8 +3874,7 @@ markup_uncomposed_list:
                $$ = $2;
        }
        | SCORELINES {
-               SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
-               parser->lexer_->push_note_state (nn);
+               parser->lexer_->push_note_state (Lily::pitchnames);
        } '{' score_body '}' {
                Score *sc = unsmob<Score> ($4);
                sc->origin ()->set_spot (@$);
@@ -3946,8 +3987,7 @@ simple_markup:
                $$ = make_simple_markup ($1);
        }
        | SCORE {
-               SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
-               parser->lexer_->push_note_state (nn);
+               parser->lexer_->push_note_state (Lily::pitchnames);
        } '{' score_body '}' {
                Score *sc = unsmob<Score> ($4);
                sc->origin ()->set_spot (@$);
@@ -4030,7 +4070,7 @@ Lily_lexer::try_special_identifiers (SCM *destination, SCM sid)
                return SCM_IDENTIFIER;
         } else if (unsmob<Context_mod> (sid)) {
                 *destination = unsmob<Context_mod> (sid)->smobbed_copy ();
-                return CONTEXT_MOD_IDENTIFIER;
+                return SCM_IDENTIFIER;
        } else if (Music *mus = unsmob<Music> (sid)) {
                mus = mus->clone ();
                *destination = mus->self_scm ();
@@ -4050,7 +4090,8 @@ Lily_lexer::try_special_identifiers (SCM *destination, SCM sid)
                *destination = unsmob<Score> (sid)->clone ()->unprotect ();
                return SCM_IDENTIFIER;
        } else if (scm_is_pair (sid)
-                  && scm_is_true (Lily::key_list_p (sid))) {
+                  && scm_is_pair (scm_car (sid))
+                  && scm_is_true (Lily::key_p (scm_caar (sid)))) {
                *destination = sid;
                return LOOKUP_IDENTIFIER;
        }