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