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