]> git.donarmstrong.com Git - lilypond.git/blob - lily/includable-lexer.cc
Change behavior of nested includes.
[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--2008 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 Includable_lexer::Includable_lexer ()
41 {
42 #if HAVE_FLEXLEXER_YY_CURRENT_BUFFER
43   yy_current_buffer = 0;
44 #endif
45 }
46
47 /** Set the new input file to NAME, remember old file.  */
48 void
49 Includable_lexer::new_input (string name, Sources *sources)
50 {
51   string current_dir = include_stack_.size () ?
52     dir_name (include_stack_.back ()->name_string ()) : "";
53
54   Source_file *file = sources->get_file (name, current_dir);
55   if (!file)
56     {
57       string msg = _f ("cannot find file: `%s'", name);
58       msg += "\n";
59       msg += _f ("(search path: `%s')",
60                  (current_dir.length () ? (current_dir + PATHSEP) : "") + sources->path_->to_string ().c_str ());
61       LexerError (msg.c_str ());
62       return;
63     }
64   file_name_strings_.push_back (file->name_string ());
65
66   char_count_stack_.push_back (0);
67   if (yy_current_buffer)
68     state_stack_.push_back (yy_current_buffer);
69
70   if (be_verbose_global)
71     progress_indication (string ("[") + file->name_string ());
72
73   include_stack_.push_back (file);
74
75   /* Ugh. We'd want to create a buffer from the bytes directly.
76
77   Whoops.  The size argument to yy_create_buffer is not the
78   filelength but a BUFFERSIZE.  Maybe this is why reading stdin fucks up.  */
79   yy_switch_to_buffer (yy_create_buffer (file->get_istream (), YY_BUF_SIZE));
80 }
81
82 void
83 Includable_lexer::new_input (string name, string data, Sources *sources)
84 {
85   Source_file *file = new Source_file (name, data);
86   sources->add (file);
87   file_name_strings_.push_back (name);
88
89   char_count_stack_.push_back (0);
90   if (yy_current_buffer)
91     state_stack_.push_back (yy_current_buffer);
92
93   if (be_verbose_global)
94     progress_indication (string ("[") + name);
95   include_stack_.push_back (file);
96
97   yy_switch_to_buffer (yy_create_buffer (file->get_istream (), YY_BUF_SIZE));
98 }
99
100 /** pop the inputstack.  conceptually this is a destructor, but it
101     does not destruct the Source_file that Includable_lexer::new_input
102     creates.  */
103 bool
104 Includable_lexer::close_input ()
105 {
106   include_stack_.pop_back ();
107   char_count_stack_.pop_back ();
108   if (be_verbose_global)
109     progress_indication ("]");
110   yy_delete_buffer (yy_current_buffer);
111 #if HAVE_FLEXLEXER_YY_CURRENT_BUFFER
112   yy_current_buffer = 0;
113 #endif
114   if (state_stack_.empty ())
115     {
116 #if HAVE_FLEXLEXER_YY_CURRENT_BUFFER
117       yy_current_buffer = 0;
118 #endif
119       return false;
120     }
121   yy_switch_to_buffer (state_stack_.back ());
122   state_stack_.pop_back ();
123   return true;
124 }
125
126 char const *
127 Includable_lexer::here_str0 () const
128 {
129   if (include_stack_.empty ())
130     return 0;
131   return include_stack_.back ()->c_str () + char_count_stack_.back ();
132 }
133
134 Includable_lexer::~Includable_lexer ()
135 {
136   while (!include_stack_.empty ())
137     close_input ();
138 }
139
140 Source_file *
141 Includable_lexer::get_source_file () const
142 {
143   if (include_stack_.empty ())
144     return 0;
145   return include_stack_.back ();
146 }