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