From: David Kastrup Date: Wed, 9 Nov 2011 18:00:29 +0000 (+0100) Subject: Separate read/eval for Scheme expressions. X-Git-Tag: release/2.15.19-1~4 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=19c631f1e843c4dbc4054ed8f681fb7c470a8420;p=lilypond.git Separate read/eval for Scheme expressions. --- diff --git a/lily/include/lily-lexer.hh b/lily/include/lily-lexer.hh index 2de3827443..c53263d33e 100644 --- a/lily/include/lily-lexer.hh +++ b/lily/include/lily-lexer.hh @@ -63,6 +63,7 @@ private: SCM start_module_; int hidden_state_; public: + SCM eval_scm (SCM); SCM extra_tokens_; void *lexval_; Input *lexloc_; diff --git a/lily/include/parse-scm.hh b/lily/include/parse-scm.hh index ad086061f8..31427f2e44 100644 --- a/lily/include/parse-scm.hh +++ b/lily/include/parse-scm.hh @@ -32,6 +32,8 @@ struct Parse_start int nchars; Input start_location_; bool safe_; + SCM (*func_)(Parse_start *ps); + SCM form_; Lily_parser *parser_; Parse_start () @@ -40,11 +42,14 @@ struct Parse_start nchars = 0; safe_ = false; parser_ = 0; + form_ = SCM_UNDEFINED; + func_ = 0; } }; SCM catch_protected_parse_body (void *); SCM protected_ly_parse_scm (Parse_start *, bool); SCM ly_parse_scm (char const *, int *, Input, bool, Lily_parser *); +SCM ly_eval_scm (SCM, Input, bool, Lily_parser *); #endif /* PARSE_SCM_HH */ diff --git a/lily/lexer.ll b/lily/lexer.ll index 3367ef6c02..1a79dbdec8 100644 --- a/lily/lexer.ll +++ b/lily/lexer.ll @@ -357,10 +357,7 @@ BOM_UTF8 \357\273\277 be_safe_global && is_main_input_, parser_); if (sval == SCM_UNDEFINED) - { - sval = SCM_UNSPECIFIED; error_level_ = 1; - } for (int i = 0; i < n; i++) { @@ -383,6 +380,7 @@ BOM_UTF8 \357\273\277 hi.step_forward (); SCM sval = ly_parse_scm (hi.start (), &n, hi, be_safe_global && is_main_input_, parser_); + sval = eval_scm (sval); for (int i = 0; i < n; i++) { @@ -390,15 +388,6 @@ BOM_UTF8 \357\273\277 } char_count_stack_.back () += n; - if (sval == SCM_UNDEFINED) - { - yylval.scm = SCM_UNSPECIFIED; - error_level_ = 1; - - LexerError (_ ("bad Scheme expression").c_str ()); - return SCM_IDENTIFIER; - } - return scan_scm_id (sval); } @@ -950,6 +939,29 @@ Lily_lexer::is_figure_state () const return get_state () == figures; } +SCM +Lily_lexer::eval_scm (SCM readerdata) +{ + SCM sval = SCM_UNDEFINED; + + if (!SCM_UNBNDP (readerdata)) + { + sval = ly_eval_scm (scm_car (readerdata), + *unsmob_input (scm_cdr (readerdata)), + be_safe_global && is_main_input_, + parser_); + } + + if (SCM_UNBNDP (sval)) + { + error_level_ = 1; + return SCM_UNSPECIFIED; + } + return sval; +} + + + /* urg, belong to string (_convert) and should be generalised diff --git a/lily/parse-scm.cc b/lily/parse-scm.cc index 0905a8b17e..7fea6f7d2c 100644 --- a/lily/parse-scm.cc +++ b/lily/parse-scm.cc @@ -28,7 +28,7 @@ using namespace std; #include "paper-book.hh" #include "source-file.hh" -/* Pass string to scm parser, evaluate one expression. +/* Pass string to scm parser, read one expression. Return result value and #chars read. Thanks to Gary Houston */ @@ -46,52 +46,54 @@ internal_ly_parse_scm (Parse_start *ps) scm_set_port_line_x (port, scm_from_int (ps->start_location_.line_number () - 1)); scm_set_port_column_x (port, scm_from_int (ps->start_location_.column_number () - 1)); - SCM answer = SCM_UNSPECIFIED; SCM form = scm_read (port); SCM to = scm_ftell (port); ps->nchars = scm_to_int (to) - scm_to_int (from); - /* Read expression from port. */ - if (!SCM_EOF_OBJECT_P (form)) - { - if (ps->parser_ && !SCM_UNBNDP (ps->parser_->local_environment_)) - answer = scm_local_eval (form, ps->parser_->local_environment_); - else if (ps->safe_) - { - static SCM module = SCM_BOOL_F; - if (module == SCM_BOOL_F) - { - SCM function = ly_lily_module_constant ("make-safe-lilypond-module"); - module = scm_call_0 (function); - } - - // We define the parser so trusted Scheme functions can - // access the real namespace underlying the parser. - if (ps->parser_) - scm_module_define (module, ly_symbol2scm ("parser"), - ps->parser_->self_scm ()); - answer = scm_eval (form, module); - } - else - answer = scm_primitive_eval (form); - } - /* Don't close the port here; if we re-enter this function via a continuation, then the next time we enter it, we'll get an error. It's a string port anyway, so there's no advantage to closing it early. */ // scm_close_port (port); - return answer; + if (!SCM_EOF_OBJECT_P (form)) + return scm_cons (form, make_input (ps->start_location_)); + + return SCM_UNDEFINED; +} + +SCM +internal_ly_eval_scm (Parse_start *ps) +{ + if (ps->parser_ && !SCM_UNBNDP (ps->parser_->local_environment_)) + return scm_local_eval (ps->form_, ps->parser_->local_environment_); + if (ps->safe_) + { + static SCM module = SCM_BOOL_F; + if (module == SCM_BOOL_F) + { + SCM function = ly_lily_module_constant ("make-safe-lilypond-module"); + module = scm_call_0 (function); + } + + // We define the parser so trusted Scheme functions can + // access the real namespace underlying the parser. + if (ps->parser_) + scm_module_define (module, ly_symbol2scm ("parser"), + ps->parser_->self_scm ()); + return scm_eval (ps->form_, module); + } + return scm_primitive_eval (ps->form_); } + SCM catch_protected_parse_body (void *p) { Parse_start *ps = (Parse_start *) p; - return internal_ly_parse_scm (ps); + return (*ps->func_) (ps); } SCM @@ -125,8 +127,8 @@ protected_ly_parse_scm (Parse_start *ps) bool parse_protect_global = true; bool parsed_objects_should_be_dead = false; -/* Try parsing. Upon failure return SCM_UNDEFINED. - FIXME: shouldn't we return SCM_UNSCPECIFIED -- jcn */ +/* Try parsing. Upon failure return SCM_UNDEFINED. */ + SCM ly_parse_scm (char const *s, int *n, Input i, bool safe, Lily_parser *parser) { @@ -134,7 +136,9 @@ ly_parse_scm (char const *s, int *n, Input i, bool safe, Lily_parser *parser) ps.str = s; ps.start_location_ = i; ps.safe_ = safe; + ps.form_ = SCM_UNDEFINED; ps.parser_ = parser; + ps.func_ = internal_ly_parse_scm; SCM ans = parse_protect_global ? protected_ly_parse_scm (&ps) : internal_ly_parse_scm (&ps); @@ -143,3 +147,20 @@ ly_parse_scm (char const *s, int *n, Input i, bool safe, Lily_parser *parser) return ans; } +SCM +ly_eval_scm (SCM form, Input i, bool safe, Lily_parser *parser) +{ + Parse_start ps; + ps.str = 0; + ps.start_location_ = i; + ps.safe_ = safe; + ps.form_ = form; + ps.parser_ = parser; + ps.func_ = internal_ly_eval_scm; + + SCM ans = parse_protect_global ? protected_ly_parse_scm (&ps) + : internal_ly_eval_scm (&ps); + scm_remember_upto_here_1 (form); + return ans; +} + diff --git a/lily/parser.yy b/lily/parser.yy index 8d7f82eeb7..f7309876a4 100644 --- a/lily/parser.yy +++ b/lily/parser.yy @@ -629,6 +629,9 @@ toplevel_expression: embedded_scm_bare: SCM_TOKEN + { + $$ = PARSER->lexer_->eval_scm ($1); + } | SCM_IDENTIFIER ;