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