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