lilypond-1.3.18
[lilypond.git] / lily / includable-lexer.cc
bloba001d1e403a6e8a23eda6c5ad1137a3deef6ae02
1 /*
2 includable-lexer.cc -- implement Includable_lexer
4 source file of the LilyPond music typesetter
6 (c) 1997--1999 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"
18 #ifndef YY_BUF_SIZE
19 #define YY_BUF_SIZE 16384
20 #endif
22 #ifndef YY_START
23 #define YY_START ((yy_start - 1) / 2)
24 #define YYSTATE YY_START
25 #endif
27 Includable_lexer::Includable_lexer ()
29 yy_current_buffer = 0;
30 allow_includes_b_ = true;
33 /** set the new input to s, remember old file.
35 void
36 Includable_lexer::new_input (String s, Sources * global_sources)
38 if (!allow_includes_b_)
40 LexerError ("include files are disallowed.");
41 return;
44 Source_file * sl = global_sources->get_file_l (s);
45 if (!sl)
47 String msg = _f ("Can't find file: `%s'", s);
48 msg += "\n";
49 msg += _f ("(search path: `%s')", global_sources->path_C_->str ().ch_C());
50 msg += "\n";
51 LexerError (msg.ch_C ());
53 return;
55 filename_str_arr_.push (sl->name_str ());
57 char_count_stack_.push (0);
58 if (yy_current_buffer)
59 state_stack_.push (yy_current_buffer);
60 progress_indication (String ("[") + s);
62 include_stack_.push (sl);
65 ugh. We'd want to create a buffer from the bytes directly.
67 Whoops. The size argument to yy_create_buffer is not the
68 filelength but a BUFFERSIZE. Maybe this is why reading stdin fucks up.
71 yy_switch_to_buffer (yy_create_buffer (sl->istream_l (), YY_BUF_SIZE));
74 void
75 Includable_lexer::new_input (String name, String data, Sources* sources)
77 Source_file* file = new Source_file (name, data);
78 sources->add (file);
79 filename_str_arr_.push (name);
81 char_count_stack_.push (0);
82 if (yy_current_buffer)
83 state_stack_.push (yy_current_buffer);
84 progress_indication (String ("[") + name);
85 include_stack_.push (file);
87 yy_switch_to_buffer (yy_create_buffer (file->istream_l (), YY_BUF_SIZE));
90 /** pop the inputstack. conceptually this is a destructor, but it
91 does not destruct the Source_file that Includable_lexer::new_input creates. */
92 bool
93 Includable_lexer::close_input ()
95 include_stack_.pop ();
96 char_count_stack_.pop ();
97 progress_indication ("]");
98 yy_delete_buffer (yy_current_buffer);
99 yy_current_buffer = 0;
100 if (state_stack_.empty ())
102 yy_current_buffer = 0;
103 return false;
105 else
107 yy_switch_to_buffer (state_stack_.pop ());
108 return true;
112 char const*
113 Includable_lexer::here_ch_C () const
115 if (include_stack_.empty ())
116 return 0;
117 return include_stack_.top ()->ch_C () + char_count_stack_.top ();
120 Includable_lexer::~Includable_lexer ()
122 while (!include_stack_.empty ())
124 close_input ();
128 Since we don't create the buffer state from the bytes directly, we
129 don't know about the location of the lexer. Add this as a
130 YY_USER_ACTION */
131 void
132 Includable_lexer::add_lexed_char (int count)
134 char_count_stack_.top () += count;
137 Source_file*
138 Includable_lexer::source_file_l () const
140 if (include_stack_.empty ())
141 return 0;
142 else
143 return include_stack_.top ();