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