2 includable-lexer.cc -- implement Includable_lexer
4 source file of the LilyPond music typesetter
6 (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "includable-lexer.hh"
16 #include "file-path.hh"
17 #include "international.hh"
19 #include "source-file.hh"
24 #define YY_BUF_SIZE 16384
30 #define YYSTATE YY_START
33 /* Flex >= 2.5.29 has include stack; but we don't use that yet. */
34 #if !HAVE_FLEXLEXER_YY_CURRENT_BUFFER
35 #define yy_current_buffer \
36 (yy_buffer_stack != 0 ? yy_buffer_stack[yy_buffer_stack_top] : 0)
39 Includable_lexer::Includable_lexer ()
41 #if HAVE_FLEXLEXER_YY_CURRENT_BUFFER
42 yy_current_buffer = 0;
44 allow_includes_b_ = true;
47 /** Set the new input file to NAME, remember old file. */
49 Includable_lexer::new_input (std::string name, Sources *sources)
51 if (!allow_includes_b_)
53 LexerError (_ ("include files are not allowed in safe mode").c_str ());
57 Source_file *file = sources->get_file (name);
60 std::string msg = _f ("can't find file: `%s'", name);
62 msg += _f ("(search path: `%s')",
63 sources->path_->to_string ().c_str ());
64 LexerError (msg.c_str ());
67 file_name_strings_.push (file->name_string ());
69 char_count_stack_.push (0);
70 if (yy_current_buffer)
71 state_stack_.push (yy_current_buffer);
73 if (be_verbose_global)
74 progress_indication (std::string ("[") + name);
76 include_stack_.push (file);
78 /* Ugh. We'd want to create a buffer from the bytes directly.
80 Whoops. The size argument to yy_create_buffer is not the
81 filelength but a BUFFERSIZE. Maybe this is why reading stdin fucks up. */
82 yy_switch_to_buffer (yy_create_buffer (file->get_istream (), YY_BUF_SIZE));
86 Includable_lexer::new_input (std::string name, std::string data, Sources *sources)
88 Source_file *file = new Source_file (name, data);
90 file_name_strings_.push (name);
92 char_count_stack_.push (0);
93 if (yy_current_buffer)
94 state_stack_.push (yy_current_buffer);
96 if (be_verbose_global)
97 progress_indication (std::string ("[") + name);
98 include_stack_.push (file);
100 yy_switch_to_buffer (yy_create_buffer (file->get_istream (), YY_BUF_SIZE));
103 /** pop the inputstack. conceptually this is a destructor, but it
104 does not destruct the Source_file that Includable_lexer::new_input
107 Includable_lexer::close_input ()
109 include_stack_.pop ();
110 char_count_stack_.pop ();
111 if (be_verbose_global)
112 progress_indication ("]");
113 yy_delete_buffer (yy_current_buffer);
114 #if HAVE_FLEXLEXER_YY_CURRENT_BUFFER
115 yy_current_buffer = 0;
117 if (state_stack_.is_empty ())
119 #if HAVE_FLEXLEXER_YY_CURRENT_BUFFER
120 yy_current_buffer = 0;
124 yy_switch_to_buffer (state_stack_.pop ());
129 Includable_lexer::here_str0 () const
131 if (include_stack_.is_empty ())
133 return include_stack_.top ()->c_str () + char_count_stack_.top ();
136 Includable_lexer::~Includable_lexer ()
138 while (!include_stack_.is_empty ())
143 Includable_lexer::get_source_file () const
145 if (include_stack_.is_empty ())
147 return include_stack_.top ();