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