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