* scm/beam.scm (check-slope-callbacks): check sign of slope.
[lilypond.git] / lily / includable-lexer.cc
blobf6d277440021b54aec82b77841ca368429bc1cb5
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>
11 #include "config.h"
13 #include "includable-lexer.hh"
14 #include "file-path.hh"
15 #include "source-file.hh"
16 #include "source.hh"
17 #include "warn.hh"
18 #include "main.hh"
20 #ifndef YY_BUF_SIZE
21 #define YY_BUF_SIZE 16384
22 #endif
24 #ifndef YY_START
25 #define YY_START\
26 ((yy_start - 1) / 2)
27 #define YYSTATE YY_START
28 #endif
30 /* Flex >= 2.5.29 has include stack; but we don't use that yet. */
31 #if !HAVE_FLEXLEXER_YY_CURRENT_BUFFER
32 #define yy_current_buffer \
33 (yy_buffer_stack != 0 ? yy_buffer_stack[yy_buffer_stack_top] : 0)
34 #endif
36 Includable_lexer::Includable_lexer ()
38 #if HAVE_FLEXLEXER_YY_CURRENT_BUFFER
39 yy_current_buffer = 0;
40 #endif
41 allow_includes_b_ = true;
44 /** Set the new input file to NAME, remember old file. */
45 void
46 Includable_lexer::new_input (String name, Sources *sources)
48 if (!allow_includes_b_)
50 LexerError (_ ("include files are not allowed").to_str0 ());
51 return;
54 Source_file *file = sources->get_file (name);
55 if (!file)
57 String msg = _f ("can't find file: `%s'", name);
58 msg += "\n";
59 msg += _f ("(search path: `%s')",
60 sources->path_->to_string ().to_str0 ());
61 msg += "\n";
62 LexerError (msg.to_str0 ());
63 return;
65 filename_strings_.push (file->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 ("[") + name);
74 include_stack_.push (file);
76 /* Ugh. We'd want to create a buffer from the bytes directly.
78 Whoops. The size argument to yy_create_buffer is not the
79 filelength but a BUFFERSIZE. Maybe this is why reading stdin fucks up. */
80 yy_switch_to_buffer (yy_create_buffer (file->get_istream (), YY_BUF_SIZE));
83 void
84 Includable_lexer::new_input (String name, String data, Sources *sources)
86 Source_file *file = new Source_file (name, data);
87 sources->add (file);
88 filename_strings_.push (name);
90 char_count_stack_.push (0);
91 if (yy_current_buffer)
92 state_stack_.push (yy_current_buffer);
94 if (verbose_global_b)
95 progress_indication (String ("[") + name);
96 include_stack_.push (file);
98 yy_switch_to_buffer (yy_create_buffer (file->get_istream (), YY_BUF_SIZE));
101 /** pop the inputstack. conceptually this is a destructor, but it
102 does not destruct the Source_file that Includable_lexer::new_input
103 creates. */
104 bool
105 Includable_lexer::close_input ()
107 include_stack_.pop ();
108 char_count_stack_.pop ();
109 if (verbose_global_b)
110 progress_indication ("]");
111 yy_delete_buffer (yy_current_buffer);
112 #if HAVE_FLEXLEXER_YY_CURRENT_BUFFER
113 yy_current_buffer = 0;
114 #endif
115 if (state_stack_.is_empty ())
117 #if HAVE_FLEXLEXER_YY_CURRENT_BUFFER
118 yy_current_buffer = 0;
119 #endif
120 return false;
122 yy_switch_to_buffer (state_stack_.pop ());
123 return true;
126 char const*
127 Includable_lexer::here_str0 () const
129 if (include_stack_.is_empty ())
130 return 0;
131 return include_stack_.top ()->to_str0 () + char_count_stack_.top ();
134 Includable_lexer::~Includable_lexer ()
136 while (!include_stack_.is_empty ())
138 close_input ();
142 Since we don't create the buffer state from the bytes directly, we
143 don't know about the location of the lexer. Add this as a
144 YY_USER_ACTION */
145 void
146 Includable_lexer::add_lexed_char (int count)
148 char_count_stack_.top () += count;
151 Source_file*
152 Includable_lexer::get_source_file () const
154 if (include_stack_.is_empty ())
155 return 0;
156 else
157 return include_stack_.top ();