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