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