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