]> git.donarmstrong.com Git - lilypond.git/blob - lily/includable-lexer.cc
587eefbba1105b209c8e55114e581d04f0119ef1
[lilypond.git] / lily / includable-lexer.cc
1 /*
2   includable-lexer.cc -- implement Includable_lexer
3
4   source file of the LilyPond music typesetter
5
6   (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <sstream>
10 #include "config.h"
11
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"
18
19 #ifndef YY_BUF_SIZE
20 #define YY_BUF_SIZE 16384
21 #endif
22
23 #ifndef YY_START
24 #define YY_START\
25  ((yy_start - 1) / 2)
26 #define YYSTATE YY_START
27 #endif
28
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
34
35 Includable_lexer::Includable_lexer ()
36 {
37 #ifdef HAVE_FLEXLEXER_YY_CURRENT_BUFFER
38   yy_current_buffer = 0;
39 #endif
40   allow_includes_b_ = true;
41 }
42
43 /** set the  new input to s, remember old file.
44 */
45 void
46 Includable_lexer::new_input (String s, Sources  * global_sources)
47 {
48   if (!allow_includes_b_)
49     {
50       LexerError (_ ("include files are not allowed").to_str0 ());
51       return;
52     }
53   
54   Source_file *sl = global_sources->get_file (s);
55   if (!sl)
56     {
57       String msg = _f ("can't find file: `%s'", s);
58       msg += "\n";
59       msg += _f ("(search path: `%s')",
60                  global_sources->path_->to_string ().to_str0 ());
61       msg += "\n";
62       LexerError (msg.to_str0 ());
63       return;
64     }
65   filename_strings_.push (sl->name_string ());
66
67   char_count_stack_.push (0);
68   if (yy_current_buffer)
69     state_stack_.push (yy_current_buffer);
70
71   if (verbose_global_b)
72     progress_indication (String ("[") + s);
73         
74   include_stack_.push (sl);
75
76   /*
77     ugh. We'd want to create a buffer from the bytes directly.
78
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.
81
82   */
83   yy_switch_to_buffer (yy_create_buffer (sl->get_istream (), YY_BUF_SIZE));
84 }
85
86 /*
87   Unused.
88  */
89 void
90 Includable_lexer::new_input (String name, String data, Sources* sources)
91 {
92   Source_file* file = new Source_file (name, data);
93   sources->add (file);
94   filename_strings_.push (name);
95
96   char_count_stack_.push (0);
97   if (yy_current_buffer)
98     state_stack_.push (yy_current_buffer);
99
100   if (verbose_global_b)
101     progress_indication (String ("[") + name);
102   include_stack_.push (file);
103
104   yy_switch_to_buffer (yy_create_buffer (file->get_istream (), YY_BUF_SIZE));
105 }
106
107 /** pop the inputstack.  conceptually this is a destructor, but it
108   does not destruct the Source_file that Includable_lexer::new_input
109   creates.  */
110 bool
111 Includable_lexer::close_input ()
112 {
113   include_stack_.pop ();
114   char_count_stack_.pop ();
115   if (verbose_global_b)
116     progress_indication ("]");
117   yy_delete_buffer (yy_current_buffer);
118 #ifdef HAVE_FLEXLEXER_YY_CURRENT_BUFFER  
119   yy_current_buffer = 0;
120 #endif  
121   if (state_stack_.is_empty ())
122     {
123 #ifdef HAVE_FLEXLEXER_YY_CURRENT_BUFFER  
124       yy_current_buffer = 0;
125 #endif  
126       return false;
127     }
128   yy_switch_to_buffer (state_stack_.pop ());
129   return true;
130 }
131
132 char const*
133 Includable_lexer::here_str0 () const
134 {
135   if (include_stack_.is_empty ())
136     return 0;
137   return include_stack_.top ()->to_str0 () + char_count_stack_.top ();
138 }
139
140 Includable_lexer::~Includable_lexer ()
141 {
142   while (!include_stack_.is_empty ())
143     {
144       close_input ();
145     }
146 }
147 /**
148   Since we don't create the buffer state from the bytes directly, we
149   don't know about the location of the lexer. Add this as a
150   YY_USER_ACTION */
151 void
152 Includable_lexer::add_lexed_char (int count)
153 {
154   char_count_stack_.top () += count;
155 }
156
157 Source_file*
158 Includable_lexer::get_source_file () const
159 {
160   if (include_stack_.is_empty ())
161     return 0;
162   else
163     return include_stack_.top ();
164 }