]> git.donarmstrong.com Git - lilypond.git/blob - lily/includable-lexer.cc
* lily/includable-lexer.cc: Use #if iso #ifdef for HAVE_ tests.
[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--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <sstream>
10 #include "config.h"
11
12 #include "includable-lexer.hh"
13 #include "file-path.hh"
14 #include "source-file.hh"
15 #include "source.hh"
16 #include "warn.hh"
17 #include "main.hh"
18
19 #ifndef YY_BUF_SIZE
20 #define YY_BUF_SIZE 16384
21 #endif
22
23 #ifndef YY_START
24 #define YY_START\
25  ((yy_start - 1) / 2)
26 #define YYSTATE YY_START
27 #endif
28
29 /* Flex >= 2.5.29 has include stack; but we don't use that yet.  */
30 #if !HAVE_FLEXLEXER_YY_CURRENT_BUFFER
31 #define yy_current_buffer \
32   (yy_buffer_stack != 0 ? yy_buffer_stack[yy_buffer_stack_top] : 0)
33 #endif
34
35 Includable_lexer::Includable_lexer ()
36 {
37 #if HAVE_FLEXLEXER_YY_CURRENT_BUFFER
38   yy_current_buffer = 0;
39 #endif
40   allow_includes_b_ = true;
41 }
42
43 /** Set the new input file to NAME, remember old file.  */
44 void
45 Includable_lexer::new_input (String name, Sources *sources)
46 {
47   if (!allow_includes_b_)
48     {
49       LexerError (_ ("include files are not allowed").to_str0 ());
50       return;
51     }
52   
53   Source_file *file = sources->get_file (name);
54   if (!file)
55     {
56       String msg = _f ("can't find file: `%s'", name);
57       msg += "\n";
58       msg += _f ("(search path: `%s')",
59                  sources->path_->to_string ().to_str0 ());
60       msg += "\n";
61       LexerError (msg.to_str0 ());
62       return;
63     }
64   filename_strings_.push (file->name_string ());
65
66   char_count_stack_.push (0);
67   if (yy_current_buffer)
68     state_stack_.push (yy_current_buffer);
69
70   if (verbose_global_b)
71     progress_indication (String ("[") + name);
72         
73   include_stack_.push (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   filename_strings_.push (name);
88
89   char_count_stack_.push (0);
90   if (yy_current_buffer)
91     state_stack_.push (yy_current_buffer);
92
93   if (verbose_global_b)
94     progress_indication (String ("[") + name);
95   include_stack_.push (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 ();
107   char_count_stack_.pop ();
108   if (verbose_global_b)
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_.is_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_.pop ());
122   return true;
123 }
124
125 char const*
126 Includable_lexer::here_str0 () const
127 {
128   if (include_stack_.is_empty ())
129     return 0;
130   return include_stack_.top ()->to_str0 () + char_count_stack_.top ();
131 }
132
133 Includable_lexer::~Includable_lexer ()
134 {
135   while (!include_stack_.is_empty ())
136     {
137       close_input ();
138     }
139 }
140 /**
141   Since we don't create the buffer state from the bytes directly, we
142   don't know about the location of the lexer. Add this as a
143   YY_USER_ACTION */
144 void
145 Includable_lexer::add_lexed_char (int count)
146 {
147   char_count_stack_.top () += count;
148 }
149
150 Source_file*
151 Includable_lexer::get_source_file () const
152 {
153   if (include_stack_.is_empty ())
154     return 0;
155   else
156     return include_stack_.top ();
157 }