From: David Kastrup Date: Thu, 6 Sep 2012 14:00:48 +0000 (+0200) Subject: Issue 2815: parser/lexer: de-unionize semantic values, make all of them SCM X-Git-Tag: release/2.17.3-1~19 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=2b5668b7afafe5939eb047e351adddf44bc40bb5;p=lilypond.git Issue 2815: parser/lexer: de-unionize semantic values, make all of them SCM Being able to reliably interpret semantic values allows for better parser extension and debugging. --- diff --git a/lily/include/lily-lexer.hh b/lily/include/lily-lexer.hh index caa3b4b44b..d27b0d3a50 100644 --- a/lily/include/lily-lexer.hh +++ b/lily/include/lily-lexer.hh @@ -91,7 +91,7 @@ public: SCM keyword_list () const; SCM lookup_identifier (string s); SCM lookup_identifier_symbol (SCM s); - void push_extra_token (int token_type, SCM scm = SCM_UNDEFINED); + void push_extra_token (int token_type, SCM scm = SCM_UNSPECIFIED); void push_chord_state (SCM alist); void push_figuredbass_state (); void push_lyric_state (); diff --git a/lily/lexer.ll b/lily/lexer.ll index 9fb4cb6d55..f0fed79c7f 100644 --- a/lily/lexer.ll +++ b/lily/lexer.ll @@ -86,13 +86,15 @@ SCM lookup_markup_list_command (string s); bool is_valid_version (string s); -#define start_quote() \ - yy_push_state (quote);\ - yylval.string = new string +#define start_quote() do { \ + yy_push_state (quote); \ + yylval = SCM_EOL; \ + } while (0) -#define start_lyric_quote() \ - yy_push_state (lyric_quote);\ - yylval.string = new string +#define start_lyric_quote() do { \ + yy_push_state (lyric_quote); \ + yylval = SCM_EOL; \ + } while (0) #define yylval (*lexval_) @@ -189,7 +191,7 @@ BOM_UTF8 \357\273\277 /* produce requested token */ int type = scm_to_int (scm_caar (extra_tokens_)); - yylval.scm = scm_cdar (extra_tokens_); + yylval = scm_cdar (extra_tokens_); extra_tokens_ = scm_cdr (extra_tokens_); if (scm_is_null (extra_tokens_)) yy_pop_state (); @@ -202,7 +204,7 @@ BOM_UTF8 \357\273\277 /* produce requested token */ int type = scm_to_int (scm_caar (extra_tokens_)); - yylval.scm = scm_cdar (extra_tokens_); + yylval = scm_cdar (extra_tokens_); extra_tokens_ = scm_cdr (extra_tokens_); if (scm_is_null (extra_tokens_)) yy_pop_state (); @@ -265,10 +267,10 @@ BOM_UTF8 \357\273\277 SCM top_scope = scm_car (scm_last_pair (scopes_)); scm_module_define (top_scope, ly_symbol2scm ("version-seen"), SCM_BOOL_T); - if (!is_valid_version (s)) + if (!is_valid_version (s)) { + yylval = SCM_UNSPECIFIED; return INVALID; - - + } } \"[^""]*\" { string s (YYText_utf8 () + 1); @@ -397,16 +399,18 @@ BOM_UTF8 \357\273\277 {RESTNAME}/[-_] | // pseudo backup rule {RESTNAME} { char const *s = YYText (); - yylval.scm = scm_from_locale_string (s); + yylval = scm_from_locale_string (s); return RESTNAME; } q/[-_] | // pseudo backup rule q { + yylval = SCM_UNSPECIFIED; return CHORD_REPETITION; } R/[-_] | // pseudo backup rule R { + yylval = SCM_UNSPECIFIED; return MULTI_MEASURE_REST; } # { //embedded scm @@ -425,7 +429,7 @@ BOM_UTF8 \357\273\277 } char_count_stack_.back () += n; - yylval.scm = sval; + yylval = sval; return SCM_TOKEN; } @@ -445,36 +449,43 @@ BOM_UTF8 \357\273\277 sval = eval_scm (sval, '$'); int token = scan_scm_id (sval); - if (!scm_is_eq (yylval.scm, SCM_UNSPECIFIED)) + if (!scm_is_eq (yylval, SCM_UNSPECIFIED)) return token; } { \<\< { + yylval = SCM_UNSPECIFIED; return DOUBLE_ANGLE_OPEN; } \>\> { + yylval = SCM_UNSPECIFIED; return DOUBLE_ANGLE_CLOSE; } } { \< { + yylval = SCM_UNSPECIFIED; return ANGLE_OPEN; } \> { + yylval = SCM_UNSPECIFIED; return ANGLE_CLOSE; } } { _ { + yylval = SCM_UNSPECIFIED; return FIGURE_SPACE; } \> { + yylval = SCM_UNSPECIFIED; return FIGURE_CLOSE; } \< { + yylval = SCM_UNSPECIFIED; return FIGURE_OPEN; } } @@ -490,39 +501,45 @@ BOM_UTF8 \357\273\277 return scan_escaped_word (YYText_utf8 () + 1); } {FRACTION} { - yylval.scm = scan_fraction (YYText ()); + yylval = scan_fraction (YYText ()); return FRACTION; } {UNSIGNED}/\/ | // backup rule {UNSIGNED} { - yylval.scm = scm_c_read_string (YYText ()); + yylval = scm_c_read_string (YYText ()); return UNSIGNED; } {E_UNSIGNED} { - yylval.i = String_convert::dec2int (string (YYText () +1)); + yylval = scm_c_read_string (YYText () + 1); return E_UNSIGNED; } } { \\{ESCAPED} { - *yylval.string += to_string (escaped_char (YYText ()[1])); + char c = escaped_char (YYText ()[1]); + yylval = scm_cons (scm_from_locale_stringn (&c, 1), + yylval); } [^\\""]+ { - *yylval.string += YYText_utf8 (); + yylval = scm_cons (scm_from_locale_string (YYText_utf8 ()), + yylval); } \" { yy_pop_state (); /* yylval is union. Must remember STRING before setting SCM*/ - string *sp = yylval.string; - yylval.scm = ly_string2scm (*sp); - delete sp; + + yylval = scm_string_concatenate_reverse (yylval, + SCM_UNDEFINED, + SCM_UNDEFINED); + return is_lyric_state () ? LYRICS_STRING : STRING; } \\ { - *yylval.string += YYText (); + yylval = scm_cons (scm_from_locale_string (YYText ()), + yylval); } } @@ -531,12 +548,12 @@ BOM_UTF8 \357\273\277 start_lyric_quote (); } {FRACTION} { - yylval.scm = scan_fraction (YYText ()); + yylval = scan_fraction (YYText ()); return FRACTION; } {UNSIGNED}/\/ | // backup rule {UNSIGNED} { - yylval.scm = scm_c_read_string (YYText ()); + yylval = scm_c_read_string (YYText ()); return UNSIGNED; } {COMMAND}/[-_] | // backup rule @@ -545,23 +562,25 @@ BOM_UTF8 \357\273\277 } {LYRICS} { /* ugr. This sux. */ - string s (YYText_utf8 ()); + string s (YYText_utf8 ()); + yylval = SCM_UNSPECIFIED; if (s == "__") - return yylval.i = EXTENDER; + return EXTENDER; if (s == "--") - return yylval.i = HYPHEN; + return HYPHEN; s = lyric_fudge (s); char c = s[s.length () - 1]; if (c == '{' || c == '}') // brace open is for not confusing dumb tools. here_input ().warning ( _ ("Brace found at end of lyric. Did you forget a space?")); - yylval.scm = ly_string2scm (s); + yylval = ly_string2scm (s); return LYRICS_STRING; } . { + yylval = SCM_UNSPECIFIED; return YYText ()[0]; // LYRICS already catches all multibytes. } } @@ -575,30 +594,36 @@ BOM_UTF8 \357\273\277 return scan_escaped_word (YYText_utf8 () + 1); } {FRACTION} { - yylval.scm = scan_fraction (YYText ()); + yylval = scan_fraction (YYText ()); return FRACTION; } {UNSIGNED}/\/ | // backup rule {UNSIGNED} { - yylval.scm = scm_c_read_string (YYText ()); + yylval = scm_c_read_string (YYText ()); return UNSIGNED; } - { + yylval = SCM_UNSPECIFIED; return CHORD_MINUS; } : { + yylval = SCM_UNSPECIFIED; return CHORD_COLON; } \/\+ { + yylval = SCM_UNSPECIFIED; return CHORD_BASS; } \/ { + yylval = SCM_UNSPECIFIED; return CHORD_SLASH; } \^ { + yylval = SCM_UNSPECIFIED; return CHORD_CARET; } . { + yylval = SCM_UNSPECIFIED; return YYText ()[0]; // WORD catches all multibyte. } } @@ -606,6 +631,7 @@ BOM_UTF8 \357\273\277 { \\score { + yylval = SCM_UNSPECIFIED; return SCORE; } {COMMAND}/[-_] | // backup rule @@ -639,7 +665,7 @@ BOM_UTF8 \357\273\277 // in reverse order, so the first token pushed in the // loop will be EXPECT_NO_MORE_ARGS. - yylval.scm = scm_car(s); + yylval = scm_car(s); // yylval now contains the function to call as token // value (for token type MARKUP_FUNCTION or @@ -660,6 +686,7 @@ BOM_UTF8 \357\273\277 return token_type; } [{}] { + yylval = SCM_UNSPECIFIED; return YYText ()[0]; } [^$#{}\"\\ \t\n\r\f]+ { @@ -670,12 +697,13 @@ BOM_UTF8 \357\273\277 if (c == '{' || c == '}') here_input ().warning ( _ ("Brace found at end of markup. Did you forget a space?")); - yylval.scm = ly_string2scm (s); + yylval = ly_string2scm (s); return STRING; } . { + yylval = SCM_UNSPECIFIED; return YYText()[0]; // Above is catchall for multibyte } } @@ -683,11 +711,14 @@ BOM_UTF8 \357\273\277 <> { LexerError (_ ("EOF found inside a comment").c_str ()); is_main_input_ = false; // should be safe , can't have \include in --safe. + yylval = SCM_UNSPECIFIED; if (!close_input ()) yyterminate (); // can't move this, since it actually rets a YY_NULL } -<> { if (is_main_input_) +<> { + yylval = SCM_UNSPECIFIED; + if (is_main_input_) { /* 2 = init.ly + current file. > because we're before closing, but is_main_input_ should @@ -715,40 +746,41 @@ BOM_UTF8 \357\273\277 } {FRACTION} { - yylval.scm = scan_fraction (YYText ()); + yylval = scan_fraction (YYText ()); return FRACTION; } -{UNSIGNED} | // backup rule {REAL} { - yylval.scm = scm_c_read_string (YYText ()); + yylval = scm_c_read_string (YYText ()); return REAL; } {UNSIGNED}/\/ | // backup rule {UNSIGNED} { - yylval.scm = scm_c_read_string (YYText ()); + yylval = scm_c_read_string (YYText ()); return UNSIGNED; } [{}] { - + yylval = SCM_UNSPECIFIED; return YYText ()[0]; } -/\. | // backup rule [*:=] { - char c = YYText ()[0]; - - return c; + yylval = SCM_UNSPECIFIED; + return YYText ()[0]; } . { + yylval = SCM_UNSPECIFIED; return YYText ()[0]; } \\. { + yylval = SCM_UNSPECIFIED; char c = YYText ()[1]; switch (c) { @@ -781,6 +813,7 @@ BOM_UTF8 \357\273\277 <*>.[\200-\277]* { string msg = _f ("invalid character: `%s'", YYText_utf8 ()); LexerError (msg.c_str ()); + yylval = SCM_UNSPECIFIED; return '%'; // Better not return half a utf8 character. } @@ -878,7 +911,7 @@ Lily_lexer::pop_state () int Lily_lexer::identifier_type (SCM sid) { - int k = try_special_identifiers (&yylval.scm , sid); + int k = try_special_identifiers (&yylval , sid); return k >= 0 ? k : SCM_IDENTIFIER; } @@ -890,6 +923,7 @@ Lily_lexer::scan_escaped_word (string str) // SCM sym = ly_symbol2scm (str.c_str ()); + yylval = SCM_UNSPECIFIED; int i = lookup_keyword (str); if (i == MARKUP && is_lyric_state ()) return LYRIC_MARKUP; @@ -903,7 +937,7 @@ Lily_lexer::scan_escaped_word (string str) string msg (_f ("unknown escaped string: `\\%s'", str)); LexerError (msg.c_str ()); - yylval.scm = ly_string2scm (str); + yylval = ly_string2scm (str); return STRING; } @@ -915,7 +949,7 @@ Lily_lexer::scan_scm_id (SCM sid) { int funtype = SCM_FUNCTION; - yylval.scm = sid; + yylval = sid; SCM s = get_music_function_signature (sid); SCM cs = scm_car (s); @@ -961,7 +995,7 @@ Lily_lexer::scan_scm_id (SCM sid) } return funtype; } - yylval.scm = sid; + yylval = sid; return identifier_type (sid); } @@ -975,20 +1009,20 @@ Lily_lexer::scan_bare_word (string str) handle = scm_hashq_get_handle (scm_cdar (pitchname_tab_stack_), sym); if (scm_is_pair (handle)) { - yylval.scm = scm_cdr (handle); - if (unsmob_pitch (yylval.scm)) + yylval = scm_cdr (handle); + if (unsmob_pitch (yylval)) return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH; - else if (scm_is_symbol (yylval.scm)) + else if (scm_is_symbol (yylval)) return DRUM_PITCH; } else if ((YYSTATE == chords) && (handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F) { - yylval.scm = scm_cdr (handle); + yylval = scm_cdr (handle); return CHORD_MODIFIER; } } - yylval.scm = ly_string2scm (str); + yylval = ly_string2scm (str); return STRING; } @@ -1064,8 +1098,8 @@ Lily_lexer::eval_scm (SCM readerdata, char extra_token) switch (extra_token) { case '$': token = scan_scm_id (scm_car (v)); - if (!scm_is_eq (yylval.scm, SCM_UNSPECIFIED)) - push_extra_token (token, yylval.scm); + if (!scm_is_eq (yylval, SCM_UNSPECIFIED)) + push_extra_token (token, yylval); break; case '#': push_extra_token (SCM_IDENTIFIER, scm_car (v)); diff --git a/lily/parser.yy b/lily/parser.yy index bacdcf2369..e69f9f5517 100644 --- a/lily/parser.yy +++ b/lily/parser.yy @@ -27,17 +27,29 @@ * inconsistent state of indentation. */ -%{ +%code requires { #define yyerror Lily_parser::parser_error /* We use custom location type: Input objects */ #define YYLTYPE Input +#define YYSTYPE SCM #define YYLLOC_DEFAULT(Current,Rhs,N) \ ((Current).set_location ((Rhs)[1], (Rhs)[N])) +#define YYPRINT(file, type, value) \ + do { \ + if (scm_is_eq (value, SCM_UNSPECIFIED)) \ + break; \ + char *p = scm_to_locale_string \ + (scm_simple_format (SCM_BOOL_F, \ + scm_from_locale_string ("~S"), \ + scm_list_1 (value))); \ + fputs (p, file); \ + free (p); \ + } while (0) -%} +} %parse-param {Lily_parser *parser} %parse-param {SCM *retval} @@ -180,15 +192,6 @@ while (0) %} -%union { - Book *book; - Output_def *outputdef; - SCM scm; - std::string *string; - Score *score; - int i; -} - %{ #define MY_MAKE_MUSIC(x, spot) make_music_with_input (ly_symbol2scm (x), spot) @@ -224,7 +227,7 @@ SCM check_scheme_arg (Lily_parser *parser, Input loc, SCM arg, SCM args, SCM pred); SCM loc_on_music (Input loc, SCM arg); SCM make_chord_elements (Input loc, SCM pitch, SCM dur, SCM modification_list); -SCM make_chord_step (int step, Rational alter); +SCM make_chord_step (SCM step, Rational alter); SCM make_simple_markup (SCM a); bool is_duration (int t); bool is_regular_identifier (SCM id); @@ -328,230 +331,57 @@ If we give names, Bison complains. %token MULTI_MEASURE_REST -%token E_UNSIGNED -%token UNSIGNED +%token E_UNSIGNED +%token UNSIGNED /* Artificial tokens, for more generic function syntax */ -%token EXPECT_MARKUP "markup?" -%token EXPECT_PITCH "ly:pitch?" -%token EXPECT_DURATION "ly:duration?" -%token EXPECT_SCM "scheme?" -%token BACKUP "(backed-up?)" -%token REPARSE "(reparsed?)" -%token EXPECT_MARKUP_LIST "markup-list?" -%token EXPECT_OPTIONAL "optional?" +%token EXPECT_MARKUP "markup?" +%token EXPECT_PITCH "ly:pitch?" +%token EXPECT_DURATION "ly:duration?" +%token EXPECT_SCM "scheme?" +%token BACKUP "(backed-up?)" +%token REPARSE "(reparsed?)" +%token EXPECT_MARKUP_LIST "markup-list?" +%token EXPECT_OPTIONAL "optional?" /* After the last argument. */ -%token EXPECT_NO_MORE_ARGS; +%token EXPECT_NO_MORE_ARGS; /* An artificial token for parsing embedded Lilypond */ -%token EMBEDDED_LILY "#{" - -%token BOOK_IDENTIFIER -%token CHORDMODIFIER_PITCH -%token CHORD_MODIFIER -%token CHORD_REPETITION -%token CONTEXT_DEF_IDENTIFIER -%token CONTEXT_MOD_IDENTIFIER -%token DRUM_PITCH -%token PITCH_IDENTIFIER -%token DURATION_IDENTIFIER -%token EVENT_IDENTIFIER -%token EVENT_FUNCTION -%token FRACTION -%token LYRICS_STRING -%token LYRIC_ELEMENT -%token LYRIC_MARKUP_IDENTIFIER -%token MARKUP_FUNCTION -%token MARKUP_LIST_FUNCTION -%token MARKUP_IDENTIFIER -%token MARKUPLIST_IDENTIFIER -%token MUSIC_FUNCTION -%token MUSIC_IDENTIFIER -%token NOTENAME_PITCH -%token NUMBER_IDENTIFIER -%token OUTPUT_DEF_IDENTIFIER -%token REAL -%token RESTNAME -%token SCM_FUNCTION -%token SCM_IDENTIFIER -%token SCM_TOKEN -%token SCORE_IDENTIFIER -%token STRING -%token STRING_IDENTIFIER -%token TONICNAME_PITCH - - -%type book_block -%type book_body -%type bookpart_block -%type bookpart_body - -%type bare_unsigned -%type figured_bass_alteration -%type dots -%type exclamations -%type optional_rest -%type questions -%type script_dir -%type sub_quotes -%type sup_quotes -%type tremolo_type - -/* Music */ -%type composite_music -%type grouped_music_list -%type braced_music_list -%type closed_music -%type music -%type music_bare -%type music_arg -%type music_assign -%type music_embedded -%type music_or_context_def -%type complex_music -%type complex_music_prefix -%type mode_changed_music -%type repeated_music -%type sequential_music -%type simple_music -%type simultaneous_music -%type chord_body -%type chord_body_element -%type command_element -%type command_event -%type context_modification -%type context_change -%type direction_less_event -%type direction_reqd_event -%type embedded_lilypond -%type event_chord -%type fingering -%type gen_text_def -%type music_property_def -%type note_chord_element -%type post_event -%type post_event_nofinger -%type re_rhythmed_music -%type simple_element -%type simple_music_property_def -%type start_symbol -%type string_number_event -%type tempo_event - -%type output_def_body -%type output_def_head -%type output_def_head_with_mode_switch -%type output_def -%type paper_block - -%type music_function_call -%type music_list -%type assignment_id -%type bare_number -%type bare_number_closed -%type unsigned_number -%type bass_figure -%type figured_bass_modification -%type br_bass_figure -%type bass_number -%type chord_body_elements -%type chord_item -%type chord_items -%type chord_separator -%type context_def_mod -%type context_def_spec_block -%type context_def_spec_body -%type context_mod -%type context_mod_arg -%type context_mod_embedded -%type context_mod_list -%type context_prop_spec -%type direction_less_char -%type duration_length -%type embedded_scm -%type embedded_scm_arg -%type embedded_scm_arg_closed -%type embedded_scm_bare -%type embedded_scm_bare_arg -%type embedded_scm_closed -%type event_function_event -%type figure_list -%type figure_spec -%type full_markup -%type full_markup_list -%type function_arglist -%type function_arglist_optional -%type function_arglist_backup -%type function_arglist_nonbackup -%type function_arglist_nonbackup_common -%type function_arglist_closed_nonbackup -%type function_arglist_skip -%type function_arglist_bare -%type function_arglist_closed -%type function_arglist_closed_optional -%type function_arglist_common -%type function_arglist_common_lyric -%type function_arglist_common_minus -%type function_arglist_closed_common -%type function_arglist_keep -%type function_arglist_closed_keep -%type identifier_init -%type lilypond -%type lilypond_header -%type lyric_element -%type lyric_element_arg -%type lyric_element_music -%type lyric_markup -%type markup -%type markup_braced_list -%type markup_braced_list_body -%type markup_composed_list -%type markup_command_list -%type markup_command_list_arguments -%type markup_command_basic_arguments -%type markup_head_1_item -%type markup_head_1_list -%type markup_list -%type markup_top -%type maybe_notemode_duration -%type mode_changing_head -%type mode_changing_head_with_context -%type multiplied_duration -%type music_function_event -%type music_function_chord_body -%type new_chord -%type new_lyrics -%type number_expression -%type number_factor -%type number_term -%type octave_check -%type optional_context_mod -%type optional_id -%type optional_notemode_duration -%type pitch -%type pitch_also_in_chords -%type post_events -%type property_operation -%type property_path property_path_revved -%type scalar -%type scalar_closed -%type scm_function_call -%type scm_function_call_closed -%type script_abbreviation -%type simple_chord_elements -%type simple_markup -%type simple_string -%type steno_duration -%type steno_pitch -%type steno_tonic_pitch -%type step_number -%type step_numbers -%type string -%type tempo_range - -%type score_block -%type score_body - +%token EMBEDDED_LILY "#{" + +%token BOOK_IDENTIFIER +%token CHORDMODIFIER_PITCH +%token CHORD_MODIFIER +%token CHORD_REPETITION +%token CONTEXT_DEF_IDENTIFIER +%token CONTEXT_MOD_IDENTIFIER +%token DRUM_PITCH +%token PITCH_IDENTIFIER +%token DURATION_IDENTIFIER +%token EVENT_IDENTIFIER +%token EVENT_FUNCTION +%token FRACTION +%token LYRICS_STRING +%token LYRIC_ELEMENT +%token LYRIC_MARKUP_IDENTIFIER +%token MARKUP_FUNCTION +%token MARKUP_LIST_FUNCTION +%token MARKUP_IDENTIFIER +%token MARKUPLIST_IDENTIFIER +%token MUSIC_FUNCTION +%token MUSIC_IDENTIFIER +%token NOTENAME_PITCH +%token NUMBER_IDENTIFIER +%token OUTPUT_DEF_IDENTIFIER +%token REAL +%token RESTNAME +%token SCM_FUNCTION +%token SCM_IDENTIFIER +%token SCM_TOKEN +%token SCORE_IDENTIFIER +%token STRING +%token STRING_IDENTIFIER +%token TONICNAME_PITCH %left '-' '+' @@ -573,7 +403,7 @@ start_symbol: } ; -lilypond: /* empty */ { } +lilypond: /* empty */ { $$ = SCM_UNSPECIFIED; } | lilypond toplevel_expression { } | lilypond assignment { @@ -594,28 +424,20 @@ toplevel_expression: parser->lexer_->set_identifier (ly_symbol2scm ("$defaultheader"), $2); } | book_block { - Book *book = $1; SCM proc = parser->lexer_->lookup_identifier ("toplevel-book-handler"); - scm_call_2 (proc, parser->self_scm (), book->self_scm ()); - book->unprotect (); + scm_call_2 (proc, parser->self_scm (), $1); } | bookpart_block { - Book *bookpart = $1; SCM proc = parser->lexer_->lookup_identifier ("toplevel-bookpart-handler"); - scm_call_2 (proc, parser->self_scm (), bookpart->self_scm ()); - bookpart->unprotect (); + scm_call_2 (proc, parser->self_scm (), $1); } | score_block { - Score *score = $1; - SCM proc = parser->lexer_->lookup_identifier ("toplevel-score-handler"); - scm_call_2 (proc, parser->self_scm (), score->self_scm ()); - score->unprotect (); + scm_call_2 (proc, parser->self_scm (), $1); } | composite_music { - Music *music = unsmob_music ($1); SCM proc = parser->lexer_->lookup_identifier ("toplevel-music-handler"); - scm_call_2 (proc, parser->self_scm (), music->self_scm ()); + scm_call_2 (proc, parser->self_scm (), $1); } | full_markup { SCM proc = parser->lexer_->lookup_identifier ("toplevel-text-handler"); @@ -627,17 +449,16 @@ toplevel_expression: } | output_def { SCM id = SCM_EOL; - Output_def * od = $1; + Output_def * od = unsmob_output_def ($1); - if ($1->c_variable ("is-paper") == SCM_BOOL_T) + if (od->c_variable ("is-paper") == SCM_BOOL_T) id = ly_symbol2scm ("$defaultpaper"); - else if ($1->c_variable ("is-midi") == SCM_BOOL_T) + else if (od->c_variable ("is-midi") == SCM_BOOL_T) id = ly_symbol2scm ("$defaultmidi"); - else if ($1->c_variable ("is-layout") == SCM_BOOL_T) + else if (od->c_variable ("is-layout") == SCM_BOOL_T) id = ly_symbol2scm ("$defaultlayout"); - parser->lexer_->set_identifier (id, od->self_scm ()); - od->unprotect(); + parser->lexer_->set_identifier (id, $1); } ; @@ -657,26 +478,10 @@ embedded_scm_bare_arg: | full_markup_list | context_modification | score_block - { - $$ = $1->self_scm (); - $1->unprotect (); - } | context_def_spec_block | book_block - { - $$ = $1->self_scm (); - $1->unprotect (); - } | bookpart_block - { - $$ = $1->self_scm (); - $1->unprotect (); - } | output_def - { - $$ = $1->self_scm (); - $1->unprotect (); - } ; /* The generic version may end in music, or not */ @@ -715,15 +520,17 @@ embedded_lilypond: } | error { parser->error_level_ = 1; + $$ = SCM_UNSPECIFIED; } | INVALID embedded_lilypond { parser->error_level_ = 1; + $$ = $2; } ; lilypond_header_body: - /* empty */ + /* empty */ { $$ = SCM_UNSPECIFIED; } | lilypond_header_body assignment { } @@ -746,70 +553,32 @@ assignment_id: assignment: assignment_id '=' identifier_init { parser->lexer_->set_identifier ($1, $3); + $$ = SCM_UNSPECIFIED; } | assignment_id property_path '=' identifier_init { SCM path = scm_cons (scm_string_to_symbol ($1), $2); parser->lexer_->set_identifier (path, $4); - ; -/* - TODO: devise standard for protection in parser. - - The parser stack lives on the C-stack, which means that -all objects can be unprotected as soon as they're here. - -*/ + $$ = SCM_UNSPECIFIED; } - | embedded_scm { } + | embedded_scm { $$ = SCM_UNSPECIFIED; } ; identifier_init: - score_block { - $$ = $1->self_scm (); - $1->unprotect (); - } - | book_block { - $$ = $1->self_scm (); - $1->unprotect (); - } - | bookpart_block { - $$ = $1->self_scm (); - $1->unprotect (); - } - | output_def { - $$ = $1->self_scm (); - $1->unprotect (); - } - | context_def_spec_block { - $$ = $1; - } - | music_assign { - $$ = $1; - } - | post_event_nofinger { - $$ = $1; - } - | number_expression { - $$ = $1; - } - | FRACTION { - $$ = $1; - } - | string { - $$ = $1; - } - | embedded_scm { - $$ = $1; - } - | full_markup { - $$ = $1; - } - | full_markup_list { - $$ = $1; - } - | context_modification { - $$ = $1; - } + score_block + | book_block + | bookpart_block + | output_def + | context_def_spec_block + | music_assign + | post_event_nofinger + | number_expression + | FRACTION + | string + | embedded_scm + | full_markup + | full_markup_list + | context_modification ; context_def_spec_block: @@ -883,60 +652,54 @@ book_block: * grok \layout and \midi? */ book_body: { - $$ = new Book; + Book *book = new Book; init_papers (parser); - $$->origin ()->set_spot (@$); - $$->paper_ = dynamic_cast (unsmob_output_def (parser->lexer_->lookup_identifier ("$defaultpaper"))->clone ()); - $$->paper_->unprotect (); - push_paper (parser, $$->paper_); - $$->header_ = get_header (parser); - parser->lexer_->set_identifier (ly_symbol2scm ("$current-book"), $$->self_scm ()); + book->origin ()->set_spot (@$); + book->paper_ = dynamic_cast (unsmob_output_def (parser->lexer_->lookup_identifier ("$defaultpaper"))->clone ()); + book->paper_->unprotect (); + push_paper (parser, book->paper_); + book->header_ = get_header (parser); + $$ = book->unprotect (); + parser->lexer_->set_identifier (ly_symbol2scm ("$current-book"), $$); } | BOOK_IDENTIFIER { - $$ = unsmob_book ($1); - $$->protect (); - $$->origin ()->set_spot (@$); + unsmob_book ($1)->origin ()->set_spot (@$); parser->lexer_->set_identifier (ly_symbol2scm ("$current-book"), $1); } | book_body paper_block { - $$->paper_ = $2; - $2->unprotect (); - set_paper (parser, $2); + unsmob_book ($1)->paper_ = unsmob_output_def ($2); + set_paper (parser, unsmob_output_def ($2)); } | book_body bookpart_block { - Book *bookpart = $2; SCM proc = parser->lexer_->lookup_identifier ("book-bookpart-handler"); - scm_call_2 (proc, $$->self_scm (), bookpart->self_scm ()); - bookpart->unprotect (); + scm_call_2 (proc, $1, $2); } | book_body score_block { - Score *score = $2; SCM proc = parser->lexer_->lookup_identifier ("book-score-handler"); - scm_call_2 (proc, $$->self_scm (), score->self_scm ()); - score->unprotect (); + scm_call_2 (proc, $1, $2); } | book_body composite_music { - Music *music = unsmob_music ($2); SCM proc = parser->lexer_->lookup_identifier ("book-music-handler"); - scm_call_3 (proc, parser->self_scm (), $$->self_scm (), music->self_scm ()); + scm_call_3 (proc, parser->self_scm (), $1, $2); } | book_body full_markup { SCM proc = parser->lexer_->lookup_identifier ("book-text-handler"); - scm_call_2 (proc, $$->self_scm (), scm_list_1 ($2)); + scm_call_2 (proc, $1, scm_list_1 ($2)); } | book_body full_markup_list { SCM proc = parser->lexer_->lookup_identifier ("book-text-handler"); - scm_call_2 (proc, $$->self_scm (), $2); + scm_call_2 (proc, $1, $2); } | book_body { - parser->lexer_->add_scope ($1->header_); + parser->lexer_->add_scope (unsmob_book ($1)->header_); } lilypond_header | book_body embedded_scm { } | book_body error { - $$->paper_ = 0; - $$->scores_ = SCM_EOL; - $$->bookparts_ = SCM_EOL; + Book *book = unsmob_book ($1); + book->paper_ = 0; + book->scores_ = SCM_EOL; + book->bookparts_ = SCM_EOL; } ; @@ -949,49 +712,46 @@ bookpart_block: bookpart_body: { - $$ = new Book; - $$->origin ()->set_spot (@$); - parser->lexer_->set_identifier (ly_symbol2scm ("$current-bookpart"), $$->self_scm ()); + Book *book = new Book; + book->origin ()->set_spot (@$); + $$ = book->unprotect (); + parser->lexer_->set_identifier (ly_symbol2scm ("$current-bookpart"), $$); } | BOOK_IDENTIFIER { - $$ = unsmob_book ($1); - $$->protect (); - $$->origin ()->set_spot (@$); + unsmob_book ($1)->origin ()->set_spot (@$); parser->lexer_->set_identifier (ly_symbol2scm ("$current-bookpart"), $1); } | bookpart_body paper_block { - $$->paper_ = $2; - $2->unprotect (); + unsmob_book ($$)->paper_ = unsmob_output_def ($2); } | bookpart_body score_block { - Score *score = $2; SCM proc = parser->lexer_->lookup_identifier ("bookpart-score-handler"); - scm_call_2 (proc, $$->self_scm (), score->self_scm ()); - score->unprotect (); + scm_call_2 (proc, $1, $2); } | bookpart_body composite_music { - Music *music = unsmob_music ($2); SCM proc = parser->lexer_->lookup_identifier ("bookpart-music-handler"); - scm_call_3 (proc, parser->self_scm (), $$->self_scm (), music->self_scm ()); + scm_call_3 (proc, parser->self_scm (), $1, $2); } | bookpart_body full_markup { SCM proc = parser->lexer_->lookup_identifier ("bookpart-text-handler"); - scm_call_2 (proc, $$->self_scm (), scm_list_1 ($2)); + scm_call_2 (proc, $1, scm_list_1 ($2)); } | bookpart_body full_markup_list { SCM proc = parser->lexer_->lookup_identifier ("bookpart-text-handler"); - scm_call_2 (proc, $$->self_scm (), $2); + scm_call_2 (proc, $1, $2); } | bookpart_body { - if (!ly_is_module ($1->header_)) - $1->header_ = ly_make_module (false); - parser->lexer_->add_scope ($1->header_); + Book *book = unsmob_book ($1); + if (!ly_is_module (book->header_)) + book->header_ = ly_make_module (false); + parser->lexer_->add_scope (book->header_); } lilypond_header | bookpart_body embedded_scm { } | bookpart_body error { - $$->paper_ = 0; - $$->scores_ = SCM_EOL; + Book *book = unsmob_book ($1); + book->paper_ = 0; + book->scores_ = SCM_EOL; } ; @@ -1003,40 +763,35 @@ score_block: score_body: music { - SCM m = $1; SCM scorify = ly_lily_module_constant ("scorify-music"); - SCM score = scm_call_2 (scorify, m, parser->self_scm ()); + $$ = scm_call_2 (scorify, $1, parser->self_scm ()); - // pass ownernship to C++ again. - $$ = unsmob_score (score); - $$->protect (); - $$->origin ()->set_spot (@$); + unsmob_score ($$)->origin ()->set_spot (@$); } | SCORE_IDENTIFIER { - $$ = unsmob_score ($1); - $$->protect (); - $$->origin ()->set_spot (@$); + unsmob_score ($$)->origin ()->set_spot (@$); } | score_body { - if (!ly_is_module ($1->get_header ())) - $1->set_header (ly_make_module (false)); - parser->lexer_->add_scope ($1->get_header ()); + Score *score = unsmob_score ($1); + if (!ly_is_module (score->get_header ())) + score->set_header (ly_make_module (false)); + parser->lexer_->add_scope (score->get_header ()); } lilypond_header | score_body output_def { - if ($2->lookup_variable (ly_symbol2scm ("is-paper")) == SCM_BOOL_T) + Output_def *od = unsmob_output_def ($2); + if (od->lookup_variable (ly_symbol2scm ("is-paper")) == SCM_BOOL_T) { parser->parser_error (@2, _("\\paper cannot be used in \\score, use \\layout instead")); } else { - $$->add_output_def ($2); + unsmob_score ($1)->add_output_def (od); } - $2->unprotect (); } | score_body error { - $$->error_found_ = true; + unsmob_score ($$)->error_found_ = true; } ; @@ -1047,12 +802,12 @@ score_body: paper_block: output_def { - $$ = $1; - if ($$->lookup_variable (ly_symbol2scm ("is-paper")) != SCM_BOOL_T) + Output_def *od = unsmob_output_def ($1); + + if (od->lookup_variable (ly_symbol2scm ("is-paper")) != SCM_BOOL_T) { parser->parser_error (@1, _ ("need \\paper for paper block")); - $1->unprotect (); - $$ = get_paper (parser); + $$ = get_paper (parser)->unprotect (); } } ; @@ -1069,20 +824,21 @@ output_def: output_def_head: PAPER { - $$ = get_paper (parser); - $$->input_origin_ = @$; - parser->lexer_->add_scope ($$->scope_); + Output_def *p = get_paper (parser); + p->input_origin_ = @$; + parser->lexer_->add_scope (p->scope_); + $$ = p->unprotect (); } | MIDI { Output_def *p = get_midi (parser); - $$ = p; + $$ = p->unprotect (); parser->lexer_->add_scope (p->scope_); } | LAYOUT { Output_def *p = get_layout (parser); parser->lexer_->add_scope (p->scope_); - $$ = p; + $$ = p->unprotect (); } ; @@ -1115,15 +871,12 @@ music_or_context_def: output_def_body: output_def_head_with_mode_switch '{' { $$ = $1; - $$->input_origin_.set_spot (@$); + unsmob_output_def ($$)->input_origin_.set_spot (@$); } | output_def_head_with_mode_switch '{' OUTPUT_DEF_IDENTIFIER { - $1->unprotect (); - Output_def *o = unsmob_output_def ($3); o->input_origin_.set_spot (@$); - $$ = o; - $$->protect (); + $$ = o->self_scm (); parser->lexer_->remove_scope (); parser->lexer_->add_scope (o->scope_); } @@ -1137,13 +890,13 @@ output_def_body: } music_or_context_def { if (unsmob_context_def ($3)) - assign_context_def ($$, $3); + assign_context_def (unsmob_output_def ($1), $3); else { SCM proc = parser->lexer_->lookup_identifier ("output-def-music-handler"); scm_call_3 (proc, parser->self_scm (), - $1->self_scm (), $3); + $1, $3); } } | output_def_body error { @@ -2216,16 +1969,16 @@ chord_body_elements: chord_body_element: pitch exclamations questions octave_check post_events { - int q = $3; - int ex = $2; + bool q = to_boolean ($3); + bool ex = to_boolean ($2); SCM check = $4; SCM post = $5; Music *n = MY_MAKE_MUSIC ("NoteEvent", @$); n->set_property ("pitch", $1); - if (q % 2) + if (q) n->set_property ("cautionary", SCM_BOOL_T); - if (ex % 2 || q % 2) + if (ex || q) n->set_property ("force-accidental", SCM_BOOL_T); if (scm_is_pair (post)) { @@ -2349,9 +2102,9 @@ post_event_nofinger: } | script_dir music_function_event { $$ = $2; - if ($1) + if (!SCM_UNBNDP ($1)) { - unsmob_music ($$)->set_property ("direction", scm_from_int ($1)); + unsmob_music ($$)->set_property ("direction", $1); } } | HYPHEN { @@ -2365,18 +2118,18 @@ post_event_nofinger: $$ = MY_MAKE_MUSIC ("ExtenderEvent", @$)->unprotect (); } | script_dir direction_reqd_event { - if ($1) + if (!SCM_UNBNDP ($1)) { Music *m = unsmob_music ($2); - m->set_property ("direction", scm_from_int ($1)); + m->set_property ("direction", $1); } $$ = $2; } | script_dir direction_less_event { - if ($1) + if (!SCM_UNBNDP ($1)) { Music *m = unsmob_music ($2); - m->set_property ("direction", scm_from_int ($1)); + m->set_property ("direction", $1); } $$ = $2; } @@ -2402,7 +2155,7 @@ post_event: string_number_event: E_UNSIGNED { Music *s = MY_MAKE_MUSIC ("StringNumberEvent", @$); - s->set_property ("string-number", scm_from_int ($1)); + s->set_property ("string-number", $1); $$ = s->unprotect (); } ; @@ -2461,7 +2214,7 @@ direction_less_event: } | tremolo_type { Music *a = MY_MAKE_MUSIC ("TremoloEvent", @$); - a->set_property ("tremolo-type", scm_from_int ($1)); + a->set_property ("tremolo-type", $1); $$ = a->unprotect (); } | event_function_event @@ -2483,42 +2236,44 @@ direction_reqd_event: octave_check: /**/ { $$ = SCM_EOL; } - | '=' { $$ = scm_from_int (0); } - | '=' sub_quotes { $$ = scm_from_int (-$2); } - | '=' sup_quotes { $$ = scm_from_int ($2); } + | '=' quotes { $$ = $2; } ; +quotes: + /* empty */ + { + $$ = SCM_INUM0; + } + | sub_quotes + | sup_quotes + ; + sup_quotes: '\'' { - $$ = 1; + $$ = scm_from_int (1); } | sup_quotes '\'' { - $$ ++; + $$ = scm_oneplus ($1); } ; sub_quotes: ',' { - $$ = 1; + $$ = scm_from_int (-1); } | sub_quotes ',' { - $$++; + $$ = scm_oneminus ($1); } ; steno_pitch: - NOTENAME_PITCH { - $$ = $1; - } - | NOTENAME_PITCH sup_quotes { - Pitch p = *unsmob_pitch ($1); - p = p.transposed (Pitch ($2,0,0)); - $$ = p.smobbed_copy (); - } - | NOTENAME_PITCH sub_quotes { - Pitch p =* unsmob_pitch ($1); - p = p.transposed (Pitch (-$2,0,0)); - $$ = p.smobbed_copy (); + NOTENAME_PITCH quotes { + if (!scm_is_eq (SCM_INUM0, $2)) + { + Pitch p = *unsmob_pitch ($1); + p = p.transposed (Pitch (scm_to_int ($2),0,0)); + $$ = p.smobbed_copy (); + } } ; @@ -2527,26 +2282,18 @@ ugh. duplication */ steno_tonic_pitch: - TONICNAME_PITCH { - $$ = $1; - } - | TONICNAME_PITCH sup_quotes { - Pitch p = *unsmob_pitch ($1); - p = p.transposed (Pitch ($2,0,0)); - $$ = p.smobbed_copy (); - } - | TONICNAME_PITCH sub_quotes { - Pitch p = *unsmob_pitch ($1); - - p = p.transposed (Pitch (-$2,0,0)); - $$ = p.smobbed_copy (); + TONICNAME_PITCH quotes { + if (!scm_is_eq (SCM_INUM0, $2)) + { + Pitch p = *unsmob_pitch ($1); + p = p.transposed (Pitch (scm_to_int ($2),0,0)); + $$ = p.smobbed_copy (); + } } ; pitch: - steno_pitch { - $$ = $1; - } + steno_pitch | PITCH_IDENTIFIER ; @@ -2602,9 +2349,9 @@ script_abbreviation: ; script_dir: - '_' { $$ = DOWN; } - | '^' { $$ = UP; } - | '-' { $$ = CENTER; } + '_' { $$ = scm_from_int (DOWN); } + | '^' { $$ = scm_from_int (UP); } + | '-' { $$ = SCM_UNDEFINED; } ; duration_length: @@ -2633,21 +2380,23 @@ optional_notemode_duration: ; steno_duration: - bare_unsigned dots { + UNSIGNED dots { int len = 0; - if (!is_duration ($1)) - parser->parser_error (@1, _f ("not a duration: %d", $1)); + int n = scm_to_int ($1); + if (!is_duration (n)) + parser->parser_error (@1, _f ("not a duration: %d", n)); else - len = intlog2 ($1); + len = intlog2 (n); - $$ = Duration (len, $2).smobbed_copy (); + $$ = Duration (len, scm_to_int ($2)).smobbed_copy (); } | DURATION_IDENTIFIER dots { Duration *d = unsmob_duration ($1); - Duration k (d->duration_log (), d->dot_count () + $2); + Duration k (d->duration_log (), + d->dot_count () + scm_to_int ($2)); k = k.compressed (d->factor ()); - *d = k; - $$ = $1; + scm_remember_upto_here_1 ($1); + $$ = k.smobbed_copy (); } ; @@ -2655,8 +2404,8 @@ multiplied_duration: steno_duration { $$ = $1; } - | multiplied_duration '*' bare_unsigned { - $$ = unsmob_duration ($$)->compressed ( $3) .smobbed_copy (); + | multiplied_duration '*' UNSIGNED { + $$ = unsmob_duration ($$)->compressed (scm_to_int ($3)).smobbed_copy (); } | multiplied_duration '*' FRACTION { Rational m (scm_to_int (scm_car ($3)), scm_to_int (scm_cdr ($3))); @@ -2667,20 +2416,21 @@ multiplied_duration: dots: /* empty */ { - $$ = 0; + $$ = SCM_INUM0; } | dots '.' { - $$ ++; + $$ = scm_oneplus ($1); } ; tremolo_type: ':' { - $$ = 0; + $$ = SCM_INUM0; } - | ':' bare_unsigned { - if (!is_duration ($2)) - parser->parser_error (@2, _f ("not a duration: %d", $2)); + | ':' UNSIGNED { + int n = scm_to_int ($2); + if (!is_duration (n)) + parser->parser_error (@2, _f ("not a duration: %d", n)); $$ = $2; } ; @@ -2792,18 +2542,22 @@ figure_spec: optional_rest: - /**/ { $$ = 0; } - | REST { $$ = 1; } + /**/ { $$ = SCM_BOOL_F; } + | REST { $$ = SCM_BOOL_T; } ; simple_element: pitch exclamations questions octave_check maybe_notemode_duration optional_rest { if (!parser->lexer_->is_note_state ()) parser->parser_error (@1, _ ("have to be in Note mode for notes")); - if ($2 || $3 || scm_is_number ($4) || !SCM_UNBNDP ($5) || $6) + if (!SCM_UNBNDP ($2) + || !SCM_UNBNDP ($3) + || scm_is_number ($4) + || !SCM_UNBNDP ($5) + || scm_is_true ($6)) { Music *n = 0; - if ($6) + if (scm_is_true ($6)) n = MY_MAKE_MUSIC ("RestEvent", @$); else n = MY_MAKE_MUSIC ("NoteEvent", @$); @@ -2821,9 +2575,9 @@ simple_element: n->set_property ("absolute-octave", scm_from_int (q-1)); } - if ($3 % 2) + if (to_boolean ($3)) n->set_property ("cautionary", SCM_BOOL_T); - if ($2 % 2 || $3 % 2) + if (to_boolean ($2) || to_boolean ($3)) n->set_property ("force-accidental", SCM_BOOL_T); $$ = n->unprotect (); @@ -2961,23 +2715,23 @@ step_numbers: ; step_number: - bare_unsigned { + UNSIGNED { $$ = make_chord_step ($1, 0); } - | bare_unsigned '+' { + | UNSIGNED '+' { $$ = make_chord_step ($1, SHARP_ALTERATION); } - | bare_unsigned CHORD_MINUS { + | UNSIGNED CHORD_MINUS { $$ = make_chord_step ($1, FLAT_ALTERATION); } ; tempo_range: - bare_unsigned { - $$ = scm_from_int ($1); + UNSIGNED { + $$ = $1; } - | bare_unsigned '~' bare_unsigned { - $$ = scm_cons (scm_from_int ($1), scm_from_int ($3)); + | UNSIGNED '~' UNSIGNED { + $$ = scm_cons ($1, $3); } ; @@ -3033,25 +2787,31 @@ bare_number_closed: | NUMBER_IDENTIFIER ; -bare_unsigned: - UNSIGNED { - $$ = scm_to_int ($1); - } - ; - unsigned_number: UNSIGNED | NUMBER_IDENTIFIER ; exclamations: - { $$ = 0; } - | exclamations '!' { $$ ++; } + { $$ = SCM_UNDEFINED; } + | exclamations '!' + { + if (SCM_UNBNDP ($1)) + $$ = SCM_BOOL_T; + else + $$ = scm_not ($1); + } ; questions: - { $$ = 0; } - | questions '?' { $$ ++; } + { $$ = SCM_UNDEFINED; } + | questions '?' + { + if (SCM_UNBNDP ($1)) + $$ = SCM_BOOL_T; + else + $$ = scm_not ($1); + } ; /* @@ -3220,9 +2980,7 @@ simple_markup: SCM nn = parser->lexer_->lookup_identifier ("pitchnames"); parser->lexer_->push_note_state (nn); } '{' score_body '}' { - Score * sc = $4; - $$ = scm_list_2 (ly_lily_module_constant ("score-markup"), sc->self_scm ()); - sc->unprotect (); + $$ = scm_list_2 (ly_lily_module_constant ("score-markup"), $4); parser->lexer_->pop_state (); } | MARKUP_FUNCTION markup_command_basic_arguments { @@ -3424,8 +3182,10 @@ set_music_properties (Music *p, SCM a) SCM -make_chord_step (int step, Rational alter) +make_chord_step (SCM step_scm, Rational alter) { + int step = scm_to_int (step_scm); + if (step == 7) alter += FLAT_ALTERATION;