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