]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
release: 1.1.62
[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
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                 /* urg */
751                 $$ = new Time_scaled_music ($3, $5, $1);
752         }
753         | Simple_music '*' unsigned              {
754                 $$ = new Time_scaled_music ($3, 1, $1);
755         }
756         ;
757
758
759 Composite_music:
760         CONTEXT STRING Music    {
761                 Context_specced_music *csm =  new Context_specced_music ($3);
762
763                 csm->translator_type_str_ = *$2;
764                 csm->translator_id_str_ = "";
765                 delete $2;
766
767                 $$ = csm;
768         }
769         | GRACE Music {
770                 $$ = new Grace_music ($2);
771         }
772         | CONTEXT STRING '=' STRING Music {
773                 Context_specced_music *csm =  new Context_specced_music ($5);
774
775                 csm->translator_type_str_ = *$2;
776                 csm->translator_id_str_ = *$4;
777                 delete $2;
778                 delete $4;
779
780                 $$ = csm;
781         }
782         | TIMES {
783                 THIS->remember_spot ();
784         }
785         /* CONTINUED */ 
786                 unsigned '/' unsigned Music     
787
788         {
789                 $$ = new Time_scaled_music ($3, $5, $6);
790                 $$->set_spot (THIS->pop_spot ());
791         }
792         | Repeated_music                { $$ = $1; }
793         | Simultaneous_music            { $$ = $1; }
794         | Sequential_music              { $$ = $1; }
795         | TRANSPOSE musical_pitch Music {
796                 $$ = new Transposed_music ($3, *$2);
797                 delete $2;
798         }
799         | TRANSPOSE steno_tonic_pitch Music {
800                 $$ = new Transposed_music ($3, *$2);
801                 delete $2;
802         }
803         | NOTES
804                 { THIS->lexer_p_->push_note_state (); }
805         Music
806                 { $$ = $3;
807                   THIS->lexer_p_->pop_state ();
808                 }
809         | CHORDS
810                 { THIS->lexer_p_->push_chord_state (); }
811         Music
812                 {
813                   $$ = $3;
814                   THIS->lexer_p_->pop_state ();
815         }
816         | LYRICS
817                 { THIS->lexer_p_->push_lyric_state (); }
818         Music
819                 {
820                   $$ = $3;
821                   THIS->lexer_p_->pop_state ();
822         }
823         | relative_music        { $$ = $1; }
824         | re_rhythmed_music     { $$ = $1; } 
825         ;
826
827 relative_music:
828         RELATIVE absolute_musical_pitch Music {
829                 $$ = new Relative_octave_music ($3, *$2);
830                 delete $2;
831         }
832         ;
833
834 re_rhythmed_music:
835         ADDLYRICS Music Music {
836                 Lyric_combine_music * l = new Lyric_combine_music ($2, $3);
837                 $$ = l;
838         }
839         ;
840
841 translator_change:
842         TRANSLATOR STRING '=' STRING  {
843                 Change_translator * t = new Change_translator;
844                 t-> change_to_type_str_ = *$2;
845                 t-> change_to_id_str_ = *$4;
846
847                 $$ = t;
848                 $$->set_spot (THIS->here_input ());
849                 delete $2;
850                 delete $4;
851         }
852         ;
853
854 property_def:
855         PROPERTY STRING '.' STRING '=' scalar {
856                 Translation_property *t = new Translation_property;
857
858                 t-> var_str_ = *$4;
859                 t-> value_ = *$6;
860
861                 Context_specced_music *csm = new Context_specced_music (t);
862                 $$ = csm;
863                 $$->set_spot (THIS->here_input ());
864
865                 csm-> translator_type_str_ = *$2;
866
867                 delete $2;
868                 delete $4;
869                 delete $6;
870         }
871         ;
872
873 scalar:
874         string          { $$ = new Scalar (*$1); delete $1; }
875         | int           { $$ = new Scalar ($1); }
876         ;
877
878
879 request_chord:
880         pre_requests simple_element post_requests       {
881                 Music_sequence *l = dynamic_cast<Music_sequence*>($2);
882                 for (int i=0; i < $1->size(); i++)
883                         l->add_music ($1->elem(i));
884                 for (int i=0; i < $3->size(); i++)
885                         l->add_music ($3->elem(i));
886                 $$ = $2;
887                 
888         }
889         | command_element
890         ;
891
892 command_element:
893         command_req {
894                 $$ = new Request_chord;
895                 $$-> set_spot (THIS->here_input ());
896                 $1-> set_spot (THIS->here_input ());
897                 ((Simultaneous_music*)$$) ->add_music ($1);//ugh
898         }
899         ;
900
901 command_req:
902         abbrev_command_req
903         | verbose_command_req semicolon { $$ = $1; }
904         ;
905
906 abbrev_command_req:
907         extender_req {
908                 $$ = $1;
909         }
910         | hyphen_req {
911                 $$ = $1;
912         }
913         | '|'                           {
914                 $$ = new Barcheck_req;
915         }
916         | '~'   {
917                 $$ = new Tie_req;
918         }
919         | '['           {
920                 Span_req*b= new Span_req;
921                 b->span_dir_ = START;
922                 b->span_type_str_ = "beam";
923                 $$ =b;
924         }
925         | ']'           {
926                 Span_req*b= new Span_req;
927                 b->span_dir_ = STOP;
928                 b->span_type_str_ = "beam";
929                 $$ = b;
930         }
931         | BREATHE {
932                 $$ = new Breathing_sign_req;
933         }
934         ;
935
936
937 verbose_command_req:
938         BAR STRING                      {
939                 $$ = new Bar_req (*$2);
940                 delete $2;
941         }
942         | MARK STRING {
943                 $$ = new Mark_req (*$2);
944                 delete $2;
945         }
946         | MARK unsigned {
947                 $$ = new Mark_req (to_str ($2));
948         }
949         | TIME_T unsigned '/' unsigned  {
950                 Time_signature_change_req *m = new Time_signature_change_req;
951                 m->beats_i_ = $2;
952                 m->one_beat_i_=$4;
953                 $$ = m;
954         }
955         | PENALTY int   {
956                 Break_req * b = new Break_req;
957                 b->penalty_i_ = $2;
958                 b-> set_spot (THIS->here_input ());
959                 $$ = b;
960         }
961         | SKIP duration_length {
962                 Skip_req * skip_p = new Skip_req;
963                 skip_p->duration_ = *$2;
964                 delete $2;
965                 $$ = skip_p;
966         }
967         | tempo_request {
968                 $$ = $1;
969         }
970         | CADENZA unsigned      {
971                 $$ = new Cadenza_req ($2);
972         }
973         | PARTIAL duration_length       {
974                 $$ = new Partial_measure_req ($2->length_mom ());
975                 delete $2;
976         }
977         | CLEF STRING {
978                 $$ = new Clef_change_req (*$2);
979                 delete $2;
980         }
981         | KEY NOTENAME_PITCH optional_modality  {
982                 Key_change_req *key_p= new Key_change_req;
983                 key_p->key_.pitch_arr_.push (*$2);
984                 key_p->key_.ordinary_key_b_ = true;
985                 key_p->key_.modality_i_ = $3;
986                 $$ = key_p;
987                 delete $2;
988         }
989         | KEYSIGNATURE pitch_list {
990                 Key_change_req *key_p= new Key_change_req;
991                 key_p->key_.pitch_arr_ = *$2;
992                 key_p->key_.ordinary_key_b_ = false;
993                 $$ = key_p;
994                 delete $2;
995         }
996
997         ;
998
999 post_requests:
1000         {
1001                 $$ = new Link_array<Request>;
1002         }
1003         | post_requests post_request {
1004                 $2->set_spot (THIS->here_input ());
1005                 $$->push ($2);
1006         }
1007         ;
1008
1009 post_request:
1010         verbose_request
1011         | request_with_dir
1012         | close_request
1013         ;
1014
1015
1016 request_that_take_dir:
1017         gen_text_def
1018         | verbose_request
1019         | script_abbreviation {
1020                 Identifier*i = THIS->lexer_p_->lookup_identifier ("dash-" + *$1);
1021                 Articulation_req *a = new Articulation_req;
1022                 a->articulation_str_ = *i->access_content_String (false);
1023                 delete $1;
1024                 $$ = a;
1025         }
1026         ;
1027
1028 request_with_dir:
1029         script_dir request_that_take_dir        {
1030                 if (Script_req * gs = dynamic_cast<Script_req*> ($2))
1031                         gs->dir_ = Direction ($1);
1032                 else if ($1)
1033                         $2->warning ("Can't specify direction for this request");
1034                 $$ = $2;
1035         }
1036         ;
1037         
1038 verbose_request:
1039         REQUEST_IDENTIFIER      {
1040                 $$ = (Request*)$1->access_content_Request (true);
1041                 $$->set_spot (THIS->here_input ());
1042         }
1043         | TEXTSCRIPT STRING STRING      {
1044                 Text_script_req *ts_p = new Text_script_req;
1045                 ts_p-> text_str_ = *$2;
1046                 ts_p-> style_str_ = *$3;
1047                 ts_p->set_spot (THIS->here_input ());
1048                 delete $3;
1049                 delete $2;
1050                 $$ = ts_p;
1051         }
1052         | SPANREQUEST int STRING {
1053                 Span_req * sp_p = new Span_req;
1054                 sp_p-> span_dir_  = Direction($2);
1055                 sp_p->span_type_str_ = *$3;
1056                 sp_p->set_spot (THIS->here_input ());
1057                 $$ = sp_p;
1058         }
1059         | abbrev_type   {
1060                 Tremolo_req* a = new Tremolo_req;
1061                 a->set_spot (THIS->here_input ());
1062                 a->type_i_ = $1;
1063                 $$ = a;
1064         }
1065         | SCRIPT STRING         { 
1066                 Articulation_req * a = new Articulation_req;
1067                 a->articulation_str_ = *$2;
1068                 a->set_spot (THIS->here_input ());
1069                 $$ = a;
1070                 delete $2;
1071         }
1072         ;
1073
1074 optional_modality:
1075         /* empty */     {
1076                 $$ = 0;
1077         }
1078         | int   {
1079                 $$ = $1;
1080         }
1081         ;
1082
1083 sup_quotes:
1084         '\'' {
1085                 $$ = 1;
1086         }
1087         | sup_quotes '\'' {
1088                 $$ ++;
1089         }
1090         ;
1091
1092 sub_quotes:
1093         ',' {
1094                 $$ = 1;
1095         }
1096         | sub_quotes ',' {
1097                 $$ ++ ;
1098         }
1099         ;
1100
1101 steno_musical_pitch:
1102         NOTENAME_PITCH  {
1103                 $$ = $1;
1104         }
1105         | NOTENAME_PITCH sup_quotes     {
1106                 $$ = $1;
1107                 $$->octave_i_ +=  $2;
1108         }
1109         | NOTENAME_PITCH sub_quotes      {
1110                 $$ = $1;
1111                 $$->octave_i_ += - $2;
1112         }
1113         ;
1114
1115 steno_tonic_pitch:
1116         TONICNAME_PITCH {
1117                 $$ = $1;
1118         }
1119         | TONICNAME_PITCH sup_quotes    {
1120                 $$ = $1;
1121                 $$->octave_i_ +=  $2;
1122         }
1123         | TONICNAME_PITCH sub_quotes     {
1124                 $$ = $1;
1125                 $$->octave_i_ += - $2;
1126         }
1127         ;
1128
1129 explicit_musical_pitch:
1130         MUSICAL_PITCH '{' int_list '}'  {/* ugh */
1131                 Array<int> &a = *$3;
1132                 ARRAY_SIZE(a,3);
1133                 $$ = new Musical_pitch;
1134                 $$->octave_i_ = a[0];
1135                 $$->notename_i_ = a[1];
1136                 $$->accidental_i_ = a[2];
1137                 delete &a;
1138         }
1139         ;
1140
1141 musical_pitch:
1142         steno_musical_pitch {
1143                 $$ = $1;
1144                 THIS->set_last_pitch ($1);
1145         }
1146         | explicit_musical_pitch {
1147                 $$ = $1;
1148                 THIS->set_last_pitch ($1);
1149         }
1150         ;
1151
1152 explicit_duration:
1153         DURATION '{' int_list '}'       {
1154                 $$ = new Duration;
1155                 Array<int> &a = *$3;
1156                 ARRAY_SIZE(a,2);
1157                         
1158                 $$-> durlog_i_ = a[0];
1159                 $$-> dots_i_ = a[1];
1160
1161                 delete &a;              
1162         }
1163         ;
1164
1165 extender_req:
1166         EXTENDER {
1167                 if (!THIS->lexer_p_->lyric_state_b ())
1168                         THIS->parser_error (_ ("have to be in Lyric mode for lyrics"));
1169                 $$ = new Extender_req;
1170         }
1171         ;
1172
1173 hyphen_req:
1174         HYPHEN {
1175                 if (!THIS->lexer_p_->lyric_state_b ())
1176                         THIS->parser_error (_ ("have to be in Lyric mode for lyrics"));
1177                 $$ = new Hyphen_req;
1178         }
1179         ;
1180
1181 close_request:
1182         close_request_parens {
1183                 $$ = $1;
1184                 dynamic_cast<Span_req*> ($$)->span_dir_ = START;
1185         }
1186         
1187 close_request_parens:
1188         '('     {
1189                 Span_req* s= new Span_req;
1190                 $$ = s;
1191                 s->span_type_str_ = "slur";
1192         }
1193         | E_SMALLER {
1194                 Span_req*s =new Span_req;
1195                 $$ = s;
1196                 s->span_type_str_ = "crescendo";
1197         }
1198         | E_BIGGER {
1199                 Span_req*s =new Span_req;
1200                 $$ = s;
1201                 s->span_type_str_ = "decrescendo";
1202         }
1203         ;
1204
1205
1206 open_request:
1207         open_request_parens {
1208                 $$ = $1;
1209                 dynamic_cast<Span_req*> ($$)->span_dir_ = STOP;
1210         }
1211         ;
1212
1213 open_request_parens:
1214         E_EXCLAMATION   {
1215                 Span_req *s =  new Span_req;
1216                 s->span_type_str_ = "crescendo";
1217                 $$ = s;
1218         }
1219         | ')'   {
1220                 Span_req* s= new Span_req;
1221                 $$ = s;
1222                 s->span_type_str_ = "slur";
1223         }
1224         ;
1225
1226 gen_text_def:
1227         string {
1228                 Text_script_req *t  = new Text_script_req;
1229                 $$ = t;
1230                 t->text_str_ = *$1;
1231                 delete $1;
1232                 $$->set_spot (THIS->here_input ());
1233         }
1234         | DIGIT {
1235                 Text_script_req* t  = new Text_script_req;
1236                 $$ = t;
1237                 t->text_str_ = to_str ($1);
1238                 t->style_str_ = "finger";
1239                 $$->set_spot (THIS->here_input ());
1240         }
1241         ;
1242
1243 script_abbreviation:
1244         '^'             {
1245                 $$ = new String ("hat");
1246         }
1247         | '+'           {
1248                 $$ = new String ("plus");
1249         }
1250         | '-'           {
1251                 $$ = new String ("dash");
1252         }
1253         | '|'           {
1254                 $$ = new String ("bar");
1255         }
1256         | '>'           {
1257                 $$ = new String ("larger");
1258         }
1259         | '.'           {
1260                 $$ = new String ("dot");
1261         }
1262         ;
1263
1264
1265 script_dir:
1266         '_'     { $$ = DOWN; }
1267         | '^'   { $$ = UP; }
1268         | '-'   { $$ = CENTER; }
1269         ;
1270
1271 pre_requests:
1272         {
1273                 $$ = new Link_array<Request>;
1274         }
1275         | pre_requests open_request {
1276                 $$->push ($2);
1277         }
1278         ;
1279
1280 absolute_musical_pitch:
1281         steno_musical_pitch     {
1282                 $$ = $1;
1283         }
1284         ;
1285
1286 duration_length:
1287         steno_duration {
1288                 $$ = $1;
1289         }
1290         | duration_length '*' unsigned {
1291                 $$->plet_.iso_i_ *= $3;
1292         }
1293         | duration_length '/' unsigned {
1294                 $$->plet_.type_i_ *= $3;
1295         }
1296         ;
1297
1298 entered_notemode_duration:
1299         steno_duration  {
1300                 THIS->set_last_duration ($1);
1301         }
1302         ;
1303
1304 optional_notemode_duration:
1305         {
1306                 $$ = new Duration (THIS->default_duration_);
1307         }
1308         | entered_notemode_duration {
1309                 $$ = $1;
1310         }
1311         ;
1312
1313 steno_duration:
1314         unsigned                {
1315                 $$ = new Duration;
1316                 if (!Duration::duration_type_b ($1))
1317                         THIS->parser_error (_f ("not a duration: %d", $1));
1318                 else {
1319                         $$->durlog_i_ = Duration_convert::i2_type ($1);
1320                      }
1321         }
1322         | DURATION_IDENTIFIER   {
1323                 $$ = $1->access_content_Duration (true);
1324         }
1325         | steno_duration '.'    {
1326                 $$->dots_i_ ++;
1327         }
1328         ;
1329
1330
1331 abbrev_type: 
1332         ':'     {
1333                 $$ =0;
1334         }
1335         | ':' unsigned {
1336                 if (!Duration::duration_type_b ($2))
1337                         THIS->parser_error (_f ("not a duration: %d", $2));
1338                 else if ($2 < 8)
1339                         THIS->parser_error (_ ("can't abbreviate"));
1340                 $$ = $2;
1341         }
1342         ;
1343
1344
1345 simple_element:
1346         musical_pitch exclamations questions optional_notemode_duration {
1347                 if (!THIS->lexer_p_->note_state_b ())
1348                         THIS->parser_error (_ ("have to be in Note mode for notes"));
1349
1350
1351                 Note_req *n = new Note_req;
1352                 
1353                 n->pitch_ = *$1;
1354                 delete $1;
1355                 n->duration_ = *$4;
1356                 delete $4;
1357                 n->cautionary_b_ = $3 % 2;
1358                 n->forceacc_b_ = $2 % 2 || n->cautionary_b_;
1359
1360                 Simultaneous_music*v = new Request_chord;
1361                 v->set_spot (THIS->here_input ());
1362                 n->set_spot (THIS->here_input ());
1363
1364                 v->add_music (n);
1365
1366                 $$ = v;
1367         }
1368         | RESTNAME optional_notemode_duration           {
1369                 $$ = THIS->get_rest_element (*$1, $2);
1370                 delete $1;  // delete notename
1371         }
1372         | MEASURES optional_notemode_duration   {
1373                 Multi_measure_rest_req* m = new Multi_measure_rest_req;
1374                 m->duration_ = *$2;
1375                 delete $2;
1376
1377                 Simultaneous_music*velt_p = new Request_chord;
1378                 velt_p->set_spot (THIS->here_input ());
1379                 velt_p->add_music (m);
1380                 $$ = velt_p;
1381         }
1382         | REPETITIONS optional_notemode_duration        {
1383                 Repetitions_req* r = new Repetitions_req;
1384                 r->duration_ = *$2;
1385                 delete $2;
1386
1387                 Simultaneous_music*velt_p = new Request_chord;
1388                 velt_p->set_spot (THIS->here_input ());
1389                 velt_p->add_music (r);
1390                 $$ = velt_p;
1391         }
1392         | STRING optional_notemode_duration     {
1393                 if (!THIS->lexer_p_->lyric_state_b ())
1394                         THIS->parser_error (_ ("have to be in Lyric mode for lyrics"));
1395                 $$ = THIS->get_word_element (*$1, $2);
1396                 delete $1;
1397         }
1398         | chord {
1399                 if (!THIS->lexer_p_->chord_state_b ())
1400                         THIS->parser_error (_ ("have to be in Chord mode for chords"));
1401                 $$ = $1;
1402         }
1403         ;
1404
1405 chord:
1406         steno_tonic_pitch optional_notemode_duration chord_additions chord_subtractions chord_inversion {
1407                 $$ = THIS->get_chord (*$1, $3, $4, $5, *$2);
1408         };
1409
1410 chord_additions: 
1411         {
1412                 $$ = new Array<Musical_pitch>;
1413         } 
1414         | '-' chord_notes {
1415                 $$ = $2;
1416         }
1417         | '-' CHORDMODIFIER_PITCH {
1418                 $$ = new Array<Musical_pitch>;
1419                 $$->push (*$2);
1420         }
1421         | '-' CHORDMODIFIER_PITCH chord_notes {
1422                 $$ = $3;
1423                 $$->push (*$2);
1424         }
1425         ;
1426
1427 chord_notes:
1428         chord_note {
1429                 $$ = new Array<Musical_pitch>;
1430                 $$->push (*$1);
1431         }
1432         | chord_notes '.' chord_note {
1433                 $$ = $1;
1434                 $$->push (*$3);
1435         }
1436         ;
1437
1438 chord_subtractions: 
1439         {
1440                 $$ = new Array<Musical_pitch>;
1441         } 
1442         | '^' chord_notes {
1443                 $$ = $2;
1444         }
1445         ;
1446
1447
1448 /*
1449         forevery : X : optional_X sucks. Devise  a solution.
1450 */
1451
1452 chord_inversion:
1453         {
1454                 $$ = 0;
1455         }
1456         | '/' steno_tonic_pitch {
1457                 $$ = $2
1458         }
1459         ;
1460
1461 chord_note:
1462         unsigned {
1463                 $$ = new Musical_pitch;
1464                 $$->notename_i_ = ($1 - 1) % 7;
1465                 $$->octave_i_ = $1 > 7 ? 1 : 0;
1466                 $$->accidental_i_ = 0;
1467         } 
1468         | unsigned '+' {
1469                 $$ = new Musical_pitch;
1470                 $$->notename_i_ = ($1 - 1) % 7;
1471                 $$->octave_i_ = $1 > 7 ? 1 : 0;
1472                 $$->accidental_i_ = 1;
1473         }
1474         | unsigned '-' {
1475                 $$ = new Musical_pitch;
1476                 $$->notename_i_ = ($1 - 1) % 7;
1477                 $$->octave_i_ = $1 > 7 ? 1 : 0;
1478                 $$->accidental_i_ = -1;
1479         }
1480         ;
1481
1482 /*
1483         UTILITIES
1484  */
1485 pitch_list:                     {
1486                 $$ = new Array<Musical_pitch>;
1487         }
1488         | pitch_list musical_pitch      {
1489                 $$->push (*$2);
1490                 delete $2;
1491         }
1492         ;
1493
1494
1495 int_list:
1496         /**/                    {
1497                 $$ = new Array<int>
1498         }
1499         | int_list int          {
1500                 $$->push ($2);          
1501         }
1502         ;
1503
1504 unsigned:
1505         UNSIGNED        {
1506                 $$ = $1;
1507         }
1508         | DIGIT         {
1509                 $$ = $1;
1510         }
1511         ;
1512
1513 int:
1514         unsigned {
1515                 $$ = $1;
1516         }
1517         | '-' unsigned {
1518                 $$ = -$2;
1519         }
1520         | INT_IDENTIFIER        {
1521                 $$ = *$1->access_content_int (false);
1522         }
1523         ;
1524
1525
1526 string:
1527         STRING          {
1528                 $$ = $1;
1529         }
1530         | STRING_IDENTIFIER     {
1531                 $$ = $1->access_content_String (true);
1532         }
1533         | string '+' string {
1534                 *$$ += *$3;
1535                 delete $3;
1536         }
1537         ;
1538
1539
1540 exclamations:
1541                 { $$ = 0; }
1542         | exclamations '!'      { $$ ++; }
1543         ;
1544
1545 questions:
1546                 { $$ = 0; }
1547         | questions '?' { $$ ++; }
1548         ;
1549
1550
1551 semicolon:
1552         ';'
1553         ;
1554 %%
1555
1556 void
1557 My_lily_parser::set_yydebug (bool b)
1558 {
1559 #ifdef YYDEBUG
1560         yydebug = b;
1561 #endif
1562 }
1563 void
1564 My_lily_parser::do_yyparse ()
1565 {
1566         yyparse ((void*)this);
1567 }
1568
1569