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