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