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