2 includable-lexer.cc -- implement Includable_lexer
4 source file of the LilyPond music typesetter
6 (c) 1997--1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
11 #include "includable-lexer.hh"
12 #include "source-file.hh"
17 #define YY_BUF_SIZE 16384
21 #define YY_START ((yy_start - 1) / 2)
22 #define YYSTATE YY_START
25 Includable_lexer::Includable_lexer ()
27 yy_current_buffer = 0;
30 /** set the new input to s, remember old file.
33 Includable_lexer::new_input (String s, Sources * global_sources)
35 Source_file * sl = global_sources->get_file_l (s);
38 String msg = _f ("can't find file: `%s\'", s);
39 LexerError (msg.ch_C ());
42 filename_str_arr_.push (sl->name_str ());
44 char_count_stack_.push (0);
45 if (yy_current_buffer)
46 state_stack_.push (yy_current_buffer);
47 *mlog << "[" << s<<flush;
48 include_stack_.push (sl);
51 ugh. We'd want to create a buffer from the bytes directly.
53 Whoops. The size argument to yy_create_buffer is not the
54 filelength but a BUFFERSIZE. Maybe this is why reading stdin fucks up.
57 yy_switch_to_buffer (yy_create_buffer (sl->istream_l (), YY_BUF_SIZE));
61 Includable_lexer::new_input (String name, String data, Sources* sources)
63 Source_file* file = new Source_file (name, data);
65 filename_str_arr_.push (name);
67 char_count_stack_.push (0);
68 if (yy_current_buffer)
69 state_stack_.push (yy_current_buffer);
70 *mlog << "[" << name << flush;
71 include_stack_.push (file);
73 yy_switch_to_buffer (yy_create_buffer (file->istream_l (), YY_BUF_SIZE));
76 /** pop the inputstack. conceptually this is a destructor, but it
77 does not destruct the Source_file that Includable_lexer::new_input creates. */
79 Includable_lexer::close_input ()
81 include_stack_.pop ();
82 char_count_stack_.pop ();
84 yy_delete_buffer (yy_current_buffer);
85 yy_current_buffer = 0;
86 if (state_stack_.empty ())
88 yy_current_buffer = 0;
93 yy_switch_to_buffer (state_stack_.pop ());
99 Includable_lexer::here_ch_C ()
101 if (include_stack_.empty ())
103 return include_stack_.top ()->ch_C () + char_count_stack_.top ();
106 Includable_lexer::~Includable_lexer ()
108 while (!include_stack_.empty ())
114 Since we don't create the buffer state from the bytes directly, we
115 don't know about the location of the lexer. Add this as a
118 Includable_lexer::add_lexed_char (int count)
120 char_count_stack_.top () += count;
124 Includable_lexer::source_file_l () const
126 if (include_stack_.empty ())
129 return include_stack_.top ();