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