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