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