]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/lily-parser.cc
Yoshinobu Ishizaki's multiple header definition patch. Thanks!
[lilypond.git] / lily / lily-parser.cc
index 76915d07e8664763310621d56aab6c0e3d7c93f3..0119f99398029bcbc45b858bb6ed650b2fe86f8b 100644 (file)
 
 Lily_parser::Lily_parser (Sources *sources)
 {
-  book_count_ = 0;
-  score_count_ = 0;
   lexer_ = 0;
   sources_ = sources;
   default_duration_ = Duration (2, 0);
   error_level_ = 0;
 
   smobify_self ();
+
+  lexer_ = new Lily_lexer (sources_);
+  lexer_->unprotect ();
 }
 
 Lily_parser::Lily_parser (Lily_parser const &src)
 {
-  book_count_ = src.book_count_;
-  score_count_ = src.score_count_;
   lexer_ = 0;
   sources_ = src.sources_;
   default_duration_ = src.default_duration_;
@@ -48,7 +47,7 @@ Lily_parser::Lily_parser (Lily_parser const &src)
   if (src.lexer_)
     lexer_ = new Lily_lexer (*src.lexer_);
 
-  scm_gc_unprotect_object (lexer_->self_scm ());
+  lexer_->unprotect ();
 }
 
 Lily_parser::~Lily_parser ()
@@ -71,9 +70,12 @@ Lily_parser::mark_smob (SCM s)
 int
 Lily_parser::print_smob (SCM s, SCM port, scm_print_state*)
 {
-  scm_puts ("#<my_lily_parser ", port);
+  scm_puts ("#<Lily_parser ", port);
   Lily_parser *parser = (Lily_parser *) SCM_CELL_WORD_1 (s);
-  (void) parser;
+  if (parser->lexer_)
+    scm_display (parser->lexer_->self_scm (), port);
+  else
+    scm_puts ("(no lexer yet)", port);
   scm_puts (" >", port);
   return 1;
 }
@@ -87,10 +89,6 @@ Lily_parser::parse_file (String init, String name, String out_name)
       try_load_text_metrics (out_name);
     }
 
-  SCM oldmod = scm_current_module ();
-
-  lexer_ = new Lily_lexer (sources_);
-  scm_gc_unprotect_object (lexer_->self_scm ());
   // TODO: use $parser 
   lexer_->set_identifier (ly_symbol2scm ("parser"),
                          self_scm ());
@@ -112,8 +110,20 @@ Lily_parser::parse_file (String init, String name, String out_name)
 
   /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
      OUT_FILE (unless IN_FILE redefines output file name).  */
+
+  SCM mod = lexer_->set_current_scope ();
   do_yyparse ();
 
+  /*
+    Don't mix cyclic pointers with weak tables.
+   */
+  lexer_->set_identifier (ly_symbol2scm ("parser"),
+                         SCM_EOL);
+  ly_reexport_module (scm_current_module ());
+  
+
+    scm_set_current_module (mod);
   if (!define_spots_.is_empty ())
     {
       define_spots_.top ().warning (_ ("braces don't match"));
@@ -122,20 +132,12 @@ Lily_parser::parse_file (String init, String name, String out_name)
 
   error_level_ = error_level_ | lexer_->error_level_;
   lexer_ = 0;
-
-  scm_set_current_module (oldmod);
 }
 
 void
 Lily_parser::parse_string (String ly_code)
 {
-  Lily_lexer *parent = lexer_;
-  lexer_ = (parent == 0 ? new Lily_lexer (sources_)
-           : new Lily_lexer (*parent));
-  scm_gc_unprotect_object (lexer_->self_scm ());
-
-  SCM oldmod = scm_current_module ();
-  // TODO: use $parser 
+   // TODO: use $parser 
   lexer_->set_identifier (ly_symbol2scm ("parser"),
                          self_scm ());
   
@@ -144,8 +146,11 @@ Lily_parser::parse_string (String ly_code)
 
   set_yydebug (0);
   lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
-  do_yyparse ();
 
+  SCM mod = lexer_->set_current_scope ();
+  do_yyparse ();
+  scm_set_current_module (mod);
+  
   if (!define_spots_.is_empty ())
     {
       if (define_spots_.is_empty ()
@@ -154,9 +159,6 @@ Lily_parser::parse_string (String ly_code)
     }
 
   error_level_ = error_level_ | lexer_->error_level_;
-
-  scm_set_current_module (oldmod);
-  lexer_ = 0;
 }
 
 char const *
@@ -213,3 +215,12 @@ get_paper (Lily_parser *parser)
   return layout;
 }
 
+SCM
+get_header(Lily_parser *parser)
+{
+  SCM id = parser->lexer_->lookup_identifier("$defaultheader");
+  if (!ly_is_module (id))
+    id = ly_make_anonymous_module (be_safe_global);
+
+  return id;
+}