]> git.donarmstrong.com Git - lilypond.git/commitdiff
Separate read/eval for Scheme expressions.
authorDavid Kastrup <dak@gnu.org>
Wed, 9 Nov 2011 18:00:29 +0000 (19:00 +0100)
committerDavid Kastrup <dak@gnu.org>
Wed, 16 Nov 2011 09:32:17 +0000 (10:32 +0100)
lily/include/lily-lexer.hh
lily/include/parse-scm.hh
lily/lexer.ll
lily/parse-scm.cc
lily/parser.yy

index 2de3827443c5c0e63f6eeeb66d3af8666175bdc7..c53263d33ec3da6ebfa40cdfd697087703acb6c1 100644 (file)
@@ -63,6 +63,7 @@ private:
   SCM start_module_;
   int hidden_state_;
 public:
+  SCM eval_scm (SCM);
   SCM extra_tokens_;
   void *lexval_;
   Input *lexloc_;
index ad086061f867e86f61ea1779be5c72d63d294974..31427f2e44f5e98e0c3065324fe79237413af730 100644 (file)
@@ -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 */
index 3367ef6c022cbb30cdcb5bd215b0a3676d907cf6..1a79dbdec85fe943fa7137c0f5ec3931ad18ce4c 100644 (file)
@@ -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 
index 0905a8b17ec5fd5320578b973d7796640a36ef56..7fea6f7d2c390e68f5d72585afeb6d42d578e7fe 100644 (file)
@@ -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 <ghouston@freewire.co.uk>  */
@@ -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;
+}
+
index 8d7f82eeb7f5b7bec71a2e2d712628dab5af032a..f7309876a44b0a8a6aeeaca865530a9f7aa93ffa 100644 (file)
@@ -629,6 +629,9 @@ toplevel_expression:
 
 embedded_scm_bare:
        SCM_TOKEN
+       {
+               $$ = PARSER->lexer_->eval_scm ($1);
+       }
        | SCM_IDENTIFIER
        ;