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