(all-grob-descriptions): remove gap from
[lilypond.git] / lily / includable-lexer.cc
blobaab8fd9fcd850446fae03fb0f7f4469a12608e30
1 /*
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>
7 */
9 #include <sstream>
10 #include "config.h"
12 #include "includable-lexer.hh"
13 #include "file-path.hh"
14 #include "source-file.hh"
15 #include "source.hh"
16 #include "warn.hh"
17 #include "main.hh"
19 #ifndef YY_BUF_SIZE
20 #define YY_BUF_SIZE 16384
21 #endif
23 #ifndef YY_START
24 #define YY_START\
25 ((yy_start - 1) / 2)
26 #define YYSTATE YY_START
27 #endif
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)
33 #endif
35 Includable_lexer::Includable_lexer ()
37 #ifdef HAVE_FLEXLEXER_YY_CURRENT_BUFFER
38 yy_current_buffer = 0;
39 #endif
40 allow_includes_b_ = true;
43 /** set the new input to s, remember old file.
45 void
46 Includable_lexer::new_input (String s, Sources * global_sources)
48 if (!allow_includes_b_)
50 LexerError (_ ("include files are not allowed").to_str0 ());
51 return;
54 Source_file * sl = global_sources->get_file (s);
55 if (!sl)
57 String msg = _f ("can't find file: `%s'", s);
58 msg += "\n";
59 msg += _f ("(search path: `%s')", global_sources->path_C_->to_string ().to_str0 ());
60 msg += "\n";
61 LexerError (msg.to_str0 ());
63 return;
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);
71 if (verbose_global_b)
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));
87 Unused.
89 void
90 Includable_lexer::new_input (String name, String data, Sources* sources)
92 Source_file* file = new Source_file (name, data);
93 sources->add (file);
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. */
109 bool
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;
119 #endif
120 if (state_stack_.is_empty ())
122 #ifdef HAVE_FLEXLEXER_YY_CURRENT_BUFFER
123 yy_current_buffer = 0;
124 #endif
125 return false;
127 else
129 yy_switch_to_buffer (state_stack_.pop ());
130 return true;
134 char const*
135 Includable_lexer::here_str0 () const
137 if (include_stack_.is_empty ())
138 return 0;
139 return include_stack_.top ()->to_str0 () + char_count_stack_.top ();
142 Includable_lexer::~Includable_lexer ()
144 while (!include_stack_.is_empty ())
146 close_input ();
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
152 YY_USER_ACTION */
153 void
154 Includable_lexer::add_lexed_char (int count)
156 char_count_stack_.top () += count;
159 Source_file*
160 Includable_lexer::get_source_file () const
162 if (include_stack_.is_empty ())
163 return 0;
164 else
165 return include_stack_.top ();