]> git.donarmstrong.com Git - lilypond.git/blob - lily/includable-lexer.cc
Revert "Issue 4550 (2/2) Avoid "using namespace std;" in included files"
[lilypond.git] / lily / includable-lexer.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 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 using std::string;
36
37 #ifndef YY_BUF_SIZE
38 #define YY_BUF_SIZE 16384
39 #endif
40
41 #ifndef YY_START
42 #define YY_START                                \
43   ((yy_start - 1) / 2)
44 #define YYSTATE YY_START
45 #endif
46
47 /* Flex >= 2.5.29 has include stack; but we don't use that yet.  */
48 #if !HAVE_FLEXLEXER_YY_CURRENT_BUFFER
49 #define yy_current_buffer                                               \
50   (yy_buffer_stack != 0 ? yy_buffer_stack[yy_buffer_stack_top] : 0)
51 #endif
52
53 extern bool relative_includes;
54
55 Includable_lexer::Includable_lexer ()
56 {
57 #if HAVE_FLEXLEXER_YY_CURRENT_BUFFER
58   yy_current_buffer = 0;
59 #endif
60 }
61
62 /** Set the new input file to NAME, remember old file.  */
63 void
64 Includable_lexer::new_input (const string &name, Sources *sources)
65 {
66   string current_dir = dir_name (main_input_name_);
67   if (relative_includes)
68     current_dir = include_stack_.size () ? dir_name (include_stack_.back ()->name_string ()) : "";
69
70   Source_file *file = sources->get_file (name, current_dir);
71   if (!file)
72     {
73       string msg = _f ("cannot find file: `%s'", name);
74       msg += "\n";
75       msg += _f ("(search path: `%s')",
76                  (current_dir.length () ? (current_dir + PATHSEP) : "") + sources->path_->to_string ().c_str ());
77       LexerError (msg.c_str ());
78       return;
79     }
80   file_name_strings_.push_back (file->name_string ());
81
82   char_count_stack_.push_back (0);
83   if (yy_current_buffer)
84     state_stack_.push_back (yy_current_buffer);
85
86   debug_output (string (state_stack_.size (), ' ') // indentation!
87                 + string ("[") + file->name_string ());
88
89   include_stack_.push_back (file);
90
91   /* Ugh. We'd want to create a buffer from the bytes directly.
92
93   Whoops.  The size argument to yy_create_buffer is not the
94   filelength but a BUFFERSIZE.  Maybe this is why reading stdin fucks up.  */
95   yy_switch_to_buffer (yy_create_buffer (file->get_istream (), YY_BUF_SIZE));
96 }
97
98 void
99 Includable_lexer::new_input (const string &name, string data, Sources *sources)
100 {
101   Source_file *file = new Source_file (name, data);
102   sources->add (file);
103   file_name_strings_.push_back (name);
104
105   char_count_stack_.push_back (0);
106   if (yy_current_buffer)
107     state_stack_.push_back (yy_current_buffer);
108
109   debug_output (string (state_stack_.size (), ' ') // indentation!
110                 + string ("[") + name);
111   include_stack_.push_back (file);
112
113   yy_switch_to_buffer (yy_create_buffer (file->get_istream (), YY_BUF_SIZE));
114 }
115
116 /** pop the inputstack.  conceptually this is a destructor, but it
117     does not destruct the Source_file that Includable_lexer::new_input
118     creates.  */
119 bool
120 Includable_lexer::close_input ()
121 {
122   include_stack_.pop_back ();
123   char_count_stack_.pop_back ();
124   debug_output ("]", false);
125   yy_delete_buffer (yy_current_buffer);
126 #if HAVE_FLEXLEXER_YY_CURRENT_BUFFER
127   yy_current_buffer = 0;
128 #endif
129   if (state_stack_.empty ())
130     {
131 #if HAVE_FLEXLEXER_YY_CURRENT_BUFFER
132       yy_current_buffer = 0;
133 #endif
134       return false;
135     }
136   yy_switch_to_buffer (state_stack_.back ());
137   state_stack_.pop_back ();
138   return true;
139 }
140
141 char const *
142 Includable_lexer::here_str0 () const
143 {
144   if (include_stack_.empty ())
145     return 0;
146   return include_stack_.back ()->c_str () + char_count_stack_.back ();
147 }
148
149 Includable_lexer::~Includable_lexer ()
150 {
151   while (!include_stack_.empty ())
152     close_input ();
153 }
154
155 Source_file *
156 Includable_lexer::get_source_file () const
157 {
158   if (include_stack_.empty ())
159     return 0;
160   return include_stack_.back ();
161 }