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