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