]> git.donarmstrong.com Git - lilypond.git/blob - lily/includable-lexer.cc
* lily/my-lily-lexer.cc (set_encoding): New method.
[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 creates.  */
109 bool
110 Includable_lexer::close_input ()
111 {
112   include_stack_.pop ();
113   char_count_stack_.pop ();
114   if (verbose_global_b)
115     progress_indication ("]");
116   yy_delete_buffer (yy_current_buffer);
117 #ifdef HAVE_FLEXLEXER_YY_CURRENT_BUFFER  
118   yy_current_buffer = 0;
119 #endif  
120   if (state_stack_.is_empty ())
121     {
122 #ifdef HAVE_FLEXLEXER_YY_CURRENT_BUFFER  
123       yy_current_buffer = 0;
124 #endif  
125       return false;
126     }
127   else
128     {
129       yy_switch_to_buffer (state_stack_.pop ());
130       return true;
131     }
132 }
133
134 char const*
135 Includable_lexer::here_str0 () const
136 {
137   if (include_stack_.is_empty ())
138     return 0;
139   return include_stack_.top ()->to_str0 () + char_count_stack_.top ();
140 }
141
142 Includable_lexer::~Includable_lexer ()
143 {
144   while (!include_stack_.is_empty ())
145     {
146       close_input ();
147     }
148 }
149 /**
150   Since we don't create the buffer state from the bytes directly, we
151   don't know about the location of the lexer. Add this as a
152   YY_USER_ACTION */
153 void
154 Includable_lexer::add_lexed_char (int count)
155 {
156   char_count_stack_.top () += count;
157 }
158
159 Source_file*
160 Includable_lexer::get_source_file () const
161 {
162   if (include_stack_.is_empty ())
163     return 0;
164   else
165     return include_stack_.top ();
166 }