]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/lily-parser-scheme.cc
Merge branch 'jneeman' of git+ssh://jneem@git.sv.gnu.org/srv/git/lilypond into jneeman
[lilypond.git] / lily / lily-parser-scheme.cc
index b25888d689e5a0c06a2ed49aa3321560de796b2e..83c3826dbdf7bf625f4becc951695b18e19a2d01 100644 (file)
@@ -3,20 +3,22 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
+  (c) 2005--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
 */
 
 #include <unistd.h>
 
+#include "file-name-map.hh"
 #include "file-name.hh"
 #include "file-path.hh"
-#include "main.hh"
-#include "lily-parser.hh"
-#include "warn.hh"
-#include "source.hh"
+#include "international.hh"
 #include "lily-lexer.hh"
+#include "lily-parser.hh"
 #include "ly-module.hh"
-#include "file-name-map.hh"
+#include "main.hh"
+#include "program-option.hh"
+#include "source.hh"
+#include "warn.hh"
 
 /* Do not append `!' suffix, since 1st argument is not modified. */
 LY_DEFINE (ly_set_point_and_click, "ly:set-point-and-click",
@@ -34,10 +36,10 @@ LY_DEFINE (ly_parse_file, "ly:parse-file",
           "Upon failure, throw @code{ly-file-failed} key.")
 {
   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
-  char const *file = scm_i_string_chars (name);
+  string file = ly_scm2string (name);
   char const *extensions[] = {"ly", "", 0};
 
-  String file_name = global_path.find (file, extensions);
+  string file_name = global_path.find (file, extensions);
 
   /* By default, use base name of input file for output file name,
      write output to cwd; do not use root and directory parts of input
@@ -51,8 +53,8 @@ LY_DEFINE (ly_parse_file, "ly:parse-file",
   out_file_name.dir_ = "";
 
   /* When running from gui, generate output in .ly source directory.  */
-  if (output_name_global.is_empty ()
-      && scm_call_0 (ly_lily_module_constant ("running-from-gui?")) == SCM_BOOL_T)
+  if (output_name_global.empty ()
+      && ly_get_option (ly_symbol2scm ("gui")) == SCM_BOOL_T)
     {
       File_name f (file);
       f.base_ = "";
@@ -60,71 +62,87 @@ LY_DEFINE (ly_parse_file, "ly:parse-file",
       output_name_global = f.to_string ();
     }
 
-  if (!output_name_global.is_empty ())
+  if (!output_name_global.empty ())
     {
+      
       /* Interpret --output=DIR to mean --output=DIR/BASE.  */
+      string dir;
       if (is_dir (output_name_global))
        {
-         char cwd[PATH_MAX];
-         getcwd (cwd, PATH_MAX);
-
-         if (output_name_global != cwd)
+         dir = output_name_global;
+         output_name_global = "";
+       }
+      else
+       {
+         File_name out (output_name_global);
+         if (is_dir (out.dir_part ()))
            {
-             global_path.prepend (cwd);
-             message (_f ("Changing working directory to `%s'",
-                          output_name_global.to_str0 ()));
-             chdir (output_name_global.to_str0 ());
-             
+             dir = out.dir_part ();
+             out_file_name = out.file_part ();
            }
-         output_name_global = "";
+       }         
+
+      if (dir != "" && dir != "." && dir != get_working_directory ())
+       {
+         global_path.prepend (get_working_directory ());
+         message (_f ("Changing working directory to `%s'",
+                      dir.c_str ()));
+         chdir (dir.c_str ());
        }
-      else      
+      else
        out_file_name = File_name (output_name_global);
     }
 
-  String init;
-  if (!init_name_global.is_empty ())
+  string init;
+  if (!init_name_global.empty ())
     init = init_name_global;
   else
     init = "init.ly";
 
-  String out_file = out_file_name.to_string ();
+  string out_file = out_file_name.to_string ();
 
-  if (init.length () && global_path.find (init).is_empty ())
+  if (init.length () && global_path.find (init).empty ())
     {
       warning (_f ("can't find init file: `%s'", init));
       warning (_f ("(search path: `%s')",
-                  global_path.to_string ().to_str0 ()));
+                  global_path.to_string ().c_str ()));
       exit (2);
     }
 
-  if ((file_name != "-") && global_path.find (file_name).is_empty ())
+
+  bool error = false;
+  if ((file_name != "-") && file_name.empty ())
     {
-      warning (_f ("can't find file: `%s'", file_name));
-      scm_throw (ly_symbol2scm ("ly-file-failed"),
-                scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
+      warning (_f ("can't find file: `%s'", file));
+      error = true;
     }
   else
     {
       Sources sources;
       sources.set_path (&global_path);
 
-      String mapped_fn = map_file_name (file_name);
-      message (_f ("Processing `%s'", mapped_fn.to_str0 ()));
+      string mapped_fn = map_file_name (file_name);
+      message (_f ("Processing `%s'", mapped_fn.c_str ()));
       progress_indication ("\n");
 
       Lily_parser *parser = new Lily_parser (&sources);
 
       parser->parse_file (init, file_name, out_file);
 
-      bool error = parser->error_level_;
+      error = parser->error_level_;
+
+      parser->clear ();
       parser->unprotect ();
-      parser = 0;
-      if (error)
-       /* TODO: pass renamed input file too.  */
-       scm_throw (ly_symbol2scm ("ly-file-failed"),
-                  scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
     }
+
+  /*
+    outside the if-else to ensure cleanup fo Sources object, 
+   */
+  if (error)
+    /* TODO: pass renamed input file too.  */
+    scm_throw (ly_symbol2scm ("ly-file-failed"),
+              scm_list_1 (scm_makfrom0str (file_name.c_str ())));
+  
   return SCM_UNSPECIFIED;
 }
 
@@ -145,7 +163,15 @@ LY_DEFINE (ly_parse_string, "ly:parse-string",
   return SCM_UNSPECIFIED;
 }
 
-LY_DEFINE (ly_clone_parser, "ly:clone-parser",
+LY_DEFINE (ly_parser_lexer, "ly:parser-lexer",
+          1, 0, 0, (SCM parser_smob),
+          "Return the lexer for PARSER_SMOB.")
+{
+  Lily_parser *parser = unsmob_lily_parser (parser_smob);
+  return parser->lexer_->self_scm ();
+}
+
+LY_DEFINE (ly_parser_clone, "ly:parser-clone",
           1, 0, 0, (SCM parser_smob),
           "Return a clone of PARSER_SMOB.")
 {
@@ -217,7 +243,6 @@ LY_DEFINE (ly_parser_set_note_names, "ly:parser-set-note-names",
   return SCM_UNSPECIFIED;
 }
 
-
 LY_DEFINE (ly_parser_output_name, "ly:parser-output-name",
           1, 0, 0, (SCM parser),
           "Return the base name of the output file.")
@@ -225,6 +250,23 @@ LY_DEFINE (ly_parser_output_name, "ly:parser-output-name",
   Lily_parser *p = unsmob_lily_parser (parser);
   SCM_ASSERT_TYPE (p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
 
-  return scm_makfrom0str (p->output_basename_.to_str0 ());
+  return scm_makfrom0str (p->output_basename_.c_str ());
 }
 
+LY_DEFINE (ly_parser_error, "ly:parser-error",
+          2, 1, 0, (SCM parser, SCM msg, SCM input),
+          "Display an error message, and make the parser fail")
+{
+  Lily_parser *p = unsmob_lily_parser (parser);
+  SCM_ASSERT_TYPE (p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
+  SCM_ASSERT_TYPE (scm_is_string (msg), msg, SCM_ARG2, __FUNCTION__, "string");
+  string s = ly_scm2string (msg);
+  
+  Input *i = unsmob_input (input);
+  if (i)
+    p->parser_error (*i, s);
+  else
+    p->parser_error (s);
+
+  return parser;
+}