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