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