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