2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "includable-lexer.hh"
27 #include "file-name.hh"
28 #include "file-path.hh"
29 #include "international.hh"
31 #include "source-file.hh"
36 #define YY_BUF_SIZE 16384
42 #define YYSTATE YY_START
45 /* Flex >= 2.5.29 has include stack; but we don't use that yet. */
46 #if !HAVE_FLEXLEXER_YY_CURRENT_BUFFER
47 #define yy_current_buffer \
48 (yy_buffer_stack != 0 ? yy_buffer_stack[yy_buffer_stack_top] : 0)
51 extern bool relative_includes;
53 Includable_lexer::Includable_lexer ()
55 #if HAVE_FLEXLEXER_YY_CURRENT_BUFFER
56 yy_current_buffer = 0;
60 /** Set the new input file to NAME, remember old file. */
62 Includable_lexer::new_input (string name, Sources *sources)
64 string current_dir = dir_name (main_input_name_);
65 if (relative_includes)
66 current_dir = include_stack_.size () ? dir_name (include_stack_.back ()->name_string ()) : "";
68 Source_file *file = sources->get_file (name, current_dir);
71 string msg = _f ("cannot find file: `%s'", name);
73 msg += _f ("(search path: `%s')",
74 (current_dir.length () ? (current_dir + PATHSEP) : "") + sources->path_->to_string ().c_str ());
75 LexerError (msg.c_str ());
78 file_name_strings_.push_back (file->name_string ());
80 char_count_stack_.push_back (0);
81 if (yy_current_buffer)
82 state_stack_.push_back (yy_current_buffer);
84 debug_output (string (state_stack_.size (), ' ') // indentation!
85 + string ("[") + file->name_string ());
87 include_stack_.push_back (file);
89 /* Ugh. We'd want to create a buffer from the bytes directly.
91 Whoops. The size argument to yy_create_buffer is not the
92 filelength but a BUFFERSIZE. Maybe this is why reading stdin fucks up. */
93 yy_switch_to_buffer (yy_create_buffer (file->get_istream (), YY_BUF_SIZE));
97 Includable_lexer::new_input (string name, string data, Sources *sources)
99 Source_file *file = new Source_file (name, data);
101 file_name_strings_.push_back (name);
103 char_count_stack_.push_back (0);
104 if (yy_current_buffer)
105 state_stack_.push_back (yy_current_buffer);
107 debug_output (string (state_stack_.size (), ' ') // indentation!
108 + string ("[") + name);
109 include_stack_.push_back (file);
111 yy_switch_to_buffer (yy_create_buffer (file->get_istream (), YY_BUF_SIZE));
115 Includable_lexer::add_string_include (string data)
117 pending_string_includes_.push_back (data);
120 /** pop the inputstack. conceptually this is a destructor, but it
121 does not destruct the Source_file that Includable_lexer::new_input
124 Includable_lexer::close_input ()
126 include_stack_.pop_back ();
127 char_count_stack_.pop_back ();
128 debug_output ("]", false);
129 yy_delete_buffer (yy_current_buffer);
130 #if HAVE_FLEXLEXER_YY_CURRENT_BUFFER
131 yy_current_buffer = 0;
133 if (state_stack_.empty ())
135 #if HAVE_FLEXLEXER_YY_CURRENT_BUFFER
136 yy_current_buffer = 0;
140 yy_switch_to_buffer (state_stack_.back ());
141 state_stack_.pop_back ();
146 Includable_lexer::here_str0 () const
148 if (include_stack_.empty ())
150 return include_stack_.back ()->c_str () + char_count_stack_.back ();
153 Includable_lexer::~Includable_lexer ()
155 while (!include_stack_.empty ())
160 Includable_lexer::get_source_file () const
162 if (include_stack_.empty ())
164 return include_stack_.back ();