]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
Parser: remove command_element and command_event synonyms of tempo_event
[lilypond.git] / lily / parser.yy
1 /* -*- mode: c++; c-file-style: "linux"; indent-tabs-mode: t -*- */
2 /*
3   This file is part of LilyPond, the GNU music typesetter.
4
5   Copyright (C) 1997--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
6                  Jan Nieuwenhuizen <janneke@gnu.org>
7
8   LilyPond is free software: you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation, either version 3 of the License, or
11   (at your option) any later version.
12
13   LilyPond is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 /* Mode and indentation are at best a rough approximation based on TAB
23  * formatting (reasonable for compatibility with unspecific editor
24  * modes as Bison modes are hard to find) and need manual correction
25  * frequently.  Without a reasonably dependable way of formatting a
26  * Bison file sensibly, there is little point in trying to fix the
27  * inconsistent state of indentation.
28  */
29
30 %{
31
32 #define yyerror Lily_parser::parser_error
33
34 /* We use custom location type: Input objects */
35 #define YYLTYPE Input
36 #define YYSTYPE SCM
37 #define YYLLOC_DEFAULT(Current,Rhs,N) \
38         ((Current).set_location ((Rhs)[1], (Rhs)[N]))
39
40 #define YYPRINT(file, type, value)                                      \
41         do {                                                            \
42                 if (scm_is_eq (value, SCM_UNSPECIFIED))                 \
43                         break;                                          \
44                 char *p = scm_to_locale_string                          \
45                         (scm_simple_format (SCM_BOOL_F,                 \
46                                             scm_from_locale_string ("~S"), \
47                                             scm_list_1 (value)));       \
48                 fputs (p, file);                                        \
49                 free (p);                                               \
50         } while (0)
51
52 %}
53
54 %parse-param {Lily_parser *parser}
55 %parse-param {SCM *retval}
56 %lex-param {Lily_parser *parser}
57 %error-verbose
58 %debug
59
60 /* We use SCMs to do strings, because it saves us the trouble of
61 deleting them.  Let's hope that a stack overflow doesn't trigger a move
62 of the parse stack onto the heap. */
63
64 %left PREC_BOT
65 %nonassoc REPEAT
66 %nonassoc ALTERNATIVE
67
68 /* The above precedences tackle the shift/reduce problem
69
70 1.  \repeat
71         \repeat .. \alternative
72
73     \repeat { \repeat .. \alternative }
74
75 or
76
77     \repeat { \repeat } \alternative
78 */
79
80 %nonassoc COMPOSITE
81 %left ADDLYRICS
82
83 %right ':' UNSIGNED REAL E_UNSIGNED EVENT_IDENTIFIER EVENT_FUNCTION '^' '_'
84        HYPHEN EXTENDER DURATION_IDENTIFIER '!'
85
86  /* The above are needed for collecting tremoli and other items (that
87     could otherwise be interpreted as belonging to the next function
88     argument) greedily, and together with the next rule will serve to
89     join numbers and units greedily instead of allowing them into
90     separate function arguments
91  */
92
93 %nonassoc NUMBER_IDENTIFIER
94
95 %left PREC_TOP
96
97
98
99
100 %pure-parser
101 %locations
102
103
104
105 %{ // -*-Fundamental-*-
106
107 /*
108 FIXME:
109
110    * The rules for who is protecting what are very shady.  Uniformise
111      this.
112
113    * There are too many lexical modes?
114 */
115
116 #include "config.hh"
117
118 #include <cctype>
119 #include <cstdlib>
120 #include <cstdio>
121 using namespace std;
122
123 #include "book.hh"
124 #include "context-def.hh"
125 #include "context-mod.hh"
126 #include "dimensions.hh"
127 #include "file-path.hh"
128 #include "input.hh"
129 #include "international.hh"
130 #include "lily-guile.hh"
131 #include "lily-lexer.hh"
132 #include "lily-parser.hh"
133 #include "main.hh"
134 #include "misc.hh"
135 #include "music.hh"
136 #include "output-def.hh"
137 #include "paper-book.hh"
138 #include "scm-hash.hh"
139 #include "score.hh"
140 #include "text-interface.hh"
141 #include "warn.hh"
142
143 void
144 Lily_parser::parser_error (Input const *i, Lily_parser *parser, SCM *, const string &s)
145 {
146         parser->parser_error (*i, s);
147 }
148
149 // The following are somewhat precarious constructs as they may change
150 // the value of the lookahead token.  That implies that the lookahead
151 // token must not yet have made an impact on the state stack other
152 // than causing the reduction of the current rule, or switching the
153 // lookahead token while Bison is mulling it over will cause trouble.
154
155 #define MYBACKUP(Token, Value, Location)                                \
156         do {                                                            \
157                 if (yychar != YYEMPTY)                                  \
158                         parser->lexer_->push_extra_token                \
159                                 (yylloc, yychar, yylval);               \
160                 if (Token)                                              \
161                         parser->lexer_->push_extra_token                \
162                                 (Location, Token, Value);               \
163                 parser->lexer_->push_extra_token (Location, BACKUP);    \
164                 yychar = YYEMPTY;                                       \
165         } while (0)
166
167
168 #define MYREPARSE(Location, Pred, Token, Value)                         \
169         do {                                                            \
170                 if (yychar != YYEMPTY)                                  \
171                         parser->lexer_->push_extra_token                \
172                                 (yylloc, yychar, yylval);               \
173                 parser->lexer_->push_extra_token                        \
174                         (Location, Token, Value);                       \
175                 parser->lexer_->push_extra_token                        \
176                         (Location, REPARSE, Pred);                      \
177                 yychar = YYEMPTY;                                       \
178         } while (0)
179
180 %}
181
182
183 %{
184
185 #define MY_MAKE_MUSIC(x, spot) \
186         make_music_with_input (ly_symbol2scm (x), \
187                                parser->lexer_->override_input (spot))
188
189 /* ES TODO:
190 - Don't use lily module, create a new module instead.
191 - delay application of the function
192 */
193 #define LOWLEVEL_MAKE_SYNTAX(proc, args)        \
194   scm_apply_0 (proc, args)
195 /* Syntactic Sugar. */
196 #define MAKE_SYNTAX(name, location, ...)                                \
197         LOWLEVEL_MAKE_SYNTAX (ly_lily_module_constant (name), scm_list_n (parser->self_scm (), make_input (parser->lexer_->override_input (location)), ##__VA_ARGS__, SCM_UNDEFINED))
198 #define START_MAKE_SYNTAX(name, ...)                                    \
199         scm_list_n (ly_lily_module_constant (name) , ##__VA_ARGS__, SCM_UNDEFINED)
200 #define FINISH_MAKE_SYNTAX(start, location, ...)                        \
201         LOWLEVEL_MAKE_SYNTAX (scm_car (start), scm_cons2 (parser->self_scm (), make_input (parser->lexer_->override_input (location)), scm_append_x (scm_list_2 (scm_cdr (start), scm_list_n (__VA_ARGS__, SCM_UNDEFINED)))))
202
203 SCM get_next_unique_context_id ();
204 SCM get_next_unique_lyrics_context_id ();
205
206 #undef _
207 #if !HAVE_GETTEXT
208 #define _(x) x
209 #else
210 #include <libintl.h>
211 #define _(x) gettext (x)
212 #endif
213
214
215 static Music *make_music_with_input (SCM name, Input where);
216 SCM check_scheme_arg (Lily_parser *parser, Input loc,
217                       SCM arg, SCM args, SCM pred, SCM disp = SCM_UNDEFINED);
218 SCM make_music_from_simple (Lily_parser *parser, Input loc, SCM pitch);
219 SCM loc_on_music (Input loc, SCM arg);
220 SCM make_chord_elements (Input loc, SCM pitch, SCM dur, SCM modification_list);
221 SCM make_chord_step (SCM step, Rational alter);
222 SCM make_simple_markup (SCM a);
223 SCM make_duration (SCM t, int dots = 0);
224 bool is_regular_identifier (SCM id, bool multiple=false);
225 SCM try_string_variants (SCM pred, SCM str);
226 int yylex (YYSTYPE *s, YYLTYPE *loc, Lily_parser *parser);
227
228 %}
229
230 /* The third option is an alias that will be used to display the
231    syntax error.  Bison CVS now correctly handles backslash escapes.
232
233    FIXME: Bison needs to translate some of these, eg, STRING.
234
235 */
236
237 /* Keyword tokens with plain escaped name.  */
238 %token END_OF_FILE 0 "end of input"
239 %token ACCEPTS "\\accepts"
240 %token ADDLYRICS "\\addlyrics"
241 %token ALIAS "\\alias"
242 %token ALTERNATIVE "\\alternative"
243 %token BOOK "\\book"
244 %token BOOKPART "\\bookpart"
245 %token CHANGE "\\change"
246 %token CHORDMODE "\\chordmode"
247 %token CHORDS "\\chords"
248 %token CONSISTS "\\consists"
249 %token CONTEXT "\\context"
250 %token DEFAULT "\\default"
251 %token DEFAULTCHILD "\\defaultchild"
252 %token DENIES "\\denies"
253 %token DESCRIPTION "\\description"
254 %token DRUMMODE "\\drummode"
255 %token DRUMS "\\drums"
256 %token FIGUREMODE "\\figuremode"
257 %token FIGURES "\\figures"
258 %token HEADER "\\header"
259 %token INVALID "\\version-error"
260 %token LAYOUT "\\layout"
261 %token LYRICMODE "\\lyricmode"
262 %token LYRICS "\\lyrics"
263 %token LYRICSTO "\\lyricsto"
264 %token MARKUP "\\markup"
265 %token MARKUPLIST "\\markuplist"
266 %token MIDI "\\midi"
267 %token NAME "\\name"
268 %token NOTEMODE "\\notemode"
269 %token OVERRIDE "\\override"
270 %token PAPER "\\paper"
271 %token REMOVE "\\remove"
272 %token REPEAT "\\repeat"
273 %token REST "\\rest"
274 %token REVERT "\\revert"
275 %token SCORE "\\score"
276 %token SCORELINES "\\score-lines"
277 %token SEQUENTIAL "\\sequential"
278 %token SET "\\set"
279 %token SIMULTANEOUS "\\simultaneous"
280 %token TEMPO "\\tempo"
281 %token TYPE "\\type"
282 %token UNSET "\\unset"
283 %token WITH "\\with"
284
285 /* Keyword token exceptions.  */
286 %token NEWCONTEXT "\\new"
287
288
289 /* Other string tokens.  */
290
291 %token CHORD_BASS "/+"
292 %token CHORD_CARET "^"
293 %token CHORD_COLON ":"
294 %token CHORD_MINUS "-"
295 %token CHORD_SLASH "/"
296 %token ANGLE_OPEN "<"
297 %token ANGLE_CLOSE ">"
298 %token DOUBLE_ANGLE_OPEN "<<"
299 %token DOUBLE_ANGLE_CLOSE ">>"
300 %token E_BACKSLASH "\\"
301 %token E_EXCLAMATION "\\!"
302 %token E_PLUS "\\+"
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 MULTI_MEASURE_REST
314
315
316 %token E_UNSIGNED
317 %token UNSIGNED
318
319 /* Artificial tokens, for more generic function syntax */
320 %token EXPECT_MARKUP "markup?"
321 %token EXPECT_SCM "scheme?"
322 %token BACKUP "(backed-up?)"
323 %token REPARSE "(reparsed?)"
324 %token EXPECT_MARKUP_LIST "markup-list?"
325 %token EXPECT_OPTIONAL "optional?"
326 /* After the last argument. */
327 %token EXPECT_NO_MORE_ARGS;
328
329 /* An artificial token for parsing embedded Lilypond */
330 %token EMBEDDED_LILY "#{"
331
332 %token BOOK_IDENTIFIER
333 %token CHORD_MODIFIER
334 %token CHORD_REPETITION
335 %token CONTEXT_DEF_IDENTIFIER
336 %token CONTEXT_MOD_IDENTIFIER
337 %token DRUM_PITCH
338 %token PITCH_IDENTIFIER
339 %token DURATION_IDENTIFIER
340 %token EVENT_IDENTIFIER
341 %token EVENT_FUNCTION
342 %token FRACTION
343 %token LYRIC_ELEMENT
344 %token MARKUP_FUNCTION
345 %token MARKUP_LIST_FUNCTION
346 %token MARKUP_IDENTIFIER
347 %token MARKUPLIST_IDENTIFIER
348 %token MUSIC_FUNCTION
349 %token MUSIC_IDENTIFIER
350 %token NOTENAME_PITCH
351 %token NUMBER_IDENTIFIER
352 %token OUTPUT_DEF_IDENTIFIER
353 %token REAL
354 %token RESTNAME
355 %token SCM_ARG
356 %token SCM_FUNCTION
357 %token SCM_IDENTIFIER
358 %token SCM_TOKEN
359 %token STRING
360 %token SYMBOL_LIST
361 %token TONICNAME_PITCH
362
363 %left '-' '+'
364
365 /* We don't assign precedence to / and *, because we might need varied
366 prec levels in different prods */
367
368 %left UNARY_MINUS
369
370 %%
371
372 start_symbol:
373         lilypond
374         | EMBEDDED_LILY {
375                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
376                 parser->lexer_->push_note_state (nn);
377         } embedded_lilypond {
378                 parser->lexer_->pop_state ();
379                 *retval = $3;
380         }
381         ;
382
383 lilypond:       /* empty */ { $$ = SCM_UNSPECIFIED; }
384         | lilypond toplevel_expression {
385         }
386         | lilypond assignment {
387         }
388         | lilypond error {
389                 parser->error_level_ = 1;
390         }
391         | lilypond INVALID      {
392                 parser->error_level_ = 1;
393         }
394         ;
395
396
397 toplevel_expression:
398         {
399                 parser->lexer_->add_scope (get_header (parser));
400         } lilypond_header {
401                 parser->lexer_->set_identifier (ly_symbol2scm ("$defaultheader"), $2);
402         }
403         | book_block {
404                 SCM proc = parser->lexer_->lookup_identifier ("toplevel-book-handler");
405                 scm_call_2 (proc, parser->self_scm (), $1);
406         }
407         | bookpart_block {
408                 SCM proc = parser->lexer_->lookup_identifier ("toplevel-bookpart-handler");
409                 scm_call_2 (proc, parser->self_scm (), $1);
410         }
411         | BOOK_IDENTIFIER {
412                 SCM proc = parser->lexer_->lookup_identifier
413                         (unsmob_book($1)->paper_
414                          ? "toplevel-book-handler"
415                          : "toplevel-bookpart-handler");
416                 scm_call_2 (proc, parser->self_scm (), $1);
417         }
418         | score_block {
419                 SCM proc = parser->lexer_->lookup_identifier ("toplevel-score-handler");
420                 scm_call_2 (proc, parser->self_scm (), $1);
421         }
422         | composite_music {
423                 SCM proc = parser->lexer_->lookup_identifier ("toplevel-music-handler");
424                 scm_call_2 (proc, parser->self_scm (), $1);
425         }
426         | full_markup {
427                 SCM proc = parser->lexer_->lookup_identifier ("toplevel-text-handler");
428                 scm_call_2 (proc, parser->self_scm (), scm_list_1 ($1));
429         }
430         | full_markup_list {
431                 SCM proc = parser->lexer_->lookup_identifier ("toplevel-text-handler");
432                 scm_call_2 (proc, parser->self_scm (), $1);
433         }
434         | SCM_TOKEN {
435                 // Evaluate and ignore #xxx, as opposed to \xxx
436                 parser->lexer_->eval_scm_token ($1);
437         }
438         | embedded_scm_active
439         {
440                 SCM out = SCM_UNDEFINED;
441                 if (Text_interface::is_markup ($1))
442                         out = scm_list_1 ($1);
443                 else if (Text_interface::is_markup_list ($1))
444                         out = $1;
445                 if (scm_is_pair (out))
446                 {
447                         SCM proc = parser->lexer_->lookup_identifier ("toplevel-text-handler");
448                         scm_call_2 (proc, parser->self_scm (), out);
449                 } else if (unsmob_score ($1))
450                 {
451                         SCM proc = parser->lexer_->lookup_identifier ("toplevel-score-handler");
452                         scm_call_2 (proc, parser->self_scm (), $1);
453                 } else if (!scm_is_eq ($1, SCM_UNSPECIFIED))
454                         parser->parser_error (@1, _("bad expression type"));
455         }
456         | output_def {
457                 SCM id = SCM_EOL;
458                 Output_def * od = unsmob_output_def ($1);
459
460                 if (od->c_variable ("is-paper") == SCM_BOOL_T)
461                         id = ly_symbol2scm ("$defaultpaper");
462                 else if (od->c_variable ("is-midi") == SCM_BOOL_T)
463                         id = ly_symbol2scm ("$defaultmidi");
464                 else if (od->c_variable ("is-layout") == SCM_BOOL_T)
465                         id = ly_symbol2scm ("$defaultlayout");
466
467                 parser->lexer_->set_identifier (id, $1);
468         }
469         ;
470
471 embedded_scm_bare:
472         SCM_TOKEN
473         {
474                 $$ = parser->lexer_->eval_scm_token ($1);
475         }
476         | SCM_IDENTIFIER
477         ;
478
479 embedded_scm_active:
480         SCM_IDENTIFIER
481         | scm_function_call
482         ;
483
484 embedded_scm_bare_arg:
485         SCM_ARG
486         | SCM_TOKEN
487         {
488                 $$ = parser->lexer_->eval_scm_token ($1);
489         }
490         | FRACTION
491         | full_markup_list
492         | context_modification
493         | score_block
494         | context_def_spec_block
495         | book_block
496         | bookpart_block
497         | output_def
498         ;
499
500 /* The generic version may end in music, or not */
501
502 embedded_scm:
503         embedded_scm_bare
504         | scm_function_call
505         ;
506
507 /* embedded_scm_arg is _not_ casting pitches to music by default, this
508  * has to be done by the function itself.  Note that this may cause
509  * the results of scm_function_call or embedded_scm_bare_arg to be
510  * turned into music from pitches as well.  Note that this creates a
511  * distinctly awkward situation for calculated drum pitches.  Those
512  * are at the current point of time rejected as music constituents as
513  * they can't be distinguished from "proper" symbols.
514  */
515
516 embedded_scm_arg:
517         embedded_scm_bare_arg
518         | scm_function_call
519         | music_assign
520         ;
521
522 scm_function_call:
523         SCM_FUNCTION function_arglist {
524                 $$ = MAKE_SYNTAX ("music-function", @$,
525                                          $1, $2);
526         }
527         ;
528
529 embedded_lilypond_number:
530         '-' embedded_lilypond_number
531         {
532                 $$ = scm_difference ($2, SCM_UNDEFINED);
533         }
534         | bare_number_common
535         | UNSIGNED NUMBER_IDENTIFIER
536         {
537                 $$ = scm_product ($1, $2);
538         }
539         ;
540
541 embedded_lilypond:
542         /* empty */
543         {
544                 // FIXME: @$ does not contain a useful source location
545                 // for empty rules, and the only token in the whole
546                 // production, EMBEDDED_LILY, is synthetic and also
547                 // contains no source location.
548                 $$ = MAKE_SYNTAX ("void-music", @$);
549         }
550         | identifier_init_nonumber
551         | embedded_lilypond_number
552         | post_event post_events
553         {
554                 $$ = scm_reverse_x ($2, SCM_EOL);
555                 if (Music *m = unsmob_music ($1))
556                 {
557                         if (m->is_mus_type ("post-event-wrapper"))
558                                 $$ = scm_append
559                                         (scm_list_2 (m->get_property ("elements"),
560                                                      $$));
561                         else
562                                 $$ = scm_cons ($1, $$);
563                 }
564                 if (scm_is_pair ($$)
565                     && scm_is_null (scm_cdr ($$)))
566                         $$ = scm_car ($$);
567                 else
568                 {
569                         Music * m = MY_MAKE_MUSIC ("PostEvents", @$);
570                         m->set_property ("elements", $$);
571                         $$ = m->unprotect ();
572                 }
573         }
574         | multiplied_duration
575         | music_embedded music_embedded music_list {
576                 $3 = scm_reverse_x ($3, SCM_EOL);
577                 if (unsmob_music ($2))
578                         $3 = scm_cons ($2, $3);
579                 if (unsmob_music ($1))
580                         $3 = scm_cons ($1, $3);
581                 $$ = MAKE_SYNTAX ("sequential-music", @$, $3);
582         }
583         | error {
584                 parser->error_level_ = 1;
585                 $$ = SCM_UNSPECIFIED;
586         }
587         | INVALID embedded_lilypond {
588                 parser->error_level_ = 1;
589                 $$ = $2;
590         }
591         ;
592
593
594 lilypond_header_body:
595         /* empty */ { $$ = SCM_UNSPECIFIED; }
596         | lilypond_header_body assignment  {
597
598         }
599         | lilypond_header_body embedded_scm  {
600
601         }
602         ;
603
604 lilypond_header:
605         HEADER '{' lilypond_header_body '}'     {
606                 $$ = parser->lexer_->remove_scope ();
607         }
608         ;
609
610 /*
611         DECLARATIONS
612 */
613 assignment_id:
614         STRING          { $$ = $1; }
615         ;
616
617 assignment:
618         assignment_id '=' identifier_init  {
619                 parser->lexer_->set_identifier ($1, $3);
620                 $$ = SCM_UNSPECIFIED;
621         }
622         | assignment_id property_path '=' identifier_init {
623                 SCM path = scm_cons (scm_string_to_symbol ($1), $2);
624                 parser->lexer_->set_identifier (path, $4);
625                 $$ = SCM_UNSPECIFIED;
626         }
627         ;
628
629
630 identifier_init:
631         identifier_init_nonumber
632         | number_expression
633         | post_event_nofinger post_events
634         {
635                 $$ = scm_reverse_x ($2, SCM_EOL);
636                 if (Music *m = unsmob_music ($1))
637                 {
638                         if (m->is_mus_type ("post-event-wrapper"))
639                                 $$ = scm_append
640                                         (scm_list_2 (m->get_property ("elements"),
641                                                      $$));
642                         else
643                                 $$ = scm_cons ($1, $$);
644                 }
645                 if (scm_is_pair ($$)
646                     && scm_is_null (scm_cdr ($$)))
647                         $$ = scm_car ($$);
648                 else
649                 {
650                         Music * m = MY_MAKE_MUSIC ("PostEvents", @$);
651                         m->set_property ("elements", $$);
652                         $$ = m->unprotect ();
653                 }
654         }
655         ;
656
657 identifier_init_nonumber:
658         score_block
659         | book_block
660         | bookpart_block
661         | output_def
662         | context_def_spec_block
663         | music_assign
664         | FRACTION
665         | string
666         | embedded_scm
667         | full_markup_list
668         | context_modification
669         ;
670
671 context_def_spec_block:
672         CONTEXT '{' context_def_spec_body '}'
673                 {
674                 $$ = $3;
675                 unsmob_context_def ($$)->origin ()->set_spot (@$);
676         }
677         ;
678
679 context_mod_arg:
680         embedded_scm
681         |
682         {
683                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
684                 parser->lexer_->push_note_state (nn);
685         }
686         composite_music
687         {
688                 parser->lexer_->pop_state ();
689                 $$ = $2;
690         }
691         ;
692
693 context_mod_embedded:
694         context_mod_arg
695         {
696                 if (unsmob_music ($1)) {
697                         SCM proc = parser->lexer_->lookup_identifier ("context-mod-music-handler");
698                         $1 = scm_call_2 (proc, parser->self_scm (), $1);
699                 }
700                 if (unsmob_context_mod ($1))
701                         $$ = $1;
702                 else {
703                         parser->parser_error (@1, _ ("not a context mod"));
704                         $$ = Context_mod ().smobbed_copy ();
705                 }
706         }
707         ;
708
709
710 context_def_spec_body:
711         /**/ {
712                 $$ = Context_def::make_scm ();
713         }
714         | CONTEXT_DEF_IDENTIFIER {
715                 $$ = $1;
716         }
717         | context_def_spec_body context_mod {
718                 if (!SCM_UNBNDP ($2))
719                         unsmob_context_def ($$)->add_context_mod ($2);
720         }
721         | context_def_spec_body context_modification {
722                 Context_def *td = unsmob_context_def ($$);
723                 SCM new_mods = unsmob_context_mod ($2)->get_mods ();
724                 for (SCM m = new_mods; scm_is_pair (m); m = scm_cdr (m)) {
725                     td->add_context_mod (scm_car (m));
726                 }
727         }
728         | context_def_spec_body context_mod_embedded {
729                 Context_def *td = unsmob_context_def ($$);
730                 SCM new_mods = unsmob_context_mod ($2)->get_mods ();
731                 for (SCM m = new_mods; scm_is_pair (m); m = scm_cdr (m)) {
732                     td->add_context_mod (scm_car (m));
733                 }
734         }
735         ;
736
737
738
739 book_block:
740         BOOK '{' book_body '}'  {
741                 $$ = $3;
742                 unsmob_book ($$)->origin ()->set_spot (@$);
743                 pop_paper (parser);
744                 parser->lexer_->set_identifier (ly_symbol2scm ("$current-book"), SCM_BOOL_F);
745         }
746         ;
747
748 /* FIXME:
749    * Use 'handlers' like for toplevel-* stuff?
750    * grok \layout and \midi?  */
751 book_body:
752         {
753                 Book *book = new Book;
754                 init_papers (parser);
755                 book->paper_ = dynamic_cast<Output_def*> (unsmob_output_def (parser->lexer_->lookup_identifier ("$defaultpaper"))->clone ());
756                 book->paper_->unprotect ();
757                 push_paper (parser, book->paper_);
758                 book->header_ = get_header (parser);
759                 $$ = book->unprotect ();
760                 parser->lexer_->set_identifier (ly_symbol2scm ("$current-book"), $$);
761         }
762         | BOOK_IDENTIFIER {
763                 parser->lexer_->set_identifier (ly_symbol2scm ("$current-book"), $1);
764         }
765         | book_body paper_block {
766                 unsmob_book ($1)->paper_ = unsmob_output_def ($2);
767                 set_paper (parser, unsmob_output_def ($2));
768         }
769         | book_body bookpart_block {
770                 SCM proc = parser->lexer_->lookup_identifier ("book-bookpart-handler");
771                 scm_call_2 (proc, $1, $2);
772         }
773         | book_body score_block {
774                 SCM proc = parser->lexer_->lookup_identifier ("book-score-handler");
775                 scm_call_2 (proc, $1, $2);
776         }
777         | book_body composite_music {
778                 SCM proc = parser->lexer_->lookup_identifier ("book-music-handler");
779                 scm_call_3 (proc, parser->self_scm (), $1, $2);
780         }
781         | book_body full_markup {
782                 SCM proc = parser->lexer_->lookup_identifier ("book-text-handler");
783                 scm_call_2 (proc, $1, scm_list_1 ($2));
784         }
785         | book_body full_markup_list {
786                 SCM proc = parser->lexer_->lookup_identifier ("book-text-handler");
787                 scm_call_2 (proc, $1, $2);
788         }
789         | book_body SCM_TOKEN {
790                 // Evaluate and ignore #xxx, as opposed to \xxx
791                 parser->lexer_->eval_scm_token ($2);
792         }
793         | book_body embedded_scm_active
794         {
795                 SCM out = SCM_UNDEFINED;
796                 if (Text_interface::is_markup ($2))
797                         out = scm_list_1 ($2);
798                 else if (Text_interface::is_markup_list ($2))
799                         out = $2;
800                 if (scm_is_pair (out))
801                 {
802                         SCM proc = parser->lexer_->lookup_identifier ("book-text-handler");
803                         scm_call_2 (proc, $1, out);
804                 } else if (unsmob_score ($2))
805                 {
806                         SCM proc = parser->lexer_->lookup_identifier ("book-score-handler");
807                         scm_call_2 (proc, $1, $2);
808                 } else if (!scm_is_eq ($2, SCM_UNSPECIFIED))
809                         parser->parser_error (@2, _("bad expression type"));
810         }
811         | book_body
812         {
813                 parser->lexer_->add_scope (unsmob_book ($1)->header_);
814         } lilypond_header
815         | book_body error {
816                 Book *book = unsmob_book ($1);
817                 book->paper_ = 0;
818                 book->scores_ = SCM_EOL;
819                 book->bookparts_ = SCM_EOL;
820         }
821         ;
822
823 bookpart_block:
824         BOOKPART '{' bookpart_body '}' {
825                 $$ = $3;
826                 unsmob_book ($$)->origin ()->set_spot (@$);
827                 parser->lexer_->set_identifier (ly_symbol2scm ("$current-bookpart"), SCM_BOOL_F);
828         }
829         ;
830
831 bookpart_body:
832         {
833                 Book *book = new Book;
834                 $$ = book->unprotect ();
835                 parser->lexer_->set_identifier (ly_symbol2scm ("$current-bookpart"), $$);
836         }
837         | BOOK_IDENTIFIER {
838                 parser->lexer_->set_identifier (ly_symbol2scm ("$current-bookpart"), $1);
839         }
840         | bookpart_body paper_block {
841                 unsmob_book ($$)->paper_ = unsmob_output_def ($2);
842         }
843         | bookpart_body score_block {
844                 SCM proc = parser->lexer_->lookup_identifier ("bookpart-score-handler");
845                 scm_call_2 (proc, $1, $2);
846         }
847         | bookpart_body composite_music {
848                 SCM proc = parser->lexer_->lookup_identifier ("bookpart-music-handler");
849                 scm_call_3 (proc, parser->self_scm (), $1, $2);
850         }
851         | bookpart_body full_markup {
852                 SCM proc = parser->lexer_->lookup_identifier ("bookpart-text-handler");
853                 scm_call_2 (proc, $1, scm_list_1 ($2));
854         }
855         | bookpart_body full_markup_list {
856                 SCM proc = parser->lexer_->lookup_identifier ("bookpart-text-handler");
857                 scm_call_2 (proc, $1, $2);
858         }
859         | bookpart_body SCM_TOKEN {
860                 // Evaluate and ignore #xxx, as opposed to \xxx
861                 parser->lexer_->eval_scm_token ($2);
862         }
863         | bookpart_body embedded_scm_active
864         {
865                 SCM out = SCM_UNDEFINED;
866                 if (Text_interface::is_markup ($2))
867                         out = scm_list_1 ($2);
868                 else if (Text_interface::is_markup_list ($2))
869                         out = $2;
870                 if (scm_is_pair (out))
871                 {
872                         SCM proc = parser->lexer_->lookup_identifier ("bookpart-text-handler");
873                         scm_call_2 (proc, $1, out);
874                 } else if (unsmob_score ($2))
875                 {
876                         SCM proc = parser->lexer_->lookup_identifier ("bookpart-score-handler");
877                         scm_call_2 (proc, $1, $2);
878                 } else if (!scm_is_eq ($2, SCM_UNSPECIFIED))
879                         parser->parser_error (@2, _("bad expression type"));
880         }
881         | bookpart_body
882         {
883                 Book *book = unsmob_book ($1);
884                 if (!ly_is_module (book->header_))
885                         book->header_ = ly_make_module (false);
886                 parser->lexer_->add_scope (book->header_);
887         } lilypond_header
888         | bookpart_body error {
889                 Book *book = unsmob_book ($1);
890                 book->paper_ = 0;
891                 book->scores_ = SCM_EOL;
892         }
893         ;
894
895 score_block:
896         SCORE '{' score_body '}'        {
897                 unsmob_score ($3)->origin ()->set_spot (@$);
898                 $$ = $3;
899         }
900         ;
901
902 score_headers:
903         /* empty */
904         {
905                 $$ = SCM_EOL;
906         }
907         | score_headers
908         {
909                 if (!scm_is_pair ($1)
910                     || !ly_is_module (scm_car ($1)))
911                         $1 = scm_cons (ly_make_module (false), $1);
912                 parser->lexer_->add_scope (scm_car ($1));
913         } lilypond_header
914         {
915                 $$ = $1;
916         }
917         | score_headers output_def
918         {
919                 Output_def *od = unsmob_output_def ($2);
920                 if (od->lookup_variable (ly_symbol2scm ("is-paper")) == SCM_BOOL_T)
921                 {
922                         parser->parser_error (@2, _("\\paper cannot be used in \\score, use \\layout instead"));
923
924                 }
925                 else
926                 {
927                         if (scm_is_pair ($1) && ly_is_module (scm_car ($1)))
928                                 scm_set_cdr_x ($1, scm_cons ($2, scm_cdr ($1)));
929                         else
930                                 $$ = scm_cons ($2, $1);
931                 }
932         }
933         ;
934
935                 
936
937 score_body:
938         score_headers music {
939                 SCM scorify = ly_lily_module_constant ("scorify-music");
940                 $$ = scm_call_2 (scorify, $2, parser->self_scm ());
941
942                 if (scm_is_pair ($1) && ly_is_module (scm_car ($1)))
943                 {
944                         unsmob_score ($$)->set_header (scm_car ($1));
945                         $1 = scm_cdr ($1);
946                 }
947                 for (SCM p = scm_reverse_x ($1, SCM_EOL);
948                      scm_is_pair (p); p = scm_cdr (p))
949                 {
950                         unsmob_score ($$)->
951                                 add_output_def (unsmob_output_def (scm_car (p)));
952                 }
953         }
954         | embedded_scm_active {
955                 if (!unsmob_score ($1))
956                 {
957                         $$ = (new Score)->unprotect ();
958                         parser->parser_error (@1, _("score expected"));
959                 }
960         }
961         | score_body
962         {
963                 Score *score = unsmob_score ($1);
964                 if (!ly_is_module (score->get_header ()))
965                         score->set_header (ly_make_module (false));
966                 parser->lexer_->add_scope (score->get_header ());
967         } lilypond_header
968         | score_body output_def {
969                 Output_def *od = unsmob_output_def ($2);
970                 if (od->lookup_variable (ly_symbol2scm ("is-paper")) == SCM_BOOL_T)
971                 {
972                         parser->parser_error (@2, _("\\paper cannot be used in \\score, use \\layout instead"));
973
974                 }
975                 else
976                 {
977                         unsmob_score ($1)->add_output_def (od);
978                 }
979         }
980         | score_body error {
981                 unsmob_score ($$)->error_found_ = true;
982         }
983         ;
984
985
986 /*
987         OUTPUT DEF
988 */
989
990 paper_block:
991         output_def {
992                 Output_def *od = unsmob_output_def ($1);
993
994                 if (od->lookup_variable (ly_symbol2scm ("is-paper")) != SCM_BOOL_T)
995                 {
996                         parser->parser_error (@1, _ ("need \\paper for paper block"));
997                         $$ = get_paper (parser)->unprotect ();
998                 }
999         }
1000         ;
1001
1002
1003 output_def:
1004         output_def_body '}' {
1005                 $$ = $1;
1006
1007                 parser->lexer_->remove_scope ();
1008                 parser->lexer_->pop_state ();
1009         }
1010         ;
1011
1012 output_def_head:
1013         PAPER {
1014                 Output_def *p = get_paper (parser);
1015                 p->input_origin_ = @$;
1016                 parser->lexer_->add_scope (p->scope_);
1017                 $$ = p->unprotect ();
1018         }
1019         | MIDI    {
1020                 Output_def *p = get_midi (parser);
1021                 $$ = p->unprotect ();
1022                 parser->lexer_->add_scope (p->scope_);
1023         }
1024         | LAYOUT        {
1025                 Output_def *p = get_layout (parser);
1026
1027                 parser->lexer_->add_scope (p->scope_);
1028                 $$ = p->unprotect ();
1029         }
1030         ;
1031
1032 output_def_head_with_mode_switch:
1033         output_def_head {
1034                 parser->lexer_->push_initial_state ();
1035                 $$ = $1;
1036         }
1037         ;
1038
1039 // We need this weird nonterminal because both music as well as a
1040 // context definition can start with \context and the difference is
1041 // only apparent after looking at the next token.  If it is '{', there
1042 // is still time to escape from notes mode.
1043
1044 music_or_context_def:
1045         music_arg
1046         | context_def_spec_block
1047         ;
1048
1049 output_def_body:
1050         output_def_head_with_mode_switch '{' {
1051                 $$ = $1;
1052                 unsmob_output_def ($$)->input_origin_.set_spot (@$);
1053         }
1054         | output_def_head_with_mode_switch '{' OUTPUT_DEF_IDENTIFIER    {
1055                 Output_def *o = unsmob_output_def ($3);
1056                 o->input_origin_.set_spot (@$);
1057                 $$ = o->self_scm ();
1058                 parser->lexer_->remove_scope ();
1059                 parser->lexer_->add_scope (o->scope_);
1060         }
1061         | output_def_body assignment  {
1062
1063         }
1064         | output_def_body embedded_scm  {
1065
1066         }
1067         | output_def_body
1068         {
1069                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
1070                 parser->lexer_->push_note_state (nn);
1071         } music_or_context_def
1072         {
1073                 parser->lexer_->pop_state ();
1074                 if (unsmob_context_def ($3))
1075                         assign_context_def (unsmob_output_def ($1), $3);
1076                 else {
1077
1078                         SCM proc = parser->lexer_->lookup_identifier
1079                                      ("output-def-music-handler");
1080                         scm_call_3 (proc, parser->self_scm (),
1081                                     $1, $3);
1082                 }
1083         }
1084         | output_def_body error {
1085
1086         }
1087         ;
1088
1089 tempo_event:
1090         TEMPO steno_duration '=' tempo_range    {
1091                 $$ = MAKE_SYNTAX ("tempo", @$, SCM_EOL, $2, $4);
1092         }
1093         | TEMPO scalar steno_duration '=' tempo_range   {
1094                 $$ = MAKE_SYNTAX ("tempo", @$, $2, $3, $5);
1095         }
1096         | TEMPO scalar {
1097                 $$ = MAKE_SYNTAX ("tempo", @$, $2);
1098         } %prec ':'
1099         ;
1100
1101 /*
1102 The representation of a  list is reversed to have efficient append.  */
1103
1104 music_list:
1105         /* empty */ {
1106                 $$ = SCM_EOL;
1107         }
1108         | music_list music_embedded {
1109                 if (unsmob_music ($2))
1110                         $$ = scm_cons ($2, $1);
1111         }
1112         | music_list error {
1113                 Music *m = MY_MAKE_MUSIC("Music", @$);
1114                 // ugh. code dup
1115                 m->set_property ("error-found", SCM_BOOL_T);
1116                 $$ = scm_cons (m->self_scm (), $1);
1117                 m->unprotect (); /* UGH */
1118         }
1119         ;
1120
1121 braced_music_list:
1122         '{' music_list '}'
1123         {
1124                 $$ = scm_reverse_x ($2, SCM_EOL);
1125         }
1126         ;
1127
1128 music:  music_arg
1129         | lyric_element_music
1130         ;
1131
1132 music_embedded:
1133         music
1134         {
1135                 if (unsmob_music ($1)->is_mus_type ("post-event")) {
1136                         parser->parser_error (@1, _ ("unexpected post-event"));
1137                         $$ = SCM_UNSPECIFIED;
1138                 }
1139         }
1140         | music_embedded_backup
1141         {
1142                 $$ = $1;
1143         }
1144         | music_embedded_backup BACKUP lyric_element_music
1145         {
1146                 $$ = $3;
1147         }
1148         | multiplied_duration post_events
1149         {
1150                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
1151
1152                 parser->default_duration_ = *unsmob_duration ($1);
1153                 n->set_property ("duration", $1);
1154
1155                 if (scm_is_pair ($2))
1156                         n->set_property ("articulations",
1157                                          scm_reverse_x ($2, SCM_EOL));
1158                 $$ = n->unprotect ();
1159         }
1160         ;
1161
1162 music_embedded_backup:
1163         embedded_scm
1164         {
1165                 if (scm_is_eq ($1, SCM_UNSPECIFIED))
1166                         $$ = $1;
1167                 else if (Music *m = unsmob_music ($1)) {
1168                         if (m->is_mus_type ("post-event")) {
1169                                 parser->parser_error
1170                                         (@1, _ ("unexpected post-event"));
1171                                 $$ = SCM_UNSPECIFIED;
1172                         } else
1173                                 $$ = $1;
1174                 } else if (parser->lexer_->is_lyric_state ()
1175                            && Text_interface::is_markup ($1))
1176                         MYBACKUP (LYRIC_ELEMENT, $1, @1);
1177                 else {
1178                         @$.warning (_ ("Ignoring non-music expression"));
1179                         $$ = $1;
1180                 }
1181         }
1182         ;
1183
1184 music_arg:
1185         simple_music
1186         {
1187                 $$ = make_music_from_simple (parser, @1, $1);
1188                 if (!unsmob_music ($$))
1189                 {
1190                         parser->parser_error (@1, _ ("music expected"));
1191                         $$ = MAKE_SYNTAX ("void-music", @$);
1192                 }
1193         }
1194         | composite_music %prec COMPOSITE
1195         ;
1196
1197 music_assign:
1198         simple_music
1199         | composite_music %prec COMPOSITE
1200         ;
1201
1202 repeated_music:
1203         REPEAT simple_string unsigned_number music
1204         {
1205                 $$ = MAKE_SYNTAX ("repeat", @$, $2, $3, $4, SCM_EOL);
1206         }
1207         | REPEAT simple_string unsigned_number music ALTERNATIVE braced_music_list
1208         {
1209                 $$ = MAKE_SYNTAX ("repeat", @$, $2, $3, $4, $6);
1210         }
1211         ;
1212
1213 sequential_music:
1214         SEQUENTIAL braced_music_list {
1215                 $$ = MAKE_SYNTAX ("sequential-music", @$, $2);
1216         }
1217         | braced_music_list {
1218                 $$ = MAKE_SYNTAX ("sequential-music", @$, $1);
1219         }
1220         ;
1221
1222 simultaneous_music:
1223         SIMULTANEOUS braced_music_list {
1224                 $$ = MAKE_SYNTAX ("simultaneous-music", @$, $2);
1225         }
1226         | DOUBLE_ANGLE_OPEN music_list DOUBLE_ANGLE_CLOSE       {
1227                 $$ = MAKE_SYNTAX ("simultaneous-music", @$, scm_reverse_x ($2, SCM_EOL));
1228         }
1229         ;
1230
1231 simple_music:
1232         event_chord
1233         | music_property_def
1234         | context_change
1235         ;
1236
1237 context_modification:
1238         WITH
1239         {
1240                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
1241                 parser->lexer_->push_note_state (nn);
1242         } '{' context_mod_list '}'
1243         {
1244                 parser->lexer_->pop_state ();
1245                 $$ = $4;
1246         }
1247         | WITH CONTEXT_MOD_IDENTIFIER
1248         {
1249                 $$ = $2;
1250         }
1251         | CONTEXT_MOD_IDENTIFIER
1252         {
1253                 $$ = $1;
1254         }
1255         | WITH context_modification_arg
1256         {
1257                 if (unsmob_music ($2)) {
1258                         SCM proc = parser->lexer_->lookup_identifier ("context-mod-music-handler");
1259                         $2 = scm_call_2 (proc, parser->self_scm (), $2);
1260                 }
1261                 if (unsmob_context_mod ($2))
1262                         $$ = $2;
1263                 else {
1264                         parser->parser_error (@2, _ ("not a context mod"));
1265                         $$ = Context_mod ().smobbed_copy ();
1266                 }
1267         }
1268         ;
1269
1270 context_modification_arg:
1271         embedded_scm
1272         | MUSIC_IDENTIFIER
1273         ;
1274
1275 optional_context_mod:
1276         /**/ {
1277             $$ = SCM_EOL;
1278         }
1279         | context_modification
1280         {
1281               $$ = $1;
1282         }
1283         ;
1284
1285 context_mod_list:
1286         /**/ {
1287             $$ = Context_mod ().smobbed_copy ();
1288         }
1289         | context_mod_list context_mod  {
1290                 if (!SCM_UNBNDP ($2))
1291                         unsmob_context_mod ($1)->add_context_mod ($2);
1292         }
1293         | context_mod_list CONTEXT_MOD_IDENTIFIER {
1294                  Context_mod *md = unsmob_context_mod ($2);
1295                  if (md)
1296                      unsmob_context_mod ($1)->add_context_mods (md->get_mods ());
1297         }
1298         | context_mod_list context_mod_embedded {
1299                 unsmob_context_mod ($1)->add_context_mods
1300                         (unsmob_context_mod ($2)->get_mods ());
1301         }
1302         ;
1303
1304 composite_music:
1305         complex_music
1306         | music_bare
1307         ;
1308
1309 music_bare:
1310         mode_changed_music
1311         | MUSIC_IDENTIFIER
1312         | grouped_music_list
1313         ;
1314
1315 grouped_music_list:
1316         simultaneous_music              { $$ = $1; }
1317         | sequential_music              { $$ = $1; }
1318         ;
1319
1320 /* Function argument lists are arguably the most complex part in the
1321  * parser.  They are pretty tricky to understand because of the way
1322  * they are processed, and because of optional arguments that can be
1323  * omitted.  When there are several optional arguments in a row,
1324  * omitting one automatically omits all following arguments.  Optional
1325  * arguments can only be omitted when either
1326  *
1327  * a) the omission is explicitly started with \default
1328  * b) the omission is implicitly started by an argument not matching
1329  *    its predicate, and there is a mandatory argument later that can
1330  *    "catch" the argument that does not fit.
1331  *
1332  * When argument parsing starts, the lexer pushes EXPECT_SCM tokens
1333  * (corresponding to mandatory arguments and having a predicate
1334  * function as semantic value) or EXPECT_OPTIONAL EXPECT_SCM (where
1335  * the semantic value of the EXPECT_OPTIONAL token is the default to
1336  * use when the optional argument is omitted, and EXPECT_SCM again has
1337  * the argument predicate as semantic value) in reverse order to the
1338  * parser, followed by EXPECT_NO_MORE_ARGS.  The argument list is then
1339  * processed inside-out while actual tokens are consumed.
1340  *
1341  * This means that the argument list tokens determine the actions
1342  * taken as they arrive.  The structure of the argument list is known
1343  * to the parser and stored in its parse stack when the first argument
1344  * is being parsed.  What the parser does not know is which predicates
1345  * will match and whether or not \default will be appearing in the
1346  * argument list, and where.
1347  *
1348  * Sequences of 0 or more optional arguments are scanned using either
1349  * function_arglist_backup or function_arglist_nonbackup.  The first
1350  * is used when optional arguments are followed by at least one
1351  * mandatory argument: in that case optional arguments may be skipped
1352  * by either a false predicate (in which case the expression will be
1353  * pushed back as one or more tokens, preceded by a BACKUP token) or
1354  * by using \default.
1355  *
1356  * If optional arguments are at the end of the argument list, they are
1357  * instead scanned using function_arglist_nonbackup: here the only
1358  * manner to enter into skipping of optional arguments is the use of
1359  * \default.
1360  *
1361  * The argument list of a normal function call is parsed using
1362  * function_arglist.  The part of an argument list before a mandatory
1363  * argument is parsed using function_arglist_optional.
1364  *
1365  * The difference is that leading optional arguments are scanned using
1366  * function_arglist_nonbackup and function_arglist_backup,
1367  * respectively.
1368  *
1369  * Most other details are obvious in the rules themselves.
1370  *
1371  */
1372
1373 symbol_list_arg:
1374         SYMBOL_LIST
1375         | SYMBOL_LIST '.' symbol_list_rev
1376         {
1377                 $$ = scm_append (scm_list_2 ($1, scm_reverse_x ($3, SCM_EOL)));
1378         }
1379         ;
1380
1381 symbol_list_rev:
1382         symbol_list_part
1383         | symbol_list_rev '.' symbol_list_part
1384         {
1385                 $$ = scm_append_x (scm_list_2 ($3, $1));
1386         }
1387         ;
1388
1389 // symbol_list_part delivers elements in reverse copy.
1390
1391 symbol_list_part:
1392         symbol_list_element
1393         {
1394                 SCM sym_l_p = ly_lily_module_constant ("symbol-list?");
1395                 $$ = try_string_variants (sym_l_p, $1);
1396                 if (SCM_UNBNDP ($$)) {
1397                         parser->parser_error (@1, _("not a symbol"));
1398                         $$ = SCM_EOL;
1399                 } else
1400                         $$ = scm_reverse ($$);
1401         }
1402         ;
1403
1404
1405 symbol_list_element:
1406         STRING
1407         | embedded_scm_bare
1408         ;
1409
1410
1411 function_arglist_nonbackup:
1412         function_arglist_common
1413         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup post_event_nofinger
1414         {
1415                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1416         }
1417         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup '-' UNSIGNED
1418         {
1419                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1420                 if (scm_is_true (scm_call_1 ($2, n)))
1421                         $$ = scm_cons (n, $3);
1422                 else {
1423                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @5);
1424                         t->set_property ("digit", $5);
1425                         $$ = check_scheme_arg (parser, @4, t->unprotect (),
1426                                                $3, $2, n);
1427                 }
1428                 
1429         }
1430         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup '-' REAL
1431         {
1432                 $$ = check_scheme_arg (parser, @4,
1433                                        scm_difference ($5, SCM_UNDEFINED),
1434                                        $3, $2);
1435         }
1436         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup '-' NUMBER_IDENTIFIER
1437         {
1438                 $$ = check_scheme_arg (parser, @4,
1439                                        scm_difference ($5, SCM_UNDEFINED),
1440                                        $3, $2);
1441         }
1442         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup embedded_scm_arg
1443         {
1444                 if (scm_is_true (scm_call_1 ($2, $4)))
1445                         $$ = scm_cons ($4, $3);
1446                 else
1447                         $$ = check_scheme_arg (parser, @4,
1448                                                make_music_from_simple
1449                                                (parser, @4, $4),
1450                                                $3, $2);
1451         }
1452         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup bare_number_common
1453         {
1454                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1455         }
1456         | function_arglist_nonbackup_reparse REPARSE duration_length
1457         {
1458                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1459         }
1460         | function_arglist_nonbackup_reparse REPARSE bare_number_common
1461         {
1462                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1463         }
1464         | function_arglist_nonbackup_reparse REPARSE SCM_ARG
1465         {
1466                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1467         }
1468         | function_arglist_nonbackup_reparse REPARSE lyric_element_music
1469         {
1470                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1471         }
1472         | function_arglist_nonbackup_reparse REPARSE symbol_list_arg
1473         {
1474                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1475         }
1476         ;
1477
1478 function_arglist_nonbackup_reparse:
1479         EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup SCM_IDENTIFIER
1480         {
1481                 $$ = $3;
1482                 SCM res = try_string_variants ($2, $4);
1483                 if (!SCM_UNBNDP (res))
1484                         if (scm_is_pair (res))
1485                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1486                         else
1487                                 MYREPARSE (@4, $2, SCM_ARG, res);
1488                 else if (scm_is_true
1489                          (scm_call_1
1490                           ($2, make_music_from_simple
1491                            (parser, @4, $4))))
1492                         MYREPARSE (@4, $2, STRING, $4);
1493                 else
1494                         MYREPARSE (@4, $2, SCM_ARG, $4);
1495         }
1496         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup STRING
1497         {
1498                 $$ = $3;
1499                 SCM res = try_string_variants ($2, $4);
1500                 if (!SCM_UNBNDP (res))
1501                         if (scm_is_pair (res))
1502                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1503                         else
1504                                 MYREPARSE (@4, $2, SCM_ARG, res);
1505                 else if (scm_is_true
1506                          (scm_call_1
1507                           ($2, make_music_from_simple
1508                            (parser, @4, $4))))
1509                         MYREPARSE (@4, $2, STRING, $4);
1510                 else
1511                         MYREPARSE (@4, $2, SCM_ARG, $4);
1512         }
1513         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup full_markup
1514         {
1515                 $$ = $3;
1516                 if (scm_is_true (scm_call_1 ($2, $4)))
1517                         MYREPARSE (@4, $2, SCM_ARG, $4);
1518                 else if (scm_is_true
1519                          (scm_call_1
1520                           ($2, make_music_from_simple
1521                            (parser, @4, $4))))
1522                         MYREPARSE (@4, $2, STRING, $4);
1523                 else
1524                         MYREPARSE (@4, $2, SCM_ARG, $4);
1525         }
1526         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup UNSIGNED
1527         {
1528                 $$ = $3;
1529                 if (scm_is_true (scm_call_1 ($2, $4)))
1530                         MYREPARSE (@4, $2, REAL, $4);
1531                 else {
1532                         SCM d = make_duration ($4);
1533                         if (SCM_UNBNDP (d) || scm_is_false (scm_call_1 ($2, d)))
1534                                 MYREPARSE (@4, $2, REAL, $4); // trigger error
1535                         else
1536                                 MYREPARSE (@4, $2, DURATION_IDENTIFIER, d);
1537                 }
1538         }
1539         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup DURATION_IDENTIFIER {
1540                 $$ = $3;
1541                 MYREPARSE (@4, $2, DURATION_IDENTIFIER, $4);
1542         }
1543         ;
1544
1545
1546 // function_arglist_backup can't occur at the end of an argument
1547 // list.
1548 function_arglist_backup:
1549         function_arglist_common
1550         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup embedded_scm_arg
1551         {
1552                 if (scm_is_true (scm_call_1 ($2, $4)))
1553                         $$ = scm_cons ($4, $3);
1554                 else {
1555                         $$ = make_music_from_simple (parser, @4, $4);
1556                         if (scm_is_true (scm_call_1 ($2, $$)))
1557                                 $$ = scm_cons ($$, $3);
1558                         else
1559                         {
1560                                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1561                                 MYBACKUP (SCM_ARG, $4, @4);
1562                         }
1563                 }
1564         }
1565         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup post_event_nofinger
1566         {
1567                 if (scm_is_true (scm_call_1 ($2, $4)))
1568                 {
1569                         $$ = scm_cons ($4, $3);
1570                 } else {
1571                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1572                         MYBACKUP (EVENT_IDENTIFIER, $4, @4);
1573                 }
1574         }
1575         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup full_markup
1576         {
1577                 if (scm_is_true (scm_call_1 ($2, $4)))
1578                         $$ = scm_cons ($4, $3);
1579                 else {
1580                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1581                         MYBACKUP (LYRIC_ELEMENT, $4, @4);
1582                 }
1583         }
1584         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup UNSIGNED
1585         {
1586                 if (scm_is_true (scm_call_1 ($2, $4)))
1587                 {
1588                         MYREPARSE (@4, $2, REAL, $4);
1589                         $$ = $3;
1590                 } else {
1591                         SCM d = make_duration ($4);
1592                         if (SCM_UNBNDP (d) || scm_is_false (scm_call_1 ($2, d)))
1593                         {
1594                                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1595                                 MYBACKUP (UNSIGNED, $4, @4);
1596                         } else {
1597                                 MYREPARSE (@4, $2, DURATION_IDENTIFIER, d);
1598                                 $$ = $3;
1599                         }
1600                 }
1601         }
1602         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup REAL
1603         {
1604                 if (scm_is_true (scm_call_1 ($2, $4)))
1605                 {
1606                         $$ = $3;
1607                         MYREPARSE (@4, $2, REAL, $4);
1608                 } else {
1609                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1610                         MYBACKUP (REAL, $4, @4);
1611                 }
1612         }
1613         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup NUMBER_IDENTIFIER
1614         {
1615                 if (scm_is_true (scm_call_1 ($2, $4)))
1616                 {
1617                         $$ = scm_cons ($4, $3);
1618                 } else {
1619                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1620                         MYBACKUP (NUMBER_IDENTIFIER, $4, @4);
1621                 }
1622         }
1623         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup '-' UNSIGNED
1624         {
1625                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1626                 if (scm_is_true (scm_call_1 ($2, n))) {
1627                         $$ = $3;
1628                         MYREPARSE (@5, $2, REAL, n);
1629                 } else {
1630                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @5);
1631                         t->set_property ("digit", $5);
1632                         $$ = t->unprotect ();
1633                         if (scm_is_true (scm_call_1 ($2, $$)))
1634                                 $$ = scm_cons ($$, $3);
1635                         else {
1636                                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1637                                 MYBACKUP (UNSIGNED, $5, @5);
1638                                 parser->lexer_->push_extra_token (@4, '-');
1639                         }
1640                 }
1641                 
1642         }
1643         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup '-' REAL
1644         {
1645                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1646                 if (scm_is_true (scm_call_1 ($2, n))) {
1647                         MYREPARSE (@5, $2, REAL, n);
1648                         $$ = $3;
1649                 } else {
1650                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1651                         MYBACKUP (REAL, n, @5);
1652                 }
1653         }
1654         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup '-' NUMBER_IDENTIFIER
1655         {
1656                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1657                 if (scm_is_true (scm_call_1 ($2, n))) {
1658                         $$ = scm_cons (n, $3);
1659                 } else {
1660                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1661                         MYBACKUP (NUMBER_IDENTIFIER, n, @5);
1662                 }
1663         }
1664         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup DURATION_IDENTIFIER
1665         {
1666                 if (scm_is_true (scm_call_1 ($2, $4)))
1667                 {
1668                         MYREPARSE (@4, $2, DURATION_IDENTIFIER, $4);
1669                         $$ = $3;
1670                 } else {
1671                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1672                         MYBACKUP (DURATION_IDENTIFIER, $4, @4);
1673                 }
1674         }
1675         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup SCM_IDENTIFIER
1676         {
1677                 SCM res = try_string_variants ($2, $4);
1678                 if (!SCM_UNBNDP (res))
1679                         if (scm_is_pair (res)) {
1680                                 $$ = $3;
1681                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1682                         }
1683                         else
1684                                 $$ = scm_cons (res, $3);
1685                 else {
1686                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1687                         MYBACKUP (SCM_IDENTIFIER, $4, @4);
1688                 }
1689         }
1690         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup STRING
1691         {
1692                 SCM res = try_string_variants ($2, $4);
1693                 if (!SCM_UNBNDP (res))
1694                         if (scm_is_pair (res)) {
1695                                 $$ = $3;
1696                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1697                         }
1698                         else
1699                                 $$ = scm_cons (res, $3);
1700                 else {
1701                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1702                         MYBACKUP (STRING, $4, @4);
1703                 }
1704         }
1705         | function_arglist_backup REPARSE music_assign
1706         {
1707                 if (scm_is_true (scm_call_1 ($2, $3)))
1708                         $$ = scm_cons ($3, $1);
1709                 else
1710                         $$ = check_scheme_arg (parser, @3,
1711                                                make_music_from_simple
1712                                                (parser, @3, $3),
1713                                                $1, $2);
1714         }
1715         | function_arglist_backup REPARSE bare_number_common
1716         {
1717                 $$ = check_scheme_arg (parser, @3,
1718                                        $3, $1, $2);
1719         }
1720         | function_arglist_backup REPARSE duration_length
1721         {
1722                 $$ = check_scheme_arg (parser, @3,
1723                                        $3, $1, $2);
1724         }
1725         | function_arglist_backup REPARSE symbol_list_arg
1726         {
1727                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1728         }
1729         ;
1730
1731 function_arglist:
1732         function_arglist_nonbackup
1733         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_nonbackup DEFAULT
1734         {
1735                 $$ = scm_cons (loc_on_music (@4, $1), $3);
1736         }
1737         ;
1738
1739 function_arglist_skip_nonbackup:
1740         function_arglist_nonbackup
1741         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_nonbackup
1742         {
1743                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1744         }
1745         ;
1746
1747 function_arglist_common:
1748         EXPECT_NO_MORE_ARGS {
1749                 $$ = SCM_EOL;
1750         }
1751         | EXPECT_SCM function_arglist_optional embedded_scm_arg
1752         {
1753                 if (scm_is_true (scm_call_1 ($1, $3)))
1754                         $$ = scm_cons ($3, $2);
1755                 else
1756                         $$ = check_scheme_arg (parser, @3,
1757                                                make_music_from_simple
1758                                                (parser, @3, $3),
1759                                                $2, $1);
1760         }
1761         | EXPECT_SCM function_arglist_optional bare_number_common
1762         {
1763                 $$ = check_scheme_arg (parser, @3,
1764                                        $3, $2, $1);
1765         }
1766         | EXPECT_SCM function_arglist_optional post_event_nofinger
1767         {
1768                 $$ = check_scheme_arg (parser, @3,
1769                                        $3, $2, $1);
1770         }
1771         | EXPECT_SCM function_arglist_optional '-' NUMBER_IDENTIFIER
1772         {
1773                 SCM n = scm_difference ($4, SCM_UNDEFINED);
1774                 $$ = check_scheme_arg (parser, @4, n, $2, $1);
1775         }
1776         | function_arglist_common_reparse REPARSE SCM_ARG
1777         {
1778                 $$ = check_scheme_arg (parser, @3,
1779                                        $3, $1, $2);
1780         }
1781         | function_arglist_common_reparse REPARSE lyric_element_music
1782         {
1783                 $$ = check_scheme_arg (parser, @3,
1784                                        $3, $1, $2);
1785         }
1786         | function_arglist_common_reparse REPARSE bare_number_common
1787         {
1788                 $$ = check_scheme_arg (parser, @3,
1789                                        $3, $1, $2);
1790         }
1791         | function_arglist_common_reparse REPARSE duration_length
1792         {
1793                 $$ = check_scheme_arg (parser, @3,
1794                                        $3, $1, $2);
1795         }
1796         | function_arglist_common_reparse REPARSE symbol_list_arg
1797         {
1798                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1799         }
1800         ;
1801
1802 function_arglist_common_reparse:
1803         EXPECT_SCM function_arglist_optional SCM_IDENTIFIER
1804         {
1805                 $$ = $2;
1806                 SCM res = try_string_variants ($1, $3);
1807                 if (!SCM_UNBNDP (res))
1808                         if (scm_is_pair (res))
1809                                 MYREPARSE (@3, $1, SYMBOL_LIST, res);
1810                         else
1811                                 MYREPARSE (@3, $1, SCM_ARG, res);
1812                 else if (scm_is_true
1813                          (scm_call_1
1814                           ($1, make_music_from_simple (parser, @3, $3))))
1815                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
1816                 else
1817                         // This is going to flag a syntax error, we
1818                         // know the predicate to be false.
1819                         MYREPARSE (@3, $1, SCM_ARG, $3);
1820         }
1821         | EXPECT_SCM function_arglist_optional STRING
1822         {
1823                 $$ = $2;
1824                 SCM res = try_string_variants ($1, $3);
1825                 if (!SCM_UNBNDP (res))
1826                         if (scm_is_pair (res))
1827                                 MYREPARSE (@3, $1, SYMBOL_LIST, res);
1828                         else
1829                                 MYREPARSE (@3, $1, SCM_ARG, res);
1830                 else if (scm_is_true
1831                          (scm_call_1
1832                           ($1, make_music_from_simple (parser, @3, $3))))
1833                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
1834                 else
1835                         // This is going to flag a syntax error, we
1836                         // know the predicate to be false.
1837                         MYREPARSE (@3, $1, SCM_ARG, $3);
1838         }
1839         | EXPECT_SCM function_arglist_optional full_markup
1840         {
1841                 $$ = $2;
1842                 if (scm_is_true (scm_call_1 ($1, $3)))
1843                         MYREPARSE (@3, $1, SCM_ARG, $3);
1844                 else if (scm_is_true
1845                          (scm_call_1
1846                           ($1, make_music_from_simple (parser, @3, $3))))
1847                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
1848                 else
1849                         // This is going to flag a syntax error, we
1850                         // know the predicate to be false.
1851                         MYREPARSE (@3, $1, SCM_ARG, $3);
1852         }
1853         | EXPECT_SCM function_arglist_optional UNSIGNED
1854         {
1855                 $$ = $2;
1856                 if (scm_is_true (scm_call_1 ($1, $3)))
1857                         MYREPARSE (@3, $1, REAL, $3);
1858                 else {
1859                         SCM d = make_duration ($3);
1860                         if (SCM_UNBNDP (d) || scm_is_false (scm_call_1 ($1, d)))
1861                                 MYREPARSE (@3, $1, REAL, $3);
1862                         else
1863                                 MYREPARSE (@3, $1, DURATION_IDENTIFIER, d);
1864                 }
1865         }
1866         | EXPECT_SCM function_arglist_optional DURATION_IDENTIFIER
1867         {
1868                 $$ = $2;
1869                 MYREPARSE (@3, $1, DURATION_IDENTIFIER, $3);
1870         }
1871         | EXPECT_SCM function_arglist_optional '-' UNSIGNED
1872         {
1873                 $$ = $2;
1874                 SCM n = scm_difference ($4, SCM_UNDEFINED);
1875                 if (scm_is_true (scm_call_1 ($1, n)))
1876                         MYREPARSE (@4, $1, REAL, n);
1877                 else {
1878                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @4);
1879                         t->set_property ("digit", $4);
1880                         SCM m = t->unprotect ();
1881                         if (scm_is_true (scm_call_1 ($1, m)))
1882                                 MYREPARSE (@4, $1, SCM_ARG, m);
1883                         else
1884                                 MYREPARSE (@4, $1, SCM_ARG, $4);
1885                 }
1886                 
1887         }
1888         | EXPECT_SCM function_arglist_optional '-' REAL
1889         {
1890                 $$ = $2;
1891                 SCM n = scm_difference ($4, SCM_UNDEFINED);
1892                 MYREPARSE (@4, $1, REAL, n);
1893         }
1894         ;
1895
1896 function_arglist_optional:
1897         function_arglist_backup
1898         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_backup DEFAULT
1899         {
1900                 $$ = scm_cons (loc_on_music (@4, $1), $3);
1901         }
1902         | function_arglist_skip_backup BACKUP
1903         ;
1904
1905 function_arglist_skip_backup:
1906         function_arglist_backup
1907         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_backup
1908         {
1909                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1910         }
1911         ;
1912
1913 music_function_call:
1914         MUSIC_FUNCTION function_arglist {
1915                 $$ = MAKE_SYNTAX ("music-function", @$,
1916                                          $1, $2);
1917         }
1918         ;
1919
1920
1921 optional_id:
1922         /**/ { $$ = SCM_EOL; }
1923         | '=' simple_string {
1924                 $$ = $2;
1925         }
1926         ;
1927
1928 complex_music:
1929         music_function_call
1930         | repeated_music                { $$ = $1; }
1931         | re_rhythmed_music     { $$ = $1; }
1932         | complex_music_prefix music
1933         {
1934                 $$ = FINISH_MAKE_SYNTAX ($1, @$, $2);
1935         }
1936         ;
1937
1938 complex_music_prefix:
1939         CONTEXT symbol optional_id optional_context_mod {
1940                 Context_mod *ctxmod = unsmob_context_mod ($4);
1941                 SCM mods = SCM_EOL;
1942                 if (ctxmod)
1943                         mods = ctxmod->get_mods ();
1944                 $$ = START_MAKE_SYNTAX ("context-specification", $2, $3, mods, SCM_BOOL_F);
1945         }
1946         | NEWCONTEXT symbol optional_id optional_context_mod {
1947                 Context_mod *ctxmod = unsmob_context_mod ($4);
1948                 SCM mods = SCM_EOL;
1949                 if (ctxmod)
1950                         mods = ctxmod->get_mods ();
1951                 $$ = START_MAKE_SYNTAX ("context-specification", $2, $3, mods, SCM_BOOL_T);
1952         }
1953         ;
1954
1955 mode_changed_music:
1956         mode_changing_head grouped_music_list {
1957                 if ($1 == ly_symbol2scm ("chords"))
1958                 {
1959                   $$ = MAKE_SYNTAX ("unrelativable-music", @$, $2);
1960                 }
1961                 else
1962                 {
1963                   $$ = $2;
1964                 }
1965                 parser->lexer_->pop_state ();
1966         }
1967         | mode_changing_head_with_context optional_context_mod grouped_music_list {
1968                 Context_mod *ctxmod = unsmob_context_mod ($2);
1969                 SCM mods = SCM_EOL;
1970                 if (ctxmod)
1971                         mods = ctxmod->get_mods ();
1972                 $$ = MAKE_SYNTAX ("context-specification", @$, $1, SCM_EOL, mods, SCM_BOOL_T, $3);
1973                 if ($1 == ly_symbol2scm ("ChordNames"))
1974                 {
1975                   $$ = MAKE_SYNTAX ("unrelativable-music", @$, $$);
1976                 }
1977                 parser->lexer_->pop_state ();
1978         }
1979         ;
1980
1981 mode_changing_head:
1982         NOTEMODE {
1983                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
1984                 parser->lexer_->push_note_state (nn);
1985
1986                 $$ = ly_symbol2scm ("notes");
1987         }
1988         | DRUMMODE
1989                 {
1990                 SCM nn = parser->lexer_->lookup_identifier ("drumPitchNames");
1991                 parser->lexer_->push_note_state (nn);
1992
1993                 $$ = ly_symbol2scm ("drums");
1994         }
1995         | FIGUREMODE {
1996                 parser->lexer_->push_figuredbass_state ();
1997
1998                 $$ = ly_symbol2scm ("figures");
1999         }
2000         | CHORDMODE {
2001                 SCM nn = parser->lexer_->lookup_identifier ("chordmodifiers");
2002                 parser->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
2003                 nn = parser->lexer_->lookup_identifier ("pitchnames");
2004                 parser->lexer_->push_chord_state (nn);
2005                 $$ = ly_symbol2scm ("chords");
2006
2007         }
2008         | LYRICMODE
2009                 { parser->lexer_->push_lyric_state ();
2010                 $$ = ly_symbol2scm ("lyrics");
2011         }
2012         ;
2013
2014 mode_changing_head_with_context:
2015         DRUMS {
2016                 SCM nn = parser->lexer_->lookup_identifier ("drumPitchNames");
2017                 parser->lexer_->push_note_state (nn);
2018
2019                 $$ = ly_symbol2scm ("DrumStaff");
2020         }
2021         | FIGURES {
2022                 parser->lexer_->push_figuredbass_state ();
2023
2024                 $$ = ly_symbol2scm ("FiguredBass");
2025         }
2026         | CHORDS {
2027                 SCM nn = parser->lexer_->lookup_identifier ("chordmodifiers");
2028                 parser->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
2029                 nn = parser->lexer_->lookup_identifier ("pitchnames");
2030                 parser->lexer_->push_chord_state (nn);
2031                 $$ = ly_symbol2scm ("ChordNames");
2032         }
2033         | LYRICS
2034                 { parser->lexer_->push_lyric_state ();
2035                 $$ = ly_symbol2scm ("Lyrics");
2036         }
2037         ;
2038
2039 new_lyrics:
2040         ADDLYRICS { parser->lexer_->push_lyric_state (); }
2041         /*cont */
2042         composite_music {
2043         /* Can also use music at the expensive of two S/Rs similar to
2044            \repeat \alternative */
2045                 parser->lexer_->pop_state ();
2046
2047                 $$ = scm_cons ($3, SCM_EOL);
2048         }
2049         | new_lyrics ADDLYRICS {
2050                 parser->lexer_->push_lyric_state ();
2051         } composite_music {
2052                 parser->lexer_->pop_state ();
2053                 $$ = scm_cons ($4, $1);
2054         }
2055         ;
2056
2057 re_rhythmed_music:
2058         composite_music new_lyrics {
2059                 $$ = MAKE_SYNTAX ("add-lyrics", @$, $1, scm_reverse_x ($2, SCM_EOL));
2060         } %prec COMPOSITE
2061         | LYRICSTO simple_string {
2062                 parser->lexer_->push_lyric_state ();
2063         } music {
2064                 parser->lexer_->pop_state ();
2065                 $$ = MAKE_SYNTAX ("lyric-combine", @$, $2, $4);
2066         }
2067         ;
2068
2069 context_change:
2070         CHANGE symbol '=' simple_string  {
2071                 $$ = MAKE_SYNTAX ("context-change", @$, $2, $4);
2072         }
2073         ;
2074
2075
2076 property_path:
2077         symbol_list_rev  {
2078                 $$ = scm_reverse_x ($1, SCM_EOL);
2079         }
2080         | symbol_list_rev property_path {
2081                 $$ = scm_reverse_x ($1, $2);
2082         }
2083         ;
2084
2085 property_operation:
2086         symbol '=' scalar {
2087                 $$ = scm_list_3 (ly_symbol2scm ("assign"), $1, $3);
2088         }
2089         | UNSET symbol {
2090                 $$ = scm_list_2 (ly_symbol2scm ("unset"), $2);
2091         }
2092         | OVERRIDE property_path '=' scalar {
2093                 if (scm_ilength ($2) < 2) {
2094                         parser->parser_error (@2, _("bad grob property path"));
2095                         $$ = SCM_UNDEFINED;
2096                 } else {
2097                         $$ = scm_cons (ly_symbol2scm ("push"),
2098                                        scm_cons2 (scm_car ($2),
2099                                                   $4,
2100                                                   scm_cdr ($2)));
2101                 }
2102         }
2103         | REVERT revert_arg {
2104                 $$ = scm_cons (ly_symbol2scm ("pop"), $2);
2105         }
2106         ;
2107
2108 // This is all quite awkward for the sake of substantial backward
2109 // compatibility while at the same time allowing a more "natural" form
2110 // of specification not separating grob specification from grob
2111 // property path.  The purpose of this definition of revert_arg is to
2112 // allow the symbol list which specifies grob and property to revert
2113 // to be optionally be split into two parts after the grob (which in
2114 // this case is just the first element of the list).  symbol_list_part
2115 // is only one path component, but it can be parsed without lookahead,
2116 // so we can follow it with a synthetic BACKUP token when needed.  If
2117 // the first symbol_list_part already contains multiple elements (only
2118 // possible if a Scheme expression provides them), we just parse for
2119 // additional elements introduced by '.', which is what the
2120 // SYMBOL_LIST backup in connection with the immediately following
2121 // rule using symbol_list_arg does.
2122 //
2123 // As long as we don't have our coffers filled with both grob and at
2124 // least one grob property specification, the rest of the required
2125 // symbol list chain may be provided either with or without a leading
2126 // dot.  This is for both allowing the traditional
2127 // \revert Accidental #'color
2128 // as well as well as the "naive" form
2129 // \revert Accidental.color
2130
2131 revert_arg:
2132         revert_arg_backup BACKUP symbol_list_arg
2133         {
2134                 $$ = $3;
2135         }
2136         ;
2137
2138 revert_arg_backup:
2139         revert_arg_part
2140         {
2141                 if (scm_is_null ($1)
2142                     || scm_is_null (scm_cdr ($1)))
2143                         MYBACKUP (SCM_ARG, $1, @1);
2144                 else
2145                         MYBACKUP (SYMBOL_LIST, scm_reverse_x ($1, SCM_EOL), @1);
2146         }
2147         ;
2148
2149 // revert_arg_part delivers results in reverse
2150 revert_arg_part:
2151         symbol_list_part
2152         | revert_arg_backup BACKUP SCM_ARG '.' symbol_list_part
2153         {
2154                 $$ = scm_append_x (scm_list_2 ($5, $3));
2155         }
2156         | revert_arg_backup BACKUP SCM_ARG symbol_list_part
2157         {
2158                 $$ = scm_append_x (scm_list_2 ($4, $3));
2159         }               
2160         ;
2161
2162 context_def_mod:
2163         CONSISTS { $$ = ly_symbol2scm ("consists"); }
2164         | REMOVE { $$ = ly_symbol2scm ("remove"); }
2165
2166         | ACCEPTS { $$ = ly_symbol2scm ("accepts"); }
2167         | DEFAULTCHILD { $$ = ly_symbol2scm ("default-child"); }
2168         | DENIES { $$ = ly_symbol2scm ("denies"); }
2169
2170         | ALIAS { $$ = ly_symbol2scm ("alias"); }
2171         | TYPE { $$ = ly_symbol2scm ("translator-type"); }
2172         | DESCRIPTION { $$ = ly_symbol2scm ("description"); }
2173         | NAME { $$ = ly_symbol2scm ("context-name"); }
2174         ;
2175
2176 context_mod:
2177         property_operation { $$ = $1; }
2178         | context_def_mod STRING {
2179                 $$ = scm_list_2 ($1, $2);
2180         }
2181         | context_def_mod embedded_scm
2182         {
2183                 if (!scm_is_string ($2)
2184                     && ly_symbol2scm ("consists") != $1
2185                     && ly_symbol2scm ("remove") != $1)
2186                 {
2187                         $$ = SCM_EOL;
2188                         parser->parser_error (@1, _ ("only \\consists and \\remove take non-string argument."));
2189                 }
2190                 else
2191                 {
2192                         $$ = scm_list_2 ($1, $2);
2193                 }
2194         }
2195         ;
2196
2197 // If defined, at least two members.
2198 grob_prop_spec:
2199         symbol_list_rev
2200         {
2201                 SCM l = scm_reverse_x ($1, SCM_EOL);
2202                 if (scm_is_pair (l)
2203                     && to_boolean
2204                     (scm_object_property (scm_car (l),
2205                                           ly_symbol2scm ("is-grob?"))))
2206                         l = scm_cons (ly_symbol2scm ("Bottom"), l);
2207                 if (scm_is_null (l) || scm_is_null (scm_cdr (l))) {
2208                         parser->parser_error (@1, _ ("bad grob property path"));
2209                         l = SCM_UNDEFINED;
2210                 }
2211                 $$ = l;
2212         }
2213         ;
2214
2215 // If defined, at least three members
2216 grob_prop_path:
2217         grob_prop_spec
2218         {
2219                 if (!SCM_UNBNDP ($1) && scm_is_null (scm_cddr ($1)))
2220                 {
2221                         parser->parser_error (@1, _ ("bad grob property path"));
2222                         $$ = SCM_UNDEFINED;
2223                 }
2224         }
2225         | grob_prop_spec property_path
2226         {
2227                 if (!SCM_UNBNDP ($1)) {
2228                         $$ = scm_append_x (scm_list_2 ($1, $2));
2229                         if (scm_is_null (scm_cddr ($$))) {
2230                                 parser->parser_error (@$, _ ("bad grob property path"));
2231                                 $$ = SCM_UNDEFINED;
2232                         }
2233                 }
2234
2235         }
2236         ;
2237
2238 // Exactly two elements or undefined
2239 context_prop_spec:
2240         symbol_list_rev
2241         {
2242                 SCM l = scm_reverse_x ($1, SCM_EOL);
2243                 switch (scm_ilength (l)) {
2244                 case 1:
2245                         l = scm_cons (ly_symbol2scm ("Bottom"), l);
2246                 case 2:
2247                         break;
2248                 default:
2249                         parser->parser_error (@1, _ ("bad context property path"));
2250                         l = SCM_UNDEFINED;
2251                 }
2252                 $$ = l;
2253         }
2254         ;
2255
2256 simple_music_property_def:
2257         OVERRIDE grob_prop_path '=' scalar {
2258                 if (SCM_UNBNDP ($2))
2259                         $$ = SCM_UNDEFINED;
2260                 else {
2261                         $$ = scm_list_5 (scm_car ($2),
2262                                          ly_symbol2scm ("OverrideProperty"),
2263                                          scm_cadr ($2),
2264                                          $4,
2265                                          scm_cddr ($2));
2266                 }
2267         }
2268         | REVERT simple_revert_context revert_arg {
2269                 $$ = scm_list_4 ($2,
2270                                  ly_symbol2scm ("RevertProperty"),
2271                                  scm_car ($3),
2272                                  scm_cdr ($3));
2273         }
2274         | SET context_prop_spec '=' scalar {
2275                 if (SCM_UNBNDP ($2))
2276                         $$ = SCM_UNDEFINED;
2277                 else
2278                         $$ = scm_list_4 (scm_car ($2),
2279                                          ly_symbol2scm ("PropertySet"),
2280                                          scm_cadr ($2),
2281                                          $4);
2282         }
2283         | UNSET context_prop_spec {
2284                 if (SCM_UNBNDP ($2))
2285                         $$ = SCM_UNDEFINED;
2286                 else
2287                         $$ = scm_list_3 (scm_car ($2),
2288                                          ly_symbol2scm ("PropertyUnset"),
2289                                          scm_cadr ($2));
2290         }
2291         ;
2292
2293
2294 // This is all quite awkward for the sake of substantial backward
2295 // compatibility while at the same time allowing a more "natural" form
2296 // of specification not separating grob specification from grob
2297 // property path.  The purpose of this definition of
2298 // simple_revert_context is to allow the symbol list which specifies
2299 // grob and property to revert to be optionally be split into two
2300 // parts after the grob (which may be preceded by a context
2301 // specification, a case which we distinguish by checking whether the
2302 // first symbol is a valid grob symbol instead).
2303 //
2304 // See revert_arg above for the main work horse of this arrangement.
2305 // simple_revert_context just caters for the context and delegates the
2306 // rest of the job to revert_arg.
2307
2308 simple_revert_context:
2309         symbol_list_part
2310         {
2311                 $1 = scm_reverse_x ($1, SCM_EOL);
2312                 if (scm_is_null ($1)
2313                     || to_boolean
2314                     (scm_object_property (scm_car ($1),
2315                                           ly_symbol2scm ("is-grob?")))) {
2316                         $$ = ly_symbol2scm ("Bottom");
2317                         parser->lexer_->push_extra_token (@1, SCM_IDENTIFIER, $1);
2318                 } else {
2319                         $$ = scm_car ($1);
2320                         parser->lexer_->push_extra_token (@1, SCM_IDENTIFIER,
2321                                                           scm_cdr ($1));
2322                 }
2323         }
2324         ;
2325
2326 music_property_def:
2327         simple_music_property_def {
2328                 if (SCM_UNBNDP ($1))
2329                         $$ = MAKE_SYNTAX ("void-music", @1);
2330                 else
2331                         $$ = LOWLEVEL_MAKE_SYNTAX (ly_lily_module_constant ("property-operation"), scm_cons2 (parser->self_scm (), make_input (@$), $1));
2332         }
2333         ;
2334
2335 string:
2336         STRING {
2337                 $$ = $1;
2338         }
2339         | full_markup
2340         ;
2341
2342 simple_string: STRING {
2343                 $$ = $1;
2344         }
2345         | embedded_scm_bare
2346         {
2347                 if (scm_is_string ($1)) {
2348                         $$ = $1;
2349                 } else {
2350                         parser->parser_error (@1, (_ ("simple string expected")));
2351                         $$ = scm_string (SCM_EOL);
2352                 }
2353         }
2354         ;
2355
2356 symbol:
2357         STRING {
2358                 $$ = scm_string_to_symbol ($1);
2359         }
2360         | embedded_scm_bare
2361         {
2362                 // This is a bit of overkill but makes the same
2363                 // routine responsible for all symbol interpretations.
2364                 $$ = try_string_variants (ly_lily_module_constant ("symbol?"),
2365                                           $1);
2366                 if (SCM_UNBNDP ($$))
2367                 {
2368                         parser->parser_error (@1, (_ ("symbol expected")));
2369                         // Generate a unique symbol in case it is used
2370                         // for an assignment or similar
2371                         $$ = scm_make_symbol (ly_string2scm ("undefined"));
2372                 }
2373         }
2374         ;
2375
2376 scalar:
2377         embedded_scm_arg
2378         | SCM_IDENTIFIER
2379         | bare_number
2380         // The following is a rather defensive variant of admitting
2381         // negative numbers: the grammar would permit number_factor or
2382         // even number_expression.  However, function arguments allow
2383         // only this simple kind of negative number, so to have things
2384         // like \tweak and \override behave reasonably similar, it
2385         // makes sense to rule out things like -- which are rather an
2386         // accent in function argument contexts.
2387         | '-' bare_number
2388         {
2389                 $$ = scm_difference ($2, SCM_UNDEFINED);
2390         }
2391         | STRING
2392         | full_markup
2393         ;
2394
2395 event_chord:
2396         simple_element post_events {
2397                 // Let the rhythmic music iterator sort this mess out.
2398                 if (scm_is_pair ($2)) {
2399                         $$ = make_music_from_simple (parser, @1, $1);
2400                         if (unsmob_music ($$))
2401                                 unsmob_music ($$)->set_property ("articulations",
2402                                                                  scm_reverse_x ($2, SCM_EOL));
2403                         else
2404                         {
2405                                 parser->parser_error (@1, _("music expected"));
2406                                 $$ = MAKE_SYNTAX ("void-music", @1);
2407                         }
2408                 }
2409         } %prec ':'
2410         | simple_chord_elements post_events     {
2411                 if (scm_is_pair ($2)) {
2412                         if (unsmob_pitch ($1))
2413                                 $1 = make_chord_elements (@1,
2414                                                           $1,
2415                                                           parser->default_duration_.smobbed_copy (),
2416                                                           SCM_EOL);
2417
2418                         SCM elts = ly_append2 ($1, scm_reverse_x ($2, SCM_EOL));
2419
2420                         $$ = MAKE_SYNTAX ("event-chord", @1, elts);
2421                 } else if (!unsmob_pitch ($1))
2422                         $$ = MAKE_SYNTAX ("event-chord", @1, $1);
2423                 // A mere pitch drops through.
2424         } %prec ':'
2425         | CHORD_REPETITION optional_notemode_duration post_events {
2426                 Input i;
2427                 i.set_location (@1, @3);
2428                 $$ = MAKE_SYNTAX ("repetition-chord", i,
2429                                   $2, scm_reverse_x ($3, SCM_EOL));
2430         } %prec ':'
2431         | MULTI_MEASURE_REST optional_notemode_duration post_events {
2432                 Input i;
2433                 i.set_location (@1, @3);
2434                 $$ = MAKE_SYNTAX ("multi-measure-rest", i, $2,
2435                                   scm_reverse_x ($3, SCM_EOL));
2436         } %prec ':'
2437         | tempo_event
2438         | note_chord_element
2439         ;
2440
2441
2442 note_chord_element:
2443         chord_body optional_notemode_duration post_events
2444         {
2445                 Music *m = unsmob_music ($1);
2446                 SCM dur = unsmob_duration ($2)->smobbed_copy ();
2447                 SCM es = m->get_property ("elements");
2448                 SCM postevs = scm_reverse_x ($3, SCM_EOL);
2449
2450                 for (SCM s = es; scm_is_pair (s); s = scm_cdr (s))
2451                   unsmob_music (scm_car (s))->set_property ("duration", dur);
2452                 es = ly_append2 (es, postevs);
2453
2454                 m-> set_property ("elements", es);
2455                 m->set_spot (@$);
2456                 $$ = m->self_scm ();
2457         } %prec ':'
2458         ;
2459
2460 chord_body:
2461         ANGLE_OPEN chord_body_elements ANGLE_CLOSE
2462         {
2463                 $$ = MAKE_SYNTAX ("event-chord", @$, scm_reverse_x ($2, SCM_EOL));
2464         }
2465         ;
2466
2467 chord_body_elements:
2468         /* empty */             { $$ = SCM_EOL; }
2469         | chord_body_elements chord_body_element {
2470                 if (!SCM_UNBNDP ($2))
2471                         $$ = scm_cons ($2, $1);
2472         }
2473         ;
2474
2475 chord_body_element:
2476         pitch exclamations questions octave_check post_events
2477         {
2478                 bool q = to_boolean ($3);
2479                 bool ex = to_boolean ($2);
2480                 SCM check = $4;
2481                 SCM post = $5;
2482
2483                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2484                 n->set_property ("pitch", $1);
2485                 if (q)
2486                         n->set_property ("cautionary", SCM_BOOL_T);
2487                 if (ex || q)
2488                         n->set_property ("force-accidental", SCM_BOOL_T);
2489
2490                 if (scm_is_pair (post)) {
2491                         SCM arts = scm_reverse_x (post, SCM_EOL);
2492                         n->set_property ("articulations", arts);
2493                 }
2494                 if (scm_is_number (check))
2495                 {
2496                         int q = scm_to_int (check);
2497                         n->set_property ("absolute-octave", scm_from_int (q-1));
2498                 }
2499
2500                 $$ = n->unprotect ();
2501         }
2502         | DRUM_PITCH post_events {
2503                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2504                 n->set_property ("drum-type", $1);
2505
2506                 if (scm_is_pair ($2)) {
2507                         SCM arts = scm_reverse_x ($2, SCM_EOL);
2508                         n->set_property ("articulations", arts);
2509                 }
2510                 $$ = n->unprotect ();
2511         }
2512         | music_function_chord_body
2513         {
2514                 Music *m = unsmob_music ($1);
2515
2516                 while (m && m->is_mus_type ("music-wrapper-music")) {
2517                         $$ = m->get_property ("element");
2518                         m = unsmob_music ($$);
2519                 }
2520
2521                 if (!(m && m->is_mus_type ("rhythmic-event"))) {
2522                         parser->parser_error (@$, _ ("not a rhythmic event"));
2523                         $$ = SCM_UNDEFINED;
2524                 }
2525         }
2526         ;
2527
2528 music_function_chord_body:
2529         music_function_call
2530         | MUSIC_IDENTIFIER
2531         ;
2532
2533 event_function_event:
2534         EVENT_FUNCTION function_arglist {
2535                 $$ = MAKE_SYNTAX ("music-function", @$,
2536                                          $1, $2);
2537         }
2538         ;
2539
2540 post_events:
2541         /* empty */ {
2542                 $$ = SCM_EOL;
2543         }
2544         | post_events post_event {
2545                 $$ = $1;
2546                 if (Music *m = unsmob_music ($2))
2547                 {
2548                         if (m->is_mus_type ("post-event-wrapper"))
2549                         {
2550                                 for (SCM p = m->get_property ("elements");
2551                                      scm_is_pair (p);
2552                                      p = scm_cdr (p))
2553                                 {
2554                                         $$ = scm_cons (scm_car (p), $$);
2555                                 }
2556                         } else {
2557                                 m->set_spot (@2);
2558                                 $$ = scm_cons ($2, $$);
2559                         }
2560                 }
2561         }
2562         ;
2563
2564 post_event_nofinger:
2565         direction_less_event {
2566                 $$ = $1;
2567         }
2568         | script_dir music_function_call {
2569                 $$ = $2;
2570                 if (!unsmob_music ($2)->is_mus_type ("post-event")) {
2571                         parser->parser_error (@2, _ ("post-event expected"));
2572                         $$ = SCM_UNSPECIFIED;
2573                 } else if (!SCM_UNBNDP ($1))
2574                 {
2575                         unsmob_music ($$)->set_property ("direction", $1);
2576                 }
2577         }
2578         | HYPHEN {
2579                 if (!parser->lexer_->is_lyric_state ())
2580                         parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2581                 $$ = MY_MAKE_MUSIC ("HyphenEvent", @$)->unprotect ();
2582         }
2583         | EXTENDER {
2584                 if (!parser->lexer_->is_lyric_state ())
2585                         parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2586                 $$ = MY_MAKE_MUSIC ("ExtenderEvent", @$)->unprotect ();
2587         }
2588         | script_dir direction_reqd_event {
2589                 if (!SCM_UNBNDP ($1))
2590                 {
2591                         Music *m = unsmob_music ($2);
2592                         m->set_property ("direction", $1);
2593                 }
2594                 $$ = $2;
2595         }
2596         | script_dir direction_less_event {
2597                 if (!SCM_UNBNDP ($1))
2598                 {
2599                         Music *m = unsmob_music ($2);
2600                         m->set_property ("direction", $1);
2601                 }
2602                 $$ = $2;
2603         }
2604         | '^' fingering
2605         {
2606                 $$ = $2;
2607                 unsmob_music ($$)->set_property ("direction", scm_from_int (UP));
2608         }
2609         | '_' fingering
2610         {
2611                 $$ = $2;
2612                 unsmob_music ($$)->set_property ("direction", scm_from_int (DOWN));
2613         }                       
2614         ;
2615
2616 post_event:
2617         post_event_nofinger
2618         | '-' fingering {
2619                 $$ = $2;
2620         }
2621         ;
2622
2623 string_number_event:
2624         E_UNSIGNED {
2625                 Music *s = MY_MAKE_MUSIC ("StringNumberEvent", @$);
2626                 s->set_property ("string-number", $1);
2627                 $$ = s->unprotect ();
2628         }
2629         ;
2630
2631 direction_less_event:
2632         string_number_event
2633         | EVENT_IDENTIFIER      {
2634                 $$ = $1;
2635         }
2636         | tremolo_type  {
2637                Music *a = MY_MAKE_MUSIC ("TremoloEvent", @$);
2638                a->set_property ("tremolo-type", $1);
2639                $$ = a->unprotect ();
2640         }
2641         | event_function_event  
2642         ;
2643
2644 direction_reqd_event:
2645         gen_text_def {
2646                 $$ = $1;
2647         }
2648         | script_abbreviation {
2649                 SCM s = parser->lexer_->lookup_identifier ("dash" + ly_scm2string ($1));
2650                 if (scm_is_string (s)) {
2651                         Music *a = MY_MAKE_MUSIC ("ArticulationEvent", @$);
2652                         a->set_property ("articulation-type", s);
2653                         $$ = a->unprotect ();
2654                 } else {
2655                         Music *original = unsmob_music (s);
2656                         if (original && original->is_mus_type ("post-event")) {
2657                                 Music *a = original->clone ();
2658                                 a->set_spot (parser->lexer_->override_input (@$));
2659                                 $$ = a->unprotect ();
2660                         } else {
2661                                 parser->parser_error (@1, _ ("expecting string or post-event as script definition"));
2662                                 $$ = MY_MAKE_MUSIC ("PostEvents", @$)->unprotect ();
2663                         }
2664                 }
2665         }
2666         ;
2667
2668 octave_check:
2669         /**/ { $$ = SCM_EOL; }
2670         | '=' quotes { $$ = $2; }
2671         ;
2672
2673 quotes:
2674         /* empty */
2675         {
2676                 $$ = SCM_INUM0;
2677         }
2678         | sub_quotes
2679         | sup_quotes
2680         ;
2681
2682 sup_quotes:
2683         '\'' {
2684                 $$ = scm_from_int (1);
2685         }
2686         | sup_quotes '\'' {
2687                 $$ = scm_oneplus ($1);
2688         }
2689         ;
2690
2691 sub_quotes:
2692         ',' {
2693                 $$ = scm_from_int (-1);
2694         }
2695         | sub_quotes ',' {
2696                 $$ = scm_oneminus ($1);
2697         }
2698         ;
2699
2700 steno_pitch:
2701         NOTENAME_PITCH quotes {
2702                 if (!scm_is_eq (SCM_INUM0, $2))
2703                 {
2704                         Pitch p = *unsmob_pitch ($1);
2705                         p = p.transposed (Pitch (scm_to_int ($2),0,0));
2706                         $$ = p.smobbed_copy ();
2707                 }
2708         }
2709         ;
2710
2711 /*
2712 ugh. duplication
2713 */
2714
2715 steno_tonic_pitch:
2716         TONICNAME_PITCH quotes {
2717                 if (!scm_is_eq (SCM_INUM0, $2))
2718                 {
2719                         Pitch p = *unsmob_pitch ($1);
2720                         p = p.transposed (Pitch (scm_to_int ($2),0,0));
2721                         $$ = p.smobbed_copy ();
2722                 }
2723         }
2724         ;
2725
2726 pitch:
2727         steno_pitch
2728         | PITCH_IDENTIFIER quotes {
2729                 if (!scm_is_eq (SCM_INUM0, $2))
2730                 {
2731                         Pitch p = *unsmob_pitch ($1);
2732                         p = p.transposed (Pitch (scm_to_int ($2),0,0));
2733                         $$ = p.smobbed_copy ();
2734                 }
2735         }
2736         ;
2737
2738 gen_text_def:
2739         full_markup {
2740                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2741                 t->set_property ("text", $1);
2742                 $$ = t->unprotect ();
2743         }
2744         | STRING {
2745                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2746                 t->set_property ("text",
2747                         make_simple_markup ($1));
2748                 $$ = t->unprotect ();
2749         }
2750         | embedded_scm
2751         {
2752                 Music *m = unsmob_music ($1);
2753                 if (m && m->is_mus_type ("post-event"))
2754                         $$ = $1;
2755                 else if (Text_interface::is_markup ($1)) {
2756                         Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2757                         t->set_property ("text", $1);
2758                         $$ = t->unprotect ();
2759                 } else
2760                         parser->parser_error (@1, _ ("not an articulation"));
2761         }
2762         ;
2763
2764 fingering:
2765         UNSIGNED {
2766                 Music *t = MY_MAKE_MUSIC ("FingeringEvent", @$);
2767                 t->set_property ("digit", $1);
2768                 $$ = t->unprotect ();
2769         }
2770         ;
2771
2772 script_abbreviation:
2773         '^'             {
2774                 $$ = scm_from_locale_string ("Hat");
2775         }
2776         | '+'           {
2777                 $$ = scm_from_locale_string ("Plus");
2778         }
2779         | '-'           {
2780                 $$ = scm_from_locale_string ("Dash");
2781         }
2782         | '!'           {
2783                 $$ = scm_from_locale_string ("Bang");
2784         }
2785         | ANGLE_CLOSE   {
2786                 $$ = scm_from_locale_string ("Larger");
2787         }
2788         | '.'           {
2789                 $$ = scm_from_locale_string ("Dot");
2790         }
2791         | '_' {
2792                 $$ = scm_from_locale_string ("Underscore");
2793         }
2794         ;
2795
2796 script_dir:
2797         '_'     { $$ = scm_from_int (DOWN); }
2798         | '^'   { $$ = scm_from_int (UP); }
2799         | '-'   { $$ = SCM_UNDEFINED; }
2800         ;
2801
2802 duration_length:
2803         multiplied_duration {
2804                 $$ = $1;
2805         }
2806         ;
2807
2808 maybe_notemode_duration:
2809         {
2810                 $$ = SCM_UNDEFINED;
2811         } %prec ':'
2812         | multiplied_duration   {
2813                 $$ = $1;
2814                 parser->default_duration_ = *unsmob_duration ($$);
2815         }
2816 ;
2817
2818
2819 optional_notemode_duration:
2820         maybe_notemode_duration
2821         {
2822                 if (SCM_UNBNDP ($$))
2823                         $$ = parser->default_duration_.smobbed_copy ();
2824         }
2825         ;
2826
2827 steno_duration:
2828         UNSIGNED dots           {
2829                 $$ = make_duration ($1, scm_to_int ($2));
2830                 if (SCM_UNBNDP ($$))
2831                 {
2832                         parser->parser_error (@1, _ ("not a duration"));
2833                         $$ = Duration ().smobbed_copy ();
2834                 }
2835         }
2836         | DURATION_IDENTIFIER dots      {
2837                 Duration *d = unsmob_duration ($1);
2838                 Duration k (d->duration_log (),
2839                             d->dot_count () + scm_to_int ($2));
2840                 k = k.compressed (d->factor ());
2841                 scm_remember_upto_here_1 ($1);
2842                 $$ = k.smobbed_copy ();
2843         }
2844         ;
2845
2846 multiplied_duration:
2847         steno_duration {
2848                 $$ = $1;
2849         }
2850         | multiplied_duration '*' UNSIGNED {
2851                 $$ = unsmob_duration ($$)->compressed (scm_to_int ($3)).smobbed_copy ();
2852         }
2853         | multiplied_duration '*' FRACTION {
2854                 Rational  m (scm_to_int (scm_car ($3)), scm_to_int (scm_cdr ($3)));
2855
2856                 $$ = unsmob_duration ($$)->compressed (m).smobbed_copy ();
2857         }
2858         ;
2859
2860 dots:
2861         /* empty */     {
2862                 $$ = SCM_INUM0;
2863         }
2864         | dots '.' {
2865                 $$ = scm_oneplus ($1);
2866         }
2867         ;
2868
2869 tremolo_type:
2870         ':'     {
2871                 $$ = SCM_INUM0;
2872         }
2873         | ':' UNSIGNED {
2874                 if (SCM_UNBNDP (make_duration ($2)))
2875                         parser->parser_error (@2, _ ("not a duration"));
2876                 $$ = $2;
2877         }
2878         ;
2879
2880 bass_number:
2881         UNSIGNED { $$ = $1; }
2882         | STRING { $$ = $1; }
2883         | full_markup { $$ = $1; }
2884         | embedded_scm_bare
2885         {
2886                 // as an integer, it needs to be non-negative, and otherwise
2887                 // it needs to be suitable as a markup.
2888                 if (scm_is_integer ($1)
2889                     ? scm_is_true (scm_negative_p ($1))
2890                     : !Text_interface::is_markup ($1))
2891                 {
2892                         parser->parser_error (@1, _ ("bass number expected"));
2893                         $$ = SCM_INUM0;
2894                 }
2895         }
2896         ;
2897
2898 figured_bass_alteration:
2899         '-'     { $$ = ly_rational2scm (FLAT_ALTERATION); }
2900         | '+'   { $$ = ly_rational2scm (SHARP_ALTERATION); }
2901         | '!'   { $$ = scm_from_int (0); }
2902         ;
2903
2904 bass_figure:
2905         FIGURE_SPACE {
2906                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
2907                 $$ = bfr->unprotect ();
2908         }
2909         | bass_number  {
2910                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
2911                 $$ = bfr->self_scm ();
2912
2913                 if (scm_is_number ($1))
2914                         bfr->set_property ("figure", $1);
2915                 else if (Text_interface::is_markup ($1))
2916                         bfr->set_property ("text", $1);
2917
2918                 bfr->unprotect ();
2919         }
2920         | bass_figure ']' {
2921                 $$ = $1;
2922                 unsmob_music ($1)->set_property ("bracket-stop", SCM_BOOL_T);
2923         }
2924         | bass_figure figured_bass_alteration {
2925                 Music *m = unsmob_music ($1);
2926                 if (scm_to_double ($2)) {
2927                         SCM salter = m->get_property ("alteration");
2928                         SCM alter = scm_is_number (salter) ? salter : scm_from_int (0);
2929                         m->set_property ("alteration",
2930                                          scm_sum (alter, $2));
2931                 } else {
2932                         m->set_property ("alteration", scm_from_int (0));
2933                 }
2934         }
2935         | bass_figure figured_bass_modification  {
2936                 Music *m = unsmob_music ($1);
2937                 m->set_property ($2, SCM_BOOL_T);
2938         }
2939         ;
2940
2941
2942 figured_bass_modification:
2943         E_PLUS          {
2944                 $$ = ly_symbol2scm ("augmented");
2945         }
2946         | E_EXCLAMATION {
2947                 $$ = ly_symbol2scm ("no-continuation");
2948         }
2949         | '/'           {
2950                 $$ = ly_symbol2scm ("diminished");
2951         }
2952         | E_BACKSLASH {
2953                 $$ = ly_symbol2scm ("augmented-slash");
2954         }
2955         ;
2956
2957 br_bass_figure:
2958         bass_figure {
2959                 $$ = $1;
2960         }
2961         | '[' bass_figure {
2962                 $$ = $2;
2963                 unsmob_music ($$)->set_property ("bracket-start", SCM_BOOL_T);
2964         }
2965         ;
2966
2967 figure_list:
2968         /**/            {
2969                 $$ = SCM_EOL;
2970         }
2971         | figure_list br_bass_figure {
2972                 $$ = scm_cons ($2, $1);
2973         }
2974         ;
2975
2976 figure_spec:
2977         FIGURE_OPEN figure_list FIGURE_CLOSE {
2978                 $$ = scm_reverse_x ($2, SCM_EOL);
2979         }
2980         ;
2981
2982
2983 optional_rest:
2984         /**/   { $$ = SCM_BOOL_F; }
2985         | REST { $$ = SCM_BOOL_T; }
2986         ;
2987
2988 simple_element:
2989         pitch exclamations questions octave_check maybe_notemode_duration optional_rest {
2990                 if (!parser->lexer_->is_note_state ())
2991                         parser->parser_error (@1, _ ("have to be in Note mode for notes"));
2992                 if (!SCM_UNBNDP ($2)
2993                     || !SCM_UNBNDP ($3)
2994                     || scm_is_number ($4)
2995                     || !SCM_UNBNDP ($5)
2996                     || scm_is_true ($6))
2997                 {
2998                         Music *n = 0;
2999                         if (scm_is_true ($6))
3000                                 n = MY_MAKE_MUSIC ("RestEvent", @$);
3001                         else
3002                                 n = MY_MAKE_MUSIC ("NoteEvent", @$);
3003                         
3004                         n->set_property ("pitch", $1);
3005                         if (SCM_UNBNDP ($5))
3006                                 n->set_property ("duration",
3007                                                  parser->default_duration_.smobbed_copy ());
3008                         else
3009                                 n->set_property ("duration", $5);
3010                         
3011                         if (scm_is_number ($4))
3012                         {
3013                                 int q = scm_to_int ($4);
3014                                 n->set_property ("absolute-octave", scm_from_int (q-1));
3015                         }
3016                         
3017                         if (to_boolean ($3))
3018                                 n->set_property ("cautionary", SCM_BOOL_T);
3019                         if (to_boolean ($2) || to_boolean ($3))
3020                                 n->set_property ("force-accidental", SCM_BOOL_T);
3021                         
3022                         $$ = n->unprotect ();
3023                 }
3024         }
3025         | DRUM_PITCH optional_notemode_duration {
3026                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
3027                 n->set_property ("duration", $2);
3028                 n->set_property ("drum-type", $1);
3029
3030                 $$ = n->unprotect ();
3031         }
3032         | RESTNAME optional_notemode_duration           {
3033                 Music *ev = 0;
3034                 if (ly_scm2string ($1) == "s") {
3035                         /* Space */
3036                         ev = MY_MAKE_MUSIC ("SkipEvent", @$);
3037                   }
3038                 else {
3039                         ev = MY_MAKE_MUSIC ("RestEvent", @$);
3040
3041                     }
3042                 ev->set_property ("duration", $2);
3043                 $$ = ev->unprotect ();
3044         }
3045         ;
3046
3047 // Can return a single pitch rather than a list.
3048 simple_chord_elements:
3049         new_chord {
3050                 if (!parser->lexer_->is_chord_state ())
3051                         parser->parser_error (@1, _ ("have to be in Chord mode for chords"));
3052                 $$ = $1;
3053         }
3054         | figure_spec optional_notemode_duration {
3055                 for (SCM s = $1; scm_is_pair (s); s = scm_cdr (s))
3056                 {
3057                         unsmob_music (scm_car (s))->set_property ("duration", $2);
3058                 }
3059                 $$ = $1;
3060         }
3061         ;
3062
3063 lyric_element:
3064         full_markup {
3065                 if (!parser->lexer_->is_lyric_state ())
3066                         parser->parser_error (@1, _ ("markup outside of text script or \\lyricmode"));
3067                 $$ = $1;
3068         }
3069         | STRING {
3070                 if (!parser->lexer_->is_lyric_state ())
3071                         parser->parser_error (@1, _ ("unrecognized string, not in text script or \\lyricmode"));
3072                 $$ = $1;
3073         }
3074         | LYRIC_ELEMENT
3075         ;
3076
3077 lyric_element_music:
3078         lyric_element optional_notemode_duration post_events {
3079                 $$ = MAKE_SYNTAX ("lyric-event", @$, $1, $2);
3080                 if (scm_is_pair ($3))
3081                         unsmob_music ($$)->set_property
3082                                 ("articulations", scm_reverse_x ($3, SCM_EOL));
3083         } %prec ':'
3084         ;
3085
3086 // Can return a single pitch rather than a list.
3087 new_chord:
3088         steno_tonic_pitch maybe_notemode_duration   {
3089                 if (SCM_UNBNDP ($2))
3090                         $$ = $1;
3091                 else
3092                         $$ = make_chord_elements (@$, $1, $2, SCM_EOL);
3093         }
3094         | steno_tonic_pitch optional_notemode_duration chord_separator chord_items {
3095                 SCM its = scm_reverse_x ($4, SCM_EOL);
3096                 $$ = make_chord_elements (@$, $1, $2, scm_cons ($3, its));
3097         } %prec ':'
3098         ;
3099
3100 chord_items:
3101         /**/ {
3102                 $$ = SCM_EOL;
3103         }
3104         | chord_items chord_item {
3105                 $$ = scm_cons ($2, $$);
3106         }
3107         ;
3108
3109 chord_separator:
3110         CHORD_COLON {
3111                 $$ = ly_symbol2scm ("chord-colon");
3112         }
3113         | CHORD_CARET {
3114                 $$ = ly_symbol2scm ("chord-caret");
3115         }
3116         | CHORD_SLASH steno_tonic_pitch {
3117                 $$ = scm_list_2 (ly_symbol2scm ("chord-slash"), $2);
3118         }
3119         | CHORD_BASS steno_tonic_pitch {
3120                 $$ = scm_list_2 (ly_symbol2scm ("chord-bass"), $2);
3121         }
3122         ;
3123
3124 chord_item:
3125         chord_separator {
3126                 $$ = $1;
3127         }
3128         | step_numbers {
3129                 $$ = scm_reverse_x ($1, SCM_EOL);
3130         }
3131         | CHORD_MODIFIER  {
3132                 $$ = $1;
3133         }
3134         ;
3135
3136 step_numbers:
3137         step_number { $$ = scm_cons ($1, SCM_EOL); }
3138         | step_numbers '.' step_number {
3139                 $$ = scm_cons ($3, $$);
3140         }
3141         ;
3142
3143 step_number:
3144         UNSIGNED {
3145                 $$ = make_chord_step ($1, 0);
3146         }
3147         | UNSIGNED '+' {
3148                 $$ = make_chord_step ($1, SHARP_ALTERATION);
3149         }
3150         | UNSIGNED CHORD_MINUS {
3151                 $$ = make_chord_step ($1, FLAT_ALTERATION);
3152         }
3153         ;
3154
3155 tempo_range:
3156         unsigned_number {
3157                 $$ = $1;
3158         } %prec ':'
3159         | unsigned_number '-' unsigned_number {
3160                 $$ = scm_cons ($1, $3);
3161         }
3162         ;
3163
3164 /*
3165         UTILITIES
3166
3167 TODO: should deprecate in favor of Scheme?
3168
3169  */
3170 number_expression:
3171         number_expression '+' number_term {
3172                 $$ = scm_sum ($1, $3);
3173         }
3174         | number_expression '-' number_term {
3175                 $$ = scm_difference ($1, $3);
3176         }
3177         | number_term
3178         ;
3179
3180 number_term:
3181         number_factor {
3182                 $$ = $1;
3183         }
3184         | number_factor '*' number_factor {
3185                 $$ = scm_product ($1, $3);
3186         }
3187         | number_factor '/' number_factor {
3188                 $$ = scm_divide ($1, $3);
3189         }
3190         ;
3191
3192 number_factor:
3193         '-'  number_factor { /* %prec UNARY_MINUS */
3194                 $$ = scm_difference ($2, SCM_UNDEFINED);
3195         }
3196         | bare_number
3197         ;
3198
3199 bare_number_common:
3200         REAL
3201         | NUMBER_IDENTIFIER
3202         | REAL NUMBER_IDENTIFIER
3203         {
3204                 $$ = scm_product ($1, $2);
3205         }
3206         ;
3207
3208 bare_number:
3209         bare_number_common
3210         | UNSIGNED
3211         | UNSIGNED NUMBER_IDENTIFIER    {
3212                 $$ = scm_product ($1, $2);
3213         }
3214         ;
3215
3216 unsigned_number:
3217         UNSIGNED
3218         | NUMBER_IDENTIFIER
3219         {
3220                 if (!scm_is_integer ($1)
3221                     || scm_is_true (scm_negative_p ($1)))
3222                 {
3223                         parser->parser_error (@1, _("not an unsigned integer"));
3224                         $$ = SCM_INUM0;
3225                 }
3226         }
3227         | embedded_scm
3228         {
3229                 if (!scm_is_integer ($1)
3230                     || scm_is_true (scm_negative_p ($1)))
3231                 {
3232                         parser->parser_error (@1, _("not an unsigned integer"));
3233                         $$ = SCM_INUM0;
3234                 }
3235         }
3236         ;
3237
3238 exclamations:
3239                 { $$ = SCM_UNDEFINED; }
3240         | exclamations '!'
3241         {
3242                 if (SCM_UNBNDP ($1))
3243                         $$ = SCM_BOOL_T;
3244                 else
3245                         $$ = scm_not ($1);
3246         }
3247         ;
3248
3249 questions:
3250 // This precedence rule is rather weird.  It triggers when '!' is
3251 // encountered after a pitch, and is used for deciding whether to save
3252 // this instead for a figure modification.  This should not actually
3253 // occur in practice as pitches and figures are generated in different
3254 // modes.  Using a greedy (%right) precedence makes sure that we don't
3255 // get stuck in a wrong interpretation.
3256         { $$ = SCM_UNDEFINED; } %prec ':'
3257         | questions '?'
3258         {
3259                 if (SCM_UNBNDP ($1))
3260                         $$ = SCM_BOOL_T;
3261                 else
3262                         $$ = scm_not ($1);
3263         }
3264         ;
3265
3266 full_markup_list:
3267         MARKUPLIST
3268                 { parser->lexer_->push_markup_state (); }
3269         markup_list {
3270                 $$ = $3;
3271                 parser->lexer_->pop_state ();
3272         }
3273         ;
3274
3275 full_markup:
3276         MARKUP
3277                 { parser->lexer_->push_markup_state (); }
3278         markup_top {
3279                 $$ = $3;
3280                 parser->lexer_->pop_state ();
3281         }
3282         ;
3283
3284 markup_top:
3285         markup_list {
3286                 $$ = scm_list_2 (ly_lily_module_constant ("line-markup"),  $1);
3287         }
3288         | markup_head_1_list simple_markup
3289         {
3290                 $$ = scm_car (MAKE_SYNTAX ("composed-markup-list",
3291                                            @2, $1, scm_list_1 ($2)));
3292         }
3293         | simple_markup {
3294                 $$ = $1;
3295         }
3296         ;
3297
3298 markup_scm:
3299         embedded_scm_bare
3300         {
3301                 if (Text_interface::is_markup ($1))
3302                         MYBACKUP (MARKUP_IDENTIFIER, $1, @1);
3303                 else if (Text_interface::is_markup_list ($1))
3304                         MYBACKUP (MARKUPLIST_IDENTIFIER, $1, @1);
3305                 else {
3306                         parser->parser_error (@1, _ ("not a markup"));
3307                         MYBACKUP (MARKUP_IDENTIFIER, scm_string (SCM_EOL), @1);
3308                 }
3309         } BACKUP
3310         ;
3311                         
3312
3313 markup_list:
3314         markup_composed_list {
3315                 $$ = $1;
3316         }
3317         | markup_uncomposed_list
3318         ;
3319
3320 markup_uncomposed_list:
3321         markup_braced_list {
3322                 $$ = $1;
3323         }
3324         | markup_command_list {
3325                 $$ = scm_list_1 ($1);
3326         }
3327         | markup_scm MARKUPLIST_IDENTIFIER
3328         {
3329                 $$ = $2;
3330         }
3331         | SCORELINES {
3332                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
3333                 parser->lexer_->push_note_state (nn);
3334         } '{' score_body '}' {
3335                 unsmob_score ($4)->origin ()->set_spot (@$);
3336                 $$ = scm_list_1 (scm_list_2 (ly_lily_module_constant ("score-lines-markup-list"), $4));
3337                 parser->lexer_->pop_state ();
3338         }
3339         ;
3340
3341 markup_composed_list:
3342         markup_head_1_list markup_uncomposed_list {
3343                 $$ = MAKE_SYNTAX ("composed-markup-list",
3344                                   @2, $1, $2);
3345         }
3346         ;
3347
3348 markup_braced_list:
3349         '{' markup_braced_list_body '}' {
3350                 $$ = scm_reverse_x ($2, SCM_EOL);
3351         }
3352         ;
3353
3354 markup_braced_list_body:
3355         /* empty */     {  $$ = SCM_EOL; }
3356         | markup_braced_list_body markup {
3357                 $$ = scm_cons ($2, $1);
3358         }
3359         | markup_braced_list_body markup_list {
3360                 $$ = scm_reverse_x ($2, $1);
3361         }
3362         ;
3363
3364 markup_command_list:
3365         MARKUP_LIST_FUNCTION markup_command_list_arguments {
3366           $$ = scm_cons ($1, scm_reverse_x($2, SCM_EOL));
3367         }
3368         ;
3369
3370 markup_command_basic_arguments:
3371         EXPECT_MARKUP_LIST markup_command_list_arguments markup_list {
3372           $$ = scm_cons ($3, $2);
3373         }
3374         | EXPECT_SCM markup_command_list_arguments embedded_scm {
3375           $$ = check_scheme_arg (parser, @3, $3, $2, $1);
3376         }
3377         | EXPECT_NO_MORE_ARGS {
3378           $$ = SCM_EOL;
3379         }
3380         ;
3381
3382 markup_command_list_arguments:
3383         markup_command_basic_arguments { $$ = $1; }
3384         | EXPECT_MARKUP markup_command_list_arguments markup {
3385           $$ = scm_cons ($3, $2);
3386         }
3387         ;
3388
3389 markup_head_1_item:
3390         MARKUP_FUNCTION EXPECT_MARKUP markup_command_list_arguments {
3391           $$ = scm_cons ($1, scm_reverse_x ($3, SCM_EOL));
3392         }
3393         ;
3394
3395 markup_head_1_list:
3396         markup_head_1_item      {
3397                 $$ = scm_list_1 ($1);
3398         }
3399         | markup_head_1_list markup_head_1_item {
3400                 $$ = scm_cons ($2, $1);
3401         }
3402         ;
3403
3404 simple_markup:
3405         STRING {
3406                 $$ = make_simple_markup ($1);
3407         }
3408         | SCORE {
3409                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
3410                 parser->lexer_->push_note_state (nn);
3411         } '{' score_body '}' {
3412                 unsmob_score ($4)->origin ()->set_spot (@$);
3413                 $$ = scm_list_2 (ly_lily_module_constant ("score-markup"), $4);
3414                 parser->lexer_->pop_state ();
3415         }
3416         | MARKUP_FUNCTION markup_command_basic_arguments {
3417                 $$ = scm_cons ($1, scm_reverse_x ($2, SCM_EOL));
3418         }
3419         | markup_scm MARKUP_IDENTIFIER
3420         {
3421                 $$ = $2;
3422         }
3423         ;
3424
3425 markup:
3426         markup_head_1_list simple_markup
3427         {
3428                 $$ = scm_car (MAKE_SYNTAX ("composed-markup-list",
3429                                            @2, $1, scm_list_1 ($2)));
3430         }
3431         | simple_markup {
3432                 $$ = $1;
3433         }
3434         ;
3435
3436 %%
3437
3438 void
3439 Lily_parser::set_yydebug (bool x)
3440 {
3441         yydebug = x;
3442 }
3443
3444 SCM
3445 Lily_parser::do_yyparse ()
3446 {
3447         SCM retval = SCM_UNDEFINED;
3448         yyparse (this, &retval);
3449         return retval;
3450 }
3451
3452
3453
3454
3455
3456 /*
3457
3458 It is a little strange to have this function in this file, but
3459 otherwise, we have to import music classes into the lexer.
3460
3461 */
3462 int
3463 Lily_lexer::try_special_identifiers (SCM *destination, SCM sid)
3464 {
3465         if (unsmob_book (sid)) {
3466                 Book *book =  unsmob_book (sid)->clone ();
3467                 *destination = book->self_scm ();
3468                 book->unprotect ();
3469
3470                 return BOOK_IDENTIFIER;
3471         } else if (scm_is_number (sid)) {
3472                 *destination = sid;
3473                 return NUMBER_IDENTIFIER;
3474         } else if (unsmob_context_def (sid)) {
3475                 Context_def *def= unsmob_context_def (sid)->clone ();
3476
3477                 *destination = def->self_scm ();
3478                 def->unprotect ();
3479
3480                 return CONTEXT_DEF_IDENTIFIER;
3481         } else if (unsmob_context_mod (sid)) {
3482                 *destination = unsmob_context_mod (sid)->smobbed_copy ();
3483
3484                 return CONTEXT_MOD_IDENTIFIER;
3485         } else if (Music *mus = unsmob_music (sid)) {
3486                 mus = mus->clone ();
3487                 *destination = mus->self_scm ();
3488                 bool is_event = mus->is_mus_type ("post-event");
3489                 mus->unprotect ();
3490                 return is_event ? EVENT_IDENTIFIER : MUSIC_IDENTIFIER;
3491         } else if (unsmob_pitch (sid)) {
3492                 *destination = unsmob_pitch (sid)->smobbed_copy ();
3493                 return PITCH_IDENTIFIER;
3494         } else if (unsmob_duration (sid)) {
3495                 *destination = unsmob_duration (sid)->smobbed_copy ();
3496                 return DURATION_IDENTIFIER;
3497         } else if (unsmob_output_def (sid)) {
3498                 Output_def *p = unsmob_output_def (sid);
3499                 p = p->clone ();
3500
3501                 *destination = p->self_scm ();
3502                 p->unprotect ();
3503                 return OUTPUT_DEF_IDENTIFIER;
3504         } else if (unsmob_score (sid)) {
3505                 *destination = unsmob_score (sid)->clone ()->unprotect ();
3506                 return SCM_IDENTIFIER;
3507         }
3508
3509         return -1;
3510 }
3511
3512 SCM
3513 get_next_unique_context_id ()
3514 {
3515         return scm_from_locale_string ("$uniqueContextId");
3516 }
3517
3518
3519 SCM
3520 get_next_unique_lyrics_context_id ()
3521 {
3522         static int new_context_count;
3523         char s[128];
3524         snprintf (s, sizeof (s)-1, "uniqueContext%d", new_context_count++);
3525         return scm_from_locale_string (s);
3526 }
3527
3528 // check_scheme_arg checks one argument with a given predicate for use
3529 // in an argument list and throws a syntax error if it is unusable.
3530 // The argument is prepended to the argument list in any case.  After
3531 // throwing a syntax error, the argument list is terminated with #f as
3532 // its last cdr in order to mark it as uncallable while not losing
3533 // track of its total length.
3534 //
3535 // There are a few special considerations: if optional argument disp
3536 // is given (otherwise it defaults to SCM_UNDEFINED), it will be used
3537 // instead of arg in a prospective error message.  This is useful if
3538 // arg is not the actual argument but rather a transformation of it.
3539 //
3540 // If arg itself is SCM_UNDEFINED, the predicate is considered false
3541 // and an error message using disp is produced unconditionally.
3542
3543 SCM check_scheme_arg (Lily_parser *parser, Input loc,
3544                       SCM arg, SCM args, SCM pred, SCM disp)
3545 {
3546         if (SCM_UNBNDP (arg))
3547                 args = scm_cons (disp, args);
3548         else {
3549                 args = scm_cons (arg, args);
3550                 if (scm_is_true (scm_call_1 (pred, arg)))
3551                         return args;
3552         }
3553         scm_set_cdr_x (scm_last_pair (args), SCM_EOL);
3554         MAKE_SYNTAX ("argument-error", loc, scm_length (args), pred,
3555                      SCM_UNBNDP (disp) ? arg : disp);
3556         scm_set_cdr_x (scm_last_pair (args), SCM_BOOL_F);
3557         return args;
3558 }
3559
3560 SCM loc_on_music (Input loc, SCM arg)
3561 {
3562         if (Music *m = unsmob_music (arg))
3563         {
3564                 m = m->clone ();
3565                 m->set_spot (loc);
3566                 return m->unprotect ();
3567         }
3568         return arg;
3569 }
3570
3571 SCM
3572 try_string_variants (SCM pred, SCM str)
3573 {
3574         // a matching predicate is always ok
3575         if (scm_is_true (scm_call_1 (pred, str)))
3576                 return str;
3577         // a symbol may be interpreted as a list of symbols if it helps
3578         if (scm_is_symbol (str)) {
3579                 str = scm_list_1 (str);
3580                 if (scm_is_true (scm_call_1 (pred, str)))
3581                         return str;
3582                 return SCM_UNDEFINED;
3583         }
3584
3585         // If this cannot be a string representation of a symbol list,
3586         // we are through.
3587
3588         if (!is_regular_identifier (str, true))
3589                 return SCM_UNDEFINED;
3590
3591         str = scm_string_split (str, SCM_MAKE_CHAR ('.'));
3592         for (SCM p = str; scm_is_pair (p); p = scm_cdr (p))
3593                 scm_set_car_x (p, scm_string_to_symbol (scm_car (p)));
3594
3595         // Let's attempt the symbol list interpretation first.
3596
3597         if (scm_is_true (scm_call_1 (pred, str)))
3598                 return str;
3599
3600         // If there is just one symbol in the list, we might interpret
3601         // it as a single symbol
3602
3603         if (scm_is_null (scm_cdr (str)))
3604         {
3605                 str = scm_car (str);
3606                 if (scm_is_true (scm_call_1 (pred, str)))
3607                         return str;
3608         }
3609
3610         return SCM_UNDEFINED;
3611 }
3612
3613 bool
3614 is_regular_identifier (SCM id, bool multiple)
3615 {
3616   if (!scm_is_string (id))
3617           return false;
3618
3619   string str = ly_scm2string (id);
3620
3621   bool middle = false;
3622
3623   for (string::iterator it=str.begin(); it != str.end (); it++)
3624   {
3625           int c = *it & 0xff;
3626           if ((c >= 'a' && c <= 'z')
3627               || (c >= 'A' && c <= 'Z')
3628               || c > 0x7f)
3629                   middle = true;
3630           else if (middle && (c == '-' || c == '_' || (multiple && c == '.')))
3631                   middle = false;
3632           else
3633                   return false;
3634   }
3635   return middle;
3636 }
3637
3638 SCM
3639 make_music_from_simple (Lily_parser *parser, Input loc, SCM simple)
3640 {
3641         if (unsmob_music (simple))
3642                 return simple;
3643         if (parser->lexer_->is_note_state ()) {
3644                 if (scm_is_symbol (simple)) {
3645                         Music *n = MY_MAKE_MUSIC ("NoteEvent", loc);
3646                         n->set_property ("duration", parser->default_duration_.smobbed_copy ());
3647                         n->set_property ("drum-type", simple);
3648                         return n->unprotect ();
3649                 }
3650                 if (unsmob_pitch (simple)) {
3651                         Music *n = MY_MAKE_MUSIC ("NoteEvent", loc);
3652                         n->set_property ("duration", parser->default_duration_.smobbed_copy ());
3653                         n->set_property ("pitch", simple);
3654                         return n->unprotect ();
3655                 }
3656                 return simple;
3657         } else if (parser->lexer_->is_lyric_state ()) {
3658                 if (Text_interface::is_markup (simple))
3659                         return MAKE_SYNTAX ("lyric-event", loc, simple,
3660                                             parser->default_duration_.smobbed_copy ());
3661         } else if (parser->lexer_->is_chord_state ()) {
3662                 if (unsmob_pitch (simple))
3663                         return MAKE_SYNTAX
3664                                 ("event-chord",
3665                                  loc,
3666                                  make_chord_elements (loc, simple,
3667                                                       parser->default_duration_.smobbed_copy (),
3668                                                       SCM_EOL));
3669         }
3670         return simple;
3671 }
3672
3673 Music *
3674 make_music_with_input (SCM name, Input where)
3675 {
3676        Music *m = make_music_by_name (name);
3677        m->set_spot (where);
3678        return m;
3679 }
3680
3681 SCM
3682 make_simple_markup (SCM a)
3683 {
3684         return a;
3685 }
3686
3687 SCM
3688 make_duration (SCM d, int dots)
3689 {
3690         int t = scm_to_int (d);
3691         if (t > 0 && (t & (t-1)) == 0)
3692                 return Duration (intlog2 (t), dots).smobbed_copy ();
3693         else
3694                 return SCM_UNDEFINED;
3695 }
3696
3697 SCM
3698 make_chord_step (SCM step_scm, Rational alter)
3699 {
3700         int step = scm_to_int (step_scm);
3701
3702         if (step == 7)
3703                 alter += FLAT_ALTERATION;
3704
3705         while (step < 0)
3706                 step += 7;
3707         Pitch m ((step -1) / 7, (step - 1) % 7, alter);
3708         return m.smobbed_copy ();
3709 }
3710
3711
3712 SCM
3713 make_chord_elements (Input loc, SCM pitch, SCM dur, SCM modification_list)
3714 {
3715         SCM chord_ctor = ly_lily_module_constant ("construct-chord-elements");
3716         SCM res = scm_call_3 (chord_ctor, pitch, dur, modification_list);
3717         for (SCM s = res; scm_is_pair (s); s = scm_cdr (s))
3718         {
3719                 unsmob_music (scm_car (s))->set_spot (loc);
3720         }
3721         return res;
3722 }
3723
3724 int
3725 yylex (YYSTYPE *s, YYLTYPE *loc, Lily_parser *parser)
3726 {
3727         Lily_lexer *lex = parser->lexer_;
3728
3729         lex->lexval_ = s;
3730         lex->lexloc_ = loc;
3731         int tok = lex->pop_extra_token ();
3732         if (tok >= 0)
3733                 return tok;
3734         lex->prepare_for_next_token ();
3735         return lex->yylex ();
3736 }