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