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