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