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