]> git.donarmstrong.com Git - lilypond.git/blob - lib/includable-lexer.cc
release: 0.1.11
[lilypond.git] / lib / includable-lexer.cc
1 /*
2   includable-lexer.cc -- implement Includable_lexer
3
4   source file of the LilyPond music typesetter
5
6   (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8 #include "includable-lexer.hh"
9 #include "source-file.hh"
10 #include "source.hh"
11
12 #ifndef YY_BUF_SIZE
13 #define YY_BUF_SIZE 16384
14 #endif
15
16 Includable_lexer::Includable_lexer()
17 {
18   yy_current_buffer = 0;
19 }
20
21 /** set the  new input to s, remember old file.
22 */
23 void
24 Includable_lexer::new_input(String s, Sources  * global_sources)
25 {
26   Source_file * sl = global_sources->get_file_l(s);
27   if (!sl) 
28     {
29       LexerError("Can't find file `" + s+ "'");
30       return; 
31     }
32
33     
34   char_count_stack_.push(0);
35   if (yy_current_buffer) 
36     state_stack_.push(yy_current_buffer);
37   cout << "[" << s<<flush;
38   include_stack_.push(sl);    
39     
40   /*
41     ugh. We'd want to create a buffer from the bytes directly.
42
43     Whoops. The size argument to yy_create_buffer is not the
44     filelength but a BUFFERSIZE. Maybe this is why reading stdin fucks up.
45       
46     */
47   yy_switch_to_buffer(yy_create_buffer(sl->istream_l(), YY_BUF_SIZE)); 
48 }
49
50 /** pop the inputstack.  conceptually this is a destructor, but it
51   does not destruct the Source_file that Includable_lexer::new_input creates.  */
52 bool
53 Includable_lexer::close_input()
54 {
55   include_stack_.pop();
56   char_count_stack_.pop();
57   cout << "]"<<flush;
58   yy_delete_buffer(yy_current_buffer);
59   yy_current_buffer = 0;
60   if (state_stack_.empty()) 
61     {
62       return false;
63     }else 
64       {
65         yy_switch_to_buffer(state_stack_.pop());
66         return true;
67       }
68 }
69
70 char const*
71 Includable_lexer::here_ch_C()
72 {
73   if (include_stack_.empty())
74     return 0;
75   return include_stack_.top()->ch_C() + char_count_stack_.top();
76 }
77
78 Includable_lexer::~Includable_lexer()
79 {
80   while (!include_stack_.empty()) 
81     {
82       close_input();
83     }
84 }
85 /** 
86   Since we don't create the buffer state from the bytes directly, we
87   don't know about the location of the lexer. Add this as a
88   YY_USER_ACTION */
89 void
90 Includable_lexer::add_lexed_char(int count)
91 {
92   char_count_stack_.top() += count;
93 }
94
95 Source_file*
96 Includable_lexer::source_file_l() const
97 {
98   if (include_stack_.empty())
99     return 0;
100   else
101     return include_stack_.top();
102 }