* lexer-gcc-3.1.sh: Remove.
[lilypond/patrick.git] / lily / includable-lexer.cc
blobcb851575ba5740edd4bcf46c1f7176aad4b34a87
1 /*
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>
7 */
9 #include "includable-lexer.hh"
11 #include <sstream>
12 using namespace std;
14 #include "config.hh"
16 #include "file-path.hh"
17 #include "international.hh"
18 #include "main.hh"
19 #include "source-file.hh"
20 #include "source.hh"
21 #include "warn.hh"
23 #ifndef YY_BUF_SIZE
24 #define YY_BUF_SIZE 16384
25 #endif
27 #ifndef YY_START
28 #define YY_START \
29 ((yy_start - 1) / 2)
30 #define YYSTATE YY_START
31 #endif
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)
37 #endif
39 Includable_lexer::Includable_lexer ()
41 #if HAVE_FLEXLEXER_YY_CURRENT_BUFFER
42 yy_current_buffer = 0;
43 #endif
44 allow_includes_b_ = true;
47 /** Set the new input file to NAME, remember old file. */
48 void
49 Includable_lexer::new_input (string name, Sources *sources)
51 if (!allow_includes_b_)
53 LexerError (_ ("include files are not allowed in safe mode").c_str ());
54 return;
57 Source_file *file = sources->get_file (&name);
58 if (!file)
60 string msg = _f ("can't find file: `%s'", name);
61 msg += "\n";
62 msg += _f ("(search path: `%s')",
63 sources->path_->to_string ().c_str ());
64 LexerError (msg.c_str ());
65 return;
67 file_name_strings_.push_back (file->name_string ());
69 char_count_stack_.push_back (0);
70 if (yy_current_buffer)
71 state_stack_.push_back (yy_current_buffer);
73 if (be_verbose_global)
74 progress_indication (string ("[") + name);
76 include_stack_.push_back (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));
85 void
86 Includable_lexer::new_input (string name, string data, Sources *sources)
88 Source_file *file = new Source_file (name, data);
89 sources->add (file);
90 file_name_strings_.push_back (name);
92 char_count_stack_.push_back (0);
93 if (yy_current_buffer)
94 state_stack_.push_back (yy_current_buffer);
96 if (be_verbose_global)
97 progress_indication (string ("[") + name);
98 include_stack_.push_back (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
105 creates. */
106 bool
107 Includable_lexer::close_input ()
109 include_stack_.pop_back ();
110 char_count_stack_.pop_back ();
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;
116 #endif
117 if (state_stack_.empty ())
119 #if HAVE_FLEXLEXER_YY_CURRENT_BUFFER
120 yy_current_buffer = 0;
121 #endif
122 return false;
124 yy_switch_to_buffer (state_stack_.back ());
125 state_stack_.pop_back ();
126 return true;
129 char const *
130 Includable_lexer::here_str0 () const
132 if (include_stack_.empty ())
133 return 0;
134 return include_stack_.back ()->c_str () + char_count_stack_.back ();
137 Includable_lexer::~Includable_lexer ()
139 while (!include_stack_.empty ())
140 close_input ();
143 Source_file *
144 Includable_lexer::get_source_file () const
146 if (include_stack_.empty ())
147 return 0;
148 return include_stack_.back ();