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