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