]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
Parser: remove pitch_arg and PITCH_ARG
[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         | command_element
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 command_element:
2541         command_event {
2542                 $$ = $1;
2543         }
2544         ;
2545
2546 command_event:
2547         tempo_event {
2548                 $$ = $1;
2549         }
2550         ;
2551
2552
2553 post_events:
2554         /* empty */ {
2555                 $$ = SCM_EOL;
2556         }
2557         | post_events post_event {
2558                 $$ = $1;
2559                 if (Music *m = unsmob_music ($2))
2560                 {
2561                         if (m->is_mus_type ("post-event-wrapper"))
2562                         {
2563                                 for (SCM p = m->get_property ("elements");
2564                                      scm_is_pair (p);
2565                                      p = scm_cdr (p))
2566                                 {
2567                                         $$ = scm_cons (scm_car (p), $$);
2568                                 }
2569                         } else {
2570                                 m->set_spot (@2);
2571                                 $$ = scm_cons ($2, $$);
2572                         }
2573                 }
2574         }
2575         ;
2576
2577 post_event_nofinger:
2578         direction_less_event {
2579                 $$ = $1;
2580         }
2581         | script_dir music_function_call {
2582                 $$ = $2;
2583                 if (!unsmob_music ($2)->is_mus_type ("post-event")) {
2584                         parser->parser_error (@2, _ ("post-event expected"));
2585                         $$ = SCM_UNSPECIFIED;
2586                 } else if (!SCM_UNBNDP ($1))
2587                 {
2588                         unsmob_music ($$)->set_property ("direction", $1);
2589                 }
2590         }
2591         | HYPHEN {
2592                 if (!parser->lexer_->is_lyric_state ())
2593                         parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2594                 $$ = MY_MAKE_MUSIC ("HyphenEvent", @$)->unprotect ();
2595         }
2596         | EXTENDER {
2597                 if (!parser->lexer_->is_lyric_state ())
2598                         parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2599                 $$ = MY_MAKE_MUSIC ("ExtenderEvent", @$)->unprotect ();
2600         }
2601         | script_dir direction_reqd_event {
2602                 if (!SCM_UNBNDP ($1))
2603                 {
2604                         Music *m = unsmob_music ($2);
2605                         m->set_property ("direction", $1);
2606                 }
2607                 $$ = $2;
2608         }
2609         | script_dir direction_less_event {
2610                 if (!SCM_UNBNDP ($1))
2611                 {
2612                         Music *m = unsmob_music ($2);
2613                         m->set_property ("direction", $1);
2614                 }
2615                 $$ = $2;
2616         }
2617         | '^' fingering
2618         {
2619                 $$ = $2;
2620                 unsmob_music ($$)->set_property ("direction", scm_from_int (UP));
2621         }
2622         | '_' fingering
2623         {
2624                 $$ = $2;
2625                 unsmob_music ($$)->set_property ("direction", scm_from_int (DOWN));
2626         }                       
2627         ;
2628
2629 post_event:
2630         post_event_nofinger
2631         | '-' fingering {
2632                 $$ = $2;
2633         }
2634         ;
2635
2636 string_number_event:
2637         E_UNSIGNED {
2638                 Music *s = MY_MAKE_MUSIC ("StringNumberEvent", @$);
2639                 s->set_property ("string-number", $1);
2640                 $$ = s->unprotect ();
2641         }
2642         ;
2643
2644 direction_less_event:
2645         string_number_event
2646         | EVENT_IDENTIFIER      {
2647                 $$ = $1;
2648         }
2649         | tremolo_type  {
2650                Music *a = MY_MAKE_MUSIC ("TremoloEvent", @$);
2651                a->set_property ("tremolo-type", $1);
2652                $$ = a->unprotect ();
2653         }
2654         | event_function_event  
2655         ;
2656
2657 direction_reqd_event:
2658         gen_text_def {
2659                 $$ = $1;
2660         }
2661         | script_abbreviation {
2662                 SCM s = parser->lexer_->lookup_identifier ("dash" + ly_scm2string ($1));
2663                 if (scm_is_string (s)) {
2664                         Music *a = MY_MAKE_MUSIC ("ArticulationEvent", @$);
2665                         a->set_property ("articulation-type", s);
2666                         $$ = a->unprotect ();
2667                 } else {
2668                         Music *original = unsmob_music (s);
2669                         if (original && original->is_mus_type ("post-event")) {
2670                                 Music *a = original->clone ();
2671                                 a->set_spot (parser->lexer_->override_input (@$));
2672                                 $$ = a->unprotect ();
2673                         } else {
2674                                 parser->parser_error (@1, _ ("expecting string or post-event as script definition"));
2675                                 $$ = MY_MAKE_MUSIC ("PostEvents", @$)->unprotect ();
2676                         }
2677                 }
2678         }
2679         ;
2680
2681 octave_check:
2682         /**/ { $$ = SCM_EOL; }
2683         | '=' quotes { $$ = $2; }
2684         ;
2685
2686 quotes:
2687         /* empty */
2688         {
2689                 $$ = SCM_INUM0;
2690         }
2691         | sub_quotes
2692         | sup_quotes
2693         ;
2694
2695 sup_quotes:
2696         '\'' {
2697                 $$ = scm_from_int (1);
2698         }
2699         | sup_quotes '\'' {
2700                 $$ = scm_oneplus ($1);
2701         }
2702         ;
2703
2704 sub_quotes:
2705         ',' {
2706                 $$ = scm_from_int (-1);
2707         }
2708         | sub_quotes ',' {
2709                 $$ = scm_oneminus ($1);
2710         }
2711         ;
2712
2713 steno_pitch:
2714         NOTENAME_PITCH quotes {
2715                 if (!scm_is_eq (SCM_INUM0, $2))
2716                 {
2717                         Pitch p = *unsmob_pitch ($1);
2718                         p = p.transposed (Pitch (scm_to_int ($2),0,0));
2719                         $$ = p.smobbed_copy ();
2720                 }
2721         }
2722         ;
2723
2724 /*
2725 ugh. duplication
2726 */
2727
2728 steno_tonic_pitch:
2729         TONICNAME_PITCH quotes {
2730                 if (!scm_is_eq (SCM_INUM0, $2))
2731                 {
2732                         Pitch p = *unsmob_pitch ($1);
2733                         p = p.transposed (Pitch (scm_to_int ($2),0,0));
2734                         $$ = p.smobbed_copy ();
2735                 }
2736         }
2737         ;
2738
2739 pitch:
2740         steno_pitch
2741         | PITCH_IDENTIFIER quotes {
2742                 if (!scm_is_eq (SCM_INUM0, $2))
2743                 {
2744                         Pitch p = *unsmob_pitch ($1);
2745                         p = p.transposed (Pitch (scm_to_int ($2),0,0));
2746                         $$ = p.smobbed_copy ();
2747                 }
2748         }
2749         ;
2750
2751 gen_text_def:
2752         full_markup {
2753                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2754                 t->set_property ("text", $1);
2755                 $$ = t->unprotect ();
2756         }
2757         | STRING {
2758                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2759                 t->set_property ("text",
2760                         make_simple_markup ($1));
2761                 $$ = t->unprotect ();
2762         }
2763         | embedded_scm
2764         {
2765                 Music *m = unsmob_music ($1);
2766                 if (m && m->is_mus_type ("post-event"))
2767                         $$ = $1;
2768                 else if (Text_interface::is_markup ($1)) {
2769                         Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2770                         t->set_property ("text", $1);
2771                         $$ = t->unprotect ();
2772                 } else
2773                         parser->parser_error (@1, _ ("not an articulation"));
2774         }
2775         ;
2776
2777 fingering:
2778         UNSIGNED {
2779                 Music *t = MY_MAKE_MUSIC ("FingeringEvent", @$);
2780                 t->set_property ("digit", $1);
2781                 $$ = t->unprotect ();
2782         }
2783         ;
2784
2785 script_abbreviation:
2786         '^'             {
2787                 $$ = scm_from_locale_string ("Hat");
2788         }
2789         | '+'           {
2790                 $$ = scm_from_locale_string ("Plus");
2791         }
2792         | '-'           {
2793                 $$ = scm_from_locale_string ("Dash");
2794         }
2795         | '!'           {
2796                 $$ = scm_from_locale_string ("Bang");
2797         }
2798         | ANGLE_CLOSE   {
2799                 $$ = scm_from_locale_string ("Larger");
2800         }
2801         | '.'           {
2802                 $$ = scm_from_locale_string ("Dot");
2803         }
2804         | '_' {
2805                 $$ = scm_from_locale_string ("Underscore");
2806         }
2807         ;
2808
2809 script_dir:
2810         '_'     { $$ = scm_from_int (DOWN); }
2811         | '^'   { $$ = scm_from_int (UP); }
2812         | '-'   { $$ = SCM_UNDEFINED; }
2813         ;
2814
2815 duration_length:
2816         multiplied_duration {
2817                 $$ = $1;
2818         }
2819         ;
2820
2821 maybe_notemode_duration:
2822         {
2823                 $$ = SCM_UNDEFINED;
2824         } %prec ':'
2825         | multiplied_duration   {
2826                 $$ = $1;
2827                 parser->default_duration_ = *unsmob_duration ($$);
2828         }
2829 ;
2830
2831
2832 optional_notemode_duration:
2833         maybe_notemode_duration
2834         {
2835                 if (SCM_UNBNDP ($$))
2836                         $$ = parser->default_duration_.smobbed_copy ();
2837         }
2838         ;
2839
2840 steno_duration:
2841         UNSIGNED dots           {
2842                 $$ = make_duration ($1, scm_to_int ($2));
2843                 if (SCM_UNBNDP ($$))
2844                 {
2845                         parser->parser_error (@1, _ ("not a duration"));
2846                         $$ = Duration ().smobbed_copy ();
2847                 }
2848         }
2849         | DURATION_IDENTIFIER dots      {
2850                 Duration *d = unsmob_duration ($1);
2851                 Duration k (d->duration_log (),
2852                             d->dot_count () + scm_to_int ($2));
2853                 k = k.compressed (d->factor ());
2854                 scm_remember_upto_here_1 ($1);
2855                 $$ = k.smobbed_copy ();
2856         }
2857         ;
2858
2859 multiplied_duration:
2860         steno_duration {
2861                 $$ = $1;
2862         }
2863         | multiplied_duration '*' UNSIGNED {
2864                 $$ = unsmob_duration ($$)->compressed (scm_to_int ($3)).smobbed_copy ();
2865         }
2866         | multiplied_duration '*' FRACTION {
2867                 Rational  m (scm_to_int (scm_car ($3)), scm_to_int (scm_cdr ($3)));
2868
2869                 $$ = unsmob_duration ($$)->compressed (m).smobbed_copy ();
2870         }
2871         ;
2872
2873 dots:
2874         /* empty */     {
2875                 $$ = SCM_INUM0;
2876         }
2877         | dots '.' {
2878                 $$ = scm_oneplus ($1);
2879         }
2880         ;
2881
2882 tremolo_type:
2883         ':'     {
2884                 $$ = SCM_INUM0;
2885         }
2886         | ':' UNSIGNED {
2887                 if (SCM_UNBNDP (make_duration ($2)))
2888                         parser->parser_error (@2, _ ("not a duration"));
2889                 $$ = $2;
2890         }
2891         ;
2892
2893 bass_number:
2894         UNSIGNED { $$ = $1; }
2895         | STRING { $$ = $1; }
2896         | full_markup { $$ = $1; }
2897         | embedded_scm_bare
2898         {
2899                 // as an integer, it needs to be non-negative, and otherwise
2900                 // it needs to be suitable as a markup.
2901                 if (scm_is_integer ($1)
2902                     ? scm_is_true (scm_negative_p ($1))
2903                     : !Text_interface::is_markup ($1))
2904                 {
2905                         parser->parser_error (@1, _ ("bass number expected"));
2906                         $$ = SCM_INUM0;
2907                 }
2908         }
2909         ;
2910
2911 figured_bass_alteration:
2912         '-'     { $$ = ly_rational2scm (FLAT_ALTERATION); }
2913         | '+'   { $$ = ly_rational2scm (SHARP_ALTERATION); }
2914         | '!'   { $$ = scm_from_int (0); }
2915         ;
2916
2917 bass_figure:
2918         FIGURE_SPACE {
2919                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
2920                 $$ = bfr->unprotect ();
2921         }
2922         | bass_number  {
2923                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
2924                 $$ = bfr->self_scm ();
2925
2926                 if (scm_is_number ($1))
2927                         bfr->set_property ("figure", $1);
2928                 else if (Text_interface::is_markup ($1))
2929                         bfr->set_property ("text", $1);
2930
2931                 bfr->unprotect ();
2932         }
2933         | bass_figure ']' {
2934                 $$ = $1;
2935                 unsmob_music ($1)->set_property ("bracket-stop", SCM_BOOL_T);
2936         }
2937         | bass_figure figured_bass_alteration {
2938                 Music *m = unsmob_music ($1);
2939                 if (scm_to_double ($2)) {
2940                         SCM salter = m->get_property ("alteration");
2941                         SCM alter = scm_is_number (salter) ? salter : scm_from_int (0);
2942                         m->set_property ("alteration",
2943                                          scm_sum (alter, $2));
2944                 } else {
2945                         m->set_property ("alteration", scm_from_int (0));
2946                 }
2947         }
2948         | bass_figure figured_bass_modification  {
2949                 Music *m = unsmob_music ($1);
2950                 m->set_property ($2, SCM_BOOL_T);
2951         }
2952         ;
2953
2954
2955 figured_bass_modification:
2956         E_PLUS          {
2957                 $$ = ly_symbol2scm ("augmented");
2958         }
2959         | E_EXCLAMATION {
2960                 $$ = ly_symbol2scm ("no-continuation");
2961         }
2962         | '/'           {
2963                 $$ = ly_symbol2scm ("diminished");
2964         }
2965         | E_BACKSLASH {
2966                 $$ = ly_symbol2scm ("augmented-slash");
2967         }
2968         ;
2969
2970 br_bass_figure:
2971         bass_figure {
2972                 $$ = $1;
2973         }
2974         | '[' bass_figure {
2975                 $$ = $2;
2976                 unsmob_music ($$)->set_property ("bracket-start", SCM_BOOL_T);
2977         }
2978         ;
2979
2980 figure_list:
2981         /**/            {
2982                 $$ = SCM_EOL;
2983         }
2984         | figure_list br_bass_figure {
2985                 $$ = scm_cons ($2, $1);
2986         }
2987         ;
2988
2989 figure_spec:
2990         FIGURE_OPEN figure_list FIGURE_CLOSE {
2991                 $$ = scm_reverse_x ($2, SCM_EOL);
2992         }
2993         ;
2994
2995
2996 optional_rest:
2997         /**/   { $$ = SCM_BOOL_F; }
2998         | REST { $$ = SCM_BOOL_T; }
2999         ;
3000
3001 simple_element:
3002         pitch exclamations questions octave_check maybe_notemode_duration optional_rest {
3003                 if (!parser->lexer_->is_note_state ())
3004                         parser->parser_error (@1, _ ("have to be in Note mode for notes"));
3005                 if (!SCM_UNBNDP ($2)
3006                     || !SCM_UNBNDP ($3)
3007                     || scm_is_number ($4)
3008                     || !SCM_UNBNDP ($5)
3009                     || scm_is_true ($6))
3010                 {
3011                         Music *n = 0;
3012                         if (scm_is_true ($6))
3013                                 n = MY_MAKE_MUSIC ("RestEvent", @$);
3014                         else
3015                                 n = MY_MAKE_MUSIC ("NoteEvent", @$);
3016                         
3017                         n->set_property ("pitch", $1);
3018                         if (SCM_UNBNDP ($5))
3019                                 n->set_property ("duration",
3020                                                  parser->default_duration_.smobbed_copy ());
3021                         else
3022                                 n->set_property ("duration", $5);
3023                         
3024                         if (scm_is_number ($4))
3025                         {
3026                                 int q = scm_to_int ($4);
3027                                 n->set_property ("absolute-octave", scm_from_int (q-1));
3028                         }
3029                         
3030                         if (to_boolean ($3))
3031                                 n->set_property ("cautionary", SCM_BOOL_T);
3032                         if (to_boolean ($2) || to_boolean ($3))
3033                                 n->set_property ("force-accidental", SCM_BOOL_T);
3034                         
3035                         $$ = n->unprotect ();
3036                 }
3037         }
3038         | DRUM_PITCH optional_notemode_duration {
3039                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
3040                 n->set_property ("duration", $2);
3041                 n->set_property ("drum-type", $1);
3042
3043                 $$ = n->unprotect ();
3044         }
3045         | RESTNAME optional_notemode_duration           {
3046                 Music *ev = 0;
3047                 if (ly_scm2string ($1) == "s") {
3048                         /* Space */
3049                         ev = MY_MAKE_MUSIC ("SkipEvent", @$);
3050                   }
3051                 else {
3052                         ev = MY_MAKE_MUSIC ("RestEvent", @$);
3053
3054                     }
3055                 ev->set_property ("duration", $2);
3056                 $$ = ev->unprotect ();
3057         }
3058         ;
3059
3060 // Can return a single pitch rather than a list.
3061 simple_chord_elements:
3062         new_chord {
3063                 if (!parser->lexer_->is_chord_state ())
3064                         parser->parser_error (@1, _ ("have to be in Chord mode for chords"));
3065                 $$ = $1;
3066         }
3067         | figure_spec optional_notemode_duration {
3068                 for (SCM s = $1; scm_is_pair (s); s = scm_cdr (s))
3069                 {
3070                         unsmob_music (scm_car (s))->set_property ("duration", $2);
3071                 }
3072                 $$ = $1;
3073         }
3074         ;
3075
3076 lyric_element:
3077         full_markup {
3078                 if (!parser->lexer_->is_lyric_state ())
3079                         parser->parser_error (@1, _ ("markup outside of text script or \\lyricmode"));
3080                 $$ = $1;
3081         }
3082         | STRING {
3083                 if (!parser->lexer_->is_lyric_state ())
3084                         parser->parser_error (@1, _ ("unrecognized string, not in text script or \\lyricmode"));
3085                 $$ = $1;
3086         }
3087         | LYRIC_ELEMENT
3088         ;
3089
3090 lyric_element_music:
3091         lyric_element optional_notemode_duration post_events {
3092                 $$ = MAKE_SYNTAX ("lyric-event", @$, $1, $2);
3093                 if (scm_is_pair ($3))
3094                         unsmob_music ($$)->set_property
3095                                 ("articulations", scm_reverse_x ($3, SCM_EOL));
3096         } %prec ':'
3097         ;
3098
3099 // Can return a single pitch rather than a list.
3100 new_chord:
3101         steno_tonic_pitch maybe_notemode_duration   {
3102                 if (SCM_UNBNDP ($2))
3103                         $$ = $1;
3104                 else
3105                         $$ = make_chord_elements (@$, $1, $2, SCM_EOL);
3106         }
3107         | steno_tonic_pitch optional_notemode_duration chord_separator chord_items {
3108                 SCM its = scm_reverse_x ($4, SCM_EOL);
3109                 $$ = make_chord_elements (@$, $1, $2, scm_cons ($3, its));
3110         } %prec ':'
3111         ;
3112
3113 chord_items:
3114         /**/ {
3115                 $$ = SCM_EOL;
3116         }
3117         | chord_items chord_item {
3118                 $$ = scm_cons ($2, $$);
3119         }
3120         ;
3121
3122 chord_separator:
3123         CHORD_COLON {
3124                 $$ = ly_symbol2scm ("chord-colon");
3125         }
3126         | CHORD_CARET {
3127                 $$ = ly_symbol2scm ("chord-caret");
3128         }
3129         | CHORD_SLASH steno_tonic_pitch {
3130                 $$ = scm_list_2 (ly_symbol2scm ("chord-slash"), $2);
3131         }
3132         | CHORD_BASS steno_tonic_pitch {
3133                 $$ = scm_list_2 (ly_symbol2scm ("chord-bass"), $2);
3134         }
3135         ;
3136
3137 chord_item:
3138         chord_separator {
3139                 $$ = $1;
3140         }
3141         | step_numbers {
3142                 $$ = scm_reverse_x ($1, SCM_EOL);
3143         }
3144         | CHORD_MODIFIER  {
3145                 $$ = $1;
3146         }
3147         ;
3148
3149 step_numbers:
3150         step_number { $$ = scm_cons ($1, SCM_EOL); }
3151         | step_numbers '.' step_number {
3152                 $$ = scm_cons ($3, $$);
3153         }
3154         ;
3155
3156 step_number:
3157         UNSIGNED {
3158                 $$ = make_chord_step ($1, 0);
3159         }
3160         | UNSIGNED '+' {
3161                 $$ = make_chord_step ($1, SHARP_ALTERATION);
3162         }
3163         | UNSIGNED CHORD_MINUS {
3164                 $$ = make_chord_step ($1, FLAT_ALTERATION);
3165         }
3166         ;
3167
3168 tempo_range:
3169         unsigned_number {
3170                 $$ = $1;
3171         } %prec ':'
3172         | unsigned_number '-' unsigned_number {
3173                 $$ = scm_cons ($1, $3);
3174         }
3175         ;
3176
3177 /*
3178         UTILITIES
3179
3180 TODO: should deprecate in favor of Scheme?
3181
3182  */
3183 number_expression:
3184         number_expression '+' number_term {
3185                 $$ = scm_sum ($1, $3);
3186         }
3187         | number_expression '-' number_term {
3188                 $$ = scm_difference ($1, $3);
3189         }
3190         | number_term
3191         ;
3192
3193 number_term:
3194         number_factor {
3195                 $$ = $1;
3196         }
3197         | number_factor '*' number_factor {
3198                 $$ = scm_product ($1, $3);
3199         }
3200         | number_factor '/' number_factor {
3201                 $$ = scm_divide ($1, $3);
3202         }
3203         ;
3204
3205 number_factor:
3206         '-'  number_factor { /* %prec UNARY_MINUS */
3207                 $$ = scm_difference ($2, SCM_UNDEFINED);
3208         }
3209         | bare_number
3210         ;
3211
3212 bare_number_common:
3213         REAL
3214         | NUMBER_IDENTIFIER
3215         | REAL NUMBER_IDENTIFIER
3216         {
3217                 $$ = scm_product ($1, $2);
3218         }
3219         ;
3220
3221 bare_number:
3222         bare_number_common
3223         | UNSIGNED
3224         | UNSIGNED NUMBER_IDENTIFIER    {
3225                 $$ = scm_product ($1, $2);
3226         }
3227         ;
3228
3229 unsigned_number:
3230         UNSIGNED
3231         | NUMBER_IDENTIFIER
3232         {
3233                 if (!scm_is_integer ($1)
3234                     || scm_is_true (scm_negative_p ($1)))
3235                 {
3236                         parser->parser_error (@1, _("not an unsigned integer"));
3237                         $$ = SCM_INUM0;
3238                 }
3239         }
3240         | embedded_scm
3241         {
3242                 if (!scm_is_integer ($1)
3243                     || scm_is_true (scm_negative_p ($1)))
3244                 {
3245                         parser->parser_error (@1, _("not an unsigned integer"));
3246                         $$ = SCM_INUM0;
3247                 }
3248         }
3249         ;
3250
3251 exclamations:
3252                 { $$ = SCM_UNDEFINED; }
3253         | exclamations '!'
3254         {
3255                 if (SCM_UNBNDP ($1))
3256                         $$ = SCM_BOOL_T;
3257                 else
3258                         $$ = scm_not ($1);
3259         }
3260         ;
3261
3262 questions:
3263 // This precedence rule is rather weird.  It triggers when '!' is
3264 // encountered after a pitch, and is used for deciding whether to save
3265 // this instead for a figure modification.  This should not actually
3266 // occur in practice as pitches and figures are generated in different
3267 // modes.  Using a greedy (%right) precedence makes sure that we don't
3268 // get stuck in a wrong interpretation.
3269         { $$ = SCM_UNDEFINED; } %prec ':'
3270         | questions '?'
3271         {
3272                 if (SCM_UNBNDP ($1))
3273                         $$ = SCM_BOOL_T;
3274                 else
3275                         $$ = scm_not ($1);
3276         }
3277         ;
3278
3279 full_markup_list:
3280         MARKUPLIST
3281                 { parser->lexer_->push_markup_state (); }
3282         markup_list {
3283                 $$ = $3;
3284                 parser->lexer_->pop_state ();
3285         }
3286         ;
3287
3288 full_markup:
3289         MARKUP
3290                 { parser->lexer_->push_markup_state (); }
3291         markup_top {
3292                 $$ = $3;
3293                 parser->lexer_->pop_state ();
3294         }
3295         ;
3296
3297 markup_top:
3298         markup_list {
3299                 $$ = scm_list_2 (ly_lily_module_constant ("line-markup"),  $1);
3300         }
3301         | markup_head_1_list simple_markup
3302         {
3303                 $$ = scm_car (MAKE_SYNTAX ("composed-markup-list",
3304                                            @2, $1, scm_list_1 ($2)));
3305         }
3306         | simple_markup {
3307                 $$ = $1;
3308         }
3309         ;
3310
3311 markup_scm:
3312         embedded_scm_bare
3313         {
3314                 if (Text_interface::is_markup ($1))
3315                         MYBACKUP (MARKUP_IDENTIFIER, $1, @1);
3316                 else if (Text_interface::is_markup_list ($1))
3317                         MYBACKUP (MARKUPLIST_IDENTIFIER, $1, @1);
3318                 else {
3319                         parser->parser_error (@1, _ ("not a markup"));
3320                         MYBACKUP (MARKUP_IDENTIFIER, scm_string (SCM_EOL), @1);
3321                 }
3322         } BACKUP
3323         ;
3324                         
3325
3326 markup_list:
3327         markup_composed_list {
3328                 $$ = $1;
3329         }
3330         | markup_uncomposed_list
3331         ;
3332
3333 markup_uncomposed_list:
3334         markup_braced_list {
3335                 $$ = $1;
3336         }
3337         | markup_command_list {
3338                 $$ = scm_list_1 ($1);
3339         }
3340         | markup_scm MARKUPLIST_IDENTIFIER
3341         {
3342                 $$ = $2;
3343         }
3344         | SCORELINES {
3345                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
3346                 parser->lexer_->push_note_state (nn);
3347         } '{' score_body '}' {
3348                 unsmob_score ($4)->origin ()->set_spot (@$);
3349                 $$ = scm_list_1 (scm_list_2 (ly_lily_module_constant ("score-lines-markup-list"), $4));
3350                 parser->lexer_->pop_state ();
3351         }
3352         ;
3353
3354 markup_composed_list:
3355         markup_head_1_list markup_uncomposed_list {
3356                 $$ = MAKE_SYNTAX ("composed-markup-list",
3357                                   @2, $1, $2);
3358         }
3359         ;
3360
3361 markup_braced_list:
3362         '{' markup_braced_list_body '}' {
3363                 $$ = scm_reverse_x ($2, SCM_EOL);
3364         }
3365         ;
3366
3367 markup_braced_list_body:
3368         /* empty */     {  $$ = SCM_EOL; }
3369         | markup_braced_list_body markup {
3370                 $$ = scm_cons ($2, $1);
3371         }
3372         | markup_braced_list_body markup_list {
3373                 $$ = scm_reverse_x ($2, $1);
3374         }
3375         ;
3376
3377 markup_command_list:
3378         MARKUP_LIST_FUNCTION markup_command_list_arguments {
3379           $$ = scm_cons ($1, scm_reverse_x($2, SCM_EOL));
3380         }
3381         ;
3382
3383 markup_command_basic_arguments:
3384         EXPECT_MARKUP_LIST markup_command_list_arguments markup_list {
3385           $$ = scm_cons ($3, $2);
3386         }
3387         | EXPECT_SCM markup_command_list_arguments embedded_scm {
3388           $$ = check_scheme_arg (parser, @3, $3, $2, $1);
3389         }
3390         | EXPECT_NO_MORE_ARGS {
3391           $$ = SCM_EOL;
3392         }
3393         ;
3394
3395 markup_command_list_arguments:
3396         markup_command_basic_arguments { $$ = $1; }
3397         | EXPECT_MARKUP markup_command_list_arguments markup {
3398           $$ = scm_cons ($3, $2);
3399         }
3400         ;
3401
3402 markup_head_1_item:
3403         MARKUP_FUNCTION EXPECT_MARKUP markup_command_list_arguments {
3404           $$ = scm_cons ($1, scm_reverse_x ($3, SCM_EOL));
3405         }
3406         ;
3407
3408 markup_head_1_list:
3409         markup_head_1_item      {
3410                 $$ = scm_list_1 ($1);
3411         }
3412         | markup_head_1_list markup_head_1_item {
3413                 $$ = scm_cons ($2, $1);
3414         }
3415         ;
3416
3417 simple_markup:
3418         STRING {
3419                 $$ = make_simple_markup ($1);
3420         }
3421         | SCORE {
3422                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
3423                 parser->lexer_->push_note_state (nn);
3424         } '{' score_body '}' {
3425                 unsmob_score ($4)->origin ()->set_spot (@$);
3426                 $$ = scm_list_2 (ly_lily_module_constant ("score-markup"), $4);
3427                 parser->lexer_->pop_state ();
3428         }
3429         | MARKUP_FUNCTION markup_command_basic_arguments {
3430                 $$ = scm_cons ($1, scm_reverse_x ($2, SCM_EOL));
3431         }
3432         | markup_scm MARKUP_IDENTIFIER
3433         {
3434                 $$ = $2;
3435         }
3436         ;
3437
3438 markup:
3439         markup_head_1_list simple_markup
3440         {
3441                 $$ = scm_car (MAKE_SYNTAX ("composed-markup-list",
3442                                            @2, $1, scm_list_1 ($2)));
3443         }
3444         | simple_markup {
3445                 $$ = $1;
3446         }
3447         ;
3448
3449 %%
3450
3451 void
3452 Lily_parser::set_yydebug (bool x)
3453 {
3454         yydebug = x;
3455 }
3456
3457 SCM
3458 Lily_parser::do_yyparse ()
3459 {
3460         SCM retval = SCM_UNDEFINED;
3461         yyparse (this, &retval);
3462         return retval;
3463 }
3464
3465
3466
3467
3468
3469 /*
3470
3471 It is a little strange to have this function in this file, but
3472 otherwise, we have to import music classes into the lexer.
3473
3474 */
3475 int
3476 Lily_lexer::try_special_identifiers (SCM *destination, SCM sid)
3477 {
3478         if (unsmob_book (sid)) {
3479                 Book *book =  unsmob_book (sid)->clone ();
3480                 *destination = book->self_scm ();
3481                 book->unprotect ();
3482
3483                 return BOOK_IDENTIFIER;
3484         } else if (scm_is_number (sid)) {
3485                 *destination = sid;
3486                 return NUMBER_IDENTIFIER;
3487         } else if (unsmob_context_def (sid)) {
3488                 Context_def *def= unsmob_context_def (sid)->clone ();
3489
3490                 *destination = def->self_scm ();
3491                 def->unprotect ();
3492
3493                 return CONTEXT_DEF_IDENTIFIER;
3494         } else if (unsmob_context_mod (sid)) {
3495                 *destination = unsmob_context_mod (sid)->smobbed_copy ();
3496
3497                 return CONTEXT_MOD_IDENTIFIER;
3498         } else if (Music *mus = unsmob_music (sid)) {
3499                 mus = mus->clone ();
3500                 *destination = mus->self_scm ();
3501                 bool is_event = mus->is_mus_type ("post-event");
3502                 mus->unprotect ();
3503                 return is_event ? EVENT_IDENTIFIER : MUSIC_IDENTIFIER;
3504         } else if (unsmob_pitch (sid)) {
3505                 *destination = unsmob_pitch (sid)->smobbed_copy ();
3506                 return PITCH_IDENTIFIER;
3507         } else if (unsmob_duration (sid)) {
3508                 *destination = unsmob_duration (sid)->smobbed_copy ();
3509                 return DURATION_IDENTIFIER;
3510         } else if (unsmob_output_def (sid)) {
3511                 Output_def *p = unsmob_output_def (sid);
3512                 p = p->clone ();
3513
3514                 *destination = p->self_scm ();
3515                 p->unprotect ();
3516                 return OUTPUT_DEF_IDENTIFIER;
3517         } else if (unsmob_score (sid)) {
3518                 *destination = unsmob_score (sid)->clone ()->unprotect ();
3519                 return SCM_IDENTIFIER;
3520         }
3521
3522         return -1;
3523 }
3524
3525 SCM
3526 get_next_unique_context_id ()
3527 {
3528         return scm_from_locale_string ("$uniqueContextId");
3529 }
3530
3531
3532 SCM
3533 get_next_unique_lyrics_context_id ()
3534 {
3535         static int new_context_count;
3536         char s[128];
3537         snprintf (s, sizeof (s)-1, "uniqueContext%d", new_context_count++);
3538         return scm_from_locale_string (s);
3539 }
3540
3541 // check_scheme_arg checks one argument with a given predicate for use
3542 // in an argument list and throws a syntax error if it is unusable.
3543 // The argument is prepended to the argument list in any case.  After
3544 // throwing a syntax error, the argument list is terminated with #f as
3545 // its last cdr in order to mark it as uncallable while not losing
3546 // track of its total length.
3547 //
3548 // There are a few special considerations: if optional argument disp
3549 // is given (otherwise it defaults to SCM_UNDEFINED), it will be used
3550 // instead of arg in a prospective error message.  This is useful if
3551 // arg is not the actual argument but rather a transformation of it.
3552 //
3553 // If arg itself is SCM_UNDEFINED, the predicate is considered false
3554 // and an error message using disp is produced unconditionally.
3555
3556 SCM check_scheme_arg (Lily_parser *parser, Input loc,
3557                       SCM arg, SCM args, SCM pred, SCM disp)
3558 {
3559         if (SCM_UNBNDP (arg))
3560                 args = scm_cons (disp, args);
3561         else {
3562                 args = scm_cons (arg, args);
3563                 if (scm_is_true (scm_call_1 (pred, arg)))
3564                         return args;
3565         }
3566         scm_set_cdr_x (scm_last_pair (args), SCM_EOL);
3567         MAKE_SYNTAX ("argument-error", loc, scm_length (args), pred,
3568                      SCM_UNBNDP (disp) ? arg : disp);
3569         scm_set_cdr_x (scm_last_pair (args), SCM_BOOL_F);
3570         return args;
3571 }
3572
3573 SCM loc_on_music (Input loc, SCM arg)
3574 {
3575         if (Music *m = unsmob_music (arg))
3576         {
3577                 m = m->clone ();
3578                 m->set_spot (loc);
3579                 return m->unprotect ();
3580         }
3581         return arg;
3582 }
3583
3584 SCM
3585 try_string_variants (SCM pred, SCM str)
3586 {
3587         // a matching predicate is always ok
3588         if (scm_is_true (scm_call_1 (pred, str)))
3589                 return str;
3590         // a symbol may be interpreted as a list of symbols if it helps
3591         if (scm_is_symbol (str)) {
3592                 str = scm_list_1 (str);
3593                 if (scm_is_true (scm_call_1 (pred, str)))
3594                         return str;
3595                 return SCM_UNDEFINED;
3596         }
3597
3598         // If this cannot be a string representation of a symbol list,
3599         // we are through.
3600
3601         if (!is_regular_identifier (str, true))
3602                 return SCM_UNDEFINED;
3603
3604         str = scm_string_split (str, SCM_MAKE_CHAR ('.'));
3605         for (SCM p = str; scm_is_pair (p); p = scm_cdr (p))
3606                 scm_set_car_x (p, scm_string_to_symbol (scm_car (p)));
3607
3608         // Let's attempt the symbol list interpretation first.
3609
3610         if (scm_is_true (scm_call_1 (pred, str)))
3611                 return str;
3612
3613         // If there is just one symbol in the list, we might interpret
3614         // it as a single symbol
3615
3616         if (scm_is_null (scm_cdr (str)))
3617         {
3618                 str = scm_car (str);
3619                 if (scm_is_true (scm_call_1 (pred, str)))
3620                         return str;
3621         }
3622
3623         return SCM_UNDEFINED;
3624 }
3625
3626 bool
3627 is_regular_identifier (SCM id, bool multiple)
3628 {
3629   if (!scm_is_string (id))
3630           return false;
3631
3632   string str = ly_scm2string (id);
3633
3634   bool middle = false;
3635
3636   for (string::iterator it=str.begin(); it != str.end (); it++)
3637   {
3638           int c = *it & 0xff;
3639           if ((c >= 'a' && c <= 'z')
3640               || (c >= 'A' && c <= 'Z')
3641               || c > 0x7f)
3642                   middle = true;
3643           else if (middle && (c == '-' || c == '_' || (multiple && c == '.')))
3644                   middle = false;
3645           else
3646                   return false;
3647   }
3648   return middle;
3649 }
3650
3651 SCM
3652 make_music_from_simple (Lily_parser *parser, Input loc, SCM simple)
3653 {
3654         if (unsmob_music (simple))
3655                 return simple;
3656         if (parser->lexer_->is_note_state ()) {
3657                 if (scm_is_symbol (simple)) {
3658                         Music *n = MY_MAKE_MUSIC ("NoteEvent", loc);
3659                         n->set_property ("duration", parser->default_duration_.smobbed_copy ());
3660                         n->set_property ("drum-type", simple);
3661                         return n->unprotect ();
3662                 }
3663                 if (unsmob_pitch (simple)) {
3664                         Music *n = MY_MAKE_MUSIC ("NoteEvent", loc);
3665                         n->set_property ("duration", parser->default_duration_.smobbed_copy ());
3666                         n->set_property ("pitch", simple);
3667                         return n->unprotect ();
3668                 }
3669                 return simple;
3670         } else if (parser->lexer_->is_lyric_state ()) {
3671                 if (Text_interface::is_markup (simple))
3672                         return MAKE_SYNTAX ("lyric-event", loc, simple,
3673                                             parser->default_duration_.smobbed_copy ());
3674         } else if (parser->lexer_->is_chord_state ()) {
3675                 if (unsmob_pitch (simple))
3676                         return MAKE_SYNTAX
3677                                 ("event-chord",
3678                                  loc,
3679                                  make_chord_elements (loc, simple,
3680                                                       parser->default_duration_.smobbed_copy (),
3681                                                       SCM_EOL));
3682         }
3683         return simple;
3684 }
3685
3686 Music *
3687 make_music_with_input (SCM name, Input where)
3688 {
3689        Music *m = make_music_by_name (name);
3690        m->set_spot (where);
3691        return m;
3692 }
3693
3694 SCM
3695 make_simple_markup (SCM a)
3696 {
3697         return a;
3698 }
3699
3700 SCM
3701 make_duration (SCM d, int dots)
3702 {
3703         int t = scm_to_int (d);
3704         if (t > 0 && (t & (t-1)) == 0)
3705                 return Duration (intlog2 (t), dots).smobbed_copy ();
3706         else
3707                 return SCM_UNDEFINED;
3708 }
3709
3710 SCM
3711 make_chord_step (SCM step_scm, Rational alter)
3712 {
3713         int step = scm_to_int (step_scm);
3714
3715         if (step == 7)
3716                 alter += FLAT_ALTERATION;
3717
3718         while (step < 0)
3719                 step += 7;
3720         Pitch m ((step -1) / 7, (step - 1) % 7, alter);
3721         return m.smobbed_copy ();
3722 }
3723
3724
3725 SCM
3726 make_chord_elements (Input loc, SCM pitch, SCM dur, SCM modification_list)
3727 {
3728         SCM chord_ctor = ly_lily_module_constant ("construct-chord-elements");
3729         SCM res = scm_call_3 (chord_ctor, pitch, dur, modification_list);
3730         for (SCM s = res; scm_is_pair (s); s = scm_cdr (s))
3731         {
3732                 unsmob_music (scm_car (s))->set_spot (loc);
3733         }
3734         return res;
3735 }
3736
3737 int
3738 yylex (YYSTYPE *s, YYLTYPE *loc, Lily_parser *parser)
3739 {
3740         Lily_lexer *lex = parser->lexer_;
3741
3742         lex->lexval_ = s;
3743         lex->lexloc_ = loc;
3744         int tok = lex->pop_extra_token ();
3745         if (tok >= 0)
3746                 return tok;
3747         lex->prepare_for_next_token ();
3748         return lex->yylex ();
3749 }