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