lilypond-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

parser.yy et al: Make \relative a music function. (issue 5123043)


From: dak
Subject: parser.yy et al: Make \relative a music function. (issue 5123043)
Date: Sat, 24 Sep 2011 23:00:08 +0000

Reviewers: ,

Message:
This is a rather straightforward patch to take \relative out of the
grammar and let it be implemented as a music function.

I have copied some code from parser.yy straight to music-scheme.cc to
make ly:make-music-relative!.  This code keeps the original indentation
so that git blame -C will recognize its origin.  If desired, the
indentation can be fixed with a separate commit instead.

I have not touched the lily_1_8_relative wart when working on this.  So
there are a few likely changes offering themselves, but it seems to be
cleaner to me if they are in a separate commit.

One surprise was that I got 18 additional shift/reduce conflicts when
removing \relative that needed to get fixed by augmenting the operator
list.

This suggests that it might be possible to introduce some artificial
terminal symbols and corresponding rules (taking the old place of
RELATIVE and more in the grammar) not actually produced by the lexer to
cut down on the number of symbols needing operator priorities.

Interesting.

Regtests pass with good visuals on my system (I think), so I am pushing
this to dev/staging.  It seems like a reasonably solid change to me, and
in good shape.  Since there will be almost no documents not going
through the new code paths, it should move to master only after a full
check including DOC builds on clean systems.

Description:
parser.yy et al: Make \relative a music function.

Please review this at http://codereview.appspot.com/5123043/

Affected files:
  M lily/lily-lexer.cc
  M lily/music-scheme.cc
  M lily/parser.yy
  M ly/music-functions-init.ly


Index: lily/lily-lexer.cc
diff --git a/lily/lily-lexer.cc b/lily/lily-lexer.cc
index f80ea67703010594d088202f86e12702d27879c8..6c1336744b3286f6e2f0ca21d13ad8c5d4a508ca 100644
--- a/lily/lily-lexer.cc
+++ b/lily/lily-lexer.cc
@@ -75,7 +75,6 @@ static Keyword_ent the_key_tab[]
   {"once", ONCE},
   {"override", OVERRIDE},
   {"paper", PAPER},
-  {"relative", RELATIVE},
   {"remove", REMOVE},
   {"repeat", REPEAT},
   {"rest", REST},
Index: lily/music-scheme.cc
diff --git a/lily/music-scheme.cc b/lily/music-scheme.cc
index 89c810a5679df344d5ab41ee8f4f70c2b9dbfd81..34545fa911d33da5293c4c656e2342da1d4ad329 100644
--- a/lily/music-scheme.cc
+++ b/lily/music-scheme.cc
@@ -20,6 +20,7 @@
 #include "music.hh"

 #include "duration.hh"
+#include "program-option.hh"
 #include "warn.hh"

 LY_DEFINE (ly_music_length, "ly:music-length",
@@ -158,6 +159,24 @@ LY_DEFINE (ly_music_compress, "ly:music-compress",
   return sc->self_scm ();
 }

+LY_DEFINE (ly_make_music_relative_x, "ly:make-music-relative!",
+          2, 0, 0, (SCM music, SCM pitch),
+          "Make @var{music} relative to @var{pitch},"
+          " return final pitch.")
+{
+  LY_ASSERT_TYPE (unsmob_music, music, 1);
+  LY_ASSERT_TYPE (unsmob_pitch, pitch, 2);
+
+       Pitch start = *unsmob_pitch (pitch);
+       Music *m = unsmob_music (music);
+       Pitch last = m->to_relative_octave (start);
+       if (lily_1_8_relative)
+               m->set_property ("last-pitch", last.smobbed_copy ());
+
+       return last.smobbed_copy ();
+}
+
+
 LY_DEFINE (ly_music_duration_length, "ly:music-duration-length", 1, 0, 0,
            (SCM mus),
            "Extract the duration field from @var{mus} and return the"
Index: lily/parser.yy
diff --git a/lily/parser.yy b/lily/parser.yy
index 4714f67326d9595728c8d7fe798483a2f297a19d..b0a584ff91e93fd71465659a7d181c7321093bce 100644
--- a/lily/parser.yy
+++ b/lily/parser.yy
@@ -62,6 +62,8 @@ or
       PITCH_IDENTIFIER NOTENAME_PITCH TONICNAME_PITCH
       SCM_FUNCTION SCM_IDENTIFIER SCM_TOKEN
       UNSIGNED DURATION_IDENTIFIER
+      CHORDMODE CHORDS DRUMMODE DRUMS FIGUREMODE FIGURES LYRICMODE LYRICS
+      NOTEMODE

  /* The above are the symbols that can start function arguments */

@@ -107,10 +109,8 @@ using namespace std;
 #include "main.hh"
 #include "misc.hh"
 #include "music.hh"
-#include "music.hh"
 #include "output-def.hh"
 #include "paper-book.hh"
-#include "program-option.hh"
 #include "scm-hash.hh"
 #include "score.hh"
 #include "text-interface.hh"
@@ -156,7 +156,6 @@ SCM get_next_unique_lyrics_context_id ();


 static Music *make_music_with_input (SCM name, Input where);
-SCM make_music_relative (Pitch start, SCM music, Input loc);
SCM run_music_function (Lily_parser *parser, Input loc, SCM func, SCM args);
 SCM check_scheme_arg (Lily_parser *parser, Input loc, SCM fallback,
                      SCM arg, SCM args, SCM pred);
@@ -217,7 +216,6 @@ void set_music_properties (Music *p, SCM a);
 %token ONCE "\\once"
 %token OVERRIDE "\\override"
 %token PAPER "\\paper"
-%token RELATIVE "\\relative"
 %token REMOVE "\\remove"
 %token REPEAT "\\repeat"
 %token REST "\\rest"
@@ -370,7 +368,6 @@ If we give names, Bison complains.
 %type <scm> post_event
 %type <scm> post_event_nofinger
 %type <scm> re_rhythmed_music
-%type <scm> relative_music
 %type <scm> simple_element
 %type <scm> simple_music_property_def
 %type <scm> start_symbol
@@ -385,7 +382,6 @@ If we give names, Bison complains.

 %type <scm> music_function_call
 %type <scm> music_list
-%type <scm> absolute_pitch
 %type <scm> assignment_id
 %type <scm> bare_number
 %type <scm> unsigned_number
@@ -1329,7 +1325,6 @@ complex_music:
                 $$ = MAKE_SYNTAX ("time-scaled-music", @$, $2, $3);
        }
        | repeated_music                { $$ = $1; }
-       | relative_music        { $$ = $1; }
        | re_rhythmed_music     { $$ = $1; }
        ;

@@ -1417,18 +1412,6 @@ mode_changing_head_with_context:
        }
        ;

-
-relative_music:
-       RELATIVE absolute_pitch music {
-               Pitch start = *unsmob_pitch ($2);
-               $$ = make_music_relative (start, $3, @$);
-       }
-       | RELATIVE composite_music {
-               Pitch middle_c (0, 0, 0);
-               $$ = make_music_relative (middle_c, $2, @$);
-       }
-       ;
-
 new_lyrics:
        ADDLYRICS { PARSER->lexer_->push_lyric_state (); }
        /*cont */
@@ -2160,13 +2143,6 @@ script_dir:
        | '-'   { $$ = CENTER; }
        ;

-
-absolute_pitch:
-       pitch   {
-               $$ = $1;
-       }
-       ;
-
 duration_length:
        multiplied_duration {
                $$ = $1;
@@ -3010,19 +2986,6 @@ ly_input_procedure_p (SCM x)
                || (scm_is_pair (x) && ly_is_procedure (scm_car (x)));
 }

-SCM
-make_music_relative (Pitch start, SCM music, Input loc)
-{
-       Music *relative = MY_MAKE_MUSIC ("RelativeOctaveMusic", loc);
-       relative->set_property ("element", music);
-
-       Music *m = unsmob_music (music);
-       Pitch last = m->to_relative_octave (start);
-       if (lily_1_8_relative)
-               m->set_property ("last-pitch", last.smobbed_copy ());
-       return relative->unprotect ();
-}
-
 int
 yylex (YYSTYPE *s, YYLTYPE *loc, void *v)
 {
Index: ly/music-functions-init.ly
diff --git a/ly/music-functions-init.ly b/ly/music-functions-init.ly
index e0b36696bab23c20eefaede5e1c1e634baa87faa..7d80c6b511f4e3b7ae12be007f70d6bc803f4e4c 100644
--- a/ly/music-functions-init.ly
+++ b/ly/music-functions-init.ly
@@ -869,6 +869,14 @@ usually contains spacers or multi-measure rests.")
                'element main-music
                'quoted-music-name what))

+relative =
+#(define-music-function (parser location pitch music)
+   ((ly:pitch? (ly:make-pitch 0 0 0)) ly:music?)
+   (_i "Make @var{music} relative to @var{pitch} (default @code{c'}).")
+   (ly:make-music-relative! music pitch)
+   (make-music 'RelativeOctaveMusic
+              'element music))
+
 removeWithTag =
 #(define-music-function (parser location tag music) (symbol? ly:music?)
    (_i "Remove elements of @var{music} that are tagged with @var{tag}.")





reply via email to

[Prev in Thread] Current Thread [Next in Thread]