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