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