]> git.donarmstrong.com Git - lilypond.git/blob - lily/includable-lexer.cc
Merge branch 'lilypond/translation' of ssh://git.sv.gnu.org/srv/git/lilypond into...
[lilypond.git] / lily / includable-lexer.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includable-lexer.hh"
21
22 #include <sstream>
23 using namespace std;
24
25 #include "config.hh"
26
27 #include "file-name.hh"
28 #include "file-path.hh"
29 #include "international.hh"
30 #include "main.hh"
31 #include "source-file.hh"
32 #include "sources.hh"
33 #include "warn.hh"
34
35 #ifndef YY_BUF_SIZE
36 #define YY_BUF_SIZE 16384
37 #endif
38
39 #ifndef YY_START
40 #define YY_START                                \
41   ((yy_start - 1) / 2)
42 #define YYSTATE YY_START
43 #endif
44
45 /* Flex >= 2.5.29 has include stack; but we don't use that yet.  */
46 #if !HAVE_FLEXLEXER_YY_CURRENT_BUFFER
47 #define yy_current_buffer                                               \
48   (yy_buffer_stack != 0 ? yy_buffer_stack[yy_buffer_stack_top] : 0)
49 #endif
50
51 extern bool relative_includes;
52
53 Includable_lexer::Includable_lexer ()
54 {
55 #if HAVE_FLEXLEXER_YY_CURRENT_BUFFER
56   yy_current_buffer = 0;
57 #endif
58 }
59
60 /** Set the new input file to NAME, remember old file.  */
61 void
62 Includable_lexer::new_input (string name, Sources *sources)
63 {
64   string current_dir = dir_name (main_input_name_);
65   if (relative_includes)
66     current_dir = include_stack_.size () ? dir_name (include_stack_.back ()->name_string ()) : "";
67
68   Source_file *file = sources->get_file (name, current_dir);
69   if (!file)
70     {
71       string msg = _f ("cannot find file: `%s'", name);
72       msg += "\n";
73       msg += _f ("(search path: `%s')",
74                  (current_dir.length () ? (current_dir + PATHSEP) : "") + sources->path_->to_string ().c_str ());
75       LexerError (msg.c_str ());
76       return;
77     }
78   file_name_strings_.push_back (file->name_string ());
79
80   char_count_stack_.push_back (0);
81   if (yy_current_buffer)
82     state_stack_.push_back (yy_current_buffer);
83
84   if (be_verbose_global)
85     {
86       string spaces = "";
87       for (size_t i = 0; i < state_stack_.size (); i++)
88         spaces += " ";
89       progress_indication (string ("\n") + spaces + string ("[") + file->name_string ());
90     }
91
92   include_stack_.push_back (file);
93
94   /* Ugh. We'd want to create a buffer from the bytes directly.
95
96   Whoops.  The size argument to yy_create_buffer is not the
97   filelength but a BUFFERSIZE.  Maybe this is why reading stdin fucks up.  */
98   yy_switch_to_buffer (yy_create_buffer (file->get_istream (), YY_BUF_SIZE));
99 }
100
101 void
102 Includable_lexer::new_input (string name, string data, Sources *sources)
103 {
104   Source_file *file = new Source_file (name, data);
105   sources->add (file);
106   file_name_strings_.push_back (name);
107
108   char_count_stack_.push_back (0);
109   if (yy_current_buffer)
110     state_stack_.push_back (yy_current_buffer);
111
112   if (be_verbose_global)
113     {
114       string spaces = "";
115       for (size_t i = 0; i < state_stack_.size (); i++)
116         spaces += " ";
117       progress_indication (string ("\n") + spaces + string ("[") + name);
118     }
119   include_stack_.push_back (file);
120
121   yy_switch_to_buffer (yy_create_buffer (file->get_istream (), YY_BUF_SIZE));
122 }
123
124 void
125 Includable_lexer::add_string_include (string data)
126 {
127   pending_string_includes_.push_back (data);
128 }
129
130 /** pop the inputstack.  conceptually this is a destructor, but it
131     does not destruct the Source_file that Includable_lexer::new_input
132     creates.  */
133 bool
134 Includable_lexer::close_input ()
135 {
136   include_stack_.pop_back ();
137   char_count_stack_.pop_back ();
138   if (be_verbose_global)
139     progress_indication ("]");
140   yy_delete_buffer (yy_current_buffer);
141 #if HAVE_FLEXLEXER_YY_CURRENT_BUFFER
142   yy_current_buffer = 0;
143 #endif
144   if (state_stack_.empty ())
145     {
146 #if HAVE_FLEXLEXER_YY_CURRENT_BUFFER
147       yy_current_buffer = 0;
148 #endif
149       return false;
150     }
151   yy_switch_to_buffer (state_stack_.back ());
152   state_stack_.pop_back ();
153   return true;
154 }
155
156 char const *
157 Includable_lexer::here_str0 () const
158 {
159   if (include_stack_.empty ())
160     return 0;
161   return include_stack_.back ()->c_str () + char_count_stack_.back ();
162 }
163
164 Includable_lexer::~Includable_lexer ()
165 {
166   while (!include_stack_.empty ())
167     close_input ();
168 }
169
170 Source_file *
171 Includable_lexer::get_source_file () const
172 {
173   if (include_stack_.empty ())
174     return 0;
175   return include_stack_.back ();
176 }