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