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