]> git.donarmstrong.com Git - lilypond.git/blob - lily/includable-lexer.cc
release: 1.3.131
[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--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <strstream.h>
10
11
12 #include "file-path.hh"
13 #include "includable-lexer.hh"
14 #include "source-file.hh"
15 #include "source.hh"
16 #include "debug.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 ((yy_start - 1) / 2)
25 #define YYSTATE YY_START
26 #endif
27
28 Includable_lexer::Includable_lexer ()
29 {
30   yy_current_buffer = 0;
31   allow_includes_b_ = true;
32 }
33
34 /** set the  new input to s, remember old file.
35 */
36 void
37 Includable_lexer::new_input (String s, Sources  * global_sources)
38 {
39   if (!allow_includes_b_)
40     {
41       LexerError ("include files are disallowed.");
42       return;
43     }
44   
45   Source_file * sl = global_sources->get_file_l (s);
46   if (!sl)
47     {
48       String msg = _f ("can't find file: `%s'", s);
49       msg += "\n";
50       msg += _f ("(search path: `%s')", global_sources->path_C_->str ().ch_C());
51       msg += "\n";
52       LexerError (msg.ch_C ());
53
54       return;
55     }
56   filename_str_arr_.push (sl->name_str ());
57
58   char_count_stack_.push (0);
59   if (yy_current_buffer)
60     state_stack_.push (yy_current_buffer);
61
62   if (verbose_global_b)
63     progress_indication (String ("[") + s);
64         
65   include_stack_.push (sl);
66
67   /*
68     ugh. We'd want to create a buffer from the bytes directly.
69
70     Whoops. The size argument to yy_create_buffer is not the
71     filelength but a BUFFERSIZE. Maybe this is why reading stdin fucks up.
72
73   */
74   yy_switch_to_buffer (yy_create_buffer (sl->istream_l (), YY_BUF_SIZE));
75
76 }
77 void
78 Includable_lexer::new_input (String name, String data, Sources* sources)
79 {
80   Source_file* file = new Source_file (name, data);
81   sources->add (file);
82   filename_str_arr_.push (name);
83
84   char_count_stack_.push (0);
85   if (yy_current_buffer)
86     state_stack_.push (yy_current_buffer);
87
88   if (verbose_global_b)
89     progress_indication (String ("[") + name);
90   include_stack_.push (file);
91
92   yy_switch_to_buffer (yy_create_buffer (file->istream_l (), YY_BUF_SIZE));
93 }
94
95 /** pop the inputstack.  conceptually this is a destructor, but it
96   does not destruct the Source_file that Includable_lexer::new_input creates.  */
97 bool
98 Includable_lexer::close_input ()
99 {
100   include_stack_.pop ();
101   char_count_stack_.pop ();
102   if (verbose_global_b)
103     progress_indication ("]");
104   yy_delete_buffer (yy_current_buffer);
105   yy_current_buffer = 0;
106   if (state_stack_.empty ())
107     {
108       yy_current_buffer = 0;
109       return false;
110     }
111   else
112     {
113       yy_switch_to_buffer (state_stack_.pop ());
114       return true;
115     }
116 }
117
118 char const*
119 Includable_lexer::here_ch_C () const
120 {
121   if (include_stack_.empty ())
122     return 0;
123   return include_stack_.top ()->ch_C () + char_count_stack_.top ();
124 }
125
126 Includable_lexer::~Includable_lexer ()
127 {
128   while (!include_stack_.empty ())
129     {
130       close_input ();
131     }
132 }
133 /**
134   Since we don't create the buffer state from the bytes directly, we
135   don't know about the location of the lexer. Add this as a
136   YY_USER_ACTION */
137 void
138 Includable_lexer::add_lexed_char (int count)
139 {
140   char_count_stack_.top () += count;
141 }
142
143 Source_file*
144 Includable_lexer::source_file_l () const
145 {
146   if (include_stack_.empty ())
147     return 0;
148   else
149     return include_stack_.top ();
150 }