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