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