]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
fb8a04b9146c70bbb9b4314d034218a25a626c3a
[lilypond.git] / lily / parser.yy
1 %{ // -*-Fundamental-*-
2
3 /*
4   parser.yy -- Bison/C++ parser for mudela
5
6   source file of the GNU LilyPond music typesetter
7
8   (c)  1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9            Jan Nieuwenhuizen <janneke@gnu.org>
10 */
11
12 #include <iostream.h>
13 #include "lily-guile.hh"
14 #include "translation-property.hh"
15 #include "lookup.hh"
16 #include "misc.hh"
17 #include "my-lily-lexer.hh"
18 #include "paper-def.hh"
19 #include "midi-def.hh"
20 #include "main.hh"
21 #include "file-path.hh"
22 #include "debug.hh"
23 #include "dimensions.hh"
24 #include "identifier.hh"
25 #include "command-request.hh"
26 #include "musical-request.hh"
27 #include "my-lily-parser.hh"
28 #include "context-specced-music.hh"
29 #include "translator-group.hh"
30 #include "score.hh"
31 #include "music-list.hh"
32 #include "change-translator.hh"
33 #include "file-results.hh"
34 #include "scope.hh"
35 #include "relative-music.hh"
36 #include "lyric-combine-music.hh"
37 #include "transposed-music.hh"
38 #include "time-scaled-music.hh"
39 #include "repeated-music.hh"
40 #include "mudela-version.hh"
41 #include "grace-music.hh"
42 #include "auto-change-music.hh"
43 #include "output-property.hh"
44
45 bool
46 is_duration_b (int t)
47 {
48   return t && t == 1 << intlog2(t);
49 }
50
51
52 // mmm JUNKME ?
53 Mudela_version oldest_version ("1.3.59");
54
55 void
56 print_mudela_versions (ostream &os)
57 {
58   os << _f ("Oldest supported input version: %s", oldest_version.str ()) 
59     << endl;
60 }
61
62
63 // needed for bison.simple's malloc() and free()
64 #include <malloc.h>
65
66 #ifndef NDEBUG
67 #define YYDEBUG 1
68 #endif
69
70 #define YYERROR_VERBOSE 1
71
72 #define YYPARSE_PARAM my_lily_parser_l
73 #define YYLEX_PARAM my_lily_parser_l
74 #define THIS ((My_lily_parser *) my_lily_parser_l)
75
76 #define yyerror THIS->parser_error
77 #define ARRAY_SIZE(a,s)   if (a.size () != s) THIS->parser_error (_f ("Expecting %d arguments", s))
78
79 %}
80
81
82 %union {
83     Array<Musical_pitch> *pitch_arr;
84     Link_array<Request> *reqvec;
85     Duration *duration;
86     Identifier *id;
87     String * string;
88     Music *music;
89     Music_list *music_list;
90     Score *score;
91     Scope *scope;
92     Interval *interval;
93     Musical_req* musreq;
94     Music_output_def * outputdef;
95     Musical_pitch * pitch;
96     Midi_def* midi;
97     Moment *moment;
98     Real real;
99     Request * request;
100
101     /* We use SCMs to do strings, because it saves us the trouble of
102 deleting them.  Let's hope that a stack overflow doesnt trigger a move
103 of the parse stack onto the heap. */
104
105     SCM scm;
106
107     Tempo_req *tempo;
108     Translator_group* trans;
109     int i;
110 }
111 %{
112
113 int
114 yylex (YYSTYPE *s,  void * v_l)
115 {
116         My_lily_parser   *pars_l = (My_lily_parser*) v_l;
117         My_lily_lexer * lex_l = pars_l->lexer_p_;
118
119         lex_l->lexval_l = (void*) s;
120         return lex_l->yylex ();
121 }
122
123
124 %}
125
126 %pure_parser
127
128 /* tokens which are not keywords */
129 %token AUTOCHANGE
130 %token TEXTSCRIPT
131 %token ACCEPTS
132 %token ALTERNATIVE
133 %token BAR
134 %token BREATHE
135 %token CHORDMODIFIERS
136 %token CHORDS
137 %token CHAR_T
138 %token CLEF
139 %token CM_T
140 %token CONSISTS
141 %token SEQUENTIAL
142 %token SIMULTANEOUS
143 %token CONSISTSEND
144 %token DURATION
145 %token EXTENDER
146 %token FONT
147 %token GRACE
148 %token HEADER
149 %token HYPHEN
150 %token IN_T
151 %token INVALID
152 %token KEY
153 %token LYRICS
154 %token MARK
155 %token MEASURES
156 %token MIDI
157 %token MM_T
158 %token MUSICAL_PITCH
159 %token NAME
160 %token NOTENAMES
161 %token NOTES
162 %token PAPER
163 %token PARTIAL
164 %token PENALTY
165 %token PROPERTY
166 %token PT_T
167 %token RELATIVE
168 %token REMOVE
169 %token REPEAT
170 %token ADDLYRICS
171 %token SCM_T
172 %token SCORE
173 %token SCRIPT
174 %token SKIP
175 %token SPANREQUEST
176 %token COMMANDSPANREQUEST
177 %token TEMPO
178 %token OUTPUTPROPERTY
179 %token TIME_T
180 %token TIMES
181 %token TRANSLATOR
182 %token TRANSPOSE
183 %token TYPE
184 %token CONTEXT
185
186 /* escaped */
187 %token E_CHAR E_EXCLAMATION E_SMALLER E_BIGGER 
188 %token CHORD_BASS CHORD_COLON CHORD_MINUS CHORD_CARET 
189
190 %type <i>       exclamations questions
191 %token <i>      DIGIT
192 %token <pitch>  NOTENAME_PITCH
193 %token <pitch>  TONICNAME_PITCH
194 %token <pitch>  CHORDMODIFIER_PITCH
195 %token <id>     DURATION_IDENTIFIER
196 %token <id>     IDENTIFIER
197 %token <id>     MUSIC_IDENTIFIER
198 %token <id>     REQUEST_IDENTIFIER
199 %token <id>     TRANS_IDENTIFIER
200 %token <scm>    NUMBER_IDENTIFIER
201
202 %token <id>     SCORE_IDENTIFIER
203 %token <id>     MUSIC_OUTPUT_DEF_IDENTIFIER
204
205 %token <scm>    STRING_IDENTIFIER SCM_IDENTIFIER 
206 %token <scm>    DURATION RESTNAME
207 %token <scm>    STRING 
208 %token <scm>    SCM_T
209 %token <i>      UNSIGNED
210 %token <real>   REAL
211
212 %type <outputdef> output_def
213 %type <scope>   mudela_header mudela_header_body
214 %type <request> open_request_parens close_request_parens open_request close_request
215 %type <request> request_with_dir request_that_take_dir verbose_request
216 %type <i>       sub_quotes sup_quotes
217 %type <music>   simple_element  request_chord command_element Simple_music  Composite_music 
218 %type <music>   Alternative_music Repeated_music
219 %type <i>       tremolo_type
220 %type <i>       bare_int  bare_unsigned
221 %type <i>       script_dir
222
223 %type <scm>     identifier_init 
224
225 %type <duration> steno_duration optional_notemode_duration
226 %type <duration> entered_notemode_duration explicit_duration
227         
228 %type <reqvec>  pre_requests post_requests
229 %type <request> gen_text_def
230 %type <pitch>   steno_musical_pitch musical_pitch absolute_musical_pitch
231 %type <pitch>   steno_tonic_pitch
232
233 %type <pitch_arr>       chord_additions chord_subtractions chord_notes chord_step
234 %type <music>   chord
235 %type <pitch>   chord_note chord_inversion chord_bass
236 %type <duration>        duration_length
237
238 %type <scm>  embedded_scm scalar
239 %type <music>   Music Sequential_music Simultaneous_music Music_sequence
240 %type <music>   relative_music re_rhythmed_music
241 %type <music>   property_def translator_change
242 %type <music_list> Music_list
243 %type <outputdef>  music_output_def_body
244 %type <request> shorthand_command_req
245 %type <request> post_request 
246 %type <request> command_req verbose_command_req
247 %type <request> extender_req
248 %type <request> hyphen_req
249 %type <scm>     string bare_number number_expression
250 %type <score>   score_block score_body
251
252 %type <trans>   translator_spec_block translator_spec_body
253 %type <tempo>   tempo_request
254 %type <scm> notenames_body notenames_block chordmodifiers_block
255 %type <scm>     script_abbreviation
256
257
258
259 %left '-' '+'
260 %left '*' '/'
261 %left UNARY_MINUS
262
263 %%
264
265 mudela: /* empty */
266         | mudela toplevel_expression {}
267         | mudela assignment  { }
268         | mudela error
269         | mudela INVALID        {
270                 THIS->error_level_i_  =1;
271         }
272         ;
273
274 toplevel_expression:
275         notenames_block                 {
276                 THIS->lexer_p_->pitchname_tab_ =  $1;
277         }
278         | chordmodifiers_block                  {
279                 THIS->lexer_p_->chordmodifier_tab_  = $1;
280         }
281         | mudela_header {
282                 delete header_global_p;
283                 header_global_p = $1;
284         }
285         | score_block {
286                 score_global_array.push ($1);
287         }
288         | output_def {
289                 Identifier * id = new
290                         Music_output_def_identifier ($1, MUSIC_OUTPUT_DEF_IDENTIFIER);
291                 if (dynamic_cast<Paper_def*> ($1))
292                         THIS->lexer_p_->set_identifier ("$defaultpaper", smobify (id));
293                 else if (dynamic_cast<Midi_def*> ($1))
294                         THIS->lexer_p_->set_identifier ("$defaultmidi", smobify (id));
295         }
296         | embedded_scm {
297                 // junk value
298         }       
299         ;
300
301 embedded_scm:
302         SCM_T
303         | SCM_IDENTIFIER 
304         ;
305
306
307 chordmodifiers_block:
308         CHORDMODIFIERS notenames_body   {  $$ = $2; }
309         ;
310
311
312 notenames_block:
313         NOTENAMES notenames_body   {  $$ = $2; }
314         ;
315
316
317
318 notenames_body:
319         embedded_scm    {
320           int i = scm_ilength ($1);
321
322           SCM tab = scm_make_vector (gh_int2scm (i), SCM_EOL);
323           for (SCM s = $1; s != SCM_EOL; s = gh_cdr (s)) {
324                 SCM pt = gh_cdar (s);
325                 if (scm_ilength (pt) != 3)
326                         THIS->parser_error ("Need three args");
327                 scm_hashq_set_x (tab, gh_caar(s), pt);
328           }
329
330           $$ = tab;
331         }
332         ;
333
334 mudela_header_body:
335         {
336                 $$ = new Scope;
337                 THIS->lexer_p_-> scope_l_arr_.push ($$);
338         }
339         | mudela_header_body assignment semicolon { 
340
341         }
342         ;
343
344 mudela_header:
345         HEADER '{' mudela_header_body '}'       {
346                 $$ = $3;
347                 THIS->lexer_p_-> scope_l_arr_.pop ();           
348         }
349         ;
350
351
352 /*
353         DECLARATIONS
354 */
355 assignment:
356         STRING {
357                 THIS->remember_spot ();
358         }
359         /* cont */ '=' identifier_init  {
360             THIS->lexer_p_->set_identifier (ly_scm2string ($1), $4);
361
362                 Identifier * id =unsmob_identifier ($4);
363                 Input spot = THIS->pop_spot ();
364                 if (id) id->set_spot (spot);
365         }
366         ;
367
368
369
370 identifier_init:
371         score_block {
372                 $$ = smobify (new Score_identifier ($1, SCORE_IDENTIFIER));
373         }
374         | output_def {
375                 $$ = smobify (new Music_output_def_identifier ($1, MUSIC_OUTPUT_DEF_IDENTIFIER));
376         }
377         | translator_spec_block {
378                 $$ = smobify (new Translator_group_identifier ($1, TRANS_IDENTIFIER));
379         }
380         | Music  {
381                 $$ = smobify (new Music_identifier ($1, MUSIC_IDENTIFIER));
382         }
383
384         | post_request {
385                 $$ = smobify (new Request_identifier ($1, REQUEST_IDENTIFIER));
386         }
387         | explicit_duration {
388                 $$ = smobify (new Duration_identifier ($1, DURATION_IDENTIFIER));
389         }
390         | number_expression {
391                 $$ = $1;
392         }
393         | string {
394                 $$ = $1;
395         }
396         | embedded_scm  {
397                 $$ = $1;
398         }
399         ;
400
401 translator_spec_block:
402         TRANSLATOR '{' translator_spec_body '}'
403                 {
404                 $$ = $3;
405         }
406         ;
407
408 translator_spec_body:
409         TRANS_IDENTIFIER        {
410                 $$ = $1->access_content_Translator_group (true);
411                 $$-> set_spot (THIS->here_input ());
412         }
413         | TYPE STRING semicolon {
414                 Translator* t = get_translator_l (ly_scm2string ($2));
415                 Translator_group * tg = dynamic_cast<Translator_group*> (t);
416
417                 if (!tg)
418                         THIS->parser_error (_("Need a translator group for a context"));
419                 
420                 tg = dynamic_cast<Translator_group*> (t->clone ());
421                 tg->set_spot (THIS->here_input ());
422                 $$ = tg;
423         }
424         | translator_spec_body STRING '=' embedded_scm                  {
425                 Translator_group* tg = dynamic_cast<Translator_group*> ($$);
426                 tg->set_property (ly_scm2string ($2), $4);
427         }
428         | translator_spec_body STRING '=' identifier_init semicolon     { 
429                 SCM v = gh_int2scm (0);
430                 if (gh_string_p ($4) || gh_number_p ($4))
431                         v = $4;
432                 else 
433                         THIS->parser_error (_("Wrong type for property value"));
434
435                 /* ugh*/
436                 Translator_group* tg = dynamic_cast<Translator_group*> ($$);
437                 
438                 tg->set_property (ly_scm2string ($2), v);
439         }
440         | translator_spec_body NAME STRING semicolon {
441                 $$->type_str_ = ly_scm2string ($3);
442         }
443         | translator_spec_body CONSISTS STRING semicolon {
444                 dynamic_cast<Translator_group*> ($$)-> set_element (ly_scm2string ($3), true);
445         }
446         | translator_spec_body CONSISTSEND STRING semicolon {
447                 dynamic_cast<Translator_group*> ($$)-> set_element (ly_scm2string ($3), true);
448         }
449         | translator_spec_body ACCEPTS STRING semicolon {
450                 dynamic_cast<Translator_group*> ($$)-> set_acceptor (ly_scm2string ($3), true);
451         }
452         | translator_spec_body REMOVE STRING semicolon {
453                 dynamic_cast<Translator_group*> ($$)-> set_element (ly_scm2string ($3), false);
454         }
455         ;
456
457 /*
458         SCORE
459 */
460 score_block:
461         SCORE { 
462         }
463         /*cont*/ '{' score_body '}'     {
464                 $$ = $4;
465                 if (!$$->def_p_arr_.size ())
466                 {
467                   Identifier *id =
468                         unsmob_identifier (THIS->lexer_p_->lookup_identifier ("$defaultpaper"));
469                   $$->add_output (id ? id->access_content_Music_output_def (true) : new Paper_def );
470                 }
471         }
472         ;
473
474 score_body:
475         Music   {
476                 $$ = new Score;
477                 $$->set_spot (THIS->here_input ());
478                 $$->music_p_ = $1;
479         }
480         | SCORE_IDENTIFIER {
481                 $$ = $1->access_content_Score (true);
482         }
483         | score_body mudela_header      {
484                 $$->header_p_ = $2;
485         }
486         | score_body output_def {
487                 $$->add_output ($2);
488         }
489         | score_body error {
490
491         }
492         ;
493
494
495 /*
496         MIDI
497 */
498 output_def:
499         music_output_def_body '}' {
500                 $$ = $1;
501                 THIS-> lexer_p_-> scope_l_arr_.pop();
502         }
503         ;
504
505 music_output_def_body:
506         MIDI '{'    {
507          Identifier *id = unsmob_identifier (THIS->lexer_p_->lookup_identifier ("$defaultmidi"));
508
509                 
510          Midi_def* p =0;
511         if (id)
512                 p = dynamic_cast<Midi_def*> (id->access_content_Music_output_def (true));
513         else
514                 p = new Midi_def;
515
516          $$ = p;
517          THIS->lexer_p_->scope_l_arr_.push (p->scope_p_);
518         }
519         | PAPER '{'     {
520                   Identifier *id = unsmob_identifier (THIS->lexer_p_->lookup_identifier ("$defaultpaper"));
521                   Paper_def *p = 0;
522                 if (id)
523                         p = dynamic_cast<Paper_def*> (id->access_content_Music_output_def (true));
524                 else
525                         p = new Paper_def;
526                 THIS-> lexer_p_-> scope_l_arr_.push (p->scope_p_);
527                 $$ = p;
528         }
529         | PAPER '{' MUSIC_OUTPUT_DEF_IDENTIFIER         {
530                 Music_output_def *p = $3->access_content_Music_output_def (true);
531                 THIS->lexer_p_->scope_l_arr_.push (p->scope_p_);
532                 $$ = p;
533         }
534         | MIDI '{' MUSIC_OUTPUT_DEF_IDENTIFIER  {
535                 Music_output_def *p = $3->access_content_Music_output_def (true);
536                 THIS->lexer_p_->scope_l_arr_.push (p->scope_p_);
537                 $$ = p;
538         }
539         | music_output_def_body assignment semicolon {
540
541         }
542         | music_output_def_body translator_spec_block   {
543                 $$-> assign_translator ($2);
544         }
545         | music_output_def_body tempo_request semicolon {
546                 /*
547                         junk this ? there already is tempo stuff in
548                         music.
549                 */
550                 dynamic_cast<Midi_def*> ($$)->set_tempo ($2->dur_.length_mom (), $2->metronome_i_);
551                 delete $2;
552         }
553         | music_output_def_body bare_int '=' FONT STRING                { // ugh, what a syntax
554                 Lookup * l = new Lookup;
555                 l->font_name_ = ly_scm2string ($5);
556                 dynamic_cast<Paper_def*> ($$)->set_lookup ($2, l);
557         }
558         | music_output_def_body error {
559
560         }
561         ;
562
563 tempo_request:
564         TEMPO steno_duration '=' bare_unsigned  {
565                 $$ = new Tempo_req;
566                 $$->dur_ = *$2;
567                 delete $2;
568                 $$-> metronome_i_ = $4;
569         }
570         ;
571
572 Music_list: /* empty */ {
573                 $$ = new Music_list;
574                 $$->set_spot (THIS->here_input ());
575         }
576         | Music_list Music {
577                 $$->add_music ($2);
578         }
579         | Music_list error {
580         }
581         ;
582
583
584 Music:
585         Simple_music
586         | Composite_music
587         ;
588
589 Alternative_music:
590         /* empty */ {
591                 $$ = 0;
592         }
593         | ALTERNATIVE Music_sequence {
594                 $$ = $2;
595                 $2->set_spot (THIS->here_input ());
596         }
597         ;
598
599
600
601
602 Repeated_music:
603         REPEAT STRING bare_unsigned Music Alternative_music
604         {
605                 Music_sequence* m = dynamic_cast <Music_sequence*> ($5);
606                 if (m && $3 < m->length_i ())
607                         $5->warning (_ ("More alternatives than repeats.  Junking excess alternatives."));
608
609                 Repeated_music * r = new Repeated_music ($4, $3 >? 1, m);
610                 $$ = r;
611                 r->type_ = ly_scm2string ($2);
612                 r->fold_b_ = (r->type_ == "fold");
613                 r->volta_fold_b_ =  (r->type_ == "volta");
614                 r->set_spot ($4->spot  ());
615         }
616         ;
617
618 Music_sequence: '{' Music_list '}'      {
619                 $$ = new Music_sequence ($2);
620                 $$->set_spot ($2->spot ());
621         }
622         ;
623
624 Sequential_music:
625         SEQUENTIAL '{' Music_list '}'           {
626                 $$ = new Sequential_music ($3);
627                 $$->set_spot ($3->spot ());
628         }
629         | '{' Music_list '}'            {
630                 $$ = new Sequential_music ($2);
631                 $$->set_spot ($2->spot ());
632         }
633         ;
634
635 Simultaneous_music:
636         SIMULTANEOUS '{' Music_list '}'{
637                 $$ = new Simultaneous_music ($3);
638                 $$->set_spot ($3->spot ());
639         }
640         | '<' Music_list '>'    {
641                 $$ = new Simultaneous_music ($2);
642                 $$->set_spot ($2->spot ());
643         }
644         ;
645
646 Simple_music:
647         request_chord           { $$ = $1; }
648         | OUTPUTPROPERTY embedded_scm embedded_scm '=' embedded_scm     {
649                 SCM pred = $2;
650                 if (!gh_symbol_p ($3))
651                 {
652                         THIS->parser_error (_("Second argument must be a symbol")); 
653                 }
654                 /*hould check # args */
655                 if (!gh_procedure_p (pred))
656                 {
657                         THIS->parser_error (_("First argument must be a procedure taking 1 argument"));
658                 }
659         
660                 $$ = new Output_property (pred,$3, $5);
661         }
662         | MUSIC_IDENTIFIER { $$ = $1->access_content_Music (true); }
663         | property_def
664         | translator_change
665         | Simple_music '*' bare_unsigned '/' bare_unsigned      {
666                 $$ = $1;
667                 $$->compress (Moment($3, $5 ));
668         }
669         | Simple_music '*' bare_unsigned                 {
670                 $$ = $1;
671                 $$->compress (Moment ($3, 1));
672         }
673         ;
674
675
676 Composite_music:
677         CONTEXT STRING Music    {
678                 Context_specced_music *csm =  new Context_specced_music ($3);
679
680                 csm->translator_type_str_ = ly_scm2string ($2);
681                 csm->translator_id_str_ = "";
682
683
684                 $$ = csm;
685         }
686         | AUTOCHANGE STRING Music       {
687                 Auto_change_music * chm = new Auto_change_music (ly_scm2string ($2), $3);
688
689                 $$ = chm;
690                 chm->set_spot ($3->spot ());
691         }
692         | GRACE Music {
693                 $$ = new Grace_music ($2);
694         }
695         | CONTEXT STRING '=' STRING Music {
696                 Context_specced_music *csm =  new Context_specced_music ($5);
697
698                 csm->translator_type_str_ = ly_scm2string ($2);
699                 csm->translator_id_str_ = ly_scm2string ($4);
700
701                 $$ = csm;
702         }
703         | TIMES {
704                 THIS->remember_spot ();
705         }
706         /* CONTINUED */ 
707                 bare_unsigned '/' bare_unsigned Music   
708
709         {
710                 $$ = new Time_scaled_music ($3, $5, $6);
711                 $$->set_spot (THIS->pop_spot ());
712         }
713         | Repeated_music                { $$ = $1; }
714         | Simultaneous_music            { $$ = $1; }
715         | Sequential_music              { $$ = $1; }
716         | TRANSPOSE musical_pitch Music {
717                 $$ = new Transposed_music ($3, *$2);
718                 delete $2;
719         }
720         | TRANSPOSE steno_tonic_pitch Music {
721                 $$ = new Transposed_music ($3, *$2);
722                 delete $2;
723         }
724         | NOTES
725                 { THIS->lexer_p_->push_note_state (); }
726         Music
727                 { $$ = $3;
728                   THIS->lexer_p_->pop_state ();
729                 }
730         | CHORDS
731                 { THIS->lexer_p_->push_chord_state (); }
732         Music
733                 {
734                   $$ = $3;
735                   THIS->lexer_p_->pop_state ();
736         }
737         | LYRICS
738                 { THIS->lexer_p_->push_lyric_state (); }
739         Music
740                 {
741                   $$ = $3;
742                   THIS->lexer_p_->pop_state ();
743         }
744         | relative_music        { $$ = $1; }
745         | re_rhythmed_music     { $$ = $1; } 
746         ;
747
748 relative_music:
749         RELATIVE absolute_musical_pitch Music {
750                 $$ = new Relative_octave_music ($3, *$2);
751                 delete $2;
752         }
753         ;
754
755 re_rhythmed_music:
756         ADDLYRICS Music Music {
757                 Lyric_combine_music * l = new Lyric_combine_music ($2, $3);
758                 $$ = l;
759         }
760         ;
761
762 translator_change:
763         TRANSLATOR STRING '=' STRING  {
764                 Change_translator * t = new Change_translator;
765                 t-> change_to_type_str_ = ly_scm2string ($2);
766                 t-> change_to_id_str_ = ly_scm2string ($4);
767
768                 $$ = t;
769                 $$->set_spot (THIS->here_input ());
770         }
771         ;
772
773 property_def:
774         PROPERTY STRING '.' STRING '='  scalar {
775                 Translation_property *t = new Translation_property;
776
777                 t->var_str_ = ly_scm2string ($4);
778                 t->value_ = $6;
779
780                 Context_specced_music *csm = new Context_specced_music (t);
781                 $$ = csm;
782                 $$->set_spot (THIS->here_input ());
783
784                 csm-> translator_type_str_ = ly_scm2string ($2);
785         }
786         ;
787
788 scalar:
789         string          { $$ = $1; }
790         | bare_int      { $$ = gh_int2scm ($1); }
791         | embedded_scm  { $$ = $1; }
792         ;
793
794
795 request_chord:
796         pre_requests simple_element post_requests       {
797                 Music_sequence *l = dynamic_cast<Music_sequence*>($2);
798                 if (l) {
799                         for (int i=0; i < $1->size(); i++)
800                                 l->add_music ($1->elem(i));
801                         for (int i=0; i < $3->size(); i++)
802                                 l->add_music ($3->elem(i));
803                         }
804                 else
805                         programming_error ("Need Sequence to add music to");
806                 $$ = $2;
807                 
808         }
809         | command_element
810         ;
811
812 command_element:
813         command_req {
814                 $$ = new Request_chord;
815                 $$-> set_spot (THIS->here_input ());
816                 $1-> set_spot (THIS->here_input ());
817                 ((Simultaneous_music*)$$) ->add_music ($1);//ugh
818         }
819         | PARTIAL duration_length ';'   {
820                 Translation_property * p = new Translation_property;
821                 p->var_str_ = "measurePosition";
822                 p->value_ =  (new Moment (-$2->length_mom ()))->smobify_self ();
823                 delete $2;
824                 Context_specced_music * sp = new Context_specced_music (p);
825                 $$ =sp ;
826                 sp-> translator_type_str_ = "Score";
827         }
828         ;
829
830 command_req:
831         shorthand_command_req
832         | verbose_command_req semicolon { $$ = $1; }
833         ;
834
835 shorthand_command_req:
836         extender_req {
837                 $$ = $1;
838         }
839         | hyphen_req {
840                 $$ = $1;
841         }
842         | '|'                           {
843                 $$ = new Barcheck_req;
844         }
845         | '~'   {
846                 $$ = new Tie_req;
847         }
848         | '['           {
849                 Span_req*b= new Span_req;
850                 b->span_dir_ = START;
851                 b->span_type_str_ = "beam";
852                 $$ =b;
853         }
854         | ']'           {
855              Span_req*b= new Span_req;
856              b->span_dir_ = STOP;
857              b->span_type_str_ = "beam";
858              $$ = b;
859         }
860         | BREATHE {
861                 $$ = new Breathing_sign_req;
862         }
863         ;
864
865
866 verbose_command_req:
867         
868         BAR STRING                      {
869                 $$ = new Bar_req (ly_scm2string ($2));
870         }
871         | COMMANDSPANREQUEST bare_int STRING {
872                 Span_req * sp_p = new Span_req;
873                 sp_p-> span_dir_  = Direction($2);
874                 sp_p->span_type_str_ = ly_scm2string ($3);
875                 sp_p->set_spot (THIS->here_input ());
876                 $$ = sp_p;
877         }
878         | MARK  {
879                 Mark_req * m = new Mark_req;
880                 $$ = m;
881         }
882         | MARK STRING {
883                 Mark_req *m = new Mark_req;
884                 m->mark_label_ = $2;
885                 $$ = m;
886
887         }
888         | MARK bare_unsigned {
889                 Mark_req *m = new Mark_req;
890                 m->mark_label_ =  gh_int2scm ($2);
891                 $$ = m;
892         }
893
894         | TIME_T bare_unsigned '/' bare_unsigned        {
895                 Time_signature_change_req *m = new Time_signature_change_req;
896                 m->beats_i_ = $2;
897                 m->one_beat_i_=$4;
898                 $$ = m;
899         }
900         | PENALTY bare_int      {
901                 Break_req * b = new Break_req;
902                 b->penalty_f_ = $2 / 100.0;
903                 b->set_spot (THIS->here_input ());
904                 $$ = b;
905         }
906         | SKIP duration_length {
907                 Skip_req * skip_p = new Skip_req;
908                 skip_p->duration_ = *$2;
909                 delete $2;
910                 $$ = skip_p;
911         }
912         | tempo_request {
913                 $$ = $1;
914         }
915         | CLEF STRING {
916                 $$ = new Clef_change_req (ly_scm2string ($2));
917
918         }
919         | KEY {
920                 Key_change_req *key_p= new Key_change_req;
921                 $$ = key_p;
922         }
923         | KEY NOTENAME_PITCH SCM_IDENTIFIER     {
924                 Key_change_req *key_p= new Key_change_req;
925                 
926                 key_p->pitch_alist_ = $3;
927                 ((Music* )key_p)->transpose (* $2);
928                 $$ = key_p; 
929         }
930         ;
931
932 post_requests:
933         {
934                 $$ = new Link_array<Request>;
935         }
936         | post_requests post_request {
937                 $2->set_spot (THIS->here_input ());
938                 $$->push ($2);
939         }
940         ;
941
942 post_request:
943         verbose_request
944         | request_with_dir
945         | close_request
946         ;
947
948
949 request_that_take_dir:
950         gen_text_def
951         | verbose_request
952         | script_abbreviation {
953                 SCM s = THIS->lexer_p_->lookup_identifier ("dash-" + ly_scm2string ($1));
954                 Articulation_req *a = new Articulation_req;
955                 if (gh_string_p (s))
956                         a->articulation_str_ = ly_scm2string (s);
957                 else THIS->parser_error (_ ("Expecting string as script definition"));
958                 $$ = a;
959         }
960         ;
961
962 request_with_dir:
963         script_dir request_that_take_dir        {
964                 if (Script_req * gs = dynamic_cast<Script_req*> ($2))
965                         gs->dir_ = Direction ($1);
966                 else if ($1)
967                         $2->warning (_ ("Can't specify direction for this request"));
968                 $$ = $2;
969         }
970         ;
971         
972 verbose_request:
973         REQUEST_IDENTIFIER      {
974                 $$ = (Request*)$1->access_content_Request (true);
975                 $$->set_spot (THIS->here_input ());
976         }
977         | TEXTSCRIPT STRING STRING      {
978                 Text_script_req *ts_p = new Text_script_req;
979                 ts_p-> text_str_ = ly_scm2string ($2);
980                 ts_p-> style_str_ = ly_scm2string ($3);
981                 ts_p->set_spot (THIS->here_input ());
982
983                 $$ = ts_p;
984         }
985         | SPANREQUEST bare_int STRING {
986                 Span_req * sp_p = new Span_req;
987                 sp_p->span_dir_  = Direction($2);
988                 sp_p->span_type_str_ = ly_scm2string ($3);
989                 sp_p->set_spot (THIS->here_input ());
990                 $$ = sp_p;
991         }
992         | tremolo_type  {
993                 Tremolo_req* a = new Tremolo_req;
994                 a->set_spot (THIS->here_input ());
995                 a->type_i_ = $1;
996                 $$ = a;
997         }
998         | SCRIPT STRING         { 
999                 Articulation_req * a = new Articulation_req;
1000                 a->articulation_str_ = ly_scm2string ($2);
1001                 a->set_spot (THIS->here_input ());
1002                 $$ = a;
1003         }
1004         ;
1005
1006 sup_quotes:
1007         '\'' {
1008                 $$ = 1;
1009         }
1010         | sup_quotes '\'' {
1011                 $$ ++;
1012         }
1013         ;
1014
1015 sub_quotes:
1016         ',' {
1017                 $$ = 1;
1018         }
1019         | sub_quotes ',' {
1020                 $$ ++ ;
1021         }
1022         ;
1023
1024 steno_musical_pitch:
1025         NOTENAME_PITCH  {
1026                 $$ = $1;
1027         }
1028         | NOTENAME_PITCH sup_quotes     {
1029                 $$ = $1;
1030                 $$->octave_i_ +=  $2;
1031         }
1032         | NOTENAME_PITCH sub_quotes      {
1033                 $$ = $1;
1034                 $$->octave_i_ += - $2;
1035         }
1036         ;
1037
1038 steno_tonic_pitch:
1039         TONICNAME_PITCH {
1040                 $$ = $1;
1041         }
1042         | TONICNAME_PITCH sup_quotes    {
1043                 $$ = $1;
1044                 $$->octave_i_ +=  $2;
1045         }
1046         | TONICNAME_PITCH sub_quotes     {
1047                 $$ = $1;
1048                 $$->octave_i_ += - $2;
1049         }
1050         ;
1051
1052 musical_pitch:
1053         steno_musical_pitch {
1054                 $$ = $1;
1055         }
1056         | MUSICAL_PITCH embedded_scm {
1057                 int sz = scm_ilength ($2);
1058                 if (sz != 3) {
1059                         THIS->parser_error (_f ("Expecting %d arguments", 3));
1060                         $2 = gh_list (gh_int2scm (0), gh_int2scm (0), gh_int2scm (0), SCM_UNDEFINED);
1061                 }
1062                 $$ = new Musical_pitch ($2);
1063         }
1064         ;
1065
1066 explicit_duration:
1067         DURATION embedded_scm   {
1068                 $$ = new Duration;
1069                 if (scm_ilength ($2) == 2)
1070                         {
1071                         $$-> durlog_i_ = gh_scm2int (gh_car($2));
1072                         $$-> dots_i_ = gh_scm2int (gh_cadr($2));
1073                         }
1074                 else
1075                         THIS->parser_error (_("Must have 2 arguments for duration"));
1076         }
1077         ;
1078
1079 extender_req:
1080         EXTENDER {
1081                 if (!THIS->lexer_p_->lyric_state_b ())
1082                         THIS->parser_error (_ ("Have to be in Lyric mode for lyrics"));
1083                 $$ = new Extender_req;
1084         }
1085         ;
1086
1087 hyphen_req:
1088         HYPHEN {
1089                 if (!THIS->lexer_p_->lyric_state_b ())
1090                         THIS->parser_error (_ ("Have to be in Lyric mode for lyrics"));
1091                 $$ = new Hyphen_req;
1092         }
1093         ;
1094
1095 close_request:
1096         close_request_parens {
1097                 $$ = $1;
1098                 dynamic_cast<Span_req*> ($$)->span_dir_ = START;
1099         }
1100         
1101 close_request_parens:
1102         '('     {
1103                 Span_req* s= new Span_req;
1104                 $$ = s;
1105                 s->span_type_str_ = "slur";
1106         }
1107         | E_SMALLER {
1108                 Span_req*s =new Span_req;
1109                 $$ = s;
1110                 s->span_type_str_ = "crescendo";
1111         }
1112         | E_BIGGER {
1113                 Span_req*s =new Span_req;
1114                 $$ = s;
1115                 s->span_type_str_ = "decrescendo";
1116         }
1117         ;
1118
1119
1120 open_request:
1121         open_request_parens {
1122                 $$ = $1;
1123                 dynamic_cast<Span_req*> ($$)->span_dir_ = STOP;
1124         }
1125         ;
1126
1127 open_request_parens:
1128         E_EXCLAMATION   {
1129                 Span_req *s =  new Span_req;
1130                 s->span_type_str_ = "crescendo";
1131                 $$ = s;
1132         }
1133         | ')'   {
1134                 Span_req* s= new Span_req;
1135                 $$ = s;
1136                 s->span_type_str_ = "slur";
1137         }
1138         ;
1139
1140 gen_text_def:
1141         string {
1142                 Text_script_req *t  = new Text_script_req;
1143                 $$ = t;
1144                 t->text_str_ = ly_scm2string ($1);
1145
1146                 $$->set_spot (THIS->here_input ());
1147         }
1148         | DIGIT {
1149                 Text_script_req* t  = new Text_script_req;
1150                 $$ = t;
1151                 t->text_str_ = to_str ($1);
1152                 t->style_str_ = "finger";
1153                 $$->set_spot (THIS->here_input ());
1154         }
1155         ;
1156
1157 script_abbreviation:
1158         '^'             {
1159                 $$ = gh_str02scm  ("hat");
1160         }
1161         | '+'           {
1162                 $$ = gh_str02scm("plus");
1163         }
1164         | '-'           {
1165                 $$ = gh_str02scm ("dash");
1166         }
1167         | '|'           {
1168                 $$ = gh_str02scm ("bar");
1169         }
1170         | '>'           {
1171                 $$ = gh_str02scm ("larger");
1172         }
1173         | '.'           {
1174                 $$ = gh_str02scm ("dot");
1175         }
1176         ;
1177
1178
1179 script_dir:
1180         '_'     { $$ = DOWN; }
1181         | '^'   { $$ = UP; }
1182         | '-'   { $$ = CENTER; }
1183         ;
1184
1185 pre_requests:
1186         {
1187                 $$ = new Link_array<Request>;
1188         }
1189         | pre_requests open_request {
1190                 $$->push ($2);
1191         }
1192         ;
1193
1194 absolute_musical_pitch:
1195         steno_musical_pitch     {
1196                 $$ = $1;
1197         }
1198         ;
1199
1200 duration_length:
1201         steno_duration {
1202                 $$ = $1;
1203         }
1204         | duration_length '*' bare_unsigned {
1205                 $$->tuplet_iso_i_ *= $3;
1206         }
1207         | duration_length '/' bare_unsigned {
1208                 $$->tuplet_type_i_ *= $3;
1209         }
1210         ;
1211
1212 entered_notemode_duration:
1213         steno_duration  {
1214                 THIS->set_last_duration ($1);
1215         }
1216         ;
1217
1218 optional_notemode_duration:
1219         {
1220                 $$ = new Duration (THIS->default_duration_);
1221         }
1222         | entered_notemode_duration {
1223                 $$ = $1;
1224         }
1225         ;
1226
1227 steno_duration:
1228         bare_unsigned           {
1229                 $$ = new Duration;
1230                 if (!is_duration_b ($1))
1231                         THIS->parser_error (_f ("not a duration: %d", $1));
1232                 else {
1233                         $$->durlog_i_ = intlog2 ($1);
1234                      }
1235         }
1236         | DURATION_IDENTIFIER   {
1237                 $$ = $1->access_content_Duration (true);
1238         }
1239         | steno_duration '.'    {
1240                 $$->dots_i_ ++;
1241         }
1242         ;
1243
1244
1245 tremolo_type: 
1246         ':'     {
1247                 $$ =0;
1248         }
1249         | ':' bare_unsigned {
1250                 if (!is_duration_b ($2))
1251                         THIS->parser_error (_f ("not a duration: %d", $2));
1252                 $$ = $2;
1253         }
1254         ;
1255
1256
1257 simple_element:
1258         musical_pitch exclamations questions optional_notemode_duration {
1259                 if (!THIS->lexer_p_->note_state_b ())
1260                         THIS->parser_error (_ ("Have to be in Note mode for notes"));
1261
1262
1263                 Note_req *n = new Note_req;
1264                 
1265                 n->pitch_ = *$1;
1266                 n->duration_ = *$4;
1267
1268                 n->cautionary_b_ = $3 % 2;
1269                 n->forceacc_b_ = $2 % 2 || n->cautionary_b_;
1270
1271                 Simultaneous_music*v = new Request_chord;
1272                 v->set_spot ($1->spot ());
1273                 n->set_spot ($1->spot ());
1274
1275                 v->add_music (n);
1276
1277                 $$ = v;
1278
1279                 delete $1;
1280                 delete $4;
1281         }
1282         | RESTNAME optional_notemode_duration           {
1283                   Simultaneous_music* velt_p = new Request_chord;
1284                   velt_p->set_spot (THIS->here_input());
1285
1286                   if (ly_scm2string ($1) =="s")
1287                     { /* Space */
1288                       Skip_req * skip_p = new Skip_req;
1289                       skip_p->duration_ = *$2;
1290
1291                       skip_p->set_spot (THIS->here_input());
1292                       velt_p->add_music (skip_p);
1293                     }
1294                   else
1295                     {
1296                       Rest_req * rest_req_p = new Rest_req;
1297                       rest_req_p->duration_ = *$2;
1298                       rest_req_p->set_spot (THIS->here_input());
1299
1300                       velt_p->add_music (rest_req_p);
1301                     }
1302
1303                   delete $2;
1304                   $$ = velt_p;
1305         }
1306         | MEASURES optional_notemode_duration   {
1307                 Skip_req * sk = new Skip_req;
1308                 sk->duration_ = *$2;
1309                 Music_list * ms = new Music_list;
1310                 Request_chord * rqc1 = new Request_chord;
1311                 Request_chord * rqc2 = new Request_chord;
1312                 Request_chord * rqc3 = new Request_chord;
1313
1314                 Span_req *sp1 = new Span_req;
1315                 Span_req *sp2 = new Span_req;
1316                 sp1-> span_dir_ = START;
1317                 sp2-> span_dir_ = STOP;
1318                 sp1->span_type_str_ = sp2->span_type_str_ = "rest";
1319                 rqc1->add_music (sp1);
1320                 rqc2->add_music (sk);
1321                 rqc3->add_music (sp2);
1322                 
1323                 ms->add_music (rqc1);
1324                 ms->add_music (rqc2);
1325                 ms->add_music (rqc3);
1326
1327                 $$ = new Sequential_music (ms);
1328         }
1329         | STRING optional_notemode_duration     {
1330                 if (!THIS->lexer_p_->lyric_state_b ())
1331                         THIS->parser_error (_ ("Have to be in Lyric mode for lyrics"));
1332                 Simultaneous_music* velt_p = new Request_chord;
1333
1334                 Lyric_req* lreq_p = new Lyric_req;
1335                 lreq_p ->text_str_ = ly_scm2string ($1);
1336                 lreq_p->duration_ = *$2;
1337                 lreq_p->set_spot (THIS->here_input());
1338
1339                 velt_p->add_music (lreq_p);
1340
1341                 delete  $2;
1342                 $$= velt_p;
1343
1344         }
1345         | chord {
1346                 if (!THIS->lexer_p_->chord_state_b ())
1347                         THIS->parser_error (_ ("Have to be in Chord mode for chords"));
1348                 $$ = $1;
1349         }
1350         ;
1351
1352
1353 chord:
1354         steno_tonic_pitch optional_notemode_duration chord_additions chord_subtractions chord_inversion chord_bass {
1355                 $$ = THIS->get_chord (*$1, $3, $4, $5, $6, *$2);
1356         };
1357
1358 chord_additions: 
1359         {
1360                 $$ = new Array<Musical_pitch>;
1361         } 
1362         | CHORD_COLON chord_notes {
1363                 $$ = $2;
1364         }
1365         ;
1366
1367 chord_notes:
1368         chord_step {
1369                 $$ = $1
1370         }
1371         | chord_notes '.' chord_step {
1372                 $$ = $1;
1373                 $$->concat (*$3);
1374         }
1375         ;
1376
1377 chord_subtractions: 
1378         {
1379                 $$ = new Array<Musical_pitch>;
1380         } 
1381         | CHORD_CARET chord_notes {
1382                 $$ = $2;
1383         }
1384         ;
1385
1386
1387 chord_inversion:
1388         {
1389                 $$ = 0;
1390         }
1391         | '/' steno_tonic_pitch {
1392                 $$ = $2;
1393                 $$->set_spot (THIS->here_input ());
1394         }
1395         ;
1396
1397 chord_bass:
1398         {
1399                 $$ = 0;
1400         }
1401         | CHORD_BASS steno_tonic_pitch {
1402                 $$ = $2;
1403                 $$->set_spot (THIS->here_input ());
1404         }
1405         ;
1406
1407 chord_step:
1408         chord_note {
1409                 $$ = new Array<Musical_pitch>;
1410                 $$->push (*$1);
1411         }
1412         | CHORDMODIFIER_PITCH {
1413                 $$ = new Array<Musical_pitch>;
1414                 $$->push (*$1);
1415         }
1416         | CHORDMODIFIER_PITCH chord_note { /* Ugh. */
1417                 $$ = new Array<Musical_pitch>;
1418                 $$->push (*$1);
1419                 $$->push (*$2);
1420         }
1421         ;
1422
1423 chord_note:
1424         bare_unsigned {
1425                 $$ = new Musical_pitch;
1426                 $$->notename_i_ = ($1 - 1) % 7;
1427                 $$->octave_i_ = $1 > 7 ? 1 : 0;
1428                 $$->accidental_i_ = 0;
1429         } 
1430         | bare_unsigned '+' {
1431                 $$ = new Musical_pitch;
1432                 $$->notename_i_ = ($1 - 1) % 7;
1433                 $$->octave_i_ = $1 > 7 ? 1 : 0;
1434                 $$->accidental_i_ = 1;
1435         }
1436         | bare_unsigned CHORD_MINUS {
1437                 $$ = new Musical_pitch;
1438                 $$->notename_i_ = ($1 - 1) % 7;
1439                 $$->octave_i_ = $1 > 7 ? 1 : 0;
1440                 $$->accidental_i_ = -1;
1441         }
1442         ;
1443
1444 /*
1445         UTILITIES
1446  */
1447 number_expression:
1448         bare_number {
1449                 $$ = $1;
1450         }
1451         | '-'  number_expression %prec UNARY_MINUS {
1452                 $$ = scm_difference ($2, SCM_UNDEFINED);
1453         }
1454         | number_expression '*' number_expression {
1455                 $$ = scm_product ($1, $3);
1456         }
1457         | number_expression '/' number_expression {
1458                 $$ = scm_divide ($1, $3);
1459         }
1460         | number_expression '+' number_expression {
1461                 $$ = scm_sum ($1, $3);
1462         }
1463         | number_expression '-' number_expression {
1464                 $$ = scm_difference ($1, $3);
1465         }
1466         | '(' number_expression ')'     {
1467                 $$ = $2;
1468         }
1469         ;
1470
1471 bare_number:
1472         UNSIGNED        {
1473                 $$ = gh_int2scm ($1);
1474         }
1475         | DIGIT         {
1476                 $$ = gh_int2scm ($1);
1477         }
1478         | REAL          {
1479                 $$ = gh_double2scm ($1);
1480         }
1481         | NUMBER_IDENTIFIER             {
1482                 $$ = $1;
1483         }
1484         | REAL CM_T     {
1485                 $$ = gh_double2scm ($1 CM);
1486         }
1487         | REAL PT_T     {
1488                 $$ = gh_double2scm ($1 PT);
1489         }
1490         | REAL IN_T     {
1491                 $$ = gh_double2scm ($1 INCH);
1492         }
1493         | REAL MM_T     {
1494                 $$ = gh_double2scm ($1 MM);
1495         }
1496         | REAL CHAR_T   {
1497                 $$ = gh_double2scm ($1 CHAR);
1498         }
1499         ;
1500
1501
1502 bare_unsigned:
1503         bare_number {
1504                 if (scm_integer_p ($1) == SCM_BOOL_T) {
1505                         $$ = gh_scm2int ($1);
1506
1507                 } else {
1508                         THIS->parser_error (_("need integer number arg"));
1509                         $$ = 0;
1510                 }
1511                 if ($$ < 0) {
1512                         THIS->parser_error (_("Must be positive integer"));
1513                         $$ = -$$;
1514                         }
1515
1516         }
1517         ;
1518 bare_int:
1519         bare_number {
1520                 if (scm_integer_p ($1) == SCM_BOOL_T)
1521                 {
1522                         int k = gh_scm2int ($1);
1523                         $$ = k;
1524                 } else
1525                 {
1526                         THIS->parser_error (_("need integer number arg"));
1527                         $$ = 0;
1528                 }
1529         }
1530         | '-' bare_int {
1531                 $$ = -$2;
1532         }
1533         ;
1534
1535
1536 string:
1537         STRING          {
1538                 $$ = $1;
1539         }
1540         | STRING_IDENTIFIER     {
1541                 $$ = $1;
1542         }
1543         | string '+' string {
1544                 $$ = scm_string_append (scm_listify ($1, $3, SCM_UNDEFINED));
1545         }
1546         ;
1547
1548
1549 exclamations:
1550                 { $$ = 0; }
1551         | exclamations '!'      { $$ ++; }
1552         ;
1553
1554 questions:
1555                 { $$ = 0; }
1556         | questions '?' { $$ ++; }
1557         ;
1558
1559
1560 semicolon:
1561         ';'
1562         ;
1563
1564 %%
1565
1566 void
1567 My_lily_parser::set_yydebug (bool b)
1568 {
1569 #ifdef YYDEBUG
1570         yydebug = b;
1571 #endif
1572 }
1573 void
1574 My_lily_parser::do_yyparse ()
1575 {
1576         yyparse ((void*)this);
1577 }
1578
1579