]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
* python/convertrules.py (conv): warning on \tempo{}
[lilypond.git] / lily / parser.yy
1 /*
2   parser.yy -- Bison/C++ parser for LilyPond
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7                  Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 %{
11
12 #define YYERROR_VERBOSE 1
13 #define YYPARSE_PARAM my_lily_parser
14 #define YYLEX_PARAM my_lily_parser
15 #define PARSER ((Lily_parser *) my_lily_parser)
16
17 #define yyerror PARSER->parser_error
18
19 /* We use custom location type: Input objects */
20 #define YYLTYPE Input
21 #define YYLLOC_DEFAULT(Current,Rhs,N) \
22         ((Current).set_location ((Rhs)[1], (Rhs)[N]))
23
24
25 %}
26
27 /* We use SCMs to do strings, because it saves us the trouble of
28 deleting them.  Let's hope that a stack overflow doesnt trigger a move
29 of the parse stack onto the heap. */
30
31 %left PREC_TOP
32 %left ADDLYRICS
33 %left PREC_BOT
34
35 %expect 1
36
37 /* One shift/reduce problem
38
39 1.  \repeat
40         \repeat .. \alternative
41
42     \repeat { \repeat .. \alternative }
43
44 or
45
46     \repeat { \repeat } \alternative 
47 */
48
49
50 %pure_parser
51 %locations
52
53
54
55 %{ // -*-Fundamental-*-
56
57 /*
58 FIXME:
59
60    * The rules for who is protecting what are very shady.  Uniformise
61      this.
62
63    * There are too many lexical modes?
64 */
65
66 #include "config.hh"
67
68 #include <cctype>
69 #include <cstdlib>
70 #include <cstdio>
71 using namespace std;
72
73 #include "book.hh"
74 #include "context-def.hh"
75 #include "dimensions.hh"
76 #include "file-path.hh"
77 #include "input-smob.hh"
78 #include "input.hh"
79 #include "international.hh"
80 #include "lily-guile.hh"
81 #include "lily-lexer.hh"
82 #include "lily-parser.hh"
83 #include "lilypond-input-version.hh"
84 #include "main.hh"
85 #include "misc.hh"
86 #include "music.hh"
87 #include "music.hh"
88 #include "output-def.hh"
89 #include "paper-book.hh"
90 #include "program-option.hh"
91 #include "scm-hash.hh"
92 #include "score.hh"
93 #include "text-interface.hh"
94 #include "warn.hh"
95
96 %}
97
98
99 %union {
100         Book *book;
101         Output_def *outputdef;
102         SCM scm;
103         std::string *string;
104         Music *music;
105         Score *score;
106         int i;
107 }
108
109 %{
110
111 #define MY_MAKE_MUSIC(x)  make_music_by_name (ly_symbol2scm (x))
112
113 /* ES TODO:
114 - Don't use lily module, create a new module instead.
115 - delay application of the function
116 */
117 #define LOWLEVEL_MAKE_SYNTAX(proc, args)        \
118   scm_apply_0 (proc, args)
119 /* Syntactic Sugar. */
120 #define MAKE_SYNTAX(name, location, ...)        \
121   LOWLEVEL_MAKE_SYNTAX (ly_lily_module_constant (name), scm_list_n (PARSER->self_scm (), make_input (location), __VA_ARGS__, SCM_UNDEFINED));
122
123 SCM get_next_unique_context_id ();
124 SCM get_next_unique_lyrics_context_id ();
125
126 #undef _
127 #if !HAVE_GETTEXT
128 #define _(x) x
129 #else
130 #include <libintl.h>
131 #define _(x) gettext (x)
132 #endif
133
134
135
136 SCM make_music_relative (Pitch start, SCM music);
137 SCM run_music_function (Lily_parser *, SCM expr);
138 SCM get_first_context_id (SCM type, Music *m);
139 SCM make_chord_elements (SCM pitch, SCM dur, SCM modification_list);
140 SCM make_chord_step (int step, int alter);
141 SCM make_simple_markup (SCM a);
142 bool is_duration (int t);
143 bool is_regular_identifier (SCM id);
144 bool ly_input_procedure_p (SCM x);
145 int yylex (YYSTYPE *s, YYLTYPE *loc, void *v);
146 void set_music_properties (Music *p, SCM a);
147
148 %}
149
150 /* The third option is an alias that will be used to display the
151    syntax error.  Bison CVS now correctly handles backslash escapes.
152
153    FIXME: Bison needs to translate some of these, eg, STRING.
154
155 */      
156    
157 /* Keyword tokens with plain escaped name.  */
158 %token ACCEPTS "\\accepts"
159 %token ADDLYRICS "\\addlyrics"
160 %token ADDQUOTE "\\addquote"
161 %token ALIAS "\\alias"
162 %token ALTERNATIVE "\\alternative"
163 %token BOOK "\\book"
164 %token CHANGE "\\change"
165 %token CHORDMODE "\\chordmode"
166 %token CHORDS "\\chords"
167 %token CONSISTS "\\consists"
168 %token CONTEXT "\\context"
169 %token DEFAULT "\\default"
170 %token DEFAULTCHILD "\\defaultchild"
171 %token DENIES "\\denies"
172 %token DESCRIPTION "\\description"
173 %token DRUMMODE "\\drummode"
174 %token DRUMS "\\drums"
175 %token FIGUREMODE "\\figuremode"
176 %token FIGURES "\\figures"
177 %token GROBDESCRIPTIONS "\\grobdescriptions"
178 %token HEADER "\\header"
179 %token INVALID "\\invalid"
180 %token KEY "\\key"
181 %token LAYOUT "\\layout"
182 %token LYRICMODE "\\lyricmode"
183 %token LYRICS "\\lyrics"
184 %token LYRICSTO "\\lyricsto"
185 %token MARK "\\mark"
186 %token MARKUP "\\markup"
187 %token MIDI "\\midi"
188 %token NAME "\\name"
189 %token NOTEMODE "\\notemode"
190 %token OBJECTID "\\objectid"    
191 %token OCTAVE "\\octave"
192 %token ONCE "\\once"
193 %token OVERRIDE "\\override"
194 %token PAPER "\\paper"
195 %token PARTIAL "\\partial"
196 %token RELATIVE "\\relative"
197 %token REMOVE "\\remove"
198 %token REPEAT "\\repeat"
199 %token REST "\\rest"
200 %token REVERT "\\revert"
201 %token SCORE "\\score"
202 %token SEQUENTIAL "\\sequential"
203 %token SET "\\set"
204 %token SIMULTANEOUS "\\simultaneous"
205 %token SKIP "\\skip"
206 %token TEMPO "\\tempo"
207 %token TIMES "\\times"
208 %token TRANSPOSE "\\transpose"
209 %token TYPE "\\type"
210 %token UNSET "\\unset"
211 %token WITH "\\with"
212
213 /* Keyword token exceptions.  */
214 %token TIME_T "\\time"
215 %token NEWCONTEXT "\\new"
216
217
218 /* Other string tokens.  */
219
220 %token CHORD_BASS "/+"
221 %token CHORD_CARET "^"
222 %token CHORD_COLON ":"
223 %token CHORD_MINUS "-"
224 %token CHORD_SLASH "/"
225 %token ANGLE_OPEN "<"
226 %token ANGLE_CLOSE ">"
227 %token DOUBLE_ANGLE_OPEN "<<"
228 %token DOUBLE_ANGLE_CLOSE ">>"
229 %token E_BACKSLASH "\\"
230 %token E_ANGLE_CLOSE "\\>"
231 %token E_CHAR "\\C[haracter]"
232 %token E_CLOSE "\\)"
233 %token E_EXCLAMATION "\\!"
234 %token E_BRACKET_OPEN "\\["
235 %token E_OPEN "\\("
236 %token E_BRACKET_CLOSE "\\]"
237 %token E_ANGLE_OPEN "\\<"
238 %token E_PLUS "\\+"
239 %token E_TILDE "\\~"
240 %token EXTENDER "__"
241
242 /*
243 If we give names, Bison complains.
244 */
245 %token FIGURE_CLOSE /* "\\>" */
246 %token FIGURE_OPEN /* "\\<" */
247 %token FIGURE_SPACE "_"
248 %token HYPHEN "--"
249
250 %token CHORDMODIFIERS
251 %token LYRIC_MARKUP
252 %token MULTI_MEASURE_REST
253 %token SCM_T
254
255
256 %token <i> DIGIT
257 %token <i> E_UNSIGNED
258 %token <i> UNSIGNED
259
260 %token <scm> BOOK_IDENTIFIER
261 %token <scm> CHORDMODIFIER_PITCH
262 %token <scm> CHORD_MODIFIER
263 %token <scm> CONTEXT_DEF_IDENTIFIER
264 %token <scm> DRUM_PITCH
265 %token <scm> DURATION_IDENTIFIER
266 %token <scm> EVENT_IDENTIFIER
267 %token <scm> FRACTION
268 %token <scm> LYRICS_STRING
269 %token <scm> LYRIC_MARKUP_IDENTIFIER
270 %token <scm> MARKUP_HEAD_EMPTY
271 %token <scm> MARKUP_HEAD_LIST0
272 %token <scm> MARKUP_HEAD_MARKUP0
273 %token <scm> MARKUP_HEAD_MARKUP0_MARKUP1
274 %token <scm> MARKUP_HEAD_SCM0
275 %token <scm> MARKUP_HEAD_SCM0_MARKUP1
276 %token <scm> MARKUP_HEAD_SCM0_SCM1
277 %token <scm> MARKUP_HEAD_SCM0_SCM1_MARKUP2
278 %token <scm> MARKUP_HEAD_SCM0_SCM1_SCM2
279 %token <scm> MARKUP_IDENTIFIER
280 %token <scm> MUSIC_FUNCTION
281 %token <scm> MUSIC_FUNCTION_MARKUP 
282 %token <scm> MUSIC_FUNCTION_MARKUP_MARKUP 
283 %token <scm> MUSIC_FUNCTION_MARKUP_MARKUP_MUSIC 
284 %token <scm> MUSIC_FUNCTION_MARKUP_MUSIC 
285 %token <scm> MUSIC_FUNCTION_MARKUP_MUSIC_MUSIC 
286 %token <scm> MUSIC_FUNCTION_MUSIC 
287 %token <scm> MUSIC_FUNCTION_MUSIC_MUSIC 
288 %token <scm> MUSIC_FUNCTION_SCM 
289 %token <scm> MUSIC_FUNCTION_SCM_MUSIC 
290 %token <scm> MUSIC_FUNCTION_SCM_MUSIC_MUSIC 
291 %token <scm> MUSIC_FUNCTION_SCM_SCM_MUSIC_MUSIC 
292 %token <scm> MUSIC_FUNCTION_SCM_SCM 
293 %token <scm> MUSIC_FUNCTION_SCM_SCM_MUSIC 
294 %token <scm> MUSIC_FUNCTION_SCM_SCM_SCM 
295 %token <scm> MUSIC_FUNCTION_SCM_SCM_SCM_MUSIC 
296 %token <scm> MUSIC_FUNCTION_SCM_SCM_SCM_SCM_MUSIC 
297 %token <scm> MUSIC_IDENTIFIER
298 %token <scm> NOTENAME_PITCH
299 %token <scm> NUMBER_IDENTIFIER
300 %token <scm> OUTPUT_DEF_IDENTIFIER
301 %token <scm> REAL
302 %token <scm> RESTNAME
303 %token <scm> SCM_IDENTIFIER
304 %token <scm> SCM_T
305 %token <scm> SCORE_IDENTIFIER
306 %token <scm> STRING
307 %token <scm> STRING_IDENTIFIER
308 %token <scm> TONICNAME_PITCH
309
310
311 %type <book> book_block
312 %type <book> book_body
313
314 %type <i> bare_unsigned
315 %type <i> figured_bass_alteration
316 %type <i> dots
317 %type <i> exclamations
318 %type <i> optional_rest
319 %type <i> questions
320 %type <i> script_dir
321 %type <i> sub_quotes
322 %type <i> sup_quotes
323 %type <i> tremolo_type
324
325 /* Music */
326 %type <scm> composite_music
327 %type <scm> grouped_music_list
328 %type <scm> music
329 %type <scm> prefix_composite_music
330 %type <scm> repeated_music
331 %type <scm> sequential_music
332 %type <scm> simple_music
333 %type <scm> simultaneous_music
334 %type <scm> chord_body
335 %type <scm> chord_body_element
336 %type <scm> command_element
337 %type <scm> command_event
338 %type <scm> context_change
339 %type <scm> direction_less_event
340 %type <scm> direction_reqd_event
341 %type <scm> event_chord
342 %type <scm> gen_text_def
343 %type <scm> music_property_def
344 %type <scm> note_chord_element
345 %type <scm> post_event
346 %type <scm> re_rhythmed_music
347 %type <scm> relative_music
348 %type <scm> simple_element
349 %type <scm> simple_music_property_def
350 %type <scm> string_number_event
351 %type <scm> tempo_event
352
353 %type <outputdef> output_def_body
354 %type <outputdef> output_def_head
355 %type <outputdef> output_def_head_with_mode_switch
356 %type <outputdef> output_def
357 %type <outputdef> paper_block 
358
359 %type <scm> alternative_music
360 %type <scm> generic_prefix_music_scm 
361 %type <scm> music_list
362 %type <scm> absolute_pitch
363 %type <scm> assignment_id
364 %type <scm> bare_number
365 %type <scm> music_function_event
366 %type <scm> music_function_chord_body
367 %type <scm> music_function_musicless_prefix
368 %type <scm> music_function_musicless_function
369 %type <scm> bass_figure
370 %type <scm> figured_bass_modification
371 %type <scm> br_bass_figure
372 %type <scm> bass_number
373 %type <scm> chord_body_elements
374 %type <scm> chord_item
375 %type <scm> chord_items
376 %type <scm> chord_separator
377 %type <scm> context_def_mod
378 %type <scm> context_def_spec_block
379 %type <scm> context_def_spec_body
380 %type <scm> context_mod
381 %type <scm> context_mod_list
382 %type <scm> context_prop_spec
383 %type <scm> direction_less_char
384 %type <scm> duration_length
385 %type <scm> embedded_scm
386 %type <scm> figure_list
387 %type <scm> figure_spec
388 %type <scm> fraction
389 %type <scm> full_markup
390 %type <scm> identifier_init
391 %type <scm> lilypond_header
392 %type <scm> lilypond_header_body
393 %type <scm> lyric_element
394 %type <scm> lyric_markup
395 %type <scm> markup
396 %type <scm> markup_braced_list
397 %type <scm> markup_braced_list_body 
398 %type <scm> markup_composed_list
399 %type <scm> markup_head_1_item
400 %type <scm> markup_head_1_list
401 %type <scm> markup_list
402 %type <scm> markup_top
403 %type <scm> mode_changing_head
404 %type <scm> mode_changing_head_with_context
405 %type <scm> multiplied_duration
406 %type <scm> new_chord
407 %type <scm> new_lyrics
408 %type <scm> number_expression
409 %type <scm> number_factor
410 %type <scm> number_term
411 %type <scm> object_id_setting
412 %type <scm> octave_check
413 %type <scm> optional_context_mod
414 %type <scm> optional_id
415 %type <scm> optional_notemode_duration
416 %type <scm> pitch
417 %type <scm> pitch_also_in_chords
418 %type <scm> post_events
419 %type <scm> property_operation
420 %type <scm> scalar
421 %type <scm> script_abbreviation
422 %type <scm> simple_chord_elements
423 %type <scm> simple_markup
424 %type <scm> simple_string
425 %type <scm> steno_duration
426 %type <scm> steno_pitch
427 %type <scm> steno_tonic_pitch
428 %type <scm> step_number
429 %type <scm> step_numbers 
430 %type <scm> string
431 %type <scm> function_scm_argument
432
433 %type <score> score_block
434 %type <score> score_body
435
436
437 %left '-' '+'
438
439 /* We don't assign precedence to / and *, because we might need varied
440 prec levels in different prods */
441
442 %left UNARY_MINUS
443
444 %%
445
446 lilypond:       /* empty */
447         | lilypond toplevel_expression {
448         }
449         | lilypond assignment {
450         }
451         | lilypond error {
452                 PARSER->error_level_ = 1;
453         }
454         | lilypond INVALID      {
455                 PARSER->error_level_ = 1;
456         }
457         ;
458
459
460 object_id_setting:
461         OBJECTID STRING { $$ = $2; } 
462         ;
463
464 toplevel_expression:
465         lilypond_header {
466                 PARSER->lexer_->set_identifier (ly_symbol2scm ("$defaultheader"), $1);
467         }
468         | book_block {
469                 Book *book = $1;
470                 SCM proc = PARSER->lexer_->lookup_identifier ("toplevel-book-handler");
471                 scm_call_2 (proc, PARSER->self_scm (), book->self_scm ());
472                 book->unprotect ();
473         }
474         | score_block {
475                 Score *score = $1;
476                 
477                 SCM proc = PARSER->lexer_->lookup_identifier ("toplevel-score-handler");
478                 scm_call_2 (proc, PARSER->self_scm (), score->self_scm ());
479                 score->unprotect ();
480         }
481         | composite_music {
482                 Music *music = unsmob_music ($1);
483                 SCM proc = PARSER->lexer_->lookup_identifier ("toplevel-music-handler");
484                 scm_call_2 (proc, PARSER->self_scm (), music->self_scm ());
485         }
486         | full_markup {
487                 SCM proc = PARSER->lexer_->lookup_identifier ("toplevel-text-handler");
488                 scm_call_2 (proc, PARSER->self_scm (), $1);
489         }
490         | output_def {
491                 SCM id = SCM_EOL;
492                 Output_def * od = $1;
493
494                 if ($1->c_variable ("is-paper") == SCM_BOOL_T)
495                         id = ly_symbol2scm ("$defaultpaper");
496                 else if ($1->c_variable ("is-midi") == SCM_BOOL_T)
497                         id = ly_symbol2scm ("$defaultmidi");
498                 else if ($1->c_variable ("is-layout") == SCM_BOOL_T)
499                         id = ly_symbol2scm ("$defaultlayout");
500
501                 PARSER->lexer_->set_identifier (id, od->self_scm ());
502                 od->unprotect();
503         }
504         ;
505
506 embedded_scm:
507         SCM_T
508         | SCM_IDENTIFIER
509         ;
510
511
512 lilypond_header_body:
513         {
514                 $$ = get_header(PARSER);
515                 PARSER->lexer_->add_scope ($$);
516         }
517         | lilypond_header_body assignment  {
518                 
519         }
520         ;
521
522 lilypond_header:
523         HEADER '{' lilypond_header_body '}'     {
524                 $$ = PARSER->lexer_->remove_scope ();
525         }
526         ;
527
528 /*
529         DECLARATIONS
530 */
531 assignment_id:
532         STRING          { $$ = $1; }
533         | LYRICS_STRING { $$ = $1; }
534         ;
535
536 assignment:
537         assignment_id '=' identifier_init  {
538                 if (! is_regular_identifier ($1))
539                 {
540 #if 0
541                         /* no longer valid with dashes in \paper{} block. */ 
542                         @1.warning (_ ("identifier should have alphabetic characters only"));
543 #endif
544                 }
545
546
547                 PARSER->lexer_->set_identifier ($1, $3);
548
549 /*
550  TODO: devise standard for protection in parser.
551
552   The parser stack lives on the C-stack, which means that
553 all objects can be unprotected as soon as they're here.
554
555 */
556         }
557         | embedded_scm { }
558         ;
559
560
561 identifier_init:
562         score_block {
563                 $$ = $1->self_scm ();
564                 $1->unprotect ();
565         }
566         | book_block {
567                 $$ = $1->self_scm ();
568                 $1->unprotect ();
569         }
570         | output_def {
571                 $$ = $1->self_scm ();
572                 $1->unprotect ();
573         }
574         | context_def_spec_block {
575                 $$ = $1;
576         }
577         | music  {
578                 /* Hack: Create event-chord around standalone events.
579                    Prevents the identifier from being interpreted as a post-event. */
580                 Music *mus = unsmob_music ($1);
581                 bool is_event = mus &&
582                         (scm_memq (ly_symbol2scm ("event"), mus->get_property ("types"))
583                                 != SCM_BOOL_F);
584                 if (!is_event)
585                         $$ = $1;
586                 else
587                         $$ = MAKE_SYNTAX ("event-chord", @$, scm_list_1 ($1));
588         }
589         | post_event {
590                 $$ = $1;
591         }
592         | number_expression {
593                 $$ = $1;
594         }
595         | string {
596                 $$ = $1;
597         }
598         | embedded_scm {
599                 $$ = $1;
600         }
601         | full_markup {
602                 $$ = $1;
603         }
604         | DIGIT {
605                 $$ = scm_from_int ($1);
606         }
607         ;
608
609 context_def_spec_block:
610         CONTEXT '{' context_def_spec_body '}'
611                 {
612                 $$ = $3;
613         }
614         ;
615
616 context_def_spec_body:
617         /**/ {
618                 $$ = Context_def::make_scm ();
619                 unsmob_context_def ($$)->set_spot (@$);
620         }
621         | CONTEXT_DEF_IDENTIFIER {
622                 $$ = $1;
623                 unsmob_context_def ($$)->set_spot (@$);
624         }
625         | context_def_spec_body GROBDESCRIPTIONS embedded_scm {
626                 Context_def*td = unsmob_context_def ($$);
627
628                 for (SCM p = $3; scm_is_pair (p); p = scm_cdr (p)) {
629                         SCM tag = scm_caar (p);
630
631                         /* TODO: should make new tag "grob-definition" ? */
632                         td->add_context_mod (scm_list_3 (ly_symbol2scm ("assign"),
633                                                         tag, scm_cons (scm_cdar (p), SCM_EOL)));
634                 }
635         }
636         | context_def_spec_body context_mod {
637                 unsmob_context_def ($$)->add_context_mod ($2);          
638         }
639         ;
640
641
642
643 book_block:
644         BOOK '{' book_body '}'  {
645                 $$ = $3;
646         }
647         ;
648
649 /* FIXME:
650    * Use 'handlers' like for toplevel-* stuff?
651    * grok \layout and \midi?  */
652 book_body:
653         {
654                 $$ = new Book;
655                 $$->set_spot (@$);
656                 $$->paper_ = dynamic_cast<Output_def*> (unsmob_output_def (PARSER->lexer_->lookup_identifier ("$defaultpaper"))->clone ());
657                 $$->paper_->unprotect ();
658                 $$->header_ = PARSER->lexer_->lookup_identifier ("$defaultheader"); 
659         }
660         | BOOK_IDENTIFIER {
661                 $$ = unsmob_book ($1);
662                 $$->set_spot (@$);
663         }
664         | book_body paper_block {
665                 $$->paper_ = $2;
666                 $2->unprotect ();
667         }
668         | book_body score_block {
669                 SCM s = $2->self_scm ();
670                 $$->add_score (s);
671                 $2->unprotect();
672         }
673         | book_body full_markup {
674                 $$->add_score ($2);
675         }
676         | book_body lilypond_header {
677                 $$->header_ = $2;
678         }
679         | book_body error {
680                 $$->paper_ = 0;
681                 $$->scores_ = SCM_EOL;
682         }
683         | book_body object_id_setting {
684                 $$->user_key_ = ly_scm2string ($2);
685         }
686         ;
687
688 score_block:
689         SCORE '{' score_body '}'        {
690                 $$ = $3;
691         }
692         ;
693
694 score_body:
695         music {
696                 SCM m = $1;
697                 SCM scorify = ly_lily_module_constant ("scorify-music");
698                 SCM score = scm_call_2 (scorify, m, PARSER->self_scm ());
699
700                 // pass ownernship to C++ again.
701                 $$ = unsmob_score (score);
702                 $$->protect ();
703                 $$->set_spot (@$);
704         }
705         | SCORE_IDENTIFIER {
706                 $$ = unsmob_score ($1);
707                 $$->set_spot (@$);
708         }
709         | score_body object_id_setting {
710                 $$->user_key_ = ly_scm2string ($2);
711         }
712         | score_body lilypond_header    {
713                 $$->header_ = $2;
714         }
715         | score_body output_def {
716                 if ($2->lookup_variable (ly_symbol2scm ("is-paper")) == SCM_BOOL_T)
717                 {
718                         PARSER->parser_error (@2, _("\\paper cannot be used in \\score, use \\layout instead"));
719                 
720                 }
721                 else
722                 {
723                         $$->add_output_def ($2);
724                 }
725                 $2->unprotect ();
726         }
727         | score_body error {
728                 $$->error_found_ = true;
729         }
730         ;
731
732
733 /*
734         OUTPUT DEF
735 */
736
737 paper_block:
738         output_def {
739                 $$ = $1;
740                 if ($$->lookup_variable (ly_symbol2scm ("is-paper")) != SCM_BOOL_T)
741                 {
742                         PARSER->parser_error (@1, _ ("need \\paper for paper block"));
743                         $$ = get_paper (PARSER);
744                 }
745         }
746         ;
747
748
749 output_def:
750         output_def_body '}' {
751                 $$ = $1;
752
753                 PARSER->lexer_->remove_scope ();
754                 PARSER->lexer_->pop_state ();
755         }
756         ;
757
758 output_def_head:
759         PAPER {
760                 $$ = get_paper (PARSER);
761                 $$->input_origin_ = @$;
762                 PARSER->lexer_->add_scope ($$->scope_);
763         }
764         | MIDI    {
765                 Output_def *p = get_midi (PARSER);
766                 $$ = p;
767                 PARSER->lexer_->add_scope (p->scope_);
768         }
769         | LAYOUT        {
770                 Output_def *p = get_layout (PARSER);
771
772                 PARSER->lexer_->add_scope (p->scope_);
773                 $$ = p;
774         }
775         ;
776
777 output_def_head_with_mode_switch:
778         output_def_head {
779                 PARSER->lexer_->push_initial_state ();
780                 $$ = $1;
781         }
782         ;
783
784 output_def_body:
785         output_def_head_with_mode_switch '{' {
786                 $$ = $1;
787                 $$->input_origin_.set_spot (@$);
788         }
789         | output_def_head_with_mode_switch '{' OUTPUT_DEF_IDENTIFIER    {
790                 $1->unprotect ();
791                 Output_def *o = unsmob_output_def ($3);
792                 o->input_origin_.set_spot (@$);
793                 $$ = o;
794                 PARSER->lexer_->remove_scope ();
795                 PARSER->lexer_->add_scope (o->scope_);
796         }
797         | output_def_body assignment  {
798
799         }
800         | output_def_body context_def_spec_block        {
801                 assign_context_def ($$, $2);
802         }
803         | output_def_body error {
804
805         }
806         ;
807
808 tempo_event:
809         TEMPO steno_duration '=' bare_unsigned  {
810                 $$ = MAKE_SYNTAX ("tempo", @$, $2, scm_int2num ($4));
811         }                               
812         ;
813
814 /*
815 The representation of a  list is the
816
817   (LIST . LAST-CONS)
818
819  to have efficient append.  */
820
821 music_list:
822         /* empty */ {
823                 $$ = scm_cons (SCM_EOL, SCM_EOL);
824         }
825         | music_list music {
826                 SCM s = $$;
827                 SCM c = scm_cons ($2, SCM_EOL);
828
829                 if (scm_is_pair (scm_cdr (s)))
830                         scm_set_cdr_x (scm_cdr (s), c); /* append */
831                 else
832                         scm_set_car_x (s, c); /* set first cons */
833                 scm_set_cdr_x (s, c);  /* remember last cell */
834         }
835         | music_list embedded_scm {
836
837         }
838         | music_list error {
839                 Music *m = MY_MAKE_MUSIC("Music");
840                 // ugh. code dup 
841                 m->set_property ("error-found", SCM_BOOL_T);
842                 SCM s = $$;
843                 SCM c = scm_cons (m->self_scm (), SCM_EOL);
844                 m->unprotect (); /* UGH */
845
846                 if (scm_is_pair (scm_cdr (s)))
847                         scm_set_cdr_x (scm_cdr (s), c); /* append */
848                 else
849                         scm_set_car_x (s, c); /* set first cons */
850                 scm_set_cdr_x (s, c);  /* remember last cell */
851         }
852         ;
853
854 music:
855         simple_music
856         | composite_music
857         ;
858
859 alternative_music:
860         /* empty */ {
861                 $$ = SCM_EOL;
862         }
863         | ALTERNATIVE '{' music_list '}' {
864                 $$ = scm_car ($3);
865         }
866         ;
867
868
869 repeated_music:
870         REPEAT simple_string bare_unsigned music alternative_music
871         {
872                 $$ = MAKE_SYNTAX ("repeat", @$, $2, scm_int2num ($3), $4, $5);
873         }
874         ;
875
876 sequential_music:
877         SEQUENTIAL '{' music_list '}'           {
878                 $$ = MAKE_SYNTAX ("sequential-music", @$, scm_car ($3));
879         }
880         | '{' music_list '}'            {
881                 $$ = MAKE_SYNTAX ("sequential-music", @$, scm_car ($2));
882         }
883         ;
884
885 simultaneous_music:
886         SIMULTANEOUS '{' music_list '}'{
887                 $$ = MAKE_SYNTAX ("simultaneous-music", @$, scm_car ($3));
888         }
889         | DOUBLE_ANGLE_OPEN music_list DOUBLE_ANGLE_CLOSE       {
890                 $$ = MAKE_SYNTAX ("simultaneous-music", @$, scm_car ($2));
891         }
892         ;
893
894 simple_music:
895         event_chord
896         | MUSIC_IDENTIFIER
897         | music_property_def
898         | context_change
899         ;
900
901 optional_context_mod:
902         /**/ { $$ = SCM_EOL; }
903         | WITH { PARSER->lexer_->push_initial_state (); }
904         '{' context_mod_list '}'
905         {
906                 PARSER->lexer_->pop_state ();
907                 $$ = $4;
908         }
909         ;
910
911 context_mod_list:
912         /* */  { $$ = SCM_EOL; }
913         | context_mod_list context_mod  {
914                  $$ = scm_cons ($2, $1);
915         }
916         ;
917
918 composite_music:
919         prefix_composite_music { $$ = $1; }
920         | grouped_music_list { $$ = $1; }
921         ;
922
923 grouped_music_list:
924         simultaneous_music              { $$ = $1; }
925         | sequential_music              { $$ = $1; }
926         ;
927
928 function_scm_argument:
929         embedded_scm  
930         | simple_string
931         ;
932
933 /*
934 TODO: use code generation for this
935 */
936 music_function_musicless_function:
937         MUSIC_FUNCTION {
938                 $$ = scm_list_2 ($1, make_input (@$));
939         }
940         | MUSIC_FUNCTION_SCM function_scm_argument {
941                 $$ = scm_list_3 ($1, make_input (@$), $2);
942         }
943         | MUSIC_FUNCTION_MARKUP full_markup {
944                 $$ = scm_list_3 ($1, make_input (@$), $2);
945         }
946         | MUSIC_FUNCTION_SCM_SCM function_scm_argument function_scm_argument {
947                 $$ = scm_list_4 ($1, make_input (@$), $2, $3);
948         }
949         | MUSIC_FUNCTION_SCM_SCM_SCM function_scm_argument function_scm_argument function_scm_argument {
950                 $$ = scm_list_5 ($1, make_input (@$), $2, $3, $4);
951         }
952         | MUSIC_FUNCTION_MARKUP_MARKUP full_markup full_markup {
953                 $$ = scm_list_4 ($1, make_input (@$), $2, $3);
954         }
955         ;
956
957 /*
958 TODO: use code generation for this
959 */
960 music_function_musicless_prefix:
961         MUSIC_FUNCTION_MUSIC {
962                 $$ = scm_list_2 ($1, make_input (@$));
963         }
964         | MUSIC_FUNCTION_SCM_MUSIC function_scm_argument { 
965                 $$ = scm_list_3 ($1, make_input (@$), $2);
966         }
967         | MUSIC_FUNCTION_SCM_SCM_MUSIC function_scm_argument function_scm_argument {
968                 $$ = scm_list_4 ($1, make_input (@$), $2, $3);
969         }
970         | MUSIC_FUNCTION_SCM_SCM_SCM_MUSIC function_scm_argument function_scm_argument function_scm_argument {
971                 $$ = scm_list_5 ($1, make_input (@$), $2, $3, $4);
972         }
973         | MUSIC_FUNCTION_SCM_SCM_SCM_SCM_MUSIC function_scm_argument function_scm_argument function_scm_argument function_scm_argument {
974                 $$ = scm_list_n ($1, make_input (@$), $2, $3, $4, $5, SCM_UNDEFINED);
975         }
976         | MUSIC_FUNCTION_MARKUP_MUSIC full_markup {
977                 $$ = scm_list_3 ($1, make_input (@$), $2);
978         }
979         ;
980
981 generic_prefix_music_scm:
982         music_function_musicless_function {
983                 $$ = $1;
984         }
985         | music_function_musicless_prefix music {
986                 $$ = ly_append2 ($1, scm_list_1 ($2));
987         }
988         | MUSIC_FUNCTION_MUSIC_MUSIC music music {
989                 $$ = scm_list_4 ($1, make_input (@$), $2, $3);
990         }
991         | MUSIC_FUNCTION_SCM_MUSIC_MUSIC function_scm_argument music music {
992                 $$ = scm_list_5 ($1, make_input (@$), $2, $3, $4);
993         }
994         | MUSIC_FUNCTION_SCM_SCM_MUSIC_MUSIC function_scm_argument function_scm_argument music music {
995                 $$ = scm_list_n ($1, make_input (@$), $2, $3, $4, $5, SCM_UNDEFINED);
996         }
997         | MUSIC_FUNCTION_MARKUP_MUSIC_MUSIC full_markup music music {
998                 $$ = scm_list_5 ($1, make_input (@$), $2, $3, $4);
999         }
1000         ;
1001
1002
1003 optional_id:
1004         /**/ { $$ = SCM_EOL; }
1005         | '=' simple_string {
1006                 $$ = $2;
1007         }
1008         ;       
1009
1010
1011 prefix_composite_music:
1012         generic_prefix_music_scm {
1013                 $$ = run_music_function (PARSER, $1);
1014         }
1015         | CONTEXT    simple_string optional_id optional_context_mod music {
1016                 $$ = MAKE_SYNTAX ("context-specification", @$, $2, $3, $5, $4, SCM_BOOL_F);
1017         }
1018         | NEWCONTEXT simple_string optional_id optional_context_mod music {
1019                 $$ = MAKE_SYNTAX ("context-specification", @$, $2, $3, $5, $4, SCM_BOOL_T);
1020         }
1021
1022         | TIMES fraction music {
1023                 $$ = MAKE_SYNTAX ("time-scaled-music", @$, $2, $3);
1024         }
1025         | repeated_music                { $$ = $1; }
1026         | TRANSPOSE pitch_also_in_chords pitch_also_in_chords music {
1027                 Pitch from = *unsmob_pitch ($2);
1028                 Pitch to = *unsmob_pitch ($3);
1029                 SCM pitch = pitch_interval (from, to).smobbed_copy ();
1030                 $$ = MAKE_SYNTAX ("transpose-music", @$, pitch, $4);
1031         }
1032         | mode_changing_head grouped_music_list {
1033                 if ($1 == ly_symbol2scm ("chords"))
1034                 {
1035                   $$ = MAKE_SYNTAX ("unrelativable-music", @$, $2);
1036                 }
1037                 else
1038                 {
1039                   $$ = $2;
1040                 }
1041                 PARSER->lexer_->pop_state ();
1042         }
1043         | mode_changing_head_with_context optional_context_mod grouped_music_list {
1044                 $$ = MAKE_SYNTAX ("context-specification", @$, $1, SCM_EOL, $3, $2, SCM_BOOL_T);
1045                 if ($1 == ly_symbol2scm ("ChordNames"))
1046                 {
1047                   $$ = MAKE_SYNTAX ("unrelativable-music", @$, $$);
1048                 }
1049                 PARSER->lexer_->pop_state ();
1050         }
1051         | relative_music        { $$ = $1; }
1052         | re_rhythmed_music     { $$ = $1; }
1053         ;
1054
1055 mode_changing_head: 
1056         NOTEMODE {
1057                 SCM nn = PARSER->lexer_->lookup_identifier ("pitchnames");
1058                 PARSER->lexer_->push_note_state (alist_to_hashq (nn));
1059
1060                 $$ = ly_symbol2scm ("notes");
1061         }
1062         | DRUMMODE 
1063                 {
1064                 SCM nn = PARSER->lexer_->lookup_identifier ("drumPitchNames");
1065                 PARSER->lexer_->push_note_state (alist_to_hashq (nn));
1066
1067                 $$ = ly_symbol2scm ("drums");
1068         }
1069         | FIGUREMODE {
1070                 PARSER->lexer_->push_figuredbass_state ();
1071
1072                 $$ = ly_symbol2scm ("figures");
1073         }
1074         | CHORDMODE {
1075                 SCM nn = PARSER->lexer_->lookup_identifier ("chordmodifiers");
1076                 PARSER->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
1077                 nn = PARSER->lexer_->lookup_identifier ("pitchnames");
1078                 PARSER->lexer_->push_chord_state (alist_to_hashq (nn));
1079                 $$ = ly_symbol2scm ("chords");
1080
1081         }
1082         | LYRICMODE
1083                 { PARSER->lexer_->push_lyric_state ();
1084                 $$ = ly_symbol2scm ("lyrics");
1085         }
1086         ;
1087
1088 mode_changing_head_with_context: 
1089         DRUMS {
1090                 SCM nn = PARSER->lexer_->lookup_identifier ("drumPitchNames");
1091                 PARSER->lexer_->push_note_state (alist_to_hashq (nn));
1092
1093                 $$ = ly_symbol2scm ("DrumStaff");
1094         }
1095         | FIGURES {
1096                 PARSER->lexer_->push_figuredbass_state ();
1097
1098                 $$ = ly_symbol2scm ("FiguredBass");
1099         }
1100         | CHORDS {
1101                 SCM nn = PARSER->lexer_->lookup_identifier ("chordmodifiers");
1102                 PARSER->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
1103                 nn = PARSER->lexer_->lookup_identifier ("pitchnames");
1104                 PARSER->lexer_->push_chord_state (alist_to_hashq (nn));
1105                 $$ = ly_symbol2scm ("ChordNames");
1106         }
1107         | LYRICS
1108                 { PARSER->lexer_->push_lyric_state ();
1109                 $$ = ly_symbol2scm ("Lyrics");
1110         }
1111         ;
1112
1113
1114 relative_music:
1115         RELATIVE absolute_pitch music {
1116                 Pitch start = *unsmob_pitch ($2);
1117                 $$ = make_music_relative (start, $3);
1118         }
1119         | RELATIVE composite_music {
1120                 Pitch middle_c (0, 0, 0);
1121                 $$ = make_music_relative (middle_c, $2);
1122         }
1123         ;
1124
1125 new_lyrics:
1126         ADDLYRICS { PARSER->lexer_->push_lyric_state (); }
1127         /*cont */
1128         grouped_music_list {
1129         /* Can also use music at the expensive of two S/Rs similar to
1130            \repeat \alternative */
1131                 PARSER->lexer_->pop_state ();
1132
1133                 $$ = scm_cons ($3, SCM_EOL);
1134         }
1135         | new_lyrics ADDLYRICS {
1136                 PARSER->lexer_->push_lyric_state ();
1137         } grouped_music_list {
1138                 PARSER->lexer_->pop_state ();
1139                 $$ = scm_cons ($4, $1);
1140         }
1141         ;
1142
1143 re_rhythmed_music:
1144         grouped_music_list new_lyrics {
1145                 $$ = MAKE_SYNTAX ("add-lyrics", @$, $1, scm_reverse_x ($2, SCM_EOL));
1146         }
1147         | LYRICSTO simple_string {
1148                 PARSER->lexer_->push_lyric_state ();
1149         } music {
1150                 PARSER->lexer_->pop_state ();
1151                 $$ = MAKE_SYNTAX ("lyric-combine", @$, $2, $4);
1152         }
1153         ;
1154
1155 context_change:
1156         CHANGE STRING '=' STRING  {
1157                 $$ = MAKE_SYNTAX ("context-change", @$, scm_string_to_symbol ($2), $4);
1158         }
1159         ;
1160
1161 property_operation:
1162         STRING '=' scalar {
1163                 $$ = scm_list_3 (ly_symbol2scm ("assign"),
1164                         scm_string_to_symbol ($1), $3);
1165         }
1166         | UNSET simple_string {
1167                 $$ = scm_list_2 (ly_symbol2scm ("unset"),
1168                         scm_string_to_symbol ($2));
1169         }
1170         | OVERRIDE simple_string embedded_scm '=' embedded_scm {
1171                 $$ = scm_list_4 (ly_symbol2scm ("push"),
1172                         scm_string_to_symbol ($2), $5, $3);
1173         }
1174         | OVERRIDE simple_string embedded_scm embedded_scm '=' embedded_scm {
1175                 $$ = scm_list_5 (ly_symbol2scm ("push"),
1176                                 scm_string_to_symbol ($2), $6, $4, $3);
1177         }
1178         | REVERT simple_string embedded_scm {
1179                 $$ = scm_list_3 (ly_symbol2scm ("pop"),
1180                         scm_string_to_symbol ($2), $3);
1181         }
1182         ;
1183
1184 context_def_mod:
1185         CONSISTS { $$ = ly_symbol2scm ("consists"); }
1186         | REMOVE { $$ = ly_symbol2scm ("remove"); }
1187
1188         | ACCEPTS { $$ = ly_symbol2scm ("accepts"); }
1189         | DEFAULTCHILD { $$ = ly_symbol2scm ("default-child"); }
1190         | DENIES { $$ = ly_symbol2scm ("denies"); }
1191
1192         | ALIAS { $$ = ly_symbol2scm ("alias"); }
1193         | TYPE { $$ = ly_symbol2scm ("translator-type"); }
1194         | DESCRIPTION { $$ = ly_symbol2scm ("description"); }
1195         | NAME { $$ = ly_symbol2scm ("context-name"); }
1196         ;
1197
1198 context_mod:
1199         property_operation { $$ = $1; }
1200         | context_def_mod STRING {
1201                 $$ = scm_list_2 ($1, $2);
1202         }
1203         ;
1204
1205 context_prop_spec:
1206         simple_string {
1207                 if (!is_regular_identifier ($1))
1208                 {
1209                         @$.error (_("Grob name should be alphanumeric"));
1210                 }
1211
1212                 $$ = scm_list_2 (ly_symbol2scm ("Bottom"),
1213                         scm_string_to_symbol ($1));
1214         }
1215         | simple_string '.' simple_string {
1216                 $$ = scm_list_2 (scm_string_to_symbol ($1),
1217                         scm_string_to_symbol ($3));
1218         }
1219         ;
1220
1221 simple_music_property_def:
1222         OVERRIDE context_prop_spec embedded_scm '=' scalar {
1223                 $$ = scm_list_5 (scm_car ($2),
1224                         ly_symbol2scm ("OverrideProperty"),
1225                         scm_cadr ($2),
1226                         $5, $3);
1227         }
1228         | OVERRIDE context_prop_spec embedded_scm  embedded_scm '=' scalar {
1229                 $$ = scm_list_n (scm_car ($2),                  
1230                         ly_symbol2scm ("OverrideProperty"),
1231                         scm_cadr ($2),
1232                         $6, $4, $3, SCM_UNDEFINED);
1233         }
1234         | REVERT context_prop_spec embedded_scm {
1235                 $$ = scm_list_4 (scm_car ($2),
1236                         ly_symbol2scm ("RevertProperty"),
1237                         scm_cadr ($2),
1238                         $3);
1239         }
1240         | SET context_prop_spec '=' scalar {
1241                 $$ = scm_list_4 (scm_car ($2),
1242                         ly_symbol2scm ("PropertySet"),
1243                         scm_cadr ($2),
1244                         $4);
1245         }
1246         | UNSET context_prop_spec {
1247                 $$ = scm_list_3 (scm_car ($2),
1248                         ly_symbol2scm ("PropertyUnset"),
1249                         scm_cadr ($2));
1250         }
1251         ;
1252
1253 music_property_def:
1254         simple_music_property_def {
1255                 $$ = LOWLEVEL_MAKE_SYNTAX (ly_lily_module_constant ("property-operation"), scm_cons (PARSER->self_scm (), scm_cons2 (make_input (@$), SCM_BOOL_F, $1)));
1256         }
1257         | ONCE simple_music_property_def {
1258                 $$ = LOWLEVEL_MAKE_SYNTAX (ly_lily_module_constant ("property-operation"), scm_cons (PARSER->self_scm (), scm_cons2 (make_input (@$), SCM_BOOL_T, $2)));
1259         }
1260         ;
1261
1262 string:
1263         STRING {
1264                 $$ = $1;
1265         }
1266         | STRING_IDENTIFIER {
1267                 $$ = $1;
1268         }
1269         | string '+' string {
1270                 $$ = scm_string_append (scm_list_2 ($1, $3));
1271         }
1272         ;
1273
1274 simple_string: STRING {
1275                 $$ = $1;
1276         }
1277         | LYRICS_STRING {
1278                 $$ = $1;
1279         }
1280         | STRING_IDENTIFIER {
1281                 $$ = $1;
1282         }
1283         ;
1284
1285 scalar: string {
1286                 $$ = $1;
1287         }
1288         | LYRICS_STRING {
1289                 $$ = $1;
1290         }
1291         | bare_number {
1292                 $$ = $1;
1293         }
1294         | embedded_scm {
1295                 $$ = $1;
1296         }
1297         | full_markup {
1298                 $$ = $1;
1299         }
1300         | DIGIT {
1301                 $$ = scm_from_int ($1);
1302         }
1303         ;
1304
1305 event_chord:
1306         /* TODO: Create a special case that avoids the creation of
1307            EventChords around simple_elements that have no post_events?
1308          */
1309         simple_chord_elements post_events       {
1310                 SCM elts = ly_append2 ($1, scm_reverse_x ($2, SCM_EOL));
1311
1312                 Input i;
1313                 /* why is this giving wrong start location? -ns
1314                  * i = @$; */
1315                 i.set_location (@1, @2);
1316                 $$ = MAKE_SYNTAX ("event-chord", i, elts);
1317         }
1318         | MULTI_MEASURE_REST optional_notemode_duration post_events {
1319                 Input i;
1320                 i.set_location (@1, @3);
1321                 $$ = MAKE_SYNTAX ("multi-measure-rest", i, $2, $3);
1322         }
1323         | command_element
1324         | note_chord_element
1325         ;
1326
1327
1328 note_chord_element:
1329         chord_body optional_notemode_duration post_events
1330         {
1331                 Music *m = unsmob_music ($1);
1332                 SCM dur = unsmob_duration ($2)->smobbed_copy ();
1333                 SCM es = m->get_property ("elements");
1334                 SCM postevs = scm_reverse_x ($3, SCM_EOL);
1335                 
1336                 for (SCM s = es; scm_is_pair (s); s = scm_cdr (s))
1337                   unsmob_music (scm_car (s))->set_property ("duration", dur);
1338                 es = ly_append2 (es, postevs);
1339
1340                 m-> set_property ("elements", es);
1341                 m->set_spot (@$);
1342                 $$ = m->self_scm ();
1343         }
1344         ;
1345
1346 chord_body:
1347         ANGLE_OPEN chord_body_elements ANGLE_CLOSE
1348         {
1349                 $$ = MAKE_SYNTAX ("event-chord", @$, scm_reverse_x ($2, SCM_EOL));
1350         }
1351         ;
1352
1353 chord_body_elements:
1354         /* empty */             { $$ = SCM_EOL; }
1355         | chord_body_elements chord_body_element {
1356                 $$ = scm_cons ($2, $1);
1357         }
1358         ;
1359
1360 chord_body_element:
1361         pitch exclamations questions octave_check post_events
1362         {
1363                 int q = $3;
1364                 int ex = $2;
1365                 SCM check = $4;
1366                 SCM post = $5;
1367
1368                 Music *n = MY_MAKE_MUSIC ("NoteEvent");
1369                 n->set_property ("pitch", $1);
1370                 n->set_spot (@$);
1371                 if (q % 2)
1372                         n->set_property ("cautionary", SCM_BOOL_T);
1373                 if (ex % 2 || q % 2)
1374                         n->set_property ("force-accidental", SCM_BOOL_T);
1375
1376                 if (scm_is_pair (post)) {
1377                         SCM arts = scm_reverse_x (post, SCM_EOL);
1378                         n->set_property ("articulations", arts);
1379                 }
1380                 if (scm_is_number (check))
1381                 {
1382                         int q = scm_to_int (check);
1383                         n->set_property ("absolute-octave", scm_from_int (q-1));
1384                 }
1385
1386                 $$ = n->unprotect ();
1387         }
1388         | DRUM_PITCH post_events {
1389                 Music *n = MY_MAKE_MUSIC ("NoteEvent");
1390                 n->set_property ("duration", $2);
1391                 n->set_property ("drum-type", $1);
1392                 n->set_spot (@$);
1393
1394                 if (scm_is_pair ($2)) {
1395                         SCM arts = scm_reverse_x ($2, SCM_EOL);
1396                         n->set_property ("articulations", arts);
1397                 }
1398                 $$ = n->unprotect ();
1399         }
1400         | music_function_chord_body { 
1401                 $$ = run_music_function (PARSER, $1);
1402         }
1403         ;
1404
1405 music_function_chord_body:
1406         music_function_musicless_function {
1407                 $$ = $1;
1408         }
1409         | music_function_musicless_prefix chord_body_element {
1410                 $$ = ly_append2 ($1, scm_list_1 ($2));
1411         }
1412         ;
1413
1414 music_function_event:
1415         music_function_musicless_prefix post_event {
1416                 $$ = ly_append2 ($1, scm_list_1 ($2));
1417         }
1418         | music_function_musicless_function {
1419                 $$ = $1;
1420         }
1421         ;
1422         
1423
1424 command_element:
1425         command_event {
1426                 $$ = $1;
1427         }
1428         | SKIP duration_length {
1429                 $$ = MAKE_SYNTAX ("skip-music", @$, $2);
1430         }
1431         | E_BRACKET_OPEN {
1432                 Music *m = MY_MAKE_MUSIC ("LigatureEvent");
1433                 m->set_property ("span-direction", scm_from_int (START));
1434                 m->set_spot (@$);
1435                 $$ = m->unprotect();
1436         }
1437         | E_BRACKET_CLOSE {
1438                 Music *m = MY_MAKE_MUSIC ("LigatureEvent");
1439                 m->set_property ("span-direction", scm_from_int (STOP));
1440                 m->set_spot (@$);
1441                 $$ = m->unprotect ();
1442         }
1443         | E_BACKSLASH {
1444                 $$ = MAKE_SYNTAX ("voice-separator", @$, SCM_UNDEFINED);
1445         }
1446         | '|'      {
1447                 SCM pipe = PARSER->lexer_->lookup_identifier ("pipeSymbol");
1448
1449                 Music *m = unsmob_music (pipe);
1450                 if (m)
1451                 {
1452                         m = m->clone ();
1453                         m->set_spot (@$);
1454                         $$ = m->unprotect ();
1455                 }
1456                 else
1457                         $$ = MAKE_SYNTAX ("bar-check", @$, SCM_UNDEFINED);
1458
1459         }
1460         | PARTIAL duration_length       {
1461                 Moment m = - unsmob_duration ($2)->get_length ();
1462                 $$ = MAKE_SYNTAX ("property-operation", @$, SCM_BOOL_F, ly_symbol2scm ("Timing"), ly_symbol2scm ("PropertySet"), ly_symbol2scm ("measurePosition"), m.smobbed_copy ());
1463                 $$ = MAKE_SYNTAX ("context-specification", @$, ly_symbol2scm ("Score"), SCM_BOOL_F, $$, SCM_EOL, SCM_BOOL_F);
1464         }
1465
1466         | TIME_T fraction  {
1467                 SCM proc = ly_lily_module_constant ("make-time-signature-set");
1468
1469                 $$ = scm_apply_2   (proc, scm_car ($2), scm_cdr ($2), SCM_EOL);
1470         }
1471         | MARK scalar {
1472                 SCM proc = ly_lily_module_constant ("make-mark-set");
1473
1474                 $$ = scm_call_1 (proc, $2);
1475         }
1476         ;
1477
1478 command_event:
1479         E_TILDE {
1480                 $$ = MY_MAKE_MUSIC ("PesOrFlexaEvent")->unprotect ();
1481         }
1482         | MARK DEFAULT  {
1483                 Music *m = MY_MAKE_MUSIC ("MarkEvent");
1484                 $$ = m->unprotect ();
1485         }
1486         | tempo_event {
1487                 $$ = $1;
1488         }
1489         | KEY DEFAULT {
1490                 Music *key = MY_MAKE_MUSIC ("KeyChangeEvent");
1491                 $$ = key->unprotect ();
1492         }
1493         | KEY NOTENAME_PITCH SCM_IDENTIFIER     {
1494
1495                 Music *key = MY_MAKE_MUSIC ("KeyChangeEvent");
1496                 if (scm_ilength ($3) > 0)
1497                 {               
1498                         key->set_property ("pitch-alist", $3);
1499                         key->set_property ("tonic", Pitch (0, 0, 0).smobbed_copy ());
1500                         key->transpose (* unsmob_pitch ($2));
1501                 } else {
1502                         PARSER->parser_error (@3, _ ("second argument must be pitch list"));
1503                 }
1504
1505                 $$ = key->unprotect ();
1506         }
1507         ;
1508
1509
1510 post_events:
1511         /* empty */ {
1512                 $$ = SCM_EOL;
1513         }
1514         | post_events post_event {
1515                 unsmob_music ($2)->set_spot (@2);
1516                 $$ = scm_cons ($2, $$);
1517         }
1518         ;
1519         
1520 post_event:
1521         direction_less_event {
1522                 $$ = $1;
1523         }
1524         | '-' music_function_event {
1525                 $$ = run_music_function (PARSER, $2);
1526         }
1527         | HYPHEN {
1528                 if (!PARSER->lexer_->is_lyric_state ())
1529                         PARSER->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
1530                 $$ = MY_MAKE_MUSIC ("HyphenEvent")->unprotect ();
1531         }
1532         | EXTENDER {
1533                 if (!PARSER->lexer_->is_lyric_state ())
1534                         PARSER->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
1535                 $$ = MY_MAKE_MUSIC ("ExtenderEvent")->unprotect ();
1536         }
1537         | script_dir direction_reqd_event {
1538                 if ($1)
1539                 {
1540                         Music *m = unsmob_music ($2);
1541                         m->set_property ("direction", scm_from_int ($1));
1542                 }
1543                 $$ = $2;
1544         }
1545         | script_dir direction_less_event {
1546                 if ($1)
1547                 {
1548                         Music *m = unsmob_music ($2);
1549                         m->set_property ("direction", scm_from_int ($1));
1550                 }
1551                 $$ = $2;
1552         }
1553         | string_number_event
1554         ;
1555
1556 string_number_event:
1557         E_UNSIGNED {
1558                 Music *s = MY_MAKE_MUSIC ("StringNumberEvent");
1559                 s->set_property ("string-number", scm_from_int ($1));
1560                 s->set_spot (@$);
1561                 $$ = s->unprotect ();
1562         }
1563         ;
1564
1565 direction_less_char:
1566         '['  {
1567                 $$ = ly_symbol2scm ("bracketOpenSymbol");
1568         }
1569         | ']'  {
1570                 $$ = ly_symbol2scm ("bracketCloseSymbol"); 
1571         }
1572         | '~'  {
1573                 $$ = ly_symbol2scm ("tildeSymbol");
1574         }
1575         | '('  {
1576                 $$ = ly_symbol2scm ("parenthesisOpenSymbol");
1577         }
1578         | ')'  {
1579                 $$ = ly_symbol2scm ("parenthesisCloseSymbol");
1580         }
1581         | E_EXCLAMATION  {
1582                 $$ = ly_symbol2scm ("escapedExclamationSymbol");
1583         }
1584         | E_OPEN  {
1585                 $$ = ly_symbol2scm ("escapedParenthesisOpenSymbol");
1586         }
1587         | E_CLOSE  {
1588                 $$ = ly_symbol2scm ("escapedParenthesisCloseSymbol");
1589         }
1590         | E_ANGLE_CLOSE  {
1591                 $$ = ly_symbol2scm ("escapedBiggerSymbol");
1592         }
1593         | E_ANGLE_OPEN  {
1594                 $$ = ly_symbol2scm ("escapedSmallerSymbol");
1595         }
1596         ;
1597
1598 direction_less_event:
1599         direction_less_char {
1600                 SCM predefd = PARSER->lexer_->lookup_identifier_symbol ($1);
1601                 Music *m = 0;
1602                 if (unsmob_music (predefd))
1603                 {
1604                         m = unsmob_music (predefd)->clone ();
1605                 }
1606                 else
1607                 {
1608                         m = MY_MAKE_MUSIC ("Music");
1609                 }
1610                 m->set_spot (@$);
1611                 $$ = m->unprotect ();
1612         }
1613         | EVENT_IDENTIFIER      {
1614                 $$ = $1;
1615         }
1616         | tremolo_type  {
1617                Music *a = MY_MAKE_MUSIC ("TremoloEvent");
1618                a->set_spot (@$);
1619                a->set_property ("tremolo-type", scm_from_int ($1));
1620                $$ = a->unprotect ();
1621         }
1622         ;       
1623         
1624 direction_reqd_event:
1625         gen_text_def {
1626                 $$ = $1;
1627         }
1628         | script_abbreviation {
1629                 SCM s = PARSER->lexer_->lookup_identifier ("dash" + ly_scm2string ($1));
1630                 Music *a = MY_MAKE_MUSIC ("ArticulationEvent");
1631                 if (scm_is_string (s))
1632                         a->set_property ("articulation-type", s);
1633                 else PARSER->parser_error (@1, _ ("expecting string as script definition"));
1634                 $$ = a->unprotect ();
1635         }
1636         ;
1637
1638 octave_check:
1639         /**/ { $$ = SCM_EOL; }
1640         | '='  { $$ = scm_from_int (0); }
1641         | '=' sub_quotes { $$ = scm_from_int ($2); }
1642         | '=' sup_quotes { $$ = scm_from_int ($2); }
1643         ;
1644
1645 sup_quotes:
1646         '\'' {
1647                 $$ = 1;
1648         }
1649         | sup_quotes '\'' {
1650                 $$ ++;
1651         }
1652         ;
1653
1654 sub_quotes:
1655         ',' {
1656                 $$ = 1;
1657         }
1658         | sub_quotes ',' {
1659                 $$++;
1660         }
1661         ;
1662
1663 steno_pitch:
1664         NOTENAME_PITCH  {
1665                 $$ = $1;
1666         }
1667         | NOTENAME_PITCH sup_quotes     {
1668                 Pitch p = *unsmob_pitch ($1);
1669                 p = p.transposed (Pitch ($2,0,0));
1670                 $$ = p.smobbed_copy ();
1671         }
1672         | NOTENAME_PITCH sub_quotes      {
1673                 Pitch p =* unsmob_pitch ($1);
1674                 p = p.transposed (Pitch (-$2,0,0));
1675                 $$ = p.smobbed_copy ();
1676         }
1677         ;
1678
1679 /*
1680 ugh. duplication
1681 */
1682
1683 steno_tonic_pitch:
1684         TONICNAME_PITCH {
1685                 $$ = $1;
1686         }
1687         | TONICNAME_PITCH sup_quotes    {
1688                 Pitch p = *unsmob_pitch ($1);
1689                 p = p.transposed (Pitch ($2,0,0));
1690                 $$ = p.smobbed_copy ();
1691         }
1692         | TONICNAME_PITCH sub_quotes     {
1693                 Pitch p =* unsmob_pitch ($1);
1694
1695                 p = p.transposed (Pitch (-$2,0,0));
1696                 $$ = p.smobbed_copy ();
1697         }
1698         ;
1699
1700 pitch:
1701         steno_pitch {
1702                 $$ = $1;
1703         }
1704         ;
1705
1706 pitch_also_in_chords:
1707         pitch
1708         | steno_tonic_pitch
1709         ;
1710
1711 gen_text_def:
1712         full_markup {
1713                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent");
1714                 t->set_property ("text", $1);
1715                 t->set_spot (@$);
1716                 $$ = t->unprotect ();
1717         }
1718         | string {
1719                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent");
1720                 t->set_property ("text",
1721                         make_simple_markup ($1));
1722                 t->set_spot (@$);
1723                 $$ = t->unprotect ();
1724         }
1725         | DIGIT {
1726                 Music *t = MY_MAKE_MUSIC ("FingeringEvent");
1727                 t->set_property ("digit", scm_from_int ($1));
1728                 t->set_spot (@$);
1729                 $$ = t->unprotect ();
1730         }
1731         ;
1732
1733 script_abbreviation:
1734         '^'             {
1735                 $$ = scm_makfrom0str ("Hat");
1736         }
1737         | '+'           {
1738                 $$ = scm_makfrom0str ("Plus");
1739         }
1740         | '-'           {
1741                 $$ = scm_makfrom0str ("Dash");
1742         }
1743         | '|'           {
1744                 $$ = scm_makfrom0str ("Bar");
1745         }
1746         | ANGLE_CLOSE   {
1747                 $$ = scm_makfrom0str ("Larger");
1748         }
1749         | '.'           {
1750                 $$ = scm_makfrom0str ("Dot");
1751         }
1752         | '_' {
1753                 $$ = scm_makfrom0str ("Underscore");
1754         }
1755         ;
1756
1757 script_dir:
1758         '_'     { $$ = DOWN; }
1759         | '^'   { $$ = UP; }
1760         | '-'   { $$ = CENTER; }
1761         ;
1762
1763
1764 absolute_pitch:
1765         steno_pitch     {
1766                 $$ = $1;
1767         }
1768         ;
1769
1770 duration_length:
1771         multiplied_duration {
1772                 $$ = $1;
1773         }
1774         ;
1775
1776 optional_notemode_duration:
1777         {
1778                 Duration dd = PARSER->default_duration_;
1779                 $$ = dd.smobbed_copy ();
1780         }
1781         | multiplied_duration   {
1782                 $$ = $1;
1783                 PARSER->default_duration_ = *unsmob_duration ($$);
1784         }
1785         ;
1786
1787 steno_duration:
1788         bare_unsigned dots              {
1789                 int len = 0;
1790                 if (!is_duration ($1))
1791                         PARSER->parser_error (@1, _f ("not a duration: %d", $1));
1792                 else
1793                         len = intlog2 ($1);
1794
1795                 $$ = Duration (len, $2).smobbed_copy ();
1796         }
1797         | DURATION_IDENTIFIER dots      {
1798                 Duration *d = unsmob_duration ($1);
1799                 Duration k (d->duration_log (), d->dot_count () + $2);
1800                 *d = k;
1801                 $$ = $1;
1802         }
1803         ;
1804
1805 multiplied_duration:
1806         steno_duration {
1807                 $$ = $1;
1808         }
1809         | multiplied_duration '*' bare_unsigned {
1810                 $$ = unsmob_duration ($$)->compressed ( $3) .smobbed_copy ();
1811         }
1812         | multiplied_duration '*' FRACTION {
1813                 Rational  m (scm_to_int (scm_car ($3)), scm_to_int (scm_cdr ($3)));
1814
1815                 $$ = unsmob_duration ($$)->compressed (m).smobbed_copy ();
1816         }
1817         ;
1818
1819 fraction:
1820         FRACTION { $$ = $1; }
1821         | UNSIGNED '/' UNSIGNED {
1822                 $$ = scm_cons (scm_from_int ($1), scm_from_int ($3));
1823         }
1824         ;
1825
1826 dots:
1827         /* empty */     {
1828                 $$ = 0;
1829         }
1830         | dots '.' {
1831                 $$ ++;
1832         }
1833         ;
1834
1835 tremolo_type:
1836         ':'     {
1837                 $$ = 0;
1838         }
1839         | ':' bare_unsigned {
1840                 if (!is_duration ($2))
1841                         PARSER->parser_error (@2, _f ("not a duration: %d", $2));
1842                 $$ = $2;
1843         }
1844         ;
1845
1846 bass_number:
1847         DIGIT   {
1848                 $$ = scm_from_int ($1);
1849         }
1850         | UNSIGNED {
1851                 $$ = scm_from_int ($1);
1852         }
1853         | STRING { $$ = $1; }
1854         | full_markup { $$ = $1; }
1855         ;
1856
1857 figured_bass_alteration:
1858         '-'     { $$ = -2; }
1859         | '+'   { $$ = 2; }
1860         | '!'   { $$ = 0; }
1861         ;
1862
1863 bass_figure:
1864         FIGURE_SPACE {
1865                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent");
1866                 $$ = bfr->self_scm ();
1867                 bfr->unprotect ();
1868                 bfr->set_spot (@1);
1869         }
1870         | bass_number  {
1871                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent");
1872                 $$ = bfr->self_scm ();
1873
1874                 if (scm_is_number ($1))
1875                         bfr->set_property ("figure", $1);
1876                 else if (Text_interface::is_markup ($1))
1877                         bfr->set_property ("text", $1);
1878
1879                 bfr->unprotect ();
1880                 bfr->set_spot (@1);
1881         }
1882         | bass_figure ']' {
1883                 $$ = $1;
1884                 unsmob_music ($1)->set_property ("bracket-stop", SCM_BOOL_T);
1885         }
1886         | bass_figure figured_bass_alteration {
1887                 Music *m = unsmob_music ($1);
1888                 if ($2) {
1889                         SCM salter = m->get_property ("alteration");
1890                         int alter = scm_is_number (salter) ? scm_to_int (salter) : 0;
1891                         m->set_property ("alteration",
1892                                 scm_from_int (alter + $2));
1893                 } else {
1894                         m->set_property ("alteration", scm_from_int (0));
1895                 }
1896         }
1897         | bass_figure figured_bass_modification  {
1898                 Music *m = unsmob_music ($1);
1899                 if ($2 == ly_symbol2scm ("plus"))
1900                         {
1901                         m->set_property ("augmented", SCM_BOOL_T);
1902                         }
1903                 else if ($2 == ly_symbol2scm ("slash"))
1904                         {
1905                         m->set_property ("diminished", SCM_BOOL_T);
1906                         }
1907                 else if ($2 == ly_symbol2scm ("exclamation"))
1908                         {
1909                         m->set_property ("no-continuation", SCM_BOOL_T);
1910                         }
1911         }
1912         ;
1913
1914
1915 figured_bass_modification:
1916         E_PLUS          {
1917                 $$ = ly_symbol2scm ("plus");
1918         }
1919         | E_EXCLAMATION {
1920                 $$ = ly_symbol2scm ("exclamation");
1921         }
1922         | '/'           {
1923                 $$ = ly_symbol2scm ("slash");
1924         }
1925         ;
1926
1927 br_bass_figure:
1928         bass_figure {
1929                 $$ = $1;
1930         }
1931         | '[' bass_figure {
1932                 $$ = $2;
1933                 unsmob_music ($$)->set_property ("bracket-start", SCM_BOOL_T);
1934         }
1935         ;
1936
1937 figure_list:
1938         /**/            {
1939                 $$ = SCM_EOL;
1940         }
1941         | figure_list br_bass_figure {
1942                 $$ = scm_cons ($2, $1);
1943         }
1944         ;
1945
1946 figure_spec:
1947         FIGURE_OPEN figure_list FIGURE_CLOSE {
1948                 $$ = scm_reverse_x ($2, SCM_EOL);
1949         }
1950         ;
1951
1952
1953 optional_rest:
1954         /**/   { $$ = 0; }
1955         | REST { $$ = 1; }
1956         ;
1957
1958 simple_element:
1959         pitch exclamations questions octave_check optional_notemode_duration optional_rest {
1960                 if (!PARSER->lexer_->is_note_state ())
1961                         PARSER->parser_error (@1, _ ("have to be in Note mode for notes"));
1962
1963                 Music *n = 0;
1964                 if ($6)
1965                         n = MY_MAKE_MUSIC ("RestEvent");
1966                 else
1967                         n = MY_MAKE_MUSIC ("NoteEvent");
1968                 
1969                 n->set_property ("pitch", $1);
1970                 n->set_property ("duration", $5);
1971
1972                 if (scm_is_number ($4))
1973                 {
1974                         int q = scm_to_int ($4);
1975                         n->set_property ("absolute-octave", scm_from_int (q-1));
1976                 }
1977
1978                 if ($3 % 2)
1979                         n->set_property ("cautionary", SCM_BOOL_T);
1980                 if ($2 % 2 || $3 % 2)
1981                         n->set_property ("force-accidental", SCM_BOOL_T);
1982
1983                 n->set_spot (@$);
1984                 $$ = n->unprotect ();
1985         }
1986         | DRUM_PITCH optional_notemode_duration {
1987                 Music *n = MY_MAKE_MUSIC ("NoteEvent");
1988                 n->set_property ("duration", $2);
1989                 n->set_property ("drum-type", $1);
1990
1991                 $$ = n->unprotect ();           
1992         }
1993         | RESTNAME optional_notemode_duration           {
1994                 Music *ev = 0;
1995                 if (ly_scm2string ($1) == "s") {
1996                         /* Space */
1997                         ev = MY_MAKE_MUSIC ("SkipEvent");
1998                   }
1999                 else {
2000                         ev = MY_MAKE_MUSIC ("RestEvent");
2001                 
2002                     }
2003                 ev->set_property ("duration", $2);
2004                 ev->set_spot (@$);
2005                 $$ = ev->unprotect ();
2006         }
2007         | lyric_element optional_notemode_duration      {
2008                 if (!PARSER->lexer_->is_lyric_state ())
2009                         PARSER->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2010
2011                 Music *levent = MY_MAKE_MUSIC ("LyricEvent");
2012                 levent->set_property ("text", $1);
2013                 levent->set_property ("duration",$2);
2014                 levent->set_spot (@$);
2015                 $$= levent->unprotect ();
2016         }
2017         ;
2018
2019 simple_chord_elements:
2020         simple_element  {
2021                 $$ = scm_list_1 ($1);
2022         }       
2023         | new_chord {
2024                 if (!PARSER->lexer_->is_chord_state ())
2025                         PARSER->parser_error (@1, _ ("have to be in Chord mode for chords"));
2026                 $$ = $1;
2027         }
2028         | figure_spec optional_notemode_duration {
2029                 for (SCM s = $1; scm_is_pair (s); s = scm_cdr (s))
2030                 {
2031                         unsmob_music (scm_car (s))->set_property ("duration", $2);
2032                 }
2033                 $$ = $1;
2034         }       
2035         ;
2036
2037 lyric_element:
2038         lyric_markup {
2039                 $$ = $1;
2040         }
2041         | LYRICS_STRING {
2042                 $$ = $1;
2043         }
2044         ;
2045
2046 new_chord:
2047         steno_tonic_pitch optional_notemode_duration   {
2048                 $$ = make_chord_elements ($1, $2, SCM_EOL);
2049         }
2050         | steno_tonic_pitch optional_notemode_duration chord_separator chord_items {
2051                 SCM its = scm_reverse_x ($4, SCM_EOL);
2052                 $$ = make_chord_elements ($1, $2, scm_cons ($3, its));
2053         }
2054         ;
2055
2056 chord_items:
2057         /**/ {
2058                 $$ = SCM_EOL;           
2059         }
2060         | chord_items chord_item {
2061                 $$ = scm_cons ($2, $$);
2062         }
2063         ;
2064
2065 chord_separator:
2066         CHORD_COLON {
2067                 $$ = ly_symbol2scm ("chord-colon");
2068         }
2069         | CHORD_CARET {
2070                 $$ = ly_symbol2scm ("chord-caret");
2071         }
2072         | CHORD_SLASH steno_tonic_pitch {
2073                 $$ = scm_list_2 (ly_symbol2scm ("chord-slash"), $2);
2074         }
2075         | CHORD_BASS steno_tonic_pitch {
2076                 $$ = scm_list_2 (ly_symbol2scm ("chord-bass"), $2);
2077         }
2078         ;
2079
2080 chord_item:
2081         chord_separator {
2082                 $$ = $1;
2083         }
2084         | step_numbers {
2085                 $$ = scm_reverse_x ($1, SCM_EOL);
2086         }
2087         | CHORD_MODIFIER  {
2088                 $$ = $1;
2089         }
2090         ;
2091
2092 step_numbers:
2093         step_number { $$ = scm_cons ($1, SCM_EOL); }
2094         | step_numbers '.' step_number {
2095                 $$ = scm_cons ($3, $$);
2096         }
2097         ;
2098
2099 step_number:
2100         bare_unsigned {
2101                 $$ = make_chord_step ($1, 0);
2102         }
2103         | bare_unsigned '+' {
2104                 $$ = make_chord_step ($1, SHARP);
2105         }
2106         | bare_unsigned CHORD_MINUS {
2107                 $$ = make_chord_step ($1, FLAT);
2108         }
2109         ;       
2110
2111 /*
2112         UTILITIES
2113
2114 TODO: should deprecate in favor of Scheme?
2115
2116  */
2117 number_expression:
2118         number_expression '+' number_term {
2119                 $$ = scm_sum ($1, $3);
2120         }
2121         | number_expression '-' number_term {
2122                 $$ = scm_difference ($1, $3);
2123         }
2124         | number_term
2125         ;
2126
2127 number_term:
2128         number_factor {
2129                 $$ = $1;
2130         }
2131         | number_factor '*' number_factor {
2132                 $$ = scm_product ($1, $3);
2133         }
2134         | number_factor '/' number_factor {
2135                 $$ = scm_divide ($1, $3);
2136         }
2137         ;
2138
2139 number_factor:
2140         '-'  number_factor { /* %prec UNARY_MINUS */
2141                 $$ = scm_difference ($2, SCM_UNDEFINED);
2142         }
2143         | bare_number
2144         ;
2145
2146
2147 bare_number:
2148         UNSIGNED        {
2149                 $$ = scm_from_int ($1);
2150         }
2151         | REAL          {
2152                 $$ = $1;
2153         }
2154         | NUMBER_IDENTIFIER             {
2155                 $$ = $1;
2156         }
2157         | REAL NUMBER_IDENTIFIER        {
2158                 $$ = scm_from_double (scm_to_double ($1) *scm_to_double ($2));
2159         }
2160         | UNSIGNED NUMBER_IDENTIFIER    {
2161                 $$ = scm_from_double ($1 *scm_to_double ($2));
2162         }
2163         ;
2164
2165
2166 bare_unsigned:
2167         UNSIGNED {
2168                         $$ = $1;
2169         }
2170         | DIGIT {
2171                 $$ = $1;
2172         }
2173         ;
2174
2175 exclamations:
2176                 { $$ = 0; }
2177         | exclamations '!'      { $$ ++; }
2178         ;
2179
2180 questions:
2181                 { $$ = 0; }
2182         | questions '?' { $$ ++; }
2183         ;
2184
2185 /*
2186 This should be done more dynamically if possible.
2187 */
2188
2189 lyric_markup:
2190         LYRIC_MARKUP_IDENTIFIER {
2191                 $$ = $1;
2192         }
2193         | LYRIC_MARKUP
2194                 { PARSER->lexer_->push_markup_state (); }
2195         markup_top {
2196                 $$ = $3;
2197                 PARSER->lexer_->pop_state ();
2198         }
2199         ;
2200
2201 full_markup:
2202         MARKUP_IDENTIFIER {
2203                 $$ = $1;
2204         }
2205         | MARKUP
2206                 { PARSER->lexer_->push_markup_state (); }
2207         markup_top {
2208                 $$ = $3;
2209                 PARSER->lexer_->pop_state ();
2210         }
2211         ;
2212
2213 markup_top:
2214         markup_list { 
2215                 $$ = scm_list_2 (ly_lily_module_constant ("line-markup"),  $1); 
2216         }
2217         | markup_head_1_list simple_markup      {
2218                 $$ = scm_car (scm_call_2 (ly_lily_module_constant ("map-markup-command-list"), $1, scm_list_1 ($2)));
2219         }
2220         | simple_markup {
2221                 $$ = $1;
2222         }
2223         ;
2224
2225 markup_list:
2226         markup_composed_list {
2227                 $$ = $1;
2228         }
2229         | markup_braced_list {
2230                 $$ = $1;
2231         }
2232         ;
2233
2234 markup_composed_list:
2235         markup_head_1_list markup_braced_list {
2236                 $$ = scm_call_2 (ly_lily_module_constant ("map-markup-command-list"), $1, $2);
2237                 
2238         }
2239         ;
2240
2241 markup_braced_list:
2242         '{' markup_braced_list_body '}' {
2243                 $$ = scm_reverse_x ($2, SCM_EOL);
2244         }
2245         ;
2246
2247 markup_braced_list_body:
2248         /* empty */     {  $$ = SCM_EOL; }
2249         | markup_braced_list_body markup {
2250                 $$ = scm_cons ($2, $1);
2251         }
2252         | markup_braced_list_body markup_list {
2253                 $$ = scm_append_x (scm_list_2 (scm_reverse_x ($2, SCM_EOL), $1));
2254         }
2255         ;
2256
2257 markup_head_1_item:
2258         MARKUP_HEAD_MARKUP0     {
2259                 $$ = scm_list_1 ($1);
2260         }
2261         | MARKUP_HEAD_SCM0_MARKUP1 embedded_scm {
2262                 $$ = scm_list_2 ($1, $2);
2263         }
2264         | MARKUP_HEAD_SCM0_SCM1_MARKUP2 embedded_scm embedded_scm       {
2265                 $$ = scm_list_3 ($1, $2, $3);
2266         }
2267         ;
2268
2269 markup_head_1_list:
2270         markup_head_1_item      {
2271                 $$ = scm_list_1 ($1);
2272         }
2273         | markup_head_1_list markup_head_1_item {
2274                 $$ = scm_cons ($2, $1);
2275         }
2276         ;
2277
2278 simple_markup:
2279         STRING {
2280                 $$ = make_simple_markup ($1);
2281         }
2282         | MARKUP_IDENTIFIER {
2283                 $$ = $1;
2284         }
2285         | LYRIC_MARKUP_IDENTIFIER {
2286                 $$ = $1;
2287         }
2288         | STRING_IDENTIFIER {
2289                 $$ = $1;
2290         }
2291         | SCORE {
2292                 SCM nn = PARSER->lexer_->lookup_identifier ("pitchnames");
2293                 PARSER->lexer_->push_note_state (alist_to_hashq (nn));
2294         } '{' score_body '}' {
2295                 Score * sc = $4;
2296                 $$ = scm_list_2 (ly_lily_module_constant ("score-markup"), sc->self_scm ());
2297                 sc->unprotect ();
2298                 PARSER->lexer_->pop_state ();
2299         }
2300         | MARKUP_HEAD_SCM0 embedded_scm {
2301                 $$ = scm_list_2 ($1, $2);
2302         }
2303         | MARKUP_HEAD_SCM0_SCM1_SCM2 embedded_scm embedded_scm embedded_scm {
2304                 $$ = scm_list_4 ($1, $2, $3, $4);
2305         }
2306         | MARKUP_HEAD_SCM0_SCM1 embedded_scm embedded_scm {
2307                 $$ = scm_list_3 ($1, $2, $3);
2308         }
2309         | MARKUP_HEAD_EMPTY {
2310                 $$ = scm_list_1 ($1);
2311         }
2312         | MARKUP_HEAD_LIST0 markup_list {
2313                 $$ = scm_list_2 ($1,$2);
2314         }
2315         | MARKUP_HEAD_MARKUP0_MARKUP1 markup markup {
2316                 $$ = scm_list_3 ($1, $2, $3);
2317         }
2318         ;
2319         
2320 markup:
2321         markup_head_1_list simple_markup        {
2322                 SCM mapper = ly_lily_module_constant ("map-markup-command-list");
2323                 $$ = scm_car (scm_call_2 (mapper, $1, scm_list_1 ($2)));
2324         }
2325         | simple_markup {
2326                 $$ = $1;
2327         }
2328         ;
2329
2330 %%
2331
2332 void
2333 Lily_parser::set_yydebug (bool )
2334 {
2335 #if 0
2336         yydebug = 1;
2337 #endif
2338 }
2339
2340 void
2341 Lily_parser::do_yyparse ()
2342 {
2343         yyparse ((void*)this);
2344 }
2345
2346
2347
2348
2349
2350 /*
2351
2352 It is a little strange to have this function in this file, but
2353 otherwise, we have to import music classes into the lexer.
2354
2355 */
2356 int
2357 Lily_lexer::try_special_identifiers (SCM *destination, SCM sid)
2358 {
2359         if (scm_is_string (sid)) {
2360                 *destination = sid;
2361                 return STRING_IDENTIFIER;
2362         } else if (unsmob_book (sid)) {
2363                 *destination = unsmob_book (sid)->clone ()->self_scm ();
2364                 return BOOK_IDENTIFIER;
2365         } else if (scm_is_number (sid)) {
2366                 *destination = sid;
2367                 return NUMBER_IDENTIFIER;
2368         } else if (unsmob_context_def (sid)) {
2369                 *destination = unsmob_context_def (sid)->clone_scm ();
2370                 return CONTEXT_DEF_IDENTIFIER;
2371         } else if (unsmob_score (sid)) {
2372                 Score *score = new Score (*unsmob_score (sid));
2373                 *destination = score->self_scm ();
2374                 return SCORE_IDENTIFIER;
2375         } else if (Music *mus = unsmob_music (sid)) {
2376                 mus = mus->clone ();
2377                 *destination = mus->self_scm ();
2378                 unsmob_music (*destination)->
2379                         set_property ("origin", make_input (last_input_));
2380
2381                 bool is_event = scm_memq (ly_symbol2scm ("event"), mus->get_property ("types"))
2382                         != SCM_BOOL_F;
2383
2384                 return is_event ? EVENT_IDENTIFIER : MUSIC_IDENTIFIER;
2385         } else if (unsmob_duration (sid)) {
2386                 *destination = unsmob_duration (sid)->smobbed_copy ();
2387                 return DURATION_IDENTIFIER;
2388         } else if (unsmob_output_def (sid)) {
2389                 Output_def *p = unsmob_output_def (sid);
2390                 p = p->clone ();
2391
2392                 *destination = p->self_scm ();
2393                 return OUTPUT_DEF_IDENTIFIER;
2394         } else if (Text_interface::is_markup (sid)) {
2395                 *destination = sid;
2396                 if (is_lyric_state ())
2397                         return LYRIC_MARKUP_IDENTIFIER;
2398                 return MARKUP_IDENTIFIER;
2399         }
2400
2401         return -1;      
2402 }
2403
2404 SCM
2405 get_next_unique_context_id ()
2406 {
2407         return scm_makfrom0str ("$uniqueContextId");
2408 }
2409
2410
2411 SCM
2412 get_next_unique_lyrics_context_id ()
2413 {
2414         static int new_context_count;
2415         char s[128];
2416         snprintf (s, sizeof (s)-1, "uniqueContext%d", new_context_count++);
2417         return scm_makfrom0str (s);
2418 }
2419
2420
2421 SCM
2422 run_music_function (Lily_parser *parser, SCM expr)
2423 {
2424         SCM func = scm_car (expr);
2425         Input *loc = unsmob_input (scm_cadr (expr));
2426         SCM args = scm_cddr (expr);
2427         SCM sig = scm_object_property (func, ly_symbol2scm ("music-function-signature"));
2428
2429         SCM type_check_proc = ly_lily_module_constant ("type-check-list");
2430
2431         if (!to_boolean (scm_call_3  (type_check_proc, scm_cadr (expr), sig, args)))
2432         {
2433                 parser->error_level_ = 1;
2434                 return LOWLEVEL_MAKE_SYNTAX (ly_lily_module_constant ("void-music"), scm_list_2 (parser->self_scm (), make_input (*loc)));
2435         }
2436
2437         SCM syntax_args = scm_list_4 (parser->self_scm (), make_input (*loc), func, args);
2438         return LOWLEVEL_MAKE_SYNTAX (ly_lily_module_constant ("music-function"), syntax_args);
2439 }
2440
2441 bool
2442 is_regular_identifier (SCM id)
2443 {
2444   string str = ly_scm2string (id);
2445   char const *s = str.c_str ();
2446
2447   bool v = true;
2448 #if 0
2449   isalpha (*s);
2450   s++;
2451 #endif
2452   while (*s && v)
2453    {
2454         v = v && isalnum (*s);
2455         s++;
2456    }
2457   return v;
2458 }
2459
2460
2461 SCM
2462 get_first_context_id (SCM type, Music *m)
2463 {
2464         SCM id = m->get_property ("context-id");
2465         if (SCM_BOOL_T == scm_equal_p (m->get_property ("context-type"), type)
2466             && scm_is_string (m->get_property ("context-id"))
2467             && scm_c_string_length (id) > 0)
2468         {
2469                 return id;
2470         }
2471         return SCM_EOL;
2472 }
2473
2474 SCM
2475 make_simple_markup (SCM a)
2476 {
2477         return a;
2478 }
2479
2480 bool
2481 is_duration (int t)
2482 {
2483   return t && t == 1 << intlog2 (t);
2484 }
2485
2486 void
2487 set_music_properties (Music *p, SCM a)
2488 {
2489   for (SCM k = a; scm_is_pair (k); k = scm_cdr (k))
2490         p->internal_set_property (scm_caar (k), scm_cdar (k));
2491 }
2492
2493
2494 SCM
2495 make_chord_step (int step, int alter)
2496 {
2497         if (step == 7)
2498                 alter += FLAT;
2499
2500         while (step < 0)
2501                 step += 7;
2502         Pitch m ((step -1) / 7, (step - 1) % 7, alter);
2503         return m.smobbed_copy ();
2504 }
2505
2506
2507 SCM
2508 make_chord_elements (SCM pitch, SCM dur, SCM modification_list)
2509 {
2510         SCM chord_ctor = ly_lily_module_constant ("construct-chord-elements");
2511         return scm_call_3 (chord_ctor, pitch, dur, modification_list);
2512 }
2513
2514
2515 /* Todo: actually also use apply iso. call too ...  */
2516 bool
2517 ly_input_procedure_p (SCM x)
2518 {
2519         return ly_is_procedure (x)
2520                 || (scm_is_pair (x) && ly_is_procedure (scm_car (x)));
2521 }
2522
2523 SCM
2524 make_music_relative (Pitch start, SCM music)
2525 {
2526         Music *relative = MY_MAKE_MUSIC ("RelativeOctaveMusic");
2527         relative->set_property ("element", music);
2528         
2529         Music *m = unsmob_music (music);
2530         Pitch last = m->to_relative_octave (start);
2531         if (lily_1_8_relative)
2532                 m->set_property ("last-pitch", last.smobbed_copy ());
2533         return relative->unprotect ();
2534 }
2535
2536 int
2537 yylex (YYSTYPE *s, YYLTYPE *loc, void *v)
2538 {
2539         Lily_parser *pars = (Lily_parser*) v;
2540         Lily_lexer *lex = pars->lexer_;
2541
2542         lex->lexval = (void*) s;
2543         lex->lexloc = loc;
2544         lex->prepare_for_next_token ();
2545         return lex->yylex ();
2546 }