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