]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
patch::: 1.0.12.hwn1
[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 "notename-table.hh"
14 #include "scalar.hh"
15 #include "translation-property.hh"
16 #include "script-def.hh"
17 #include "symtable.hh"
18 #include "lookup.hh"
19 #include "misc.hh"
20 #include "my-lily-lexer.hh"
21 #include "paper-def.hh"
22 #include "midi-def.hh"
23 #include "main.hh"
24 #include "file-path.hh"
25 #include "keyword.hh"
26 #include "debug.hh"
27 #include "parseconstruct.hh"
28 #include "dimension.hh"
29 #include "identifier.hh"
30 #include "command-request.hh"
31 #include "musical-request.hh"
32 #include "my-lily-parser.hh"
33 #include "text-def.hh"
34 #include "translator-group.hh"
35 #include "score.hh"
36 #include "music-list.hh"
37 #include "header.hh"
38 #include "duration-convert.hh"
39 #include "change-translator.hh"
40 #include "file-results.hh"
41 #include "mudela-version.hh"
42 #include "scope.hh"
43 #include "relative-music.hh"
44 #include "transposed-music.hh"
45
46 // mmm
47 Mudela_version oldest_version ("1.0.6");
48 Mudela_version version ("1.0.6");
49
50
51 // needed for bison.simple's malloc() and free()
52 #include <malloc.h>
53
54 int const GUESS_PLET = 5;
55 int guess_plet_a[GUESS_PLET] =
56
57   1,
58   3,
59   2,
60   3,
61   4
62 };
63
64 Paper_def* current_paper = 0;
65
66 #ifndef NDEBUG
67 #define YYDEBUG 1
68 #endif
69
70 #define YYERROR_VERBOSE 1
71
72 #define YYPARSE_PARAM my_lily_parser_l
73 #define YYLEX_PARAM my_lily_parser_l
74 #define THIS ((My_lily_parser *) my_lily_parser_l)
75
76 #define yyerror THIS->parser_error
77 #define ARRAY_SIZE(a,s)   if (a.size () != s) THIS->parser_error (_f("expecting %d arguments", s))
78
79
80 %}
81
82
83 %union {
84     Array<Interval>* intarr;
85     Array<Musical_pitch> *pitch_arr;
86     Array<String> * strvec;
87     Array<int> *intvec;
88     Box *box;
89     Simultaneous_music *chord;
90     Duration *duration;
91     Identifier *id;
92     Translator* trans;
93     Music *music;
94     Music_list *music_list;
95     Score *score;
96     Header *header;
97     Interval *interval;
98     Lookup*lookup;
99
100     Musical_req* musreq;
101     Music_output_def * outputdef;
102     Musical_pitch * pitch;
103     Midi_def* midi;
104     Moment *moment;
105     Note_req *notereq;
106     Notename_table *notenametab;
107     Paper_def *paper;
108     Real real;
109     Request * request;
110     General_script_def * script;
111     Scalar *scalar;
112     String *string;
113     Atom * symbol;
114     Symtable * symtable;
115     Symtables * symtables;
116     Text_def * textdef;
117     Tempo_req *tempo;
118     char c;
119     const char *consstr;
120     int i;
121     int pair[2];
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 BAR
146 %token BEAMPLET
147 %token CADENZA
148 %token CLEF
149 %token CM_T
150 %token CONSISTS
151 %token DURATION
152 %token END
153 %token FONT
154 %token GROUPING
155 %token HEADER
156 %token IN_T
157 %token KEY
158 %token KEYSIGNATURE
159 %token LYRIC
160 %token MAEBTELP
161 %token MARK
162 %token MEASURES
163 %token MIDI
164 %token MM_T
165 %token MUSIC
166 %token MUSICAL_PITCH
167 %token NOTENAMES
168 %token NOTES
169 %token OCTAVE
170 %token OUTPUT
171 %token PAPER
172 %token PARTIAL
173 %token PENALTY
174 %token PLET
175 %token PROPERTY
176 %token PT_T
177 %token RELATIVE
178 %token REMOVE
179 %token SCORE
180 %token SCRIPT
181 %token SHAPE
182 %token SKIP
183 %token SPANDYNAMIC
184 %token SYMBOLTABLES
185 %token TABLE
186 %token TELP
187 %token TEMPO
188 %token TIME_T
189 %token TRANSLATOR
190 %token TRANSPOSE
191 %token TYPE
192 %token VERSION
193
194 /* escaped */
195 %token E_EXCLAMATION E_SMALLER E_BIGGER E_CHAR
196
197 %type <i>       dots
198 %token <i>      DIGIT
199 %token <pitch>  NOTENAME_PITCH
200 %token <id>     DURATION_IDENTIFIER
201 %token <id>     IDENTIFIER
202 %token <id>     NOTENAME_TABLE_IDENTIFIER
203 %token <id>     MUSIC_IDENTIFIER
204 %token <id>     POST_REQUEST_IDENTIFIER
205 %token <id>     SCRIPT_IDENTIFIER
206 %token <id>     COMMAND_IDENTIFIER
207 %token <id>     REAL_IDENTIFIER
208 %token <id>     STRING_IDENTIFIER
209 %token <id>     TRANS_IDENTIFIER
210 %token <id>     INT_IDENTIFIER
211 %token <id>     SCORE_IDENTIFIER
212 %token <id>     MIDI_IDENTIFIER
213 %token <id>     PAPER_IDENTIFIER
214 %token <id>     REQUEST_IDENTIFIER
215 %token <real>   REAL
216 %token <string> DURATION RESTNAME
217 %token <string> STRING
218 %token <i>      UNSIGNED
219
220
221 %type <outputdef> output_def
222 %type <header>  mudela_header mudela_header_body
223 %type <box>     box
224 %type <i>       open_request_parens close_request_parens
225 %type <i>       open_abbrev_parens
226 %type <i>       open_plet_parens close_plet_parens
227 %type <i>       sub_quotes sup_quotes
228 %type <music>   simple_element  request_chord command_element
229 %type <i>       abbrev_type
230 %type <i>       int unsigned
231 %type <i>       script_dir
232 %type <id>      identifier_init simple_identifier_init
233 %type <duration> steno_duration notemode_duration
234 %type <duration> entered_notemode_duration explicit_duration
235 %type <interval>        dinterval
236 %type <intvec>  intastint_list int_list
237 %type <lookup>  symtables symtables_body
238
239 %type <pitch>   explicit_musical_pitch steno_musical_pitch musical_pitch absolute_musical_pitch
240 %type <notereq> steno_notepitch
241 %type <pitch_arr>       pitch_list
242 %type <midi>    midi_block midi_body
243 %type <duration>        duration_length
244
245 %type <scalar>  scalar
246 %type <music>   Music  relative_music Sequential_music Simultaneous_music
247 %type <music>   property_def translator_change
248 %type <music_list> Music_list
249 %type <paper>   paper_block paper_def_body
250 %type <real>    dim real
251 %type <real>    real_mult_expression real_primary
252 %type <real>    unit
253 %type <request> abbrev_command_req
254 %type <request> post_request structured_post_request
255 %type <pair>    plet_fraction
256 %type <request> command_req verbose_command_req
257 %type <request> script_req  dynamic_req
258 %type <string>  string
259 %type <string>  string_primary
260 %type <score>   score_block score_body
261 %type <intarr>  shape_array
262 %type <script>  script_definition script_body mudela_script gen_script_def
263 %type <textdef> text_def finger
264 %type <string>  script_abbreviation
265 %type <symbol>  symboldef
266 %type <symtable>        symtable symtable_body
267 %type <trans>   translator_spec translator_spec_body
268 %type <tempo>   tempo_request
269 %type <notenametab> notenames_body notenames_block
270 %expect 1
271
272
273 %%
274
275 mudela: /* empty */
276         | mudela toplevel_expression {}
277         | mudela add_declaration { }
278         | mudela error
279         | mudela check_version { }
280         ;
281
282 toplevel_expression:
283         notenames_block                 {
284                 THIS->lexer_p_->set_notename_table ($1);
285         }
286         | mudela_header {
287                 delete header_global_p;
288                 header_global_p = $1;
289         }
290         | score_block {
291                 score_global_array.push ($1);
292         }
293         | paper_block {
294                 Identifier * id = new
295                         Paper_def_identifier ($1, PAPER_IDENTIFIER);
296                 THIS->lexer_p_->set_identifier ("$defaultpaper", id)
297         }
298         | midi_block {
299                 Identifier * id = new
300                         Midi_def_identifier ($1, MIDI_IDENTIFIER);
301                 THIS->lexer_p_->set_identifier ("$defaultmidi", id)
302         }
303         ;
304
305 check_version:
306         VERSION STRING ';'              {
307                 Mudela_version ver (*$2);
308                 if (!((ver >= oldest_version) && (ver <= version))) {
309                         if (THIS->ignore_version_b_) {
310                                 THIS->here_input ().error (_f ("incorrect mudela version: %s (%s, %s)", ver.str (), oldest_version.str (), version.str ()));
311                         } else {
312                                 THIS->fatal_error_i_ = 1;
313                                 THIS->parser_error (_f ("incorrect mudela version: %s (%s, %s)", ver.str (), oldest_version.str (), version.str ()));
314                         }
315                 }
316         }
317         ;
318
319
320 notenames_block:
321         NOTENAMES '{' notenames_body '}'  {  $$ = $3; }
322         ;
323
324
325
326 notenames_body:
327         /**/    {
328                 $$ = new Notename_table;
329         }
330         | NOTENAME_TABLE_IDENTIFIER     {
331                 $$ = $1-> access_Notename_table();
332         }
333         | notenames_body STRING '=' explicit_musical_pitch {
334                 (*$$)[*$2] = *$4;
335
336                 delete $4;
337                 delete $2;
338         }
339         ;
340
341 mudela_header_body:
342                 {
343                 $$ = new Header;
344         }
345         | mudela_header_body STRING '=' string ';' {
346                 (*$$)[*$2] = *$4;
347                 delete $2;
348                 delete $4;
349         }
350         ;
351
352 mudela_header:
353         HEADER '{' mudela_header_body '}'       {
354                 $$ = $3;
355         }
356         ;
357
358
359 /*
360         DECLARATIONS
361 */
362 add_declaration:
363         STRING {
364                 THIS->remember_spot ();
365         }
366         /* cont */ '=' identifier_init optional_semicolon {
367             THIS->lexer_p_->set_identifier (*$1, $4);
368             $4->init_b_ = THIS->init_parse_b_;
369             $4->set_spot (THIS->pop_spot ());
370         }
371         ;
372 simple_identifier_init: identifier_init
373         ;
374 identifier_init:
375         score_block {
376                 $$ = new Score_identifier ($1, SCORE_IDENTIFIER);
377
378         }
379         | notenames_block {
380                 $$ = new Notename_table_identifier ($1, NOTENAME_TABLE_IDENTIFIER);
381         }
382         | paper_block {
383                 $$ = new Paper_def_identifier ($1, PAPER_IDENTIFIER);
384         }
385         | midi_block {
386                 $$ = new Midi_def_identifier ($1, MIDI_IDENTIFIER);
387
388         }
389         | script_definition {
390                 $$ = new General_script_def_identifier ($1, SCRIPT_IDENTIFIER);
391
392         }
393         | Music  {
394                 $$ = new Music_identifier ($1, MUSIC_IDENTIFIER);
395
396         }
397         | symtables {
398                 $$ = new Lookup_identifier ($1, IDENTIFIER);
399
400         }
401         | post_request {
402                 $$ = new Request_identifier ($1, POST_REQUEST_IDENTIFIER);
403         }
404         | explicit_duration {
405                 $$ = new Duration_identifier ($1, DURATION_IDENTIFIER);
406         }
407         | real {
408                 $$ = new Real_identifier (new Real ($1), REAL_IDENTIFIER);
409         }
410         | string {
411                 $$ = new String_identifier ($1, STRING_IDENTIFIER);
412         }
413         | int   {
414                 $$ = new int_identifier (new int ($1), INT_IDENTIFIER);
415         }
416         | translator_spec {
417                 $$ = new Translator_identifier ($1, TRANS_IDENTIFIER);
418         }
419         ;
420
421 translator_spec:
422         TRANSLATOR '{' translator_spec_body '}'
423                 { $$ = $3; }
424         ;
425
426 translator_spec_body:
427         TRANS_IDENTIFIER        {
428                 $$ = $1->access_Translator ();
429                 $$-> set_spot (THIS->here_input ());
430         }
431         | TYPE STRING ';'       {
432                 Translator* t = get_translator_l (*$2);
433                 Translator_group * tg = t->access_Translator_group ();
434
435                 if (!tg)
436                         THIS->parser_error (_("Need a translator group for a context"));
437                 
438                 t = t->clone ();
439                 t->set_spot (THIS->here_input ());
440                 $$ = t;
441                 delete $2;
442         }
443         | translator_spec_body STRING '=' simple_identifier_init ';'    { 
444                 Identifier* id = $4;
445                 String_identifier *s = id->access_String_identifier ();
446                 Real_identifier *r= id->access_Real_identifier ();
447                 int_identifier *i = id->access_int_identifier ();
448         
449                 String str;
450                 if (s) str = *s->access_String (false); 
451                 if (i) str = to_str (*i->access_int (false));
452                 if (r) str = to_str (*r->access_Real (false));
453                 if (!s && !i && !r)
454                         THIS->parser_error (_("Wrong type for property value"));
455
456                 delete $4;
457                 $$->set_property (*$2, str);
458         }
459         | translator_spec_body STRING ';' {
460                 $$->type_str_ = *$2;
461                 delete $2;
462         }
463         | translator_spec_body CONSISTS STRING ';' {
464                 $$->access_Translator_group ()-> set_element (*$3, true);
465                 delete $3;
466         }
467         | translator_spec_body ACCEPTS STRING ';' {
468                 $$->access_Translator_group ()-> set_acceptor (*$3, true);
469                 delete $3;
470         }
471         | translator_spec_body REMOVE STRING ';' {
472                 $$->access_Translator_group ()-> set_element (*$3, false);
473                 delete $3;
474         }
475         ;
476
477 /*
478         SCORE
479 */
480 score_block:
481         SCORE { THIS->remember_spot ();
482                 THIS->error_level_i_ =0;
483         }
484         /*cont*/ '{' score_body '}'     {
485                 $$ = $4;
486                 $$->set_spot (THIS->pop_spot ());
487                 if (!$$->def_p_arr_.size ())
488                         $$->add_output (THIS->default_paper_p ());
489
490                 /* handle error levels. */
491                 $$->errorlevel_i_ = THIS->error_level_i_;
492                 THIS->error_level_i_ = 0;
493         }
494         ;
495
496 score_body:             {
497                 $$ = new Score;
498         }
499         | SCORE_IDENTIFIER {
500                 $$ = $1->access_Score ();
501         }
502         | score_body mudela_header      {
503                 $$->header_p_ = $2;
504         }
505         | score_body Music      {
506                 if ($$->music_p_)
507                         $2->warning (_ ("More than one music block"));  
508                 $$->music_p_ = $2;
509         }
510         | score_body output_def {
511                 $$->add_output ($2);
512         }
513         | score_body error {
514
515         }
516         ;
517
518 output_def:
519         paper_block {
520                 $$ = $1;
521         }
522         |  midi_block           {
523                 $$= $1;
524         }
525         ;
526
527 intastint_list:
528         /* */   { $$ =new Array<int>; }
529         | intastint_list int '*' int    {
530                 $$->push ($2); $$->push ($4);
531         }
532         | intastint_list int    {
533                 $$->push ($2); $$->push (1);
534         }
535         ;       
536
537
538 /*
539         PAPER
540 */
541 paper_block:
542         PAPER '{' paper_def_body '}'    { 
543                 $$ = $3;
544                 THIS-> lexer_p_->scope_l_arr_.pop ();
545         }
546         ;
547
548 optional_semicolon:
549         /* empty */
550         | ';'
551         ;
552
553 paper_def_body:
554         /* empty */                     {
555                 Paper_def *p = THIS->default_paper_p ();
556                 THIS-> lexer_p_-> scope_l_arr_.push (p->scope_p_);
557                 $$ = p;
558         }
559         | PAPER_IDENTIFIER optional_semicolon   {
560                 Paper_def *p = $1->access_Paper_def ();
561                 THIS->lexer_p_->scope_l_arr_.push (p->scope_p_);
562                 $$ = p;
563         }
564         | paper_def_body int '=' symtables              { // ugh, what a syntax
565                 $$->set_lookup ($2, $4);
566         }
567         | paper_def_body STRING '=' simple_identifier_init ';' {
568               THIS->lexer_p_->set_identifier (*$2, $4);
569               delete $2;
570         }
571         | paper_def_body translator_spec {
572                 $$-> assign_translator ($2);
573         }
574         | paper_def_body SHAPE '=' shape_array ';' {
575                 $$->shape_int_a_ = *$4;
576                 delete $4;
577         }
578         | paper_def_body error {
579
580         }
581         ;
582
583 real_primary:
584         REAL            {
585                 $$ = $1;
586         }
587         | REAL_IDENTIFIER               {
588                 Real *r_p = $1->access_Real ();
589                 $$ = * r_p;
590                 DOUT << "Lookup real: " << *r_p << "\n";
591                 delete r_p;
592         }
593         | dim
594         | '(' real ')' {
595                 $$ = $2;
596         }
597         ;
598
599 real:
600         real_mult_expression
601         | real '+' real_mult_expression {
602                 $$ = $1 + $3;
603         }
604         | real '-' real_mult_expression {
605                 $$ = $1 - $3;
606         }
607         ;
608
609 real_mult_expression:
610         real_primary
611         | real_mult_expression '*' real_primary {
612                 $$ = $1 * $3;
613         }
614         | real_mult_expression '/' real_primary {
615                 $$ = $1 / $3;
616         }
617         ;
618
619 shape_array:
620         /* empty */ {
621                 $$ = new Array<Interval>;
622         }
623         | shape_array dim dim {
624                 $$->push(Interval($2, $2 + $3));
625         };
626
627 /*
628         MIDI
629 */
630 midi_block:
631         MIDI
632
633         '{' midi_body '}'       { $$ = $3; }
634         ;
635
636 midi_body: /* empty */          {
637                 $$ = THIS->default_midi_p ();
638         }
639         | MIDI_IDENTIFIER       {
640                 $$ = $1-> access_Midi_def ();           
641         }
642         | midi_body translator_spec     {
643                 $$-> assign_translator ($2);
644         }
645         | midi_body tempo_request ';' {
646                 $$->set_tempo ($2->dur_.length (), $2->metronome_i_);
647                 delete $2;
648         }
649         | midi_body error {
650
651         }
652         ;
653
654 tempo_request:
655         TEMPO entered_notemode_duration '=' unsigned    {
656                 $$ = new Tempo_req;
657                 $$->dur_ = *$2;
658                 delete $2;
659                 $$-> metronome_i_ = $4;
660         }
661         ;
662
663 Music_list: /* empty */ {
664                 $$ = new Music_list;
665         }
666         | Music_list Music {
667                 $$->add_music ($2);
668         }
669         | Music_list error {
670         }
671         ;
672
673
674 Sequential_music: '{' Music_list '}'            {
675                 $$ = new Sequential_music ($2);
676         }
677         ;
678
679 Simultaneous_music: '<' Music_list '>'  {
680                 $$ = new Simultaneous_music ($2);
681         }
682         ;
683 Music:
684         request_chord           { $$ = $1; }
685         | TYPE STRING Music     {
686                 $$ = $3;
687                 $$->translator_type_str_ = *$2;
688                 delete $2;
689         }
690         | TYPE STRING '=' STRING Music {
691                 $$ = $5;
692                 $$->translator_type_str_ = *$2;
693                 $$->translator_id_str_ = *$4;
694                 delete $2;
695                 delete $4;
696         }
697         | Simultaneous_music            { $$ = $1; }
698         | Sequential_music              { $$ = $1; }
699         | TRANSPOSE musical_pitch Music {
700                 $$ = new Transposed_music ($3, *$2);
701                 delete $2;
702         }
703         | MUSIC_IDENTIFIER { $$ = $1->access_Music (); }
704         | NOTES
705                 { THIS->lexer_p_->push_note_state (); }
706         Music
707                 { $$ = $3;
708                   THIS->lexer_p_->pop_state ();
709                 }
710
711         | LYRIC
712                 { THIS->lexer_p_->push_lyric_state (); }
713         Music
714                 {
715                   $$ = $3;
716                   THIS->lexer_p_->pop_state ();
717                 }
718         | property_def
719         | translator_change
720         | relative_music        { $$ = $1; }
721         ;
722
723 relative_music:
724         RELATIVE absolute_musical_pitch Music {
725                 $$ = new Relative_octave_music ($3, *$2);
726                 delete $2;
727         }
728         ;
729
730 translator_change:
731         TRANSLATOR STRING '=' STRING  {
732                 Change_translator * t = new Change_translator;
733                 t-> change_to_type_str_ = *$2;
734                 t-> change_to_id_str_ = *$4;
735
736                 $$ = t;
737                 $$->set_spot (THIS->here_input ());
738                 delete $2;
739                 delete $4;
740         }
741         ;
742
743 property_def:
744         PROPERTY STRING '.' STRING '=' scalar   {
745                 Translation_property *t = new Translation_property;
746                 t-> translator_type_str_ = *$2;
747                 t-> var_str_ = *$4;
748                 t-> value_ = *$6;
749                 $$ = t;
750                 $$->set_spot (THIS->here_input ());
751                 delete $2;
752                 delete $4;
753                 delete $6;
754         }
755         ;
756
757 scalar:
758         STRING          { $$ = new Scalar (*$1); delete $1; }
759         | int           { $$ = new Scalar ($1); }
760         ;
761
762
763
764 request_chord:
765         pre_requests simple_element post_requests       {
766                 THIS->add_requests ((Simultaneous_music*)$2);//ugh
767                 $$ = $2;
768         }
769         | command_element
770         ;
771
772
773
774
775 command_element:
776         command_req {
777                 $$ = new Request_chord;
778                 $$-> set_spot (THIS->here_input ());
779                 $1-> set_spot (THIS->here_input ());
780                 ((Simultaneous_music*)$$) ->add_music ($1);//ugh
781
782         }
783         ;
784
785 command_req:
786         abbrev_command_req
787         | verbose_command_req ';'       { $$ = $1; }
788         ;
789
790 abbrev_command_req:
791         '|'                             {
792                 $$ = new Barcheck_req;
793         }
794         | COMMAND_IDENTIFIER    {
795                 $$ = $1->access_Request ();
796         }
797         ;
798
799 verbose_command_req:
800         BAR STRING                      {
801                 $$ = new Bar_req (*$2);
802                 delete $2;
803         }
804         | MARK STRING {
805                 $$ = new Mark_req (*$2);
806                 delete $2;
807         }
808         | MARK unsigned {
809                 $$ = new Mark_req (to_str ($2));
810         }
811         | TIME_T unsigned '/' unsigned  {
812                 Time_signature_change_req *m = new Time_signature_change_req;
813                 m->beats_i_ = $2;
814                 m->one_beat_i_=$4;
815                 $$ = m;
816         }
817         | PENALTY '=' int       {
818                 Break_req * b = new Break_req;
819                 b->penalty_i_ = $3;
820                 b-> set_spot (THIS->here_input ());
821                 $$ = b;
822         }
823         | SKIP duration_length {
824                 Skip_req * skip_p = new Skip_req;
825                 skip_p->duration_ = *$2;
826                 delete $2;
827                 $$ = skip_p;
828         }
829         | tempo_request {
830                 $$ = $1;
831         }
832         | CADENZA unsigned      {
833                 $$ = new Cadenza_req ($2);
834         }
835         | PARTIAL duration_length       {
836                 $$ = new Partial_measure_req ($2->length ());
837                 delete $2;
838         }
839         | CLEF STRING {
840                 $$ = new Clef_change_req (*$2);
841                 delete $2;
842         }
843         | KEY NOTENAME_PITCH    {
844                 Key_change_req *key_p= new Key_change_req;
845                 key_p->pitch_arr_.push(*$2);
846                 key_p->ordinary_key_b_ = true;
847                 $$ = key_p;
848                 delete $2;
849         }
850         | KEYSIGNATURE pitch_list       {
851                 Key_change_req *key_p= new Key_change_req;
852                 key_p->pitch_arr_ = *$2;
853                 key_p->ordinary_key_b_ = false;
854                 $$ = key_p;
855                 delete $2;
856         }
857         | GROUPING intastint_list {
858                 $$ = get_grouping_req (*$2); delete $2;
859         }
860         ;
861
862 post_requests:
863         {
864                 /* something silly happened.  Junk this stuff*/
865                 if (!THIS->post_reqs.empty ())
866                 {
867                         warning ("Junking post-requests");
868                         THIS->post_reqs.clear ();
869                 }
870         }
871         | post_requests structured_post_request {
872                 $2->set_spot (THIS->here_input ());
873                 THIS->post_reqs.push ($2);
874         }
875         | post_requests close_request_parens    {
876                 Array<Request*>& r = *THIS->get_parens_request ($2);
877                 for (int i = 0; i < r.size (); i++ )
878                         r[i]->set_spot (THIS->here_input ());
879                 THIS->post_reqs.concat (r);
880                 delete &r;
881         }
882         ;
883
884 structured_post_request:
885         script_req
886         | post_request
887         ;
888
889 post_request:
890         POST_REQUEST_IDENTIFIER {
891                 $$ = (Request*)$1->access_Request ();
892         }
893         | dynamic_req {
894                 $$ = $1;
895         }
896         | abbrev_type   {
897                 Abbreviation_req* a = new Abbreviation_req;
898                 a->type_i_ = $1;
899                 $$ = a;
900         }
901         ;
902
903 sup_quotes:
904         '\'' {
905                 $$ = 1;
906         }
907         | sup_quotes '\'' {
908                 $$ ++;
909         }
910         ;
911 sub_quotes:
912         ',' {
913                 $$ = 1;
914         }
915         | sub_quotes ',' {
916                 $$ ++ ;
917         }
918         ;
919
920 steno_musical_pitch:
921         NOTENAME_PITCH  {
922                 $$ = $1;
923         }
924         | NOTENAME_PITCH sup_quotes     {
925                 $$ = $1;
926                 $$->octave_i_ +=  $2;
927         }
928         | NOTENAME_PITCH sub_quotes      {
929                 $$ = $1;
930                 $$->octave_i_ += - $2;
931         }
932         ;
933
934 explicit_musical_pitch:
935         MUSICAL_PITCH '{' int_list '}'  {/* ugh */
936                 Array<int> &a = *$3;
937                 ARRAY_SIZE(a,3);
938                 $$ = new Musical_pitch;
939                 $$->octave_i_ = a[0];
940                 $$->notename_i_ = a[1];
941                 $$->accidental_i_ = a[2];
942                 delete &a;
943         }
944         ;
945
946 musical_pitch:
947         steno_musical_pitch
948         | explicit_musical_pitch
949         ;
950
951 steno_notepitch:
952         musical_pitch   {
953                 $$ = new Note_req;
954                 
955                 $$->pitch_ = *$1;
956                 delete $1;
957         }
958         | steno_notepitch  '!'          {
959                 $$->forceacc_b_ = ! $$->forceacc_b_;
960         }
961         ;
962
963
964 explicit_duration:
965         DURATION '{' int_list '}'       {
966                 $$ = new Duration;
967                 Array<int> &a = *$3;
968                 ARRAY_SIZE(a,2);
969                         
970                 $$-> durlog_i_ = a[0];
971                 $$-> dots_i_ = a[1];
972
973                 delete &a;              
974         }
975         ;
976
977 dynamic_req:
978         ABSDYNAMIC '{' unsigned '}'     {
979                 Absolute_dynamic_req *ad_p = new Absolute_dynamic_req;
980                 ad_p ->loudness_ = (Dynamic_req::Loudness)$3;
981                 $$ =ad_p;
982         }
983         | SPANDYNAMIC '{' int int '}' {
984                 Span_dynamic_req * sp_p = new Span_dynamic_req;
985                 sp_p->spantype = (Span_req::Spantype)$4;
986                 sp_p-> dynamic_dir_  = (Direction)$3;
987                 $$ = sp_p;
988         }
989         ;
990
991 plet_fraction:
992         unsigned '/' unsigned {
993                 $$[0] = $1;
994                 $$[1] = $3;
995         }
996         |
997         '/' unsigned {
998                 int num = $2 >? 1;
999                 $$[0] = guess_plet_a[(num <? GUESS_PLET) - 1];
1000                 $$[1] = num;
1001         }
1002         ;
1003
1004 close_plet_parens:
1005         ']' plet_fraction {
1006                 $$ = MAEBTELP;
1007                 THIS->plet_.type_i_ = $2[1];
1008                 THIS->plet_.iso_i_ = $2[0];
1009                 THIS->default_duration_.plet_ = THIS->plet_;
1010         }
1011         | TELP {
1012                 $$ = TELP;
1013                 THIS->plet_.type_i_ = 1;
1014                 THIS->plet_.iso_i_ = 1;
1015                 THIS->default_duration_.plet_ = THIS->plet_;
1016         }
1017         | TELP plet_fraction {
1018                 $$ = TELP;
1019                 THIS->plet_.type_i_ = $2[1];
1020                 THIS->plet_.iso_i_ = $2[0];
1021                 THIS->default_duration_.plet_ = THIS->plet_;
1022         }
1023         ;
1024
1025 close_request_parens:
1026         '~'     {
1027                 $$ = '~';
1028         }
1029         | '('   {
1030                 $$='(';
1031         }
1032         | ']'   {
1033                 $$ = ']';
1034         }
1035         | E_SMALLER {
1036                 $$ = '<';
1037         }
1038         | E_BIGGER {
1039                 $$ = '>';
1040         }
1041         | close_plet_parens
1042         ;
1043
1044 open_abbrev_parens:
1045         '[' ':' unsigned {
1046                 $$ = '[';
1047                 if (!Duration::duration_type_b ($3))
1048                         THIS->parser_error (_f ("not a duration: %d", $3));
1049                 else if ($3 < 8)
1050                         THIS->parser_error (_ ("can't abbreviate"));
1051                 else
1052                         THIS->set_abbrev_beam ($3);
1053         }
1054         ;
1055
1056 open_plet_parens:
1057         '[' plet_fraction {
1058                 $$ = BEAMPLET;
1059                 THIS->plet_.type_i_ = $2[1];
1060                 THIS->plet_.iso_i_ = $2[0];
1061                 THIS->default_duration_.plet_ = THIS->plet_;
1062         }
1063         | PLET plet_fraction {
1064                 $$ = PLET;
1065                 THIS->plet_.type_i_ = $2[1];
1066                 THIS->plet_.iso_i_ = $2[0];
1067                 THIS->default_duration_.plet_ = THIS->plet_;
1068         }
1069         ;
1070
1071 open_request_parens:
1072         E_EXCLAMATION   {
1073                 $$ = '!';
1074         }
1075         | ')'   {
1076                 $$=')';
1077         }
1078         | '['   {
1079                 $$='[';
1080         }
1081         | open_abbrev_parens
1082         | open_plet_parens
1083         ;
1084
1085
1086
1087 script_definition:
1088         SCRIPT '{' script_body '}'      { $$ = $3; }
1089         ;
1090
1091 script_body:
1092         STRING int int int int int              {
1093                 Script_def *s = new Script_def;
1094                 s->set_from_input (*$1,$2, $3,$4,$5, $6);
1095                 $$  = s;
1096                 delete $1;
1097         }
1098         ;
1099
1100 script_req:
1101         script_dir gen_script_def       {
1102                 Musical_script_req *m = new Musical_script_req;
1103                 $$ = m;
1104                 m->scriptdef_p_ = $2;
1105                 m->set_spot (THIS->here_input ());
1106                 if (!m->dir_)
1107                   m->dir_  = (Direction)$1;
1108         }
1109         ;
1110
1111 gen_script_def:
1112         text_def        { 
1113                 $$ = $1;
1114                 ((Text_def*) $$)->align_dir_ = LEFT; /* UGH */
1115         }
1116         | mudela_script { 
1117                 $$ = $1;
1118                 $$-> set_spot (THIS->here_input ());
1119         }
1120         | finger {
1121                 $$ = $1;
1122                 ((Text_def*)$$)->align_dir_ = RIGHT; /* UGH */
1123         }
1124         ;
1125
1126 text_def:
1127         string {
1128                 Text_def *t  = new Text_def;
1129                 $$ = t;
1130                 t->text_str_ = *$1;
1131                 delete $1;
1132                 $$->set_spot (THIS->here_input ());
1133         }
1134         ;
1135
1136 finger:
1137          DIGIT {
1138                 Text_def* t  = new Text_def;
1139                 $$ = t;
1140                 t->text_str_ = to_str ($1);
1141                 t->style_str_ = "finger";
1142                 $$->set_spot (THIS->here_input ());
1143         }
1144         ;
1145
1146 script_abbreviation:
1147         '^'             { $$ = get_scriptdef ('^'); }
1148         | '+'           { $$ = get_scriptdef ('+'); }
1149         | '-'           { $$ = get_scriptdef ('-'); }
1150         | '|'           { $$ = get_scriptdef ('|'); }
1151         | 'o'           { $$ = get_scriptdef ('o'); }
1152         | '>'           { $$ = get_scriptdef ('>'); }
1153         | '.'           {
1154                 $$ = get_scriptdef ('.');
1155         }
1156         ;
1157
1158 mudela_script:
1159         SCRIPT_IDENTIFIER               { $$ = $1->access_General_script_def (true); }
1160         | script_definition             { $$ = $1; }
1161         | script_abbreviation           {
1162                 $$ = THIS->lexer_p_->lookup_identifier (*$1)->access_General_script_def (true);
1163                 delete $1;
1164         }
1165         ;
1166
1167 script_dir:
1168         '_'     { $$ = -1; }
1169         | '^'   { $$ = 1; }
1170         | '-'   { $$ = 0; }
1171         ;
1172
1173 pre_requests:
1174         {
1175         }
1176         | pre_requests open_request_parens {
1177                 Array<Request*>& r = *THIS->get_parens_request ($2);
1178                 for (int i = 0; i < r.size (); i++ )
1179                         r[i]->set_spot (THIS->here_input ());
1180                 THIS->pre_reqs.concat (r);
1181                 delete &r;
1182         }
1183         ;
1184
1185 absolute_musical_pitch:
1186         steno_musical_pitch     {
1187                 $$ = $1;
1188         }
1189         ;
1190
1191 duration_length:
1192         steno_duration {
1193                 $$ = $1;
1194         }
1195         ;
1196
1197 dots:
1198         '.'             { $$ = 1; }
1199         | dots '.'      { $$ ++; }
1200         ;
1201
1202 entered_notemode_duration:
1203         /* */           {
1204                 $$ = new Duration (THIS->default_duration_);
1205         }
1206         | dots          {
1207                 $$ = new Duration (THIS->default_duration_);
1208                 $$->dots_i_  = $1;
1209         }
1210         | steno_duration        {
1211                 THIS->set_last_duration ($1);
1212         }
1213         ;
1214
1215 notemode_duration:
1216         entered_notemode_duration {
1217                 $$ = $1;
1218         }
1219         ;
1220
1221 steno_duration:
1222         unsigned                {
1223                 $$ = new Duration;
1224                 if (!Duration::duration_type_b ($1))
1225                         THIS->parser_error (_f ("not a duration: %d", $1));
1226                 else {
1227                         $$->durlog_i_ = Duration_convert::i2_type ($1);
1228                         $$->set_plet (THIS->plet_.iso_i_, THIS->plet_.type_i_);
1229                      }
1230         }
1231         | DURATION_IDENTIFIER   {
1232                 $$ = $1->access_Duration ();
1233         }
1234         | steno_duration '.'    {
1235                 $$->dots_i_ ++;
1236         }
1237         | steno_duration '*' unsigned  {
1238                 $$->plet_.iso_i_ *= $3;
1239         }
1240         | steno_duration '/' unsigned {
1241                 $$->plet_.type_i_ *= $3;
1242         }
1243         ;
1244
1245
1246 abbrev_type: 
1247         ':'     {
1248                 $$ =0;
1249         }
1250         | ':' unsigned {
1251                 if (!Duration::duration_type_b ($2))
1252                         THIS->parser_error (_f ("not a duration: %d", $2));
1253                 else if ($2 < 8)
1254                         THIS->parser_error (_ ("can't abbreviate"));
1255                 $$ = $2;
1256         }
1257         ;
1258
1259
1260
1261 simple_element:
1262         steno_notepitch notemode_duration  {
1263                 if (!THIS->lexer_p_->note_state_b ())
1264                         THIS->parser_error (_ ("have to be in Note mode for notes"));
1265                 $1->set_duration (*$2);
1266                 int durlog_i = $2->durlog_i_;
1267                 $$ = THIS->get_note_element ($1, $2);
1268         }
1269         | RESTNAME notemode_duration            {
1270                 $$ = THIS->get_rest_element (*$1, $2);
1271                 delete $1;  // delete notename
1272         }
1273         | MEASURES notemode_duration    {
1274                 Multi_measure_rest_req* m = new Multi_measure_rest_req;
1275                 m->duration_ = *$2;
1276                 delete $2;
1277
1278                 Simultaneous_music*velt_p = new Request_chord;
1279                 velt_p->set_spot (THIS->here_input ());
1280                 velt_p->add_music (m);
1281                 $$ = velt_p;
1282         }
1283         | STRING notemode_duration                      {
1284                 if (!THIS->lexer_p_->lyric_state_b ())
1285                         THIS->parser_error (_ ("have to be in Lyric mode for lyrics"));
1286                 $$ = THIS->get_word_element (*$1, $2);
1287                 delete $1;
1288         }
1289         ;
1290
1291
1292 /*
1293         UTILITIES
1294  */
1295 pitch_list:                     {
1296                 $$ = new Array<Musical_pitch>;
1297         }
1298         | pitch_list musical_pitch      {
1299                 $$->push (*$2);
1300                 delete $2;
1301         }
1302         ;
1303
1304
1305 int_list:
1306         /**/                    {
1307                 $$ = new Array<int>
1308         }
1309         | int_list int          {
1310                 $$->push ($2);          
1311         }
1312         ;
1313
1314 unsigned:
1315         UNSIGNED        {
1316                 $$ = $1;
1317         }
1318         | DIGIT {
1319                 $$ = $1;
1320         };
1321
1322 int:
1323         unsigned {
1324                 $$ = $1;
1325         }
1326         | '-' unsigned {
1327                 $$ = -$2;
1328         }
1329         | INT_IDENTIFIER        {
1330                 int *i_p = $1->access_int ();
1331                 $$ = *i_p;
1332                 delete i_p;
1333         }
1334         ;
1335
1336 string_primary:
1337         STRING          {
1338                 $$ = $1;
1339         }
1340         | STRING_IDENTIFIER     {
1341                 String *s_p = $1->access_String ();
1342
1343                 $$ = s_p;
1344                 DOUT << "Lookup string: " << *s_p << "\n";
1345         }
1346         ;
1347
1348 string:
1349         string_primary {
1350                 $$ = $1;
1351         }
1352         | string '+' string_primary {
1353                 *$$ += *$3;
1354                 delete $3;
1355         }
1356         ;
1357
1358 dim:
1359         real_primary unit       { $$ = $1*$2; }
1360         ;
1361
1362
1363 unit:   CM_T            { $$ = 1 CM; }
1364         |IN_T           { $$ = 1 INCH; }
1365         |MM_T           { $$ = 1 MM; }
1366         |PT_T           { $$ = 1 PT; }
1367         ;
1368
1369 /*
1370         symbol tables
1371 */
1372 symtables:
1373         SYMBOLTABLES '{' symtables_body '}'     { $$ = $3; }
1374         ;
1375
1376 symtables_body:
1377                         {
1378                 $$ = new Lookup;
1379         }
1380         | IDENTIFIER            {
1381                 $$ = $1->access_Lookup ();
1382         }
1383         | symtables_body FONT STRING            {
1384                 $$->font_ = *$3;
1385                 $$->font_path_ = global_path.find (*$3);
1386                 if  (!$$->font_path_.length_i ())
1387                         THIS->here_input ().error (_f("can't open file: `%s'", $3->ch_C()));
1388
1389                 delete $3;
1390         }
1391         | symtables_body STRING '=' symtable            {
1392                 $$->add (*$2, $4);
1393                 delete $2;
1394         }
1395         ;
1396
1397 symtable:
1398         TABLE '{' symtable_body '}' { $$ = $3; }
1399         ;
1400
1401 symtable_body:
1402                                 { $$ = new Symtable; }
1403         | symtable_body STRING  symboldef {
1404                 $$->add (*$2, *$3);
1405                 delete $2;
1406                 delete $3;
1407         }
1408         ;
1409
1410 symboldef:
1411         STRING  box             {
1412                 $$ = new Atom (*$1, *$2);
1413                 delete $1;
1414                 delete $2;
1415         }
1416         | STRING {
1417                 Box b (Interval (0,0), Interval (0,0));
1418                 $$ = new Atom (*$1, b);
1419                 delete $1;
1420         }
1421         ;
1422
1423 box:
1424         dinterval dinterval     {
1425                 $$ = new Box (*$1, *$2);
1426                 delete $1;
1427                 delete $2;
1428         }
1429         ;
1430
1431 dinterval: dim  dim             {
1432                 $$ = new Interval ($1, $2);
1433         }
1434         ;
1435
1436 %%
1437
1438 void
1439 My_lily_parser::set_yydebug (bool b)
1440 {
1441 #ifdef YYDEBUG
1442         yydebug = b;
1443 #endif
1444 }
1445 void
1446 My_lily_parser::do_yyparse ()
1447 {
1448         yyparse ((void*)this);
1449 }
1450