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