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