]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/parser.yy
Issue 3706: Allow computed identifiers for \change
[lilypond.git] / lily / parser.yy
index 79afe5ac1125d44b86333d806fc3e188f4968dab..79a4b1114f214c70cd8942e8bd6273e1e417a880 100644 (file)
@@ -269,6 +269,7 @@ int yylex (YYSTYPE *s, YYLTYPE *loc, Lily_parser *parser);
 %token REST "\\rest"
 %token REVERT "\\revert"
 %token SCORE "\\score"
+%token SCORELINES "\\score-lines"
 %token SEQUENTIAL "\\sequential"
 %token SET "\\set"
 %token SIMULTANEOUS "\\simultaneous"
@@ -325,12 +326,14 @@ If we give names, Bison complains.
 %token EMBEDDED_LILY "#{"
 
 %token BOOK_IDENTIFIER
+%token CHORD_BODY_IDENTIFIER
 %token CHORD_MODIFIER
 %token CHORD_REPETITION
 %token CONTEXT_DEF_IDENTIFIER
 %token CONTEXT_MOD_IDENTIFIER
 %token DRUM_PITCH
 %token PITCH_IDENTIFIER
+%token PITCH_ARG
 %token DURATION_IDENTIFIER
 %token EVENT_IDENTIFIER
 %token EVENT_FUNCTION
@@ -472,6 +475,7 @@ embedded_scm_bare_arg:
        {
                $$ = parser->lexer_->eval_scm_token ($1);
        }
+       | FRACTION
        | full_markup_list
        | context_modification
        | score_block
@@ -510,6 +514,18 @@ scm_function_call:
        }
        ;
 
+embedded_lilypond_number:
+       '-' embedded_lilypond_number
+       {
+               $$ = scm_difference ($2, SCM_UNDEFINED);
+       }
+       | bare_number_common
+       | UNSIGNED NUMBER_IDENTIFIER
+       {
+               $$ = scm_product ($1, $2);
+       }
+       ;
+
 embedded_lilypond:
        /* empty */
        {
@@ -519,7 +535,31 @@ embedded_lilypond:
                // contains no source location.
                $$ = MAKE_SYNTAX ("void-music", @$);
        }
-       | identifier_init
+       | identifier_init_nonumber
+       | embedded_lilypond_number
+       | post_event post_events
+       {
+               $$ = scm_reverse_x ($2, SCM_EOL);
+               if (Music *m = unsmob_music ($1))
+               {
+                       if (m->is_mus_type ("post-event-wrapper"))
+                               $$ = scm_append
+                                       (scm_list_2 (m->get_property ("elements"),
+                                                    $$));
+                       else
+                               $$ = scm_cons ($1, $$);
+               }
+               if (scm_is_pair ($$)
+                   && scm_is_null (scm_cdr ($$)))
+                       $$ = scm_car ($$);
+               else
+               {
+                       Music * m = MY_MAKE_MUSIC ("PostEvents", @$);
+                       m->set_property ("elements", $$);
+                       $$ = m->unprotect ();
+               }
+       }
+       | multiplied_duration
        | music_embedded music_embedded music_list {
                $3 = scm_reverse_x ($3, SCM_EOL);
                if (unsmob_music ($2))
@@ -576,12 +616,8 @@ assignment:
 
 
 identifier_init:
-       score_block
-       | book_block
-       | bookpart_block
-       | output_def
-       | context_def_spec_block
-       | music_assign
+       identifier_init_nonumber
+       | number_expression
        | post_event_nofinger post_events
        {
                $$ = scm_reverse_x ($2, SCM_EOL);
@@ -604,7 +640,15 @@ identifier_init:
                        $$ = m->unprotect ();
                }
        }
-       | number_expression
+       ;
+
+identifier_init_nonumber:
+       score_block
+       | book_block
+       | bookpart_block
+       | output_def
+       | context_def_spec_block
+       | music_assign
        | FRACTION
        | string
         | embedded_scm
@@ -1039,6 +1083,18 @@ music_embedded:
        {
                $$ = $3;
        }
+       | multiplied_duration post_events
+       {
+               Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
+
+               parser->default_duration_ = *unsmob_duration ($1);
+               n->set_property ("duration", $1);
+
+               if (scm_is_pair ($2))
+                       n->set_property ("articulations",
+                                        scm_reverse_x ($2, SCM_EOL));
+               $$ = n->unprotect ();
+       }
        ;
 
 music_embedded_backup:
@@ -1280,11 +1336,7 @@ grouped_music_list:
  */
 
 function_arglist_nonbackup_common:
-       EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup FRACTION
-       {
-               $$ = check_scheme_arg (parser, @4, $4, $3, $2);
-       }
-       | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup post_event_nofinger
+       EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup post_event_nofinger
        {
                $$ = check_scheme_arg (parser, @4, $4, $3, $2);
        }
@@ -1489,13 +1541,14 @@ function_arglist_nonbackup_reparse:
        ;
 
 
+// function_arglist_backup can't occur at the end of an argument
+// list.  It needs to be careful about avoiding lookahead only until
+// it has made a decision whether or not to accept the parsed entity.
+// At that point of time, music requiring lookahead to parse becomes
+// fine.
 function_arglist_backup:
-       function_arglist_backup_common
-       | function_arglist_common
-       ;
-
-function_arglist_backup_common:
-       EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup embedded_scm_arg_closed
+       function_arglist_common
+       | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup embedded_scm_arg_closed
        {
                if (scm_is_true (scm_call_1 ($2, $4)))
                {
@@ -1519,6 +1572,17 @@ function_arglist_backup_common:
                        MYBACKUP (REPEAT_IDENTIFIER, scm_cons ($5, $6), @4);
                }
        }
+       | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup chord_body
+       {
+               if (scm_is_true (scm_call_1 ($2, $4)))
+               {
+                       $$ = $3;
+                       MYREPARSE (@4, $2, CHORD_BODY_IDENTIFIER, $4);
+               } else {
+                       $$ = scm_cons (loc_on_music (@3, $1), $3);
+                       MYBACKUP (CHORD_BODY_IDENTIFIER, $4, @4);
+               }
+       }
        | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup post_event_nofinger
        {
                if (scm_is_true (scm_call_1 ($2, $4)))
@@ -1577,16 +1641,6 @@ function_arglist_backup_common:
                        MYBACKUP (NUMBER_IDENTIFIER, $4, @4);
                }
        }
-       | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup FRACTION
-       {
-               if (scm_is_true (scm_call_1 ($2, $4)))
-               {
-                       $$ = scm_cons ($4, $3);
-               } else {
-                       $$ = scm_cons (loc_on_music (@3, $1), $3);
-                       MYBACKUP (FRACTION, $4, @4);
-               }
-       }
        | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup '-' UNSIGNED
        {
                SCM n = scm_difference ($5, SCM_UNDEFINED);
@@ -1630,9 +1684,15 @@ function_arglist_backup_common:
        }
        | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup PITCH_IDENTIFIER
        {
-               if (scm_is_true (scm_call_1 ($2, $4)))
+               SCM m = make_music_from_simple (parser, @4, $4);
+               if (unsmob_music (m) && scm_is_true (scm_call_1 ($2, m)))
                {
-                       $$ = scm_cons ($4, $3);
+                       MYREPARSE (@4, $2, PITCH_IDENTIFIER, $4);
+                       $$ = $3;
+               } else if (scm_is_true (scm_call_1 ($2, $4)))
+               {
+                       MYREPARSE (@4, $2, PITCH_ARG, $4);
+                       $$ = $3;
                } else {
                        $$ = scm_cons (loc_on_music (@3, $1), $3);
                        MYBACKUP (PITCH_IDENTIFIER, $4, @4);
@@ -1640,10 +1700,15 @@ function_arglist_backup_common:
        }
        | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup NOTENAME_PITCH
        {
-               if (scm_is_true (scm_call_1 ($2, $4)))
+               SCM m = make_music_from_simple (parser, @4, $4);
+               if (unsmob_music (m) && scm_is_true (scm_call_1 ($2, m)))
                {
                        MYREPARSE (@4, $2, NOTENAME_PITCH, $4);
                        $$ = $3;
+               } else if (scm_is_true (scm_call_1 ($2, $4)))
+               {
+                       MYREPARSE (@4, $2, PITCH_ARG, $4);
+                       $$ = $3;
                } else {
                        $$ = scm_cons (loc_on_music (@3, $1), $3);
                        MYBACKUP (NOTENAME_PITCH, $4, @4);
@@ -1651,10 +1716,15 @@ function_arglist_backup_common:
        }
        | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup TONICNAME_PITCH
        {
-               if (scm_is_true (scm_call_1 ($2, $4)))
+               SCM m = make_music_from_simple (parser, @4, $4);
+               if (unsmob_music (m) && scm_is_true (scm_call_1 ($2, m)))
                {
                        MYREPARSE (@4, $2, TONICNAME_PITCH, $4);
                        $$ = $3;
+               } else if (scm_is_true (scm_call_1 ($2, $4)))
+               {
+                       MYREPARSE (@4, $2, PITCH_ARG, $4);
+                       $$ = $3;
                } else {
                        $$ = scm_cons (loc_on_music (@3, $1), $3);
                        MYBACKUP (TONICNAME_PITCH, $4, @4);
@@ -1711,6 +1781,11 @@ function_arglist_backup_common:
                                               (parser, @3, $3),
                                               $1, $2);
        }
+       | function_arglist_backup REPARSE pitch_arg
+       {
+               $$ = check_scheme_arg (parser, @3,
+                                      $3, $1, $2);
+       }               
        | function_arglist_backup REPARSE bare_number_common
        {
                $$ = check_scheme_arg (parser, @3,
@@ -1762,11 +1837,6 @@ function_arglist_common:
                $$ = check_scheme_arg (parser, @3,
                                       $3, $2, $1);
        }
-       | EXPECT_SCM function_arglist_optional FRACTION
-       {
-               $$ = check_scheme_arg (parser, @3,
-                                      $3, $2, $1);
-       }
        | EXPECT_SCM function_arglist_optional post_event_nofinger
        {
                $$ = check_scheme_arg (parser, @3,
@@ -1930,11 +2000,6 @@ function_arglist_closed_common:
                $$ = check_scheme_arg (parser, @3,
                                       $3, $2, $1);
        }
-       | EXPECT_SCM function_arglist_optional FRACTION
-       {
-               $$ = check_scheme_arg (parser, @3,
-                                      $3, $2, $1);
-       }
        | function_arglist_common_reparse REPARSE SCM_ARG
        {
                $$ = check_scheme_arg (parser, @3,
@@ -2143,8 +2208,8 @@ re_rhythmed_music:
        ;
 
 context_change:
-       CHANGE STRING '=' STRING  {
-               $$ = MAKE_SYNTAX ("context-change", @$, scm_string_to_symbol ($2), $4);
+       CHANGE symbol '=' simple_string  {
+               $$ = MAKE_SYNTAX ("context-change", @$, $2, $4);
        }
        ;
 
@@ -2464,7 +2529,6 @@ scalar:
        {
                $$ = scm_difference ($2, SCM_UNDEFINED);
        }
-       | FRACTION
        | STRING
        | full_markup
        ;
@@ -2485,13 +2549,19 @@ event_chord:
                }
        } %prec ':'
        | simple_chord_elements post_events     {
-               SCM elts = ly_append2 ($1, scm_reverse_x ($2, SCM_EOL));
-
-               Input i;
-               /* why is this giving wrong start location? -ns
-                * i = @$; */
-               i.set_location (@1, @2);
-               $$ = MAKE_SYNTAX ("event-chord", i, elts);
+               if (scm_is_pair ($2)) {
+                       if (unsmob_pitch ($1))
+                               $1 = make_chord_elements (@1,
+                                                         $1,
+                                                         parser->default_duration_.smobbed_copy (),
+                                                         SCM_EOL);
+
+                       SCM elts = ly_append2 ($1, scm_reverse_x ($2, SCM_EOL));
+
+                       $$ = MAKE_SYNTAX ("event-chord", @1, elts);
+               } else if (!unsmob_pitch ($1))
+                       $$ = MAKE_SYNTAX ("event-chord", @1, $1);
+               // A mere pitch drops through.
        } %prec ':'
        | CHORD_REPETITION optional_notemode_duration post_events {
                Input i;
@@ -2533,6 +2603,7 @@ chord_body:
        {
                $$ = MAKE_SYNTAX ("event-chord", @$, scm_reverse_x ($2, SCM_EOL));
        }
+       | CHORD_BODY_IDENTIFIER
        ;
 
 chord_body_elements:
@@ -2743,11 +2814,18 @@ direction_reqd_event:
        }
        | script_abbreviation {
                SCM s = parser->lexer_->lookup_identifier ("dash" + ly_scm2string ($1));
-               Music *a = MY_MAKE_MUSIC ("ArticulationEvent", @$);
-               if (scm_is_string (s))
+               if (scm_is_string (s)) {
+                       Music *a = MY_MAKE_MUSIC ("ArticulationEvent", @$);
                        a->set_property ("articulation-type", s);
-               else parser->parser_error (@1, _ ("expecting string as script definition"));
-               $$ = a->unprotect ();
+                       $$ = a->unprotect ();
+               } else if (ly_prob_type_p (s, ly_symbol2scm ("ArticulationEvent"))) {
+                       $$ = s;
+                       if (Music *original = unsmob_music (s)) {
+                               Music *a = original->clone ();
+                               a->set_spot (parser->lexer_->override_input (@$));
+                               $$ = a->unprotect ();
+                       }
+               } else parser->parser_error (@1, _ ("expecting string or ArticulationEvent as script definition"));
        }
        ;
 
@@ -2811,7 +2889,25 @@ steno_tonic_pitch:
 
 pitch:
        steno_pitch
-       | PITCH_IDENTIFIER
+       | PITCH_IDENTIFIER quotes {
+                if (!scm_is_eq (SCM_INUM0, $2))
+                {
+                        Pitch p = *unsmob_pitch ($1);
+                        p = p.transposed (Pitch (scm_to_int ($2),0,0));
+                        $$ = p.smobbed_copy ();
+                }
+       }
+       ;
+
+pitch_arg:
+       PITCH_ARG quotes {
+                if (!scm_is_eq (SCM_INUM0, $2))
+                {
+                        Pitch p = *unsmob_pitch ($1);
+                        p = p.transposed (Pitch (scm_to_int ($2),0,0));
+                        $$ = p.smobbed_copy ();
+                }
+       }
        ;
 
 gen_text_def:
@@ -3123,6 +3219,7 @@ simple_element:
        }
        ;
 
+// Can return a single pitch rather than a list.
 simple_chord_elements:
        new_chord {
                 if (!parser->lexer_->is_chord_state ())
@@ -3161,9 +3258,13 @@ lyric_element_music:
        } %prec ':'
        ;
 
+// Can return a single pitch rather than a list.
 new_chord:
-       steno_tonic_pitch optional_notemode_duration   {
-               $$ = make_chord_elements (@$, $1, $2, SCM_EOL);
+       steno_tonic_pitch maybe_notemode_duration   {
+               if (SCM_UNBNDP ($2))
+                       $$ = $1;
+               else
+                       $$ = make_chord_elements (@$, $1, $2, SCM_EOL);
        }
        | steno_tonic_pitch optional_notemode_duration chord_separator chord_items {
                SCM its = scm_reverse_x ($4, SCM_EOL);
@@ -3227,10 +3328,10 @@ step_number:
        ;
 
 tempo_range:
-       UNSIGNED {
+       unsigned_number {
                $$ = $1;
-       }
-       | UNSIGNED '-' UNSIGNED {
+       } %prec ':'
+       | unsigned_number '-' unsigned_number {
                $$ = scm_cons ($1, $3);
        }
        ;
@@ -3299,6 +3400,23 @@ bare_number_closed:
 unsigned_number:
        UNSIGNED
        | NUMBER_IDENTIFIER
+       {
+               if (!scm_is_integer ($1)
+                   || scm_is_true (scm_negative_p ($1)))
+               {
+                       parser->parser_error (@1, _("not an unsigned integer"));
+                       $$ = SCM_INUM0;
+               }
+       }
+       | embedded_scm
+       {
+               if (!scm_is_integer ($1)
+                   || scm_is_true (scm_negative_p ($1)))
+               {
+                       parser->parser_error (@1, _("not an unsigned integer"));
+                       $$ = SCM_INUM0;
+               }
+       }
        ;
 
 exclamations:
@@ -3342,7 +3460,7 @@ full_markup:
        ;
 
 markup_top:
-       simple_markup_list {
+       markup_list {
                $$ = scm_list_2 (ly_lily_module_constant ("line-markup"),  $1);
        }
        | markup_head_1_list simple_markup
@@ -3370,7 +3488,7 @@ markup_scm:
        ;
                        
 
-simple_markup_list:
+markup_list:
        markup_composed_list {
                $$ = $1;
        }
@@ -3388,22 +3506,11 @@ markup_uncomposed_list:
        {
                $$ = $2;
        }
-       ;
-
-markup_list:
-       simple_markup_list
-       | markup_score
-       {
-               $$ = scm_list_1 (scm_list_2 (ly_lily_module_constant ("score-lines-markup-list"), $1));
-       }
-       ;
-
-markup_score:
-       SCORE {
+       | SCORELINES {
                SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
                parser->lexer_->push_note_state (nn);
        } '{' score_body '}' {
-               $$ = $4;
+               $$ = scm_list_1 (scm_list_2 (ly_lily_module_constant ("score-lines-markup-list"), $4));
                parser->lexer_->pop_state ();
        }
        ;
@@ -3426,7 +3533,7 @@ markup_braced_list_body:
        | markup_braced_list_body markup {
                $$ = scm_cons ($2, $1);
        }
-       | markup_braced_list_body simple_markup_list {
+       | markup_braced_list_body markup_list {
                $$ = scm_reverse_x ($2, $1);
        }
        ;
@@ -3475,6 +3582,13 @@ simple_markup:
        STRING {
                $$ = make_simple_markup ($1);
        }
+       | SCORE {
+               SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
+               parser->lexer_->push_note_state (nn);
+       } '{' score_body '}' {
+               $$ = scm_list_2 (ly_lily_module_constant ("score-markup"), $4);
+               parser->lexer_->pop_state ();
+       }
        | MARKUP_FUNCTION markup_command_basic_arguments {
                $$ = scm_cons ($1, scm_reverse_x ($2, SCM_EOL));
        }
@@ -3482,10 +3596,6 @@ simple_markup:
        {
                $$ = $2;
        }
-       | markup_score
-       {
-               $$ = scm_list_2 (ly_lily_module_constant ("score-markup"), $1);
-       }
        ;
 
 markup:
@@ -3723,9 +3833,12 @@ make_music_from_simple (Lily_parser *parser, Input loc, SCM simple)
                                            parser->default_duration_.smobbed_copy ());
        } else if (parser->lexer_->is_chord_state ()) {
                if (unsmob_pitch (simple))
-                       return make_chord_elements (loc, simple,
-                                                   parser->default_duration_.smobbed_copy (),
-                                                   SCM_EOL);
+                       return MAKE_SYNTAX
+                               ("event-chord",
+                                loc,
+                                make_chord_elements (loc, simple,
+                                                     parser->default_duration_.smobbed_copy (),
+                                                     SCM_EOL));
        }
        return simple;
 }