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