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