]> git.donarmstrong.com Git - lilypond.git/blob - lily/includable-lexer.cc
* flower/international.cc:
[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--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "includable-lexer.hh"
10
11 #include <sstream>
12 using namespace std;
13
14 #include "config.hh"
15 #include "file-path.hh"
16 #include "source-file.hh"
17 #include "source.hh"
18 #include "warn.hh"
19 #include "main.hh"
20
21 #ifndef YY_BUF_SIZE
22 #define YY_BUF_SIZE 16384
23 #endif
24
25 #ifndef YY_START
26 #define YY_START                                \
27   ((yy_start - 1) / 2)
28 #define YYSTATE YY_START
29 #endif
30
31 /* Flex >= 2.5.29 has include stack; but we don't use that yet.  */
32 #if !HAVE_FLEXLEXER_YY_CURRENT_BUFFER
33 #define yy_current_buffer                                               \
34   (yy_buffer_stack != 0 ? yy_buffer_stack[yy_buffer_stack_top] : 0)
35 #endif
36
37 Includable_lexer::Includable_lexer ()
38 {
39 #if HAVE_FLEXLEXER_YY_CURRENT_BUFFER
40   yy_current_buffer = 0;
41 #endif
42   allow_includes_b_ = true;
43 }
44
45 /** Set the new input file to NAME, remember old file.  */
46 void
47 Includable_lexer::new_input (String name, Sources *sources)
48 {
49   if (!allow_includes_b_)
50     {
51       LexerError (_ ("include files are not allowed in safe mode").c_str ());
52       return;
53     }
54
55   Source_file *file = sources->get_file (name);
56   if (!file)
57     {
58       String msg = _f ("can't find file: `%s'", name);
59       msg += "\n";
60       msg += _f ("(search path: `%s')",
61                  sources->path_->to_string ().c_str ());
62       LexerError (msg.to_str0 ());
63       return;
64     }
65   file_name_strings_.push (file->name_string ());
66
67   char_count_stack_.push (0);
68   if (yy_current_buffer)
69     state_stack_.push (yy_current_buffer);
70
71   if (be_verbose_global)
72     progress_indication (String ("[") + name);
73
74   include_stack_.push (file);
75
76   /* Ugh. We'd want to create a buffer from the bytes directly.
77
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));
81 }
82
83 void
84 Includable_lexer::new_input (String name, String data, Sources *sources)
85 {
86   Source_file *file = new Source_file (name, data);
87   sources->add (file);
88   file_name_strings_.push (name);
89
90   char_count_stack_.push (0);
91   if (yy_current_buffer)
92     state_stack_.push (yy_current_buffer);
93
94   if (be_verbose_global)
95     progress_indication (String ("[") + name);
96   include_stack_.push (file);
97
98   yy_switch_to_buffer (yy_create_buffer (file->get_istream (), YY_BUF_SIZE));
99 }
100
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 ()
106 {
107   include_stack_.pop ();
108   char_count_stack_.pop ();
109   if (be_verbose_global)
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 ())
116     {
117 #if HAVE_FLEXLEXER_YY_CURRENT_BUFFER
118       yy_current_buffer = 0;
119 #endif
120       return false;
121     }
122   yy_switch_to_buffer (state_stack_.pop ());
123   return true;
124 }
125
126 char const *
127 Includable_lexer::here_str0 () const
128 {
129   if (include_stack_.is_empty ())
130     return 0;
131   return include_stack_.top ()->to_str0 () + char_count_stack_.top ();
132 }
133
134 Includable_lexer::~Includable_lexer ()
135 {
136   while (!include_stack_.is_empty ())
137     close_input ();
138 }
139
140 Source_file *
141 Includable_lexer::get_source_file () const
142 {
143   if (include_stack_.is_empty ())
144     return 0;
145   return include_stack_.top ();
146 }