2 includable-lexer.cc -- implement Includable_lexer
4 source file of the LilyPond music typesetter
6 (c) 1997--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
12 #include "file-path.hh"
13 #include "includable-lexer.hh"
14 #include "source-file.hh"
20 #define YY_BUF_SIZE 16384
26 #define YYSTATE YY_START
29 Includable_lexer::Includable_lexer ()
31 yy_current_buffer = 0;
32 allow_includes_b_ = true;
35 /** set the new input to s, remember old file.
38 Includable_lexer::new_input (String s, Sources * global_sources)
40 if (!allow_includes_b_)
42 LexerError ("include files are disallowed.");
46 Source_file * sl = global_sources->get_file (s);
49 String msg = _f ("can't find file: `%s'", s);
51 msg += _f ("(search path: `%s')", global_sources->path_C_->string ().to_str0 ());
53 LexerError (msg.to_str0 ());
57 filename_strings_.push (sl->name_string ());
59 char_count_stack_.push (0);
60 if (yy_current_buffer)
61 state_stack_.push (yy_current_buffer);
64 progress_indication (String ("[") + s);
66 include_stack_.push (sl);
69 ugh. We'd want to create a buffer from the bytes directly.
71 Whoops. The size argument to yy_create_buffer is not the
72 filelength but a BUFFERSIZE. Maybe this is why reading stdin fucks up.
75 yy_switch_to_buffer (yy_create_buffer (sl->get_istream (), YY_BUF_SIZE));
80 Includable_lexer::new_input (String name, String data, Sources* sources)
82 Source_file* file = new Source_file (name, data);
84 filename_strings_.push (name);
86 char_count_stack_.push (0);
87 if (yy_current_buffer)
88 state_stack_.push (yy_current_buffer);
91 progress_indication (String ("[") + name);
92 include_stack_.push (file);
94 yy_switch_to_buffer (yy_create_buffer (file->get_istream (), YY_BUF_SIZE));
97 /** pop the inputstack. conceptually this is a destructor, but it
98 does not destruct the Source_file that Includable_lexer::new_input creates. */
100 Includable_lexer::close_input ()
102 include_stack_.pop ();
103 char_count_stack_.pop ();
104 if (verbose_global_b)
105 progress_indication ("]");
106 yy_delete_buffer (yy_current_buffer);
107 yy_current_buffer = 0;
108 if (state_stack_.empty ())
110 yy_current_buffer = 0;
115 yy_switch_to_buffer (state_stack_.pop ());
121 Includable_lexer::here_str0 () const
123 if (include_stack_.empty ())
125 return include_stack_.top ()->to_str0 () + char_count_stack_.top ();
128 Includable_lexer::~Includable_lexer ()
130 while (!include_stack_.empty ())
136 Since we don't create the buffer state from the bytes directly, we
137 don't know about the location of the lexer. Add this as a
140 Includable_lexer::add_lexed_char (int count)
142 char_count_stack_.top () += count;
146 Includable_lexer::get_source_file () const
148 if (include_stack_.empty ())
151 return include_stack_.top ();