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