lilypond-1.4.2
[lilypond.git] / lily / includable-lexer.cc
blob7da0ba497431b49657d8f0e503138d0567f8704f
1 /*
2 includable-lexer.cc -- implement Includable_lexer
4 source file of the LilyPond music typesetter
6 (c) 1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
9 #include <strstream.h>
12 #include "file-path.hh"
13 #include "includable-lexer.hh"
14 #include "source-file.hh"
15 #include "source.hh"
16 #include "debug.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 Includable_lexer::Includable_lexer ()
31 yy_current_buffer = 0;
32 allow_includes_b_ = true;
35 /** set the new input to s, remember old file.
37 void
38 Includable_lexer::new_input (String s, Sources * global_sources)
40 if (!allow_includes_b_)
42 LexerError ("include files are disallowed.");
43 return;
46 Source_file * sl = global_sources->get_file_l (s);
47 if (!sl)
49 String msg = _f ("can't find file: `%s'", s);
50 msg += "\n";
51 msg += _f ("(search path: `%s')", global_sources->path_C_->str ().ch_C ());
52 msg += "\n";
53 LexerError (msg.ch_C ());
55 return;
57 filename_str_arr_.push (sl->name_str ());
59 char_count_stack_.push (0);
60 if (yy_current_buffer)
61 state_stack_.push (yy_current_buffer);
63 if (verbose_global_b)
64 progress_indication (String ("[") + s);
66 include_stack_.push (sl);
69 ugh. We'd want to create a buffer from the bytes directly.
71 Whoops. The size argument to yy_create_buffer is not the
72 filelength but a BUFFERSIZE. Maybe this is why reading stdin fucks up.
75 yy_switch_to_buffer (yy_create_buffer (sl->istream_l (), YY_BUF_SIZE));
78 void
79 Includable_lexer::new_input (String name, String data, Sources* sources)
81 Source_file* file = new Source_file (name, data);
82 sources->add (file);
83 filename_str_arr_.push (name);
85 char_count_stack_.push (0);
86 if (yy_current_buffer)
87 state_stack_.push (yy_current_buffer);
89 if (verbose_global_b)
90 progress_indication (String ("[") + name);
91 include_stack_.push (file);
93 yy_switch_to_buffer (yy_create_buffer (file->istream_l (), 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 creates. */
98 bool
99 Includable_lexer::close_input ()
101 include_stack_.pop ();
102 char_count_stack_.pop ();
103 if (verbose_global_b)
104 progress_indication ("]");
105 yy_delete_buffer (yy_current_buffer);
106 yy_current_buffer = 0;
107 if (state_stack_.empty ())
109 yy_current_buffer = 0;
110 return false;
112 else
114 yy_switch_to_buffer (state_stack_.pop ());
115 return true;
119 char const*
120 Includable_lexer::here_ch_C () const
122 if (include_stack_.empty ())
123 return 0;
124 return include_stack_.top ()->ch_C () + char_count_stack_.top ();
127 Includable_lexer::~Includable_lexer ()
129 while (!include_stack_.empty ())
131 close_input ();
135 Since we don't create the buffer state from the bytes directly, we
136 don't know about the location of the lexer. Add this as a
137 YY_USER_ACTION */
138 void
139 Includable_lexer::add_lexed_char (int count)
141 char_count_stack_.top () += count;
144 Source_file*
145 Includable_lexer::source_file_l () const
147 if (include_stack_.empty ())
148 return 0;
149 else
150 return include_stack_.top ();