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