]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
7fff408f7d9d0aa59d25fa5ca9970da13a9e602e
[lilypond.git] / lily / parser.yy
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2010 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_SCM;
274 %token <i> EXPECT_MARKUP_LIST
275 /* After the last argument. */
276 %token <i> EXPECT_NO_MORE_ARGS;
277
278 %token <scm> BOOK_IDENTIFIER
279 %token <scm> CHORDMODIFIER_PITCH
280 %token <scm> CHORD_MODIFIER
281 %token <scm> CHORD_REPETITION
282 %token <scm> CONTEXT_DEF_IDENTIFIER
283 %token <scm> CONTEXT_MOD_IDENTIFIER
284 %token <scm> DRUM_PITCH
285 %token <scm> DURATION_IDENTIFIER
286 %token <scm> EVENT_IDENTIFIER
287 %token <scm> FRACTION
288 %token <scm> LYRICS_STRING
289 %token <scm> LYRIC_MARKUP_IDENTIFIER
290 %token <scm> MARKUP_FUNCTION
291 %token <scm> MARKUP_LIST_FUNCTION
292 %token <scm> MARKUP_IDENTIFIER
293 %token <scm> MUSIC_FUNCTION
294 %token <scm> MUSIC_IDENTIFIER
295 %token <scm> NOTENAME_PITCH
296 %token <scm> NUMBER_IDENTIFIER
297 %token <scm> OUTPUT_DEF_IDENTIFIER
298 %token <scm> REAL
299 %token <scm> RESTNAME
300 %token <scm> SCM_IDENTIFIER
301 %token <scm> SCM_TOKEN
302 %token <scm> SCORE_IDENTIFIER
303 %token <scm> STRING
304 %token <scm> STRING_IDENTIFIER
305 %token <scm> TONICNAME_PITCH
306
307
308 %type <book> book_block
309 %type <book> book_body
310 %type <book> bookpart_block
311 %type <book> bookpart_body
312
313 %type <i> bare_unsigned
314 %type <scm> figured_bass_alteration
315 %type <i> dots
316 %type <i> exclamations
317 %type <i> optional_rest
318 %type <i> questions
319 %type <i> script_dir
320 %type <i> sub_quotes
321 %type <i> sup_quotes
322 %type <i> tremolo_type
323
324 /* Music */
325 %type <scm> composite_music
326 %type <scm> grouped_music_list
327 %type <scm> music
328 %type <scm> prefix_composite_music
329 %type <scm> repeated_music
330 %type <scm> sequential_music
331 %type <scm> simple_music
332 %type <scm> simultaneous_music
333 %type <scm> chord_body
334 %type <scm> chord_body_element
335 %type <scm> command_element
336 %type <scm> command_event
337 %type <scm> context_modification
338 %type <scm> context_change
339 %type <scm> direction_less_event
340 %type <scm> direction_reqd_event
341 %type <scm> event_chord
342 %type <scm> gen_text_def
343 %type <scm> music_property_def
344 %type <scm> note_chord_element
345 %type <scm> post_event
346 %type <scm> re_rhythmed_music
347 %type <scm> relative_music
348 %type <scm> simple_element
349 %type <scm> simple_music_property_def
350 %type <scm> string_number_event
351 %type <scm> tempo_event
352
353 %type <outputdef> output_def_body
354 %type <outputdef> output_def_head
355 %type <outputdef> output_def_head_with_mode_switch
356 %type <outputdef> output_def
357 %type <outputdef> paper_block
358
359 %type <scm> alternative_music
360 %type <scm> generic_prefix_music_scm
361 %type <scm> music_list
362 %type <scm> absolute_pitch
363 %type <scm> assignment_id
364 %type <scm> bare_number
365 %type <scm> unsigned_number
366 %type <scm> bass_figure
367 %type <scm> figured_bass_modification
368 %type <scm> br_bass_figure
369 %type <scm> bass_number
370 %type <scm> chord_body_elements
371 %type <scm> chord_item
372 %type <scm> chord_items
373 %type <scm> chord_separator
374 %type <scm> context_def_mod
375 %type <scm> context_def_spec_block
376 %type <scm> context_def_spec_body
377 %type <scm> context_mod
378 %type <scm> context_mod_list
379 %type <scm> context_prop_spec
380 %type <scm> direction_less_char
381 %type <scm> duration_length
382 %type <scm> embedded_scm
383 %type <scm> figure_list
384 %type <scm> figure_spec
385 %type <scm> fraction
386 %type <scm> full_markup
387 %type <scm> full_markup_list
388 %type <scm> function_scm_argument
389 %type <scm> function_arglist
390 %type <scm> function_arglist_music_last
391 %type <scm> function_arglist_nonmusic_last
392 %type <scm> function_arglist_nonmusic
393 %type <scm> identifier_init
394 %type <scm> lilypond_header
395 %type <scm> lilypond_header_body
396 %type <scm> lyric_element
397 %type <scm> lyric_markup
398 %type <scm> markup
399 %type <scm> markup_braced_list
400 %type <scm> markup_braced_list_body
401 %type <scm> markup_composed_list
402 %type <scm> markup_command_list
403 %type <scm> markup_command_list_arguments
404 %type <scm> markup_command_basic_arguments
405 %type <scm> markup_head_1_item
406 %type <scm> markup_head_1_list
407 %type <scm> markup_list
408 %type <scm> markup_top
409 %type <scm> mode_changing_head
410 %type <scm> mode_changing_head_with_context
411 %type <scm> multiplied_duration
412 %type <scm> music_function_identifier_musicless_prefix
413 %type <scm> music_function_event
414 %type <scm> music_function_chord_body
415 %type <scm> new_chord
416 %type <scm> new_lyrics
417 %type <scm> number_expression
418 %type <scm> number_factor
419 %type <scm> number_term
420 %type <scm> octave_check
421 %type <scm> optional_context_mod
422 %type <scm> optional_id
423 %type <scm> optional_notemode_duration
424 %type <scm> pitch
425 %type <scm> pitch_also_in_chords
426 %type <scm> post_events
427 %type <scm> property_operation
428 %type <scm> property_path property_path_revved
429 %type <scm> scalar
430 %type <scm> script_abbreviation
431 %type <scm> simple_chord_elements
432 %type <scm> simple_markup
433 %type <scm> simple_string
434 %type <scm> steno_duration
435 %type <scm> steno_pitch
436 %type <scm> steno_tonic_pitch
437 %type <scm> step_number
438 %type <scm> step_numbers
439 %type <scm> string
440
441 %type <score> score_block
442 %type <score> score_body
443
444
445 %left '-' '+'
446
447 /* We don't assign precedence to / and *, because we might need varied
448 prec levels in different prods */
449
450 %left UNARY_MINUS
451
452 %%
453
454 lilypond:       /* empty */
455         | lilypond toplevel_expression {
456         }
457         | lilypond assignment {
458         }
459         | lilypond error {
460                 PARSER->error_level_ = 1;
461         }
462         | lilypond INVALID      {
463                 PARSER->error_level_ = 1;
464         }
465         ;
466
467
468 toplevel_expression:
469         lilypond_header {
470                 PARSER->lexer_->set_identifier (ly_symbol2scm ("$defaultheader"), $1);
471         }
472         | book_block {
473                 Book *book = $1;
474                 SCM proc = PARSER->lexer_->lookup_identifier ("toplevel-book-handler");
475                 scm_call_2 (proc, PARSER->self_scm (), book->self_scm ());
476                 book->unprotect ();
477         }
478         | bookpart_block {
479                 Book *bookpart = $1;
480                 SCM proc = PARSER->lexer_->lookup_identifier ("toplevel-bookpart-handler");
481                 scm_call_2 (proc, PARSER->self_scm (), bookpart->self_scm ());
482                 bookpart->unprotect ();
483         }
484         | score_block {
485                 Score *score = $1;
486
487                 SCM proc = PARSER->lexer_->lookup_identifier ("toplevel-score-handler");
488                 scm_call_2 (proc, PARSER->self_scm (), score->self_scm ());
489                 score->unprotect ();
490         }
491         | composite_music {
492                 Music *music = unsmob_music ($1);
493                 SCM proc = PARSER->lexer_->lookup_identifier ("toplevel-music-handler");
494                 scm_call_2 (proc, PARSER->self_scm (), music->self_scm ());
495         }
496         | full_markup {
497                 SCM proc = PARSER->lexer_->lookup_identifier ("toplevel-text-handler");
498                 scm_call_2 (proc, PARSER->self_scm (), scm_list_1 ($1));
499         }
500         | full_markup_list {
501                 SCM proc = PARSER->lexer_->lookup_identifier ("toplevel-text-handler");
502                 scm_call_2 (proc, PARSER->self_scm (), $1);
503         }
504         | output_def {
505                 SCM id = SCM_EOL;
506                 Output_def * od = $1;
507
508                 if ($1->c_variable ("is-paper") == SCM_BOOL_T)
509                         id = ly_symbol2scm ("$defaultpaper");
510                 else if ($1->c_variable ("is-midi") == SCM_BOOL_T)
511                         id = ly_symbol2scm ("$defaultmidi");
512                 else if ($1->c_variable ("is-layout") == SCM_BOOL_T)
513                         id = ly_symbol2scm ("$defaultlayout");
514
515                 PARSER->lexer_->set_identifier (id, od->self_scm ());
516                 od->unprotect();
517         }
518         ;
519
520 embedded_scm:
521         SCM_TOKEN
522         | SCM_IDENTIFIER
523         ;
524
525
526 lilypond_header_body:
527         {
528                 $$ = get_header (PARSER);
529                 PARSER->lexer_->add_scope ($$);
530         }
531         | lilypond_header_body assignment  {
532
533         }
534         ;
535
536 lilypond_header:
537         HEADER '{' lilypond_header_body '}'     {
538                 $$ = PARSER->lexer_->remove_scope ();
539         }
540         ;
541
542 /*
543         DECLARATIONS
544 */
545 assignment_id:
546         STRING          { $$ = $1; }
547         | LYRICS_STRING { $$ = $1; }
548         ;
549
550 assignment:
551         assignment_id '=' identifier_init  {
552                 PARSER->lexer_->set_identifier ($1, $3);
553         }
554         | assignment_id property_path '=' identifier_init {
555                 SCM path = scm_cons (scm_string_to_symbol ($1), $2);
556                 PARSER->lexer_->set_identifier (path, $4);
557         ;
558 /*
559  TODO: devise standard for protection in parser.
560
561   The parser stack lives on the C-stack, which means that
562 all objects can be unprotected as soon as they're here.
563
564 */
565         }
566         | embedded_scm { }
567         ;
568
569
570 identifier_init:
571         score_block {
572                 $$ = $1->self_scm ();
573                 $1->unprotect ();
574         }
575         | book_block {
576                 $$ = $1->self_scm ();
577                 $1->unprotect ();
578         }
579         | bookpart_block {
580                 $$ = $1->self_scm ();
581                 $1->unprotect ();
582         }
583         | output_def {
584                 $$ = $1->self_scm ();
585                 $1->unprotect ();
586         }
587         | context_def_spec_block {
588                 $$ = $1;
589         }
590         | music  {
591                 /* Hack: Create event-chord around standalone events.
592                    Prevents the identifier from being interpreted as a post-event. */
593                 Music *mus = unsmob_music ($1);
594                 bool is_event = mus &&
595                         (scm_memq (ly_symbol2scm ("event"), mus->get_property ("types"))
596                                 != SCM_BOOL_F);
597                 if (!is_event)
598                         $$ = $1;
599                 else
600                         $$ = MAKE_SYNTAX ("event-chord", @$, scm_list_1 ($1));
601         }
602         | post_event {
603                 $$ = $1;
604         }
605         | number_expression {
606                 $$ = $1;
607         }
608         | string {
609                 $$ = $1;
610         }
611         | embedded_scm {
612                 $$ = $1;
613         }
614         | full_markup {
615                 $$ = $1;
616         }
617         | DIGIT {
618                 $$ = scm_from_int ($1);
619         }
620         | context_modification {
621                 $$ = $1;
622         }
623         ;
624
625 context_def_spec_block:
626         CONTEXT '{' context_def_spec_body '}'
627                 {
628                 $$ = $3;
629         }
630         ;
631
632 context_def_spec_body:
633         /**/ {
634                 $$ = Context_def::make_scm ();
635                 unsmob_context_def ($$)->origin ()->set_spot (@$);
636         }
637         | CONTEXT_DEF_IDENTIFIER {
638                 $$ = $1;
639                 unsmob_context_def ($$)->origin ()->set_spot (@$);
640         }
641         | context_def_spec_body GROBDESCRIPTIONS embedded_scm {
642                 Context_def*td = unsmob_context_def ($$);
643
644                 for (SCM p = $3; scm_is_pair (p); p = scm_cdr (p)) {
645                         SCM tag = scm_caar (p);
646
647                         /* TODO: should make new tag "grob-definition" ? */
648                         td->add_context_mod (scm_list_3 (ly_symbol2scm ("assign"),
649                                                         tag, scm_cons (scm_cdar (p), SCM_EOL)));
650                 }
651         }
652         | context_def_spec_body context_mod {
653                 unsmob_context_def ($$)->add_context_mod ($2);
654         }
655         | context_def_spec_body context_modification {
656                 Context_def *td = unsmob_context_def ($$);
657                 SCM new_mods = unsmob_context_mod ($2)->get_mods ();
658                 for (SCM m = new_mods; scm_is_pair (m); m = scm_cdr (m)) {
659                     td->add_context_mod (scm_car (m));
660                 }
661         }
662         ;
663
664
665
666 book_block:
667         BOOK '{' book_body '}'  {
668                 $$ = $3;
669                 pop_paper (PARSER);
670                 PARSER->lexer_->set_identifier (ly_symbol2scm ("$current-book"), SCM_BOOL_F);
671         }
672         ;
673
674 /* FIXME:
675    * Use 'handlers' like for toplevel-* stuff?
676    * grok \layout and \midi?  */
677 book_body:
678         {
679                 $$ = new Book;
680                 init_papers (PARSER);
681                 $$->origin ()->set_spot (@$);
682                 $$->paper_ = dynamic_cast<Output_def*> (unsmob_output_def (PARSER->lexer_->lookup_identifier ("$defaultpaper"))->clone ());
683                 $$->paper_->unprotect ();
684                 push_paper (PARSER, $$->paper_);
685                 $$->header_ = PARSER->lexer_->lookup_identifier ("$defaultheader");
686                 PARSER->lexer_->set_identifier (ly_symbol2scm ("$current-book"), $$->self_scm ());
687                 PARSER->lexer_->set_identifier (ly_symbol2scm ("book-output-suffix"), SCM_BOOL_F);
688                 PARSER->lexer_->set_identifier (ly_symbol2scm ("book-filename"), SCM_BOOL_F);
689         }
690         | BOOK_IDENTIFIER {
691                 $$ = unsmob_book ($1);
692                 $$->protect ();
693                 $$->origin ()->set_spot (@$);
694                 PARSER->lexer_->set_identifier (ly_symbol2scm ("$current-book"), $1);
695         }
696         | book_body paper_block {
697                 $$->paper_ = $2;
698                 $2->unprotect ();
699                 set_paper (PARSER, $2);
700         }
701         | book_body bookpart_block {
702                 Book *bookpart = $2;
703                 SCM proc = PARSER->lexer_->lookup_identifier ("book-bookpart-handler");
704                 scm_call_2 (proc, $$->self_scm (), bookpart->self_scm ());
705                 bookpart->unprotect ();
706         }
707         | book_body score_block {
708                 Score *score = $2;
709                 SCM proc = PARSER->lexer_->lookup_identifier ("book-score-handler");
710                 scm_call_2 (proc, $$->self_scm (), score->self_scm ());
711                 score->unprotect ();
712         }
713         | book_body composite_music {
714                 Music *music = unsmob_music ($2);
715                 SCM proc = PARSER->lexer_->lookup_identifier ("book-music-handler");
716                 scm_call_3 (proc, PARSER->self_scm (), $$->self_scm (), music->self_scm ());
717         }
718         | book_body full_markup {
719                 SCM proc = PARSER->lexer_->lookup_identifier ("book-text-handler");
720                 scm_call_2 (proc, $$->self_scm (), scm_list_1 ($2));
721         }
722         | book_body full_markup_list {
723                 SCM proc = PARSER->lexer_->lookup_identifier ("book-text-handler");
724                 scm_call_2 (proc, $$->self_scm (), $2);
725         }
726         | book_body lilypond_header {
727                 $$->header_ = $2;
728         }
729         | book_body error {
730                 $$->paper_ = 0;
731                 $$->scores_ = SCM_EOL;
732                 $$->bookparts_ = SCM_EOL;
733         }
734         ;
735
736 bookpart_block:
737         BOOKPART '{' bookpart_body '}' {
738                 $$ = $3;
739                 PARSER->lexer_->set_identifier (ly_symbol2scm ("$current-bookpart"), SCM_BOOL_F);
740         }
741         ;
742
743 bookpart_body:
744         {
745                 $$ = new Book;
746                 $$->origin ()->set_spot (@$);
747                 PARSER->lexer_->set_identifier (ly_symbol2scm ("$current-bookpart"), $$->self_scm ());
748         }
749         | BOOK_IDENTIFIER {
750                 $$ = unsmob_book ($1);
751                 $$->protect ();
752                 $$->origin ()->set_spot (@$);
753                 PARSER->lexer_->set_identifier (ly_symbol2scm ("$current-bookpart"), $1);
754         }
755         | bookpart_body paper_block {
756                 $$->paper_ = $2;
757                 $2->unprotect ();
758         }
759         | bookpart_body score_block {
760                 Score *score = $2;
761                 SCM proc = PARSER->lexer_->lookup_identifier ("bookpart-score-handler");
762                 scm_call_2 (proc, $$->self_scm (), score->self_scm ());
763                 score->unprotect ();
764         }
765         | bookpart_body composite_music {
766                 Music *music = unsmob_music ($2);
767                 SCM proc = PARSER->lexer_->lookup_identifier ("bookpart-music-handler");
768                 scm_call_3 (proc, PARSER->self_scm (), $$->self_scm (), music->self_scm ());
769         }
770         | bookpart_body full_markup {
771                 SCM proc = PARSER->lexer_->lookup_identifier ("bookpart-text-handler");
772                 scm_call_2 (proc, $$->self_scm (), scm_list_1 ($2));
773         }
774         | bookpart_body full_markup_list {
775                 SCM proc = PARSER->lexer_->lookup_identifier ("bookpart-text-handler");
776                 scm_call_2 (proc, $$->self_scm (), $2);
777         }
778         | bookpart_body lilypond_header {
779                 $$->header_ = $2;
780         }
781         | bookpart_body error {
782                 $$->paper_ = 0;
783                 $$->scores_ = SCM_EOL;
784         }
785         ;
786
787 score_block:
788         SCORE '{' score_body '}'        {
789                 $$ = $3;
790         }
791         ;
792
793 score_body:
794         music {
795                 SCM m = $1;
796                 SCM scorify = ly_lily_module_constant ("scorify-music");
797                 SCM score = scm_call_2 (scorify, m, PARSER->self_scm ());
798
799                 // pass ownernship to C++ again.
800                 $$ = unsmob_score (score);
801                 $$->protect ();
802                 $$->origin ()->set_spot (@$);
803         }
804         | SCORE_IDENTIFIER {
805                 $$ = unsmob_score ($1);
806                 $$->protect ();
807                 $$->origin ()->set_spot (@$);
808         }
809         | score_body lilypond_header    {
810                 $$->set_header ($2);
811         }
812         | score_body output_def {
813                 if ($2->lookup_variable (ly_symbol2scm ("is-paper")) == SCM_BOOL_T)
814                 {
815                         PARSER->parser_error (@2, _("\\paper cannot be used in \\score, use \\layout instead"));
816
817                 }
818                 else
819                 {
820                         $$->add_output_def ($2);
821                 }
822                 $2->unprotect ();
823         }
824         | score_body error {
825                 $$->error_found_ = true;
826         }
827         ;
828
829
830 /*
831         OUTPUT DEF
832 */
833
834 paper_block:
835         output_def {
836                 $$ = $1;
837                 if ($$->lookup_variable (ly_symbol2scm ("is-paper")) != SCM_BOOL_T)
838                 {
839                         PARSER->parser_error (@1, _ ("need \\paper for paper block"));
840                         $1->unprotect ();
841                         $$ = get_paper (PARSER);
842                 }
843         }
844         ;
845
846
847 output_def:
848         output_def_body '}' {
849                 $$ = $1;
850
851                 PARSER->lexer_->remove_scope ();
852                 PARSER->lexer_->pop_state ();
853         }
854         ;
855
856 output_def_head:
857         PAPER {
858                 $$ = get_paper (PARSER);
859                 $$->input_origin_ = @$;
860                 PARSER->lexer_->add_scope ($$->scope_);
861         }
862         | MIDI    {
863                 Output_def *p = get_midi (PARSER);
864                 $$ = p;
865                 PARSER->lexer_->add_scope (p->scope_);
866         }
867         | LAYOUT        {
868                 Output_def *p = get_layout (PARSER);
869
870                 PARSER->lexer_->add_scope (p->scope_);
871                 $$ = p;
872         }
873         ;
874
875 output_def_head_with_mode_switch:
876         output_def_head {
877                 PARSER->lexer_->push_initial_state ();
878                 $$ = $1;
879         }
880         ;
881
882 output_def_body:
883         output_def_head_with_mode_switch '{' {
884                 $$ = $1;
885                 $$->input_origin_.set_spot (@$);
886         }
887         | output_def_head_with_mode_switch '{' OUTPUT_DEF_IDENTIFIER    {
888                 $1->unprotect ();
889
890                 Output_def *o = unsmob_output_def ($3);
891                 o->input_origin_.set_spot (@$);
892                 $$ = o;
893                 $$->protect ();
894                 PARSER->lexer_->remove_scope ();
895                 PARSER->lexer_->add_scope (o->scope_);
896         }
897         | output_def_body assignment  {
898
899         }
900         | output_def_body context_def_spec_block        {
901                 assign_context_def ($$, $2);
902         }
903         | output_def_body error {
904
905         }
906         ;
907
908 tempo_event:
909         TEMPO steno_duration '=' bare_unsigned  {
910                 $$ = MAKE_SYNTAX ("tempo", @$, SCM_BOOL_F, $2, scm_int2num ($4));
911         }
912         | TEMPO string steno_duration '=' bare_unsigned {
913                 $$ = MAKE_SYNTAX ("tempo", @$, make_simple_markup($2), $3, scm_int2num ($5));
914         }
915         | TEMPO full_markup steno_duration '=' bare_unsigned    {
916                 $$ = MAKE_SYNTAX ("tempo", @$, $2, $3, scm_int2num ($5));
917         }
918         | TEMPO string {
919                 $$ = MAKE_SYNTAX ("tempoText", @$, make_simple_markup($2) );
920         }
921         | TEMPO full_markup {
922                 $$ = MAKE_SYNTAX ("tempoText", @$, $2 );
923         }
924         ;
925
926 /*
927 The representation of a  list is the
928
929   (LIST . LAST-CONS)
930
931  to have efficient append.  */
932
933 music_list:
934         /* empty */ {
935                 $$ = scm_cons (SCM_EOL, SCM_EOL);
936         }
937         | music_list music {
938                 SCM s = $$;
939                 SCM c = scm_cons ($2, SCM_EOL);
940
941                 if (scm_is_pair (scm_cdr (s)))
942                         scm_set_cdr_x (scm_cdr (s), c); /* append */
943                 else
944                         scm_set_car_x (s, c); /* set first cons */
945                 scm_set_cdr_x (s, c);  /* remember last cell */
946         }
947         | music_list embedded_scm {
948
949         }
950         | music_list error {
951                 Music *m = MY_MAKE_MUSIC("Music", @$);
952                 // ugh. code dup
953                 m->set_property ("error-found", SCM_BOOL_T);
954                 SCM s = $$;
955                 SCM c = scm_cons (m->self_scm (), SCM_EOL);
956                 m->unprotect (); /* UGH */
957
958                 if (scm_is_pair (scm_cdr (s)))
959                         scm_set_cdr_x (scm_cdr (s), c); /* append */
960                 else
961                         scm_set_car_x (s, c); /* set first cons */
962                 scm_set_cdr_x (s, c);  /* remember last cell */
963         }
964         ;
965
966 music:
967         simple_music
968         | composite_music
969         ;
970
971 alternative_music:
972         /* empty */ {
973                 $$ = SCM_EOL;
974         }
975         | ALTERNATIVE '{' music_list '}' {
976                 $$ = scm_car ($3);
977         }
978         ;
979
980
981 repeated_music:
982         REPEAT simple_string unsigned_number music alternative_music
983         {
984                 $$ = MAKE_SYNTAX ("repeat", @$, $2, $3, $4, $5);
985         }
986         ;
987
988 sequential_music:
989         SEQUENTIAL '{' music_list '}'           {
990                 $$ = MAKE_SYNTAX ("sequential-music", @$, scm_car ($3));
991         }
992         | '{' music_list '}'            {
993                 $$ = MAKE_SYNTAX ("sequential-music", @$, scm_car ($2));
994         }
995         ;
996
997 simultaneous_music:
998         SIMULTANEOUS '{' music_list '}'{
999                 $$ = MAKE_SYNTAX ("simultaneous-music", @$, scm_car ($3));
1000         }
1001         | DOUBLE_ANGLE_OPEN music_list DOUBLE_ANGLE_CLOSE       {
1002                 $$ = MAKE_SYNTAX ("simultaneous-music", @$, scm_car ($2));
1003         }
1004         ;
1005
1006 simple_music:
1007         event_chord
1008         | MUSIC_IDENTIFIER
1009         | music_property_def
1010         | context_change
1011         ;
1012
1013 context_modification:
1014         WITH { PARSER->lexer_->push_initial_state (); } '{' context_mod_list '}'
1015         {
1016                 PARSER->lexer_->pop_state ();
1017                 $$ = $4;
1018         }
1019         | WITH CONTEXT_MOD_IDENTIFIER
1020         {
1021                 $$ = $2;
1022         }
1023         | CONTEXT_MOD_IDENTIFIER
1024         {
1025                 $$ = $1;
1026         }
1027         ;
1028
1029 optional_context_mod:
1030         /**/ {
1031             $$ = SCM_EOL;
1032         }
1033         | context_modification
1034         {
1035               $$ = $1;
1036         }
1037         ;
1038
1039 context_mod_list:
1040         /**/ {
1041             $$ = Context_mod ().smobbed_copy ();
1042         }
1043         | context_mod_list context_mod  {
1044                  unsmob_context_mod ($1)->add_context_mod ($2);
1045         }
1046         | context_mod_list CONTEXT_MOD_IDENTIFIER {
1047                  Context_mod *md = unsmob_context_mod ($2);
1048                  if (md)
1049                      unsmob_context_mod ($1)->add_context_mods (md->get_mods ());
1050         }
1051         ;
1052
1053 composite_music:
1054         prefix_composite_music { $$ = $1; }
1055         | grouped_music_list { $$ = $1; }
1056         ;
1057
1058 grouped_music_list:
1059         simultaneous_music              { $$ = $1; }
1060         | sequential_music              { $$ = $1; }
1061         ;
1062
1063 function_scm_argument:
1064         embedded_scm
1065         | simple_string
1066         ;
1067
1068 /* An argument list. If a function \foo expects scm scm music, then the lexer expands \foo into the token sequence:
1069  MUSIC_FUNCTION EXPECT_MUSIC EXPECT_SCM EXPECT_SCM EXPECT_NO_MORE_ARGS
1070 and this rule returns the reversed list of arguments. */
1071
1072 function_arglist_music_last:
1073         EXPECT_MUSIC function_arglist music {
1074                 $$ = scm_cons ($3, $2);
1075         }
1076         ;
1077
1078 function_arglist_nonmusic_last:
1079         EXPECT_MARKUP function_arglist full_markup {
1080                 $$ = scm_cons ($3, $2);
1081         }
1082         | EXPECT_MARKUP function_arglist simple_string {
1083                 $$ = scm_cons ($3, $2);
1084         }
1085         | EXPECT_SCM function_arglist function_scm_argument {
1086                 $$ = scm_cons ($3, $2);
1087         }
1088         ;
1089
1090 function_arglist_nonmusic: EXPECT_NO_MORE_ARGS {
1091                 $$ = SCM_EOL;
1092         }
1093         | EXPECT_MARKUP function_arglist_nonmusic full_markup {
1094                 $$ = scm_cons ($3, $2);
1095         }
1096         | EXPECT_MARKUP function_arglist_nonmusic simple_string {
1097                 $$ = scm_cons ($3, $2);
1098         }
1099         | EXPECT_SCM function_arglist_nonmusic function_scm_argument {
1100                 $$ = scm_cons ($3, $2);
1101         }
1102         ;
1103
1104 function_arglist: EXPECT_NO_MORE_ARGS {
1105                 /* This is for 0-ary functions, so they don't need to
1106                    read a lookahead token */
1107                 $$ = SCM_EOL;
1108         }
1109         | function_arglist_music_last
1110         | function_arglist_nonmusic_last
1111         ;
1112
1113 generic_prefix_music_scm:
1114         MUSIC_FUNCTION function_arglist {
1115                 $$ = ly_append2 (scm_list_2 ($1, make_input (@$)), scm_reverse_x ($2, SCM_EOL));
1116         }
1117         ;
1118
1119
1120 optional_id:
1121         /**/ { $$ = SCM_EOL; }
1122         | '=' simple_string {
1123                 $$ = $2;
1124         }
1125         ;
1126
1127
1128 prefix_composite_music:
1129         generic_prefix_music_scm {
1130                 $$ = run_music_function (PARSER, $1);
1131         }
1132         | CONTEXT simple_string optional_id optional_context_mod music {
1133                 Context_mod *ctxmod = unsmob_context_mod ($4);
1134                 SCM mods = SCM_EOL;
1135                 if (ctxmod)
1136                         mods = ctxmod->get_mods ();
1137                 $$ = MAKE_SYNTAX ("context-specification", @$, $2, $3, $5, mods, SCM_BOOL_F);
1138         }
1139         | NEWCONTEXT simple_string optional_id optional_context_mod music {
1140                 Context_mod *ctxmod = unsmob_context_mod ($4);
1141                 SCM mods = SCM_EOL;
1142                 if (ctxmod)
1143                         mods = ctxmod->get_mods ();
1144                 $$ = MAKE_SYNTAX ("context-specification", @$, $2, $3, $5, mods, SCM_BOOL_T);
1145         }
1146
1147         | TIMES fraction music {
1148                 $$ = MAKE_SYNTAX ("time-scaled-music", @$, $2, $3);
1149         }
1150         | repeated_music                { $$ = $1; }
1151         | TRANSPOSE pitch_also_in_chords pitch_also_in_chords music {
1152                 Pitch from = *unsmob_pitch ($2);
1153                 Pitch to = *unsmob_pitch ($3);
1154                 SCM pitch = pitch_interval (from, to).smobbed_copy ();
1155                 $$ = MAKE_SYNTAX ("transpose-music", @$, pitch, $4);
1156         }
1157         | mode_changing_head grouped_music_list {
1158                 if ($1 == ly_symbol2scm ("chords"))
1159                 {
1160                   $$ = MAKE_SYNTAX ("unrelativable-music", @$, $2);
1161                 }
1162                 else
1163                 {
1164                   $$ = $2;
1165                 }
1166                 PARSER->lexer_->pop_state ();
1167         }
1168         | mode_changing_head_with_context optional_context_mod grouped_music_list {
1169                 Context_mod *ctxmod = unsmob_context_mod ($2);
1170                 SCM mods = SCM_EOL;
1171                 if (ctxmod)
1172                         mods = ctxmod->get_mods ();
1173                 $$ = MAKE_SYNTAX ("context-specification", @$, $1, SCM_EOL, $3, mods, SCM_BOOL_T);
1174                 if ($1 == ly_symbol2scm ("ChordNames"))
1175                 {
1176                   $$ = MAKE_SYNTAX ("unrelativable-music", @$, $$);
1177                 }
1178                 PARSER->lexer_->pop_state ();
1179         }
1180         | relative_music        { $$ = $1; }
1181         | re_rhythmed_music     { $$ = $1; }
1182         ;
1183
1184 mode_changing_head:
1185         NOTEMODE {
1186                 SCM nn = PARSER->lexer_->lookup_identifier ("pitchnames");
1187                 PARSER->lexer_->push_note_state (alist_to_hashq (nn));
1188
1189                 $$ = ly_symbol2scm ("notes");
1190         }
1191         | DRUMMODE
1192                 {
1193                 SCM nn = PARSER->lexer_->lookup_identifier ("drumPitchNames");
1194                 PARSER->lexer_->push_note_state (alist_to_hashq (nn));
1195
1196                 $$ = ly_symbol2scm ("drums");
1197         }
1198         | FIGUREMODE {
1199                 PARSER->lexer_->push_figuredbass_state ();
1200
1201                 $$ = ly_symbol2scm ("figures");
1202         }
1203         | CHORDMODE {
1204                 SCM nn = PARSER->lexer_->lookup_identifier ("chordmodifiers");
1205                 PARSER->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
1206                 nn = PARSER->lexer_->lookup_identifier ("pitchnames");
1207                 PARSER->lexer_->push_chord_state (alist_to_hashq (nn));
1208                 $$ = ly_symbol2scm ("chords");
1209
1210         }
1211         | LYRICMODE
1212                 { PARSER->lexer_->push_lyric_state ();
1213                 $$ = ly_symbol2scm ("lyrics");
1214         }
1215         ;
1216
1217 mode_changing_head_with_context:
1218         DRUMS {
1219                 SCM nn = PARSER->lexer_->lookup_identifier ("drumPitchNames");
1220                 PARSER->lexer_->push_note_state (alist_to_hashq (nn));
1221
1222                 $$ = ly_symbol2scm ("DrumStaff");
1223         }
1224         | FIGURES {
1225                 PARSER->lexer_->push_figuredbass_state ();
1226
1227                 $$ = ly_symbol2scm ("FiguredBass");
1228         }
1229         | CHORDS {
1230                 SCM nn = PARSER->lexer_->lookup_identifier ("chordmodifiers");
1231                 PARSER->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
1232                 nn = PARSER->lexer_->lookup_identifier ("pitchnames");
1233                 PARSER->lexer_->push_chord_state (alist_to_hashq (nn));
1234                 $$ = ly_symbol2scm ("ChordNames");
1235         }
1236         | LYRICS
1237                 { PARSER->lexer_->push_lyric_state ();
1238                 $$ = ly_symbol2scm ("Lyrics");
1239         }
1240         ;
1241
1242
1243 relative_music:
1244         RELATIVE absolute_pitch music {
1245                 Pitch start = *unsmob_pitch ($2);
1246                 $$ = make_music_relative (start, $3, @$);
1247         }
1248         | RELATIVE composite_music {
1249                 Pitch middle_c (0, 0, 0);
1250                 $$ = make_music_relative (middle_c, $2, @$);
1251         }
1252         ;
1253
1254 new_lyrics:
1255         ADDLYRICS { PARSER->lexer_->push_lyric_state (); }
1256         /*cont */
1257         grouped_music_list {
1258         /* Can also use music at the expensive of two S/Rs similar to
1259            \repeat \alternative */
1260                 PARSER->lexer_->pop_state ();
1261
1262                 $$ = scm_cons ($3, SCM_EOL);
1263         }
1264         | new_lyrics ADDLYRICS {
1265                 PARSER->lexer_->push_lyric_state ();
1266         } grouped_music_list {
1267                 PARSER->lexer_->pop_state ();
1268                 $$ = scm_cons ($4, $1);
1269         }
1270         ;
1271
1272 re_rhythmed_music:
1273         grouped_music_list new_lyrics {
1274                 $$ = MAKE_SYNTAX ("add-lyrics", @$, $1, scm_reverse_x ($2, SCM_EOL));
1275         }
1276         | LYRICSTO simple_string {
1277                 PARSER->lexer_->push_lyric_state ();
1278         } music {
1279                 PARSER->lexer_->pop_state ();
1280                 $$ = MAKE_SYNTAX ("lyric-combine", @$, $2, $4);
1281         }
1282         ;
1283
1284 context_change:
1285         CHANGE STRING '=' STRING  {
1286                 $$ = MAKE_SYNTAX ("context-change", @$, scm_string_to_symbol ($2), $4);
1287         }
1288         ;
1289
1290
1291 property_path_revved:
1292         embedded_scm {
1293                 $$ = scm_cons ($1, SCM_EOL);
1294         }
1295         | property_path_revved embedded_scm {
1296                 $$ = scm_cons ($2, $1);
1297         }
1298         ;
1299
1300 property_path:
1301         property_path_revved  {
1302                 $$ = scm_reverse_x ($1, SCM_EOL);
1303         }
1304         ;
1305
1306 property_operation:
1307         STRING '=' scalar {
1308                 $$ = scm_list_3 (ly_symbol2scm ("assign"),
1309                         scm_string_to_symbol ($1), $3);
1310         }
1311         | UNSET simple_string {
1312                 $$ = scm_list_2 (ly_symbol2scm ("unset"),
1313                         scm_string_to_symbol ($2));
1314         }
1315         | OVERRIDE simple_string property_path '=' scalar {
1316                 $$ = scm_append (scm_list_2 (scm_list_3 (ly_symbol2scm ("push"),
1317                                                         scm_string_to_symbol ($2), $5),
1318                                              $3));
1319         }
1320         | REVERT simple_string embedded_scm {
1321                 $$ = scm_list_3 (ly_symbol2scm ("pop"),
1322                         scm_string_to_symbol ($2), $3);
1323         }
1324         ;
1325
1326 context_def_mod:
1327         CONSISTS { $$ = ly_symbol2scm ("consists"); }
1328         | REMOVE { $$ = ly_symbol2scm ("remove"); }
1329
1330         | ACCEPTS { $$ = ly_symbol2scm ("accepts"); }
1331         | DEFAULTCHILD { $$ = ly_symbol2scm ("default-child"); }
1332         | DENIES { $$ = ly_symbol2scm ("denies"); }
1333
1334         | ALIAS { $$ = ly_symbol2scm ("alias"); }
1335         | TYPE { $$ = ly_symbol2scm ("translator-type"); }
1336         | DESCRIPTION { $$ = ly_symbol2scm ("description"); }
1337         | NAME { $$ = ly_symbol2scm ("context-name"); }
1338         ;
1339
1340 context_mod:
1341         property_operation { $$ = $1; }
1342         | context_def_mod STRING {
1343                 $$ = scm_list_2 ($1, $2);
1344         }
1345         | context_def_mod embedded_scm {
1346            if (ly_symbol2scm ("consists") != $1)
1347            {
1348              $$ = SCM_EOL;
1349              PARSER->parser_error (@1, _ ("only \\consists takes non-string argument."));
1350            }
1351            else
1352            {
1353              $$ = scm_list_2 ($1, $2);
1354            }
1355         }
1356         ;
1357
1358 context_prop_spec:
1359         simple_string {
1360                 if (!is_regular_identifier ($1))
1361                 {
1362                         @$.error (_("Grob name should be alphanumeric"));
1363                 }
1364
1365                 $$ = scm_list_2 (ly_symbol2scm ("Bottom"),
1366                         scm_string_to_symbol ($1));
1367         }
1368         | simple_string '.' simple_string {
1369                 $$ = scm_list_2 (scm_string_to_symbol ($1),
1370                         scm_string_to_symbol ($3));
1371         }
1372         ;
1373
1374 simple_music_property_def:
1375         OVERRIDE context_prop_spec property_path '=' scalar {
1376                 $$ = scm_append (scm_list_2 (scm_list_n (scm_car ($2),
1377                                 ly_symbol2scm ("OverrideProperty"),
1378                                 scm_cadr ($2),
1379                                 $5, SCM_UNDEFINED),
1380                                 $3));
1381         }
1382         | REVERT context_prop_spec embedded_scm {
1383                 $$ = scm_list_4 (scm_car ($2),
1384                         ly_symbol2scm ("RevertProperty"),
1385                         scm_cadr ($2),
1386                         $3);
1387         }
1388         | SET context_prop_spec '=' scalar {
1389                 $$ = scm_list_4 (scm_car ($2),
1390                         ly_symbol2scm ("PropertySet"),
1391                         scm_cadr ($2),
1392                         $4);
1393         }
1394         | UNSET context_prop_spec {
1395                 $$ = scm_list_3 (scm_car ($2),
1396                         ly_symbol2scm ("PropertyUnset"),
1397                         scm_cadr ($2));
1398         }
1399         ;
1400
1401 music_property_def:
1402         simple_music_property_def {
1403                 $$ = LOWLEVEL_MAKE_SYNTAX (ly_lily_module_constant ("property-operation"), scm_cons (PARSER->self_scm (), scm_cons2 (make_input (@$), SCM_BOOL_F, $1)));
1404         }
1405         | ONCE simple_music_property_def {
1406                 $$ = LOWLEVEL_MAKE_SYNTAX (ly_lily_module_constant ("property-operation"), scm_cons (PARSER->self_scm (), scm_cons2 (make_input (@$), SCM_BOOL_T, $2)));
1407         }
1408         ;
1409
1410 string:
1411         STRING {
1412                 $$ = $1;
1413         }
1414         | STRING_IDENTIFIER {
1415                 $$ = $1;
1416         }
1417         | string '+' string {
1418                 $$ = scm_string_append (scm_list_2 ($1, $3));
1419         }
1420         ;
1421
1422 simple_string: STRING {
1423                 $$ = $1;
1424         }
1425         | LYRICS_STRING {
1426                 $$ = $1;
1427         }
1428         | STRING_IDENTIFIER {
1429                 $$ = $1;
1430         }
1431         ;
1432
1433 scalar: string {
1434                 $$ = $1;
1435         }
1436         | LYRICS_STRING {
1437                 $$ = $1;
1438         }
1439         | bare_number {
1440                 $$ = $1;
1441         }
1442         | embedded_scm {
1443                 $$ = $1;
1444         }
1445         | full_markup {
1446                 $$ = $1;
1447         }
1448         | DIGIT {
1449                 $$ = scm_from_int ($1);
1450         }
1451         ;
1452
1453 event_chord:
1454         /* TODO: Create a special case that avoids the creation of
1455            EventChords around simple_elements that have no post_events?
1456          */
1457         simple_chord_elements post_events       {
1458                 SCM elts = ly_append2 ($1, scm_reverse_x ($2, SCM_EOL));
1459
1460                 Input i;
1461                 /* why is this giving wrong start location? -ns
1462                  * i = @$; */
1463                 i.set_location (@1, @2);
1464                 $$ = MAKE_SYNTAX ("event-chord", i, elts);
1465         }
1466         | CHORD_REPETITION optional_notemode_duration post_events {
1467                 Input i;
1468                 i.set_location (@1, @3);
1469                 $$ = MAKE_SYNTAX ("repetition-chord", i,
1470                                   PARSER->lexer_->chord_repetition_.last_chord_,
1471                                   PARSER->lexer_->chord_repetition_.repetition_function_,
1472                                   $2, scm_reverse_x ($3, SCM_EOL));
1473         }
1474         | MULTI_MEASURE_REST optional_notemode_duration post_events {
1475                 Input i;
1476                 i.set_location (@1, @3);
1477                 $$ = MAKE_SYNTAX ("multi-measure-rest", i, $2,
1478                                   scm_reverse_x ($3, SCM_EOL));
1479         }
1480         | command_element
1481         /* note chord elements are memorized into
1482            PARSER->lexer_->chord_repetition_ so that the chord repetition
1483            mechanism copy them when a chord repetition symbol is found
1484         */
1485         | note_chord_element    {
1486                 PARSER->lexer_->chord_repetition_.last_chord_ = $$;
1487         }
1488         ;
1489
1490
1491 note_chord_element:
1492         chord_body optional_notemode_duration post_events
1493         {
1494                 Music *m = unsmob_music ($1);
1495                 SCM dur = unsmob_duration ($2)->smobbed_copy ();
1496                 SCM es = m->get_property ("elements");
1497                 SCM postevs = scm_reverse_x ($3, SCM_EOL);
1498
1499                 for (SCM s = es; scm_is_pair (s); s = scm_cdr (s))
1500                   unsmob_music (scm_car (s))->set_property ("duration", dur);
1501                 es = ly_append2 (es, postevs);
1502
1503                 m-> set_property ("elements", es);
1504                 m->set_spot (@$);
1505                 $$ = m->self_scm ();
1506         }
1507         ;
1508
1509 chord_body:
1510         ANGLE_OPEN chord_body_elements ANGLE_CLOSE
1511         {
1512                 $$ = MAKE_SYNTAX ("event-chord", @$, scm_reverse_x ($2, SCM_EOL));
1513         }
1514         ;
1515
1516 chord_body_elements:
1517         /* empty */             { $$ = SCM_EOL; }
1518         | chord_body_elements chord_body_element {
1519                 $$ = scm_cons ($2, $1);
1520         }
1521         ;
1522
1523 chord_body_element:
1524         pitch exclamations questions octave_check post_events
1525         {
1526                 int q = $3;
1527                 int ex = $2;
1528                 SCM check = $4;
1529                 SCM post = $5;
1530
1531                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
1532                 n->set_property ("pitch", $1);
1533                 if (q % 2)
1534                         n->set_property ("cautionary", SCM_BOOL_T);
1535                 if (ex % 2 || q % 2)
1536                         n->set_property ("force-accidental", SCM_BOOL_T);
1537
1538                 if (scm_is_pair (post)) {
1539                         SCM arts = scm_reverse_x (post, SCM_EOL);
1540                         n->set_property ("articulations", arts);
1541                 }
1542                 if (scm_is_number (check))
1543                 {
1544                         int q = scm_to_int (check);
1545                         n->set_property ("absolute-octave", scm_from_int (q-1));
1546                 }
1547
1548                 $$ = n->unprotect ();
1549         }
1550         | DRUM_PITCH post_events {
1551                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
1552                 n->set_property ("duration", $2);
1553                 n->set_property ("drum-type", $1);
1554
1555                 if (scm_is_pair ($2)) {
1556                         SCM arts = scm_reverse_x ($2, SCM_EOL);
1557                         n->set_property ("articulations", arts);
1558                 }
1559                 $$ = n->unprotect ();
1560         }
1561         | music_function_chord_body {
1562                 $$ = run_music_function (PARSER, $1);
1563         }
1564         ;
1565
1566 music_function_identifier_musicless_prefix: MUSIC_FUNCTION {
1567                 SCM sig = scm_object_property (yylval.scm, ly_symbol2scm ("music-function-signature"));
1568                 if (scm_is_pair (sig) && to_boolean (scm_memq (ly_music_p_proc, scm_cdr (scm_reverse (sig)))))
1569                 {
1570                         PARSER->parser_error (@$, "Music function applied to event may not have a Music argument, except as the last argument.");
1571                 }
1572         }
1573         ;
1574
1575 music_function_chord_body:
1576         /* We could allow chord functions to have multiple music arguments,
1577            but it's more consistent with music_function_event if we
1578            prohibit it here too */
1579         music_function_identifier_musicless_prefix EXPECT_MUSIC function_arglist_nonmusic chord_body_element {
1580                 $$ = ly_append2 (scm_list_2 ($1, make_input (@$)), scm_reverse_x ($3, scm_list_1 ($4)));
1581         }
1582         | music_function_identifier_musicless_prefix function_arglist_nonmusic {
1583                 $$ = ly_append2 (scm_list_2 ($1, make_input (@$)), scm_reverse_x ($2, SCM_EOL));
1584         }
1585         ;
1586
1587 music_function_event:
1588         /* Post-events can only have the last argument as music, without this
1589            restriction we get a shift/reduce conflict from e.g.
1590            c8-\partcombine c8 -. */
1591         music_function_identifier_musicless_prefix EXPECT_MUSIC function_arglist_nonmusic post_event {
1592                 $$ = ly_append2 (scm_list_2 ($1, make_input (@$)), scm_reverse_x ($3, scm_list_1 ($4)));
1593         }
1594         | music_function_identifier_musicless_prefix function_arglist_nonmusic {
1595                 $$ = ly_append2 (scm_list_2 ($1, make_input (@$)), scm_reverse_x ($2, SCM_EOL));
1596         }
1597         ;
1598
1599 command_element:
1600         command_event {
1601                 $$ = $1;
1602         }
1603         | SKIP duration_length {
1604                 $$ = MAKE_SYNTAX ("skip-music", @$, $2);
1605         }
1606         | E_BRACKET_OPEN {
1607                 Music *m = MY_MAKE_MUSIC ("LigatureEvent", @$);
1608                 m->set_property ("span-direction", scm_from_int (START));
1609                 $$ = m->unprotect();
1610         }
1611         | E_BRACKET_CLOSE {
1612                 Music *m = MY_MAKE_MUSIC ("LigatureEvent", @$);
1613                 m->set_property ("span-direction", scm_from_int (STOP));
1614                 $$ = m->unprotect ();
1615         }
1616         | E_BACKSLASH {
1617                 $$ = MAKE_SYNTAX ("voice-separator", @$, SCM_UNDEFINED);
1618         }
1619         | '|'      {
1620                 SCM pipe = PARSER->lexer_->lookup_identifier ("pipeSymbol");
1621
1622                 Music *m = unsmob_music (pipe);
1623                 if (m)
1624                 {
1625                         m = m->clone ();
1626                         m->set_spot (@$);
1627                         $$ = m->unprotect ();
1628                 }
1629                 else
1630                         $$ = MAKE_SYNTAX ("bar-check", @$, SCM_UNDEFINED);
1631
1632         }
1633         | PARTIAL duration_length       {
1634                 Moment m = - unsmob_duration ($2)->get_length ();
1635                 $$ = MAKE_SYNTAX ("property-operation", @$, SCM_BOOL_F, ly_symbol2scm ("Timing"), ly_symbol2scm ("PropertySet"), ly_symbol2scm ("measurePosition"), m.smobbed_copy ());
1636                 $$ = MAKE_SYNTAX ("context-specification", @$, ly_symbol2scm ("Score"), SCM_BOOL_F, $$, SCM_EOL, SCM_BOOL_F);
1637         }
1638
1639         | TIME_T fraction  {
1640                 SCM proc = ly_lily_module_constant ("make-time-signature-set");
1641
1642                 $$ = scm_apply_2   (proc, scm_car ($2), scm_cdr ($2), SCM_EOL);
1643         }
1644         | MARK scalar {
1645                 $$ = MAKE_SYNTAX ("make-mark-set", @$, $2);
1646         }
1647         ;
1648
1649 command_event:
1650         E_TILDE {
1651                 $$ = MY_MAKE_MUSIC ("PesOrFlexaEvent", @$)->unprotect ();
1652         }
1653         | MARK DEFAULT  {
1654                 Music *m = MY_MAKE_MUSIC ("MarkEvent", @$);
1655                 $$ = m->unprotect ();
1656         }
1657         | tempo_event {
1658                 $$ = $1;
1659         }
1660         | KEY DEFAULT {
1661                 Music *key = MY_MAKE_MUSIC ("KeyChangeEvent", @$);
1662                 $$ = key->unprotect ();
1663         }
1664         | KEY NOTENAME_PITCH SCM_IDENTIFIER     {
1665
1666                 Music *key = MY_MAKE_MUSIC ("KeyChangeEvent", @$);
1667                 if (scm_ilength ($3) > 0)
1668                 {
1669                         key->set_property ("pitch-alist", $3);
1670                         key->set_property ("tonic", Pitch (0, 0, 0).smobbed_copy ());
1671                         key->transpose (* unsmob_pitch ($2));
1672                 } else {
1673                         PARSER->parser_error (@3, _ ("second argument must be pitch list"));
1674                 }
1675
1676                 $$ = key->unprotect ();
1677         }
1678         ;
1679
1680
1681 post_events:
1682         /* empty */ {
1683                 $$ = SCM_EOL;
1684         }
1685         | post_events post_event {
1686                 unsmob_music ($2)->set_spot (@2);
1687                 $$ = scm_cons ($2, $$);
1688         }
1689         ;
1690
1691 post_event:
1692         direction_less_event {
1693                 $$ = $1;
1694         }
1695         | '-' music_function_event {
1696                 $$ = run_music_function (PARSER, $2);
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 /*
2280         UTILITIES
2281
2282 TODO: should deprecate in favor of Scheme?
2283
2284  */
2285 number_expression:
2286         number_expression '+' number_term {
2287                 $$ = scm_sum ($1, $3);
2288         }
2289         | number_expression '-' number_term {
2290                 $$ = scm_difference ($1, $3);
2291         }
2292         | number_term
2293         ;
2294
2295 number_term:
2296         number_factor {
2297                 $$ = $1;
2298         }
2299         | number_factor '*' number_factor {
2300                 $$ = scm_product ($1, $3);
2301         }
2302         | number_factor '/' number_factor {
2303                 $$ = scm_divide ($1, $3);
2304         }
2305         ;
2306
2307 number_factor:
2308         '-'  number_factor { /* %prec UNARY_MINUS */
2309                 $$ = scm_difference ($2, SCM_UNDEFINED);
2310         }
2311         | bare_number
2312         ;
2313
2314
2315 bare_number:
2316         UNSIGNED        {
2317                 $$ = scm_from_int ($1);
2318         }
2319         | REAL          {
2320                 $$ = $1;
2321         }
2322         | NUMBER_IDENTIFIER             {
2323                 $$ = $1;
2324         }
2325         | REAL NUMBER_IDENTIFIER        {
2326                 $$ = scm_from_double (scm_to_double ($1) *scm_to_double ($2));
2327         }
2328         | UNSIGNED NUMBER_IDENTIFIER    {
2329                 $$ = scm_from_double ($1 *scm_to_double ($2));
2330         }
2331         ;
2332
2333
2334 bare_unsigned:
2335         UNSIGNED {
2336                         $$ = $1;
2337         }
2338         | DIGIT {
2339                 $$ = $1;
2340         }
2341         ;
2342
2343 unsigned_number:
2344         bare_unsigned  { $$ = scm_from_int ($1); }
2345         | NUMBER_IDENTIFIER {
2346                 $$ = $1;
2347         }
2348         ;
2349
2350
2351 exclamations:
2352                 { $$ = 0; }
2353         | exclamations '!'      { $$ ++; }
2354         ;
2355
2356 questions:
2357                 { $$ = 0; }
2358         | questions '?' { $$ ++; }
2359         ;
2360
2361 /*
2362 This should be done more dynamically if possible.
2363 */
2364
2365 lyric_markup:
2366         LYRIC_MARKUP_IDENTIFIER {
2367                 $$ = $1;
2368         }
2369         | LYRIC_MARKUP
2370                 { PARSER->lexer_->push_markup_state (); }
2371         markup_top {
2372                 $$ = $3;
2373                 PARSER->lexer_->pop_state ();
2374         }
2375         ;
2376
2377 full_markup_list:
2378         MARKUPLINES
2379                 { PARSER->lexer_->push_markup_state (); }
2380         markup_list {
2381                 $$ = $3;
2382                 PARSER->lexer_->pop_state ();
2383         }
2384         ;
2385
2386 full_markup:
2387         MARKUP_IDENTIFIER {
2388                 $$ = $1;
2389         }
2390         | MARKUP
2391                 { PARSER->lexer_->push_markup_state (); }
2392         markup_top {
2393                 $$ = $3;
2394                 PARSER->lexer_->pop_state ();
2395         }
2396         ;
2397
2398 markup_top:
2399         markup_list {
2400                 $$ = scm_list_2 (ly_lily_module_constant ("line-markup"),  $1);
2401         }
2402         | markup_head_1_list simple_markup      {
2403                 $$ = scm_car (scm_call_2 (ly_lily_module_constant ("map-markup-command-list"), $1, scm_list_1 ($2)));
2404         }
2405         | simple_markup {
2406                 $$ = $1;
2407         }
2408         ;
2409
2410 markup_list:
2411         markup_composed_list {
2412                 $$ = $1;
2413         }
2414         | markup_braced_list {
2415                 $$ = $1;
2416         }
2417         | markup_command_list {
2418                 $$ = scm_list_1 ($1);
2419         }
2420         ;
2421
2422 markup_composed_list:
2423         markup_head_1_list markup_braced_list {
2424                 $$ = scm_call_2 (ly_lily_module_constant ("map-markup-command-list"), $1, $2);
2425
2426         }
2427         ;
2428
2429 markup_braced_list:
2430         '{' markup_braced_list_body '}' {
2431                 $$ = scm_reverse_x ($2, SCM_EOL);
2432         }
2433         ;
2434
2435 markup_braced_list_body:
2436         /* empty */     {  $$ = SCM_EOL; }
2437         | markup_braced_list_body markup {
2438                 $$ = scm_cons ($2, $1);
2439         }
2440         | markup_braced_list_body markup_list {
2441                 $$ = scm_reverse_x ($2, $1);
2442         }
2443         ;
2444
2445 markup_command_list:
2446         MARKUP_LIST_FUNCTION markup_command_list_arguments {
2447           $$ = scm_cons ($1, scm_reverse_x($2, SCM_EOL));
2448         }
2449         ;
2450
2451 markup_command_basic_arguments:
2452         EXPECT_MARKUP_LIST markup_command_list_arguments markup_list {
2453           $$ = scm_cons ($3, $2);
2454         }
2455         | EXPECT_SCM markup_command_list_arguments embedded_scm {
2456           $$ = scm_cons ($3, $2);
2457         }
2458         | EXPECT_NO_MORE_ARGS {
2459           $$ = SCM_EOL;
2460         }
2461         ;
2462
2463 markup_command_list_arguments:
2464         markup_command_basic_arguments { $$ = $1; }
2465         | EXPECT_MARKUP markup_command_list_arguments markup {
2466           $$ = scm_cons ($3, $2);
2467         }
2468         ;
2469
2470 markup_head_1_item:
2471         MARKUP_FUNCTION EXPECT_MARKUP markup_command_list_arguments {
2472           $$ = scm_cons ($1, scm_reverse_x ($3, SCM_EOL));
2473         }
2474         ;
2475
2476 markup_head_1_list:
2477         markup_head_1_item      {
2478                 $$ = scm_list_1 ($1);
2479         }
2480         | markup_head_1_list markup_head_1_item {
2481                 $$ = scm_cons ($2, $1);
2482         }
2483         ;
2484
2485 simple_markup:
2486         STRING {
2487                 $$ = make_simple_markup ($1);
2488         }
2489         | MARKUP_IDENTIFIER {
2490                 $$ = $1;
2491         }
2492         | LYRIC_MARKUP_IDENTIFIER {
2493                 $$ = $1;
2494         }
2495         | STRING_IDENTIFIER {
2496                 $$ = $1;
2497         }
2498         | SCORE {
2499                 SCM nn = PARSER->lexer_->lookup_identifier ("pitchnames");
2500                 PARSER->lexer_->push_note_state (alist_to_hashq (nn));
2501         } '{' score_body '}' {
2502                 Score * sc = $4;
2503                 $$ = scm_list_2 (ly_lily_module_constant ("score-markup"), sc->self_scm ());
2504                 sc->unprotect ();
2505                 PARSER->lexer_->pop_state ();
2506         }
2507         | MARKUP_FUNCTION markup_command_basic_arguments {
2508                 $$ = scm_cons ($1, scm_reverse_x ($2, SCM_EOL));
2509         }
2510         ;
2511
2512 markup:
2513         markup_head_1_list simple_markup        {
2514                 SCM mapper = ly_lily_module_constant ("map-markup-command-list");
2515                 $$ = scm_car (scm_call_2 (mapper, $1, scm_list_1 ($2)));
2516         }
2517         | simple_markup {
2518                 $$ = $1;
2519         }
2520         ;
2521
2522 %%
2523
2524 void
2525 Lily_parser::set_yydebug (bool x)
2526 {
2527         yydebug = x;
2528 }
2529
2530 void
2531 Lily_parser::do_yyparse ()
2532 {
2533         yyparse ((void*)this);
2534 }
2535
2536
2537
2538
2539
2540 /*
2541
2542 It is a little strange to have this function in this file, but
2543 otherwise, we have to import music classes into the lexer.
2544
2545 */
2546 int
2547 Lily_lexer::try_special_identifiers (SCM *destination, SCM sid)
2548 {
2549         if (scm_is_string (sid)) {
2550                 *destination = sid;
2551                 return STRING_IDENTIFIER;
2552         } else if (unsmob_book (sid)) {
2553                 Book *book =  unsmob_book (sid)->clone ();
2554                 *destination = book->self_scm ();
2555                 book->unprotect ();
2556
2557                 return BOOK_IDENTIFIER;
2558         } else if (scm_is_number (sid)) {
2559                 *destination = sid;
2560                 return NUMBER_IDENTIFIER;
2561         } else if (unsmob_context_def (sid)) {
2562                 Context_def *def= unsmob_context_def (sid)->clone ();
2563
2564                 *destination = def->self_scm ();
2565                 def->unprotect ();
2566
2567                 return CONTEXT_DEF_IDENTIFIER;
2568         } else if (unsmob_context_mod (sid)) {
2569                 *destination = unsmob_context_mod (sid)->smobbed_copy ();
2570
2571                 return CONTEXT_MOD_IDENTIFIER;
2572         } else if (unsmob_score (sid)) {
2573                 Score *score = new Score (*unsmob_score (sid));
2574                 *destination = score->self_scm ();
2575
2576                 score->unprotect ();
2577                 return SCORE_IDENTIFIER;
2578         } else if (Music *mus = unsmob_music (sid)) {
2579                 mus = mus->clone ();
2580                 *destination = mus->self_scm ();
2581                 unsmob_music (*destination)->
2582                         set_property ("origin", make_input (last_input_));
2583
2584                 bool is_event = scm_memq (ly_symbol2scm ("event"), mus->get_property ("types"))
2585                         != SCM_BOOL_F;
2586
2587                 mus->unprotect ();
2588                 return is_event ? EVENT_IDENTIFIER : MUSIC_IDENTIFIER;
2589         } else if (unsmob_duration (sid)) {
2590                 *destination = unsmob_duration (sid)->smobbed_copy ();
2591                 return DURATION_IDENTIFIER;
2592         } else if (unsmob_output_def (sid)) {
2593                 Output_def *p = unsmob_output_def (sid);
2594                 p = p->clone ();
2595
2596                 *destination = p->self_scm ();
2597                 p->unprotect ();
2598                 return OUTPUT_DEF_IDENTIFIER;
2599         } else if (Text_interface::is_markup (sid)) {
2600                 *destination = sid;
2601                 if (is_lyric_state ())
2602                         return LYRIC_MARKUP_IDENTIFIER;
2603                 return MARKUP_IDENTIFIER;
2604         }
2605
2606         return -1;
2607 }
2608
2609 SCM
2610 get_next_unique_context_id ()
2611 {
2612         return scm_from_locale_string ("$uniqueContextId");
2613 }
2614
2615
2616 SCM
2617 get_next_unique_lyrics_context_id ()
2618 {
2619         static int new_context_count;
2620         char s[128];
2621         snprintf (s, sizeof (s)-1, "uniqueContext%d", new_context_count++);
2622         return scm_from_locale_string (s);
2623 }
2624
2625
2626 SCM
2627 run_music_function (Lily_parser *parser, SCM expr)
2628 {
2629         SCM func = scm_car (expr);
2630         Input *loc = unsmob_input (scm_cadr (expr));
2631         SCM args = scm_cddr (expr);
2632         SCM sig = scm_object_property (func, ly_symbol2scm ("music-function-signature"));
2633
2634         SCM type_check_proc = ly_lily_module_constant ("type-check-list");
2635
2636         if (!to_boolean (scm_call_3  (type_check_proc, scm_cadr (expr), sig, args)))
2637         {
2638                 parser->error_level_ = 1;
2639                 return LOWLEVEL_MAKE_SYNTAX (ly_lily_module_constant ("void-music"), scm_list_2 (parser->self_scm (), make_input (*loc)));
2640         }
2641
2642         SCM syntax_args = scm_list_4 (parser->self_scm (), make_input (*loc), func, args);
2643         return LOWLEVEL_MAKE_SYNTAX (ly_lily_module_constant ("music-function"), syntax_args);
2644 }
2645
2646 bool
2647 is_regular_identifier (SCM id)
2648 {
2649   string str = ly_scm2string (id);
2650   char const *s = str.c_str ();
2651
2652   bool v = true;
2653 #if 0
2654   isalpha (*s);
2655   s++;
2656 #endif
2657   while (*s && v)
2658    {
2659         v = v && isalnum (*s);
2660         s++;
2661    }
2662   return v;
2663 }
2664
2665 Music *
2666 make_music_with_input (SCM name, Input where)
2667 {
2668        Music *m = make_music_by_name (name);
2669        m->set_spot (where);
2670        return m;
2671 }
2672
2673 SCM
2674 get_first_context_id (SCM type, Music *m)
2675 {
2676         SCM id = m->get_property ("context-id");
2677         if (SCM_BOOL_T == scm_equal_p (m->get_property ("context-type"), type)
2678             && scm_is_string (m->get_property ("context-id"))
2679             && scm_c_string_length (id) > 0)
2680         {
2681                 return id;
2682         }
2683         return SCM_EOL;
2684 }
2685
2686 SCM
2687 make_simple_markup (SCM a)
2688 {
2689         return a;
2690 }
2691
2692 bool
2693 is_duration (int t)
2694 {
2695   return t && t == 1 << intlog2 (t);
2696 }
2697
2698 void
2699 set_music_properties (Music *p, SCM a)
2700 {
2701   for (SCM k = a; scm_is_pair (k); k = scm_cdr (k))
2702         p->set_property (scm_caar (k), scm_cdar (k));
2703 }
2704
2705
2706 SCM
2707 make_chord_step (int step, Rational alter)
2708 {
2709         if (step == 7)
2710                 alter += FLAT_ALTERATION;
2711
2712         while (step < 0)
2713                 step += 7;
2714         Pitch m ((step -1) / 7, (step - 1) % 7, alter);
2715         return m.smobbed_copy ();
2716 }
2717
2718
2719 SCM
2720 make_chord_elements (SCM pitch, SCM dur, SCM modification_list)
2721 {
2722         SCM chord_ctor = ly_lily_module_constant ("construct-chord-elements");
2723         return scm_call_3 (chord_ctor, pitch, dur, modification_list);
2724 }
2725
2726
2727 /* Todo: actually also use apply iso. call too ...  */
2728 bool
2729 ly_input_procedure_p (SCM x)
2730 {
2731         return ly_is_procedure (x)
2732                 || (scm_is_pair (x) && ly_is_procedure (scm_car (x)));
2733 }
2734
2735 SCM
2736 make_music_relative (Pitch start, SCM music, Input loc)
2737 {
2738         Music *relative = MY_MAKE_MUSIC ("RelativeOctaveMusic", loc);
2739         relative->set_property ("element", music);
2740
2741         Music *m = unsmob_music (music);
2742         Pitch last = m->to_relative_octave (start);
2743         if (lily_1_8_relative)
2744                 m->set_property ("last-pitch", last.smobbed_copy ());
2745         return relative->unprotect ();
2746 }
2747
2748 int
2749 yylex (YYSTYPE *s, YYLTYPE *loc, void *v)
2750 {
2751         Lily_parser *pars = (Lily_parser*) v;
2752         Lily_lexer *lex = pars->lexer_;
2753
2754         lex->lexval_ = (void*) s;
2755         lex->lexloc_ = loc;
2756         lex->prepare_for_next_token ();
2757         return lex->yylex ();
2758 }