2 includable-lexer.cc -- implement Includable_lexer
4 source file of the LilyPond music typesetter
6 (c) 1997--2007 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;
46 /** Set the new input file to NAME, remember old file. */
48 Includable_lexer::new_input (string name, Sources *sources)
50 Source_file *file = sources->get_file (&name);
53 string msg = _f ("cannot find file: `%s'", name);
55 msg += _f ("(search path: `%s')",
56 sources->path_->to_string ().c_str ());
57 LexerError (msg.c_str ());
60 file_name_strings_.push_back (file->name_string ());
62 char_count_stack_.push_back (0);
63 if (yy_current_buffer)
64 state_stack_.push_back (yy_current_buffer);
66 if (be_verbose_global)
67 progress_indication (string ("[") + name);
69 include_stack_.push_back (file);
71 /* Ugh. We'd want to create a buffer from the bytes directly.
73 Whoops. The size argument to yy_create_buffer is not the
74 filelength but a BUFFERSIZE. Maybe this is why reading stdin fucks up. */
75 yy_switch_to_buffer (yy_create_buffer (file->get_istream (), YY_BUF_SIZE));
79 Includable_lexer::new_input (string name, string data, Sources *sources)
81 Source_file *file = new Source_file (name, data);
83 file_name_strings_.push_back (name);
85 char_count_stack_.push_back (0);
86 if (yy_current_buffer)
87 state_stack_.push_back (yy_current_buffer);
89 if (be_verbose_global)
90 progress_indication (string ("[") + name);
91 include_stack_.push_back (file);
93 yy_switch_to_buffer (yy_create_buffer (file->get_istream (), YY_BUF_SIZE));
96 /** pop the inputstack. conceptually this is a destructor, but it
97 does not destruct the Source_file that Includable_lexer::new_input
100 Includable_lexer::close_input ()
102 include_stack_.pop_back ();
103 char_count_stack_.pop_back ();
104 if (be_verbose_global)
105 progress_indication ("]");
106 yy_delete_buffer (yy_current_buffer);
107 #if HAVE_FLEXLEXER_YY_CURRENT_BUFFER
108 yy_current_buffer = 0;
110 if (state_stack_.empty ())
112 #if HAVE_FLEXLEXER_YY_CURRENT_BUFFER
113 yy_current_buffer = 0;
117 yy_switch_to_buffer (state_stack_.back ());
118 state_stack_.pop_back ();
123 Includable_lexer::here_str0 () const
125 if (include_stack_.empty ())
127 return include_stack_.back ()->c_str () + char_count_stack_.back ();
130 Includable_lexer::~Includable_lexer ()
132 while (!include_stack_.empty ())
137 Includable_lexer::get_source_file () const
139 if (include_stack_.empty ())
141 return include_stack_.back ();