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