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