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