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