2 includable-lexer.cc -- implement Includable_lexer
4 source file of the LilyPond music typesetter
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
12 #include "includable-lexer.hh"
13 #include "file-path.hh"
14 #include "source-file.hh"
20 #define YY_BUF_SIZE 16384
26 #define YYSTATE YY_START
29 /* Flex >= 2.5.29 has include stack; but we don't use that yet. */
30 #ifndef HAVE_FLEXLEXER_YY_CURRENT_BUFFER
31 #define yy_current_buffer \
32 (yy_buffer_stack != 0 ? yy_buffer_stack[yy_buffer_stack_top] : 0)
35 Includable_lexer::Includable_lexer ()
37 #ifdef HAVE_FLEXLEXER_YY_CURRENT_BUFFER
38 yy_current_buffer = 0;
40 allow_includes_b_ = true;
43 /** set the new input to s, remember old file.
46 Includable_lexer::new_input (String s, Sources * global_sources)
48 if (!allow_includes_b_)
50 LexerError (_ ("include files are not allowed").to_str0 ());
54 Source_file * sl = global_sources->get_file (s);
57 String msg = _f ("can't find file: `%s'", s);
59 msg += _f ("(search path: `%s')", global_sources->path_C_->to_string ().to_str0 ());
61 LexerError (msg.to_str0 ());
65 filename_strings_.push (sl->name_string ());
67 char_count_stack_.push (0);
68 if (yy_current_buffer)
69 state_stack_.push (yy_current_buffer);
72 progress_indication (String ("[") + s);
74 include_stack_.push (sl);
77 ugh. We'd want to create a buffer from the bytes directly.
79 Whoops. The size argument to yy_create_buffer is not the
80 filelength but a BUFFERSIZE. Maybe this is why reading stdin fucks up.
83 yy_switch_to_buffer (yy_create_buffer (sl->get_istream (), YY_BUF_SIZE));
90 Includable_lexer::new_input (String name, String data, Sources* sources)
92 Source_file* file = new Source_file (name, data);
94 filename_strings_.push (name);
96 char_count_stack_.push (0);
97 if (yy_current_buffer)
98 state_stack_.push (yy_current_buffer);
100 if (verbose_global_b)
101 progress_indication (String ("[") + name);
102 include_stack_.push (file);
104 yy_switch_to_buffer (yy_create_buffer (file->get_istream (), YY_BUF_SIZE));
107 /** pop the inputstack. conceptually this is a destructor, but it
108 does not destruct the Source_file that Includable_lexer::new_input creates. */
110 Includable_lexer::close_input ()
112 include_stack_.pop ();
113 char_count_stack_.pop ();
114 if (verbose_global_b)
115 progress_indication ("]");
116 yy_delete_buffer (yy_current_buffer);
117 #ifdef HAVE_FLEXLEXER_YY_CURRENT_BUFFER
118 yy_current_buffer = 0;
120 if (state_stack_.is_empty ())
122 #ifdef HAVE_FLEXLEXER_YY_CURRENT_BUFFER
123 yy_current_buffer = 0;
129 yy_switch_to_buffer (state_stack_.pop ());
135 Includable_lexer::here_str0 () const
137 if (include_stack_.is_empty ())
139 return include_stack_.top ()->to_str0 () + char_count_stack_.top ();
142 Includable_lexer::~Includable_lexer ()
144 while (!include_stack_.is_empty ())
150 Since we don't create the buffer state from the bytes directly, we
151 don't know about the location of the lexer. Add this as a
154 Includable_lexer::add_lexed_char (int count)
156 char_count_stack_.top () += count;
160 Includable_lexer::get_source_file () const
162 if (include_stack_.is_empty ())
165 return include_stack_.top ();