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