]> git.donarmstrong.com Git - lilypond.git/blob - src/parser.y
release: 0.0.25
[lilypond.git] / src / parser.y
1 %{ // -*-Fundamental-*-
2 #include <iostream.h>
3
4 #include "lookup.hh"
5 #include "misc.hh"
6 #include "lexer.hh"
7 #include "paper.hh"
8 #include "inputscore.hh"
9 #include "main.hh"
10 #include "keyword.hh"
11 #include "inputcommand.hh"
12 #include "debug.hh"
13 #include "parseconstruct.hh"
14 #include "dimen.hh"
15 #include "identifier.hh"
16
17 #ifndef NDEBUG
18 #define YYDEBUG 1
19 #endif
20
21 Array<Request*> pre_reqs, post_reqs;
22 sstack<String> define_spots;
23
24 Paperdef*default_paper();
25 %}
26
27
28 %union {
29     Request * request;
30     Real real;
31     Input_command *command;
32     Identifier *id;    
33     Voice *voice;    
34     Voice_element *el;  
35     String *string;
36     const char *consstr;
37     Paperdef *paper;
38     Input_music *music;
39     Music_general_chord *chord;
40     Music_voice *mvoice; 
41     int i;
42     char c;
43     int ii[10];
44         Moment *moment;
45
46     Array<String> * strvec;
47     Array<Input_command*> *commandvec;
48     Array<int> *intvec;
49
50     Input_staff *staff;    
51     Input_score *score;
52     Symtables * symtables;
53     Symtable * symtable;
54     Symbol * symbol;
55     Lookup*lookup;
56     Interval *interval;
57     Box *box;
58     Notename_tab *notename_tab;
59     Script_def * script;
60     Text_def * textdef;
61 }
62
63 %token VOICE STAFF SCORE TITLE  BAR NOTENAME OUTPUT
64 %token CM IN PT MM PAPER WIDTH METER UNITSPACE SKIP COMMANDS COMMAND
65 %token GEOMETRIC START_T DURATIONCOMMAND OCTAVECOMMAND
66 o%token KEY CLEF MULTI TABLE CHORD VOICES
67 %token PARTIAL RHYTHMIC MELODIC MUSIC LYRIC GROUPING CADENZA
68 %token END SYMBOLTABLES TEXID TABLE NOTENAMES SCRIPT TEXTSTYLE PLET
69 %token MARK GOTO
70
71 %token <id>  IDENTIFIER
72 %token <string> NEWIDENTIFIER 
73 %token <string> PITCHMOD DURATION RESTNAME
74 %token <ii> NOTENAME 
75 %token <real> REAL
76 %token <string> STRING
77
78 %token <i> DOTS INT
79 %type <consstr> unit
80 %type <intvec> pitch_list
81 %type <c> open_request_parens close_request_parens
82 %type <id> declaration
83 %type <string> declarable_identifier
84 %type <paper> paper_block paper_body
85 %type <real> dim
86 %type <ii> duration
87 %type <moment> duration_length
88 %type <el> voice_elt full_element lyrics_elt
89 %type <command> score_command staff_command position_command
90 %type <score> score_block score_body
91 %type <staff> staff_block staff_init staff_body
92 %type <i> int
93 %type <intvec> int_list
94 %type <commandvec> score_commands_block score_commands_body
95 %type <commandvec> staff_commands_block staff_commands_body
96 %type <request> post_request pre_request 
97 %type <string> pitchmod
98 %type <music> music 
99 %type <chord> music_chord music_chord_body
100
101 %type <mvoice>  music_voice_body music_voice 
102
103 %type <interval> dinterval
104 %type <box> box
105 %type <symtable> symtable symtable_body
106 %type <lookup> symtables symtables_body
107 %type <symbol> symboldef
108 %type <notename_tab> notename_tab notename_tab_body
109 %type <i> script_dir
110 %type <script> script_definition script_body mudela_script
111 %type <request> script_req textscript_req
112 %type <textdef> mudela_text
113
114
115 %%
116
117 mudela: /* empty */
118         | mudela score_block { 
119                 add_score($2);
120         }
121         | mudela add_declaration { }
122         | mudela mudela_command  {}
123         ;
124
125 mudela_command:
126         notename_tab                    { lexer->set($1); }
127         ;
128
129 /*
130         DECLARATIONS
131 */
132 add_declaration: declaration    {
133                 lexer->add_identifier($1);
134         }
135         ;
136
137 declarable_identifier:
138         NEWIDENTIFIER { $$ = $1; }
139         | IDENTIFIER { $$ = new String($1->name); }
140         ;
141
142 declaration:
143         declarable_identifier '=' staff_block  {
144                 $$ = new Staff_id(*$1, $3);
145                 delete $1; // this sux
146         }
147         | declarable_identifier '=' music_voice {
148                 $$ = new M_voice_id(*$1, $3);
149                 delete $1;
150         }
151         | declarable_identifier '=' script_definition {
152                 $$ = new Script_id(*$1, $3);
153                 delete $1;
154         }
155         | declarable_identifier '=' music_chord  {
156                 $$ = new M_chord_id(*$1, $3);
157                 delete $1;
158         }
159         | declarable_identifier '=' symtables {
160                 $$ = new Lookup_id(*$1, $3);
161                 delete $1;
162         }
163         | declarable_identifier '=' notename_tab {
164                 $$ = new Notetab_id(*$1, $3);
165                 delete $1;
166         }
167         ;
168
169 notename_tab:
170         NOTENAMES '{' notename_tab_body '}'     { $$ = $3; }
171         ;
172
173 notename_tab_body:                              {
174                 $$ = new Notename_tab;
175         }
176         | IDENTIFIER                            {
177                 $$ = $1->notename_tab(true);
178         }
179         | notename_tab_body STRING int int                      {
180                 $$->set($3, $4, *$2);
181                 delete $2;
182         }
183         ;
184
185 /*
186         SCORE
187 */
188 score_block: SCORE 
189                 { define_spots.push(lexer->spot()); }
190         '{' score_body '}'      {
191                 $$ = $4;
192                 $$->define_spot_str_ = define_spots.pop();
193                 if (!$$->paper_)
194                         $$->paper_ = default_paper();
195         }
196         ;
197
198 score_body:             { $$ = new Input_score; }
199         | score_body staff_block        { $$->add($2); }
200         | score_body score_commands_block       {
201                 $$->add(*$2);
202                 delete $2;
203         }
204         | score_body paper_block                { $$->set($2);  }
205         ;
206 /*
207         COMMANDS
208 */
209 score_commands_block:
210         COMMANDS '{' score_commands_body '}' { $$ =$3;}
211         ;
212
213 score_commands_body:                    { $$ = new Array<Input_command*>; }
214         | score_commands_body score_command             {
215                 $$->add($2);
216         }
217         | score_commands_body position_command          {
218                 $$->add($2);
219         }
220         ;
221
222 staff_commands_block: COMMANDS '{' staff_commands_body '}'      {       
223                 $$ = $3; }
224         ;
225
226 staff_commands_body:
227         /* empty */                     { $$ = new Array<Input_command*>; }
228         | staff_commands_body staff_command     {
229                 $$->add($2);
230         }
231         | staff_commands_body position_command  {
232                 $$->add($2);
233         }
234         ;
235
236 staff_command:
237         KEY pitch_list  {/*UGH*/
238                 $$ = get_key_interpret_command(*$2);
239                 delete $2;
240         }
241         | CLEF STRING                   {
242                 $$ = get_clef_interpret_command(*$2);
243                 delete $2;
244         }
245         ;
246
247 duration_length:        
248         duration                {
249                 $$ = new Moment(wholes($1[0], $1[1]));
250         }
251         |int '*' duration       {
252                 $$ = new Moment($1 * wholes($3[0], $3[1]));
253         }
254         ;
255
256 position_command:
257         SKIP int ':' duration_length            {
258                 $$ = get_skip_command($2, *$4);
259                 delete $4;
260         }
261         | GOTO STRING   {
262                 $$ = get_goto_command(*$2);
263                 delete $2;
264         }
265         ;
266
267 score_command:
268         BAR STRING                      {
269                 $$ = get_bar_command(*$2);
270                 delete $2;
271         }
272         | METER  int '*' int            {
273                 $$ = get_meterchange_command($2, $4);
274         }
275         | PARTIAL duration_length               {
276                 $$ = get_partial_command(*$2);
277                 delete $2;
278         }
279         | GROUPING int_list             {
280                 $$ = get_grouping_command(*$2);
281                 delete $2;
282         }
283         | CADENZA int   {
284                 $$ = get_cadenza_toggle($2);
285         }
286         ;
287
288
289
290 /*
291         PAPER
292 */
293 paper_block:
294         PAPER
295
296         '{' paper_body '}'      { $$ = $3; }
297         ;
298
299 paper_body:
300         /* empty */                     {
301                 $$ = default_paper();
302         }
303         | paper_body WIDTH dim          { $$->linewidth = $3;}
304         | paper_body OUTPUT STRING      { $$->outfile = *$3;
305                 delete $3;
306         }
307         | paper_body symtables          { $$->set($2); }
308         | paper_body UNITSPACE dim      { $$->whole_width = $3; }
309         | paper_body GEOMETRIC REAL     { $$->geometric_ = $3; }
310         ;
311
312 /*
313         STAFFs
314 */
315 staff_block:
316         STAFF   { define_spots.push(lexer->spot()); }
317 /*cont*/        '{' staff_body '}'      {
318                 $$ = $4; 
319                 $$->define_spot_str_ = define_spots.pop();
320         }
321         ;
322
323
324
325 staff_init:
326         IDENTIFIER              { $$ = $1->staff(true); }
327         | RHYTHMIC              {
328                 $$ = new Input_staff("rhythmic");
329         }
330         | MELODIC               {
331                 $$ = new Input_staff( "melodic");
332         }
333         | LYRIC                 {
334                 $$ = new Input_staff( "lyric");
335         }
336         ;
337
338 staff_body:
339         staff_init
340         | staff_body music      {
341                 $$->add($2);
342         }
343         | staff_body staff_commands_block {
344                 $$->add(*$2);
345                 delete $2;
346         }
347         ;
348
349 /*
350         MUSIC
351 */
352 music:
353         music_voice     { $$ = $1; }
354         | music_chord   { $$ = $1; }
355         ;
356
357 music_voice:  MUSIC '{' music_voice_body '}'    { $$ = $3; }
358         ;
359
360 music_voice_body:                       {
361                 $$ = new Music_voice;
362         }
363         | music_voice_body IDENTIFIER {
364                 $$->concatenate($2->mvoice());
365         }
366         | music_voice_body full_element {
367                 $$->add_elt($2);
368         }
369         | music_voice_body voice_command {
370         }
371         | music_voice_body music        {
372                 $$->add($2);
373         }
374         ;
375
376 music_chord:  '{' music_chord_body '}'  { $$ = $2; }
377         ;
378
379 music_chord_body:               {
380                 $$ = new Music_general_chord;
381         }
382         | music_chord_body IDENTIFIER {
383                 $$->concatenate($2->mchord());
384         }
385         | music_chord_body music {
386                 $$ -> add($2);
387         }
388         | music_chord_body full_element {
389                 $$ ->add_elt($2);
390         }
391         ;
392
393
394
395 /*
396         VOICE ELEMENTS
397 */
398 full_element:   pre_requests voice_elt post_requests {
399                 add_requests($2, pre_reqs);
400                 add_requests($2, post_reqs);
401                 $$ = $2;
402         }
403         | MARK STRING   {
404                 $$ = get_mark_element(*$2);
405                 delete $2;
406         }
407         | COMMAND '{' staff_command '}' { $$=get_command_element($3); }
408         | COMMAND '{' score_command '}' { $$=get_command_element($3); }
409         | '|'                           { $$ = get_barcheck_element(); }
410         | lyrics_elt
411         ;
412                 
413 post_requests:
414         {
415                 assert(post_reqs.empty());
416         }
417         | post_requests post_request {
418                 post_reqs.add($2);
419         }
420         ;
421
422 post_request:
423         close_request_parens            { $$ = get_request($1); }
424         | script_req
425         | textscript_req
426         ;
427 close_request_parens:
428         '('     { $$='('; }
429         |']'    { $$ = ']' }
430         ;
431
432 open_request_parens:
433         ')'     {$$=')'}
434         |'['    {$$='['}
435         ;
436
437 script_definition:
438         SCRIPT '{' script_body '}'      { $$ = $3; }
439         ;
440
441 script_body:
442         STRING int int int              {
443                 $$ = new Script_def(*$1,$2, $3,$4);
444                 delete $1;
445         }       
446         ;
447
448 textscript_req:
449         script_dir mudela_text          { $$ = get_text_req($1,$2); }
450         ;
451
452 mudela_text:
453         STRING                  { $$ = get_text(*$1); delete $1; }
454         ;
455
456 script_req:
457         script_dir mudela_script        { $$ = get_script_req($1, $2); }
458         ;
459
460 mudela_script:
461         IDENTIFIER              { $$ = $1->script(true); }
462         | script_definition             { $$ = $1; }
463         | '^'           { $$ = get_scriptdef('^'); }
464         | '+'           { $$ = get_scriptdef('+'); }
465         | '-'           { $$ = get_scriptdef('-'); }
466         | '|'           { $$ = get_scriptdef('|'); }
467         | 'o'           { $$ = get_scriptdef('o'); }
468         | '>'           { $$ = get_scriptdef('>'); }
469         | '.'           { $$ = get_scriptdef('.'); }
470         | DOTS          {
471                 if ($1>1) error("too many staccato reqs");
472                 $$ = get_scriptdef('.');
473         }
474         ;
475
476 script_dir:
477         '_'     { $$ = -1; }
478         |'^'    { $$ = 1; }
479         |'-'    { $$ = 0; }
480         ;
481
482 pre_requests:
483         | pre_requests pre_request {
484                 pre_reqs.add($2);
485         }
486         ;
487
488 pre_request: 
489         open_request_parens             { $$ = get_request($1); }
490         ;
491
492
493
494 voice_command:
495         PLET    '{' INT '/' INT '}'             {
496                 set_plet($3,$5);
497         }
498         | DURATIONCOMMAND '{' duration '}'      {
499                 set_default_duration($3);
500         }
501         | OCTAVECOMMAND '{' pitchmod '}'        {
502                 set_default_octave(*$3);
503                 delete $3;
504         }
505         | TEXTSTYLE STRING      {
506                 set_text_style(*$2);
507                 delete $2;
508         }
509         ;
510
511 duration:               {
512                 get_default_duration($$);
513         }
514         | int           {
515                 get_default_duration($$);
516                 $$[0] = $1;
517                 $$[1] = 0;
518         }
519         | int DOTS      {
520                 $$[0] = $1;
521                 $$[1] = $2;
522         }
523         | DOTS  {
524                 get_default_duration($$);
525                 $$[1] = $1;
526         }
527         ;
528
529 pitchmod:               { $$ = new String; }
530         |PITCHMOD       
531         ;
532
533 voice_elt:
534         pitchmod NOTENAME duration                      {
535                 $$ = get_note_element(*$1, $2, $3);
536                 delete $1;
537         }
538         | RESTNAME duration             {
539                 $$ = get_rest_element(*$1, $2);
540                 delete $1;
541
542         }
543         ;
544
545 lyrics_elt:
546         mudela_text duration                    {
547                 $$ = get_word_element($1, $2);
548         };
549
550 /*
551         UTILITIES
552 */
553 pitch_list:                     {
554                 $$ = new Array<int>;
555         }
556         | pitch_list NOTENAME   {
557                 $$->add($2[0]);
558                 $$->add($2[1]);         
559         }
560         ;
561
562 int:
563         REAL                    {
564                 $$ = int($1);
565                 if ( distance($1,Real(int($$)) ) > 1e-8)
566                         error("expecting integer number");
567         }
568         | INT
569         ;
570
571 int_list:
572         /* */           {
573                 $$ = new Array<int>;
574         }
575         | int_list int          {
576                 $$->add($2);
577         }
578         ;
579
580 dim:
581         REAL unit       { $$ = convert_dimen($1,$2); }
582         ;
583
584
585 unit:   CM              { $$ = "cm"; }
586         |IN             { $$ = "in"; }
587         |MM             { $$ = "mm"; }
588         |PT             { $$ = "pt"; }
589         ;
590         
591 /*
592         symbol tables
593 */
594 symtables:
595         SYMBOLTABLES '{' symtables_body '}'     { $$ = $3; }
596         ;
597
598 symtables_body:
599                         {
600                 $$ = new Lookup;
601         }
602         | IDENTIFIER            {
603                 $$ = new Lookup(*$1->lookup(true));
604         }
605         | symtables_body TEXID STRING           {
606                 $$->texsetting = *$3;
607                 delete $3;
608         }
609         | symtables_body STRING '=' symtable            {
610                 $$->add(*$2, $4);
611                 delete $2;
612         }
613         ;
614
615 symtable:
616         TABLE '{' symtable_body '}' { $$ = $3; }
617         ;
618
619 symtable_body:
620                                 { $$ = new Symtable; }
621         | symtable_body STRING  symboldef {
622                 $$->add(*$2, *$3);
623                 delete $2;
624                 delete $3;
625         }
626         ;
627
628 symboldef:
629         STRING  box             {
630                 $$ = new Symbol(*$1, *$2);
631                 delete $1;
632                 delete $2;
633         }
634         | STRING {
635                 Box b;
636                 $$ = new Symbol(*$1, b);
637                 delete $1;
638         }
639         ;
640
641 box:
642         dinterval dinterval     {
643                 $$ = new Box(*$1, *$2);
644                 delete $1;
645                 delete $2;
646         }
647         ;
648
649 dinterval: dim  dim             {
650                 $$ = new Interval($1, $2);      
651         }
652         ;
653
654 %%
655
656 void
657 parse_file(String s)
658 {
659    *mlog << "Parsing ... ";
660
661 #ifdef YYDEBUG
662    yydebug = !monitor.silence("InitParser") && check_debug;
663 #endif
664
665    set_lexer();
666    lexer->new_input("symbol.ini");
667    yyparse();
668
669 #ifdef YYDEBUG
670    yydebug = !monitor.silence("Parser") && check_debug;
671 #endif
672
673    lexer->new_input(s);
674    yyparse();
675    kill_lexer();
676    assert(define_spots.empty());
677 }
678
679 Paperdef*
680 default_paper()
681 {
682     return new Paperdef(
683         lexer->lookup_identifier("default_table")->lookup(true));
684 }
685
686