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